Move io tests to subdirectory, and add a test for "System" class lookups.

This commit is contained in:
Stu Hood 2015-09-18 17:15:52 -07:00
parent 5a3b95cd6d
commit 198820bc53
8 changed files with 32 additions and 9 deletions

View File

@ -44,10 +44,11 @@ object IO {
if (codeSource ne null) {
codeSource.getLocation
} else {
Option(ClassLoader.getSystemClassLoader.getResource(classfilePathForClassname(cl.getName)))
// NB: this assumes that System-class-loaded classes are located in jars, and thus relies on
// uses forward-slash-separated paths and `urlAsFile`'s truncation to the containing jar file
val clsfile = s"${cl.getName.replace('.', '/')}.class"
Option(ClassLoader.getSystemClassLoader.getResource(clsfile))
.flatMap {
// TODO: assuming that System-class-loaded classes are located in jars, which
// will cause this method to truncate to the jar
urlAsFile
}.getOrElse {
sys.error("No class location for " + cl)
@ -96,12 +97,6 @@ object IO {
case _ => None
}
/**
* @return The path for the given classname.
*
* TODO: crossplatform
*/
def classfilePathForClassname(clsname: String): String = s"${clsname.replace('.', '/')}.class"
private[this] def uriToFile(uriString: String): File =
{
val uri = new URI(uriString)

View File

@ -0,0 +1,28 @@
/* sbt -- Simple Build Tool
* Copyright 2008 Mark Harrah */
package sbt
import util.Try
import org.scalacheck._
import Prop._
object IOSpecification extends Properties("IO") {
property("classLocation able to determine containing directories") =
Prop.forAll(classes) { (c: Class[_]) =>
Try(IO.classLocationFile(c)).toOption.exists {
case jar if jar.getName.endsWith(".jar") => jar.isFile
case dir => dir.isDirectory
}
}
implicit def classes: Gen[Class[_]] =
Gen.oneOf(
this.getClass,
classOf[java.lang.Integer],
classOf[String],
classOf[Thread],
classOf[Properties]
)
}