Skip to content

Commit

Permalink
add custom protocol hadler test
Browse files Browse the repository at this point in the history
  • Loading branch information
rolang committed Mar 11, 2024
1 parent 10148c2 commit 9554289
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ inThisBuild(List(
"yanbo.ai@gmail.com",
url("https://aiyanbo.github.io/")
)
)
),
Test / fork := true
))

coverageScalacPluginVersion := "2.0.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class MavenRepoMetadataLoader(url: String)(implicit ec: ExecutionContext) extend
artifactId: String,
attrs: Map[String, String]
): Future[Seq[ArtifactVersion]] = {
val location =
protocol + s"$base/${organization.split('.').mkString("/")}/$artifactId/maven-metadata.xml"
val location = new URI(s"$protocol$base/${organization.split('.').mkString("/")}/$artifactId/maven-metadata.xml")
.normalize()
.toString()
download(organization, artifactId, location).map { file =>
val stream = Files.newInputStream(file)
try {
Expand Down
31 changes: 31 additions & 0 deletions src/test/scala/org/jmotor/sbt/service/VersionServiceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import sbt.util.Logger

import scala.concurrent.Await
import scala.concurrent.duration.*
import java.net.{URI, URL, URLConnection, URLStreamHandler, URLStreamHandlerFactory}
import java.util.concurrent.atomic.AtomicReference

/** Component: Description: Date: 2018/3/1
*
Expand Down Expand Up @@ -57,4 +59,33 @@ class VersionServiceSpec extends AnyFunSuite {
assert(status.status == Status.Expired)
}

test("uses custom protocol handlers") {
val downloadsCalled = new AtomicReference(Vector.empty[String])

// setting stream handler can be executed only once on jvm
// to be able to execute it multiple times from sbt shell it requires forking enabled like "Test / fork := true"
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory {
def createURLStreamHandler(protocol: String): URLStreamHandler = protocol match {
case "artifactregistry" =>
new URLStreamHandler {
protected def openConnection(url: URL): URLConnection = {
downloadsCalled.getAndUpdate(_ :+ url.toString())
new URI(s"https://${url.getHost}${url.getPath()}").normalize.toURL.openConnection
}
}
case _ => null
}
})

val testResolver = MavenRepo("m2", "artifactregistry://repo1.maven.org/maven2/")
val versionService = VersionService(Logger.Null, "2.12.4", "2.12", Seq(testResolver), Seq.empty)
Await.result(versionService.checkForUpdates(ModuleID("com.google.guava", "guava", "23.0-jre")), 30.seconds)

assert(
downloadsCalled
.get()
.contains("artifactregistry://repo1.maven.org/maven2/com/google/guava/guava/maven-metadata.xml")
)
}

}

0 comments on commit 9554289

Please sign in to comment.