diff options
author | Som Snytt <som.snytt@gmail.com> | 2013-06-20 09:41:08 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2013-07-04 12:40:56 -0700 |
commit | 06606e83dc942693dd95a30f704876c41eb4d8e5 (patch) | |
tree | ad303e79331ba9b714e4d72c024d4ca4ec0149c0 | |
parent | d0df4c514f662312e2a9284fef2cf24a284aff28 (diff) | |
download | scala-06606e83dc942693dd95a30f704876c41eb4d8e5.tar.gz scala-06606e83dc942693dd95a30f704876c41eb4d8e5.tar.bz2 scala-06606e83dc942693dd95a30f704876c41eb4d8e5.zip |
SI-7265 General test for spec version
The test for isJavaAtLeast uses the specification.version.
The method argument must have the form "major.minor".
The scaladoc is updated to reflect the new reality and a
test is added under junit.
Note that this implementation aims to be a simple
compromise between the functional and imperative camps,
that is, to be free of both closures and while loops.
And to elicit no cruft like regexes and wrappers for strings.
No doubt even more could be done in this department, but we
don't wish to spoil the fun on codegolf.stackexchange.com.
However, we might decide to sponsor a new site:
codereviewpingpong.com
For 2.10.x, javaSpecVersion is provided as a private member.
The active test is under `run` and the `junit` test must
bide its time in `pending`.
For 2.11, the private members can be public and the app test
replaced with the unit test.
-rw-r--r-- | src/library/scala/util/Properties.scala | 35 | ||||
-rw-r--r-- | test/files/run/t7265.scala | 27 | ||||
-rw-r--r-- | test/pending/junit/scala/util/t7265.scala | 46 |
3 files changed, 98 insertions, 10 deletions
diff --git a/src/library/scala/util/Properties.scala b/src/library/scala/util/Properties.scala index 3cd6eb8659..d04e5e48fe 100644 --- a/src/library/scala/util/Properties.scala +++ b/src/library/scala/util/Properties.scala @@ -128,6 +128,10 @@ private[scala] trait PropertiesTrait { def javaVmName = propOrEmpty("java.vm.name") def javaVmVendor = propOrEmpty("java.vm.vendor") def javaVmVersion = propOrEmpty("java.vm.version") + // this property must remain less-well-known until 2.11 + private def javaSpecVersion = propOrEmpty("java.specification.version") + //private def javaSpecVendor = propOrEmpty("java.specification.vendor") + //private def javaSpecName = propOrEmpty("java.specification.name") def osName = propOrEmpty("os.name") def scalaHome = propOrEmpty("scala.home") def tmpDir = propOrEmpty("java.io.tmpdir") @@ -152,18 +156,29 @@ private[scala] trait PropertiesTrait { def scalaCmd = if (isWin) "scala.bat" else "scala" def scalacCmd = if (isWin) "scalac.bat" else "scalac" - /** Can the java version be determined to be at least as high as the argument? - * Hard to properly future proof this but at the rate 1.7 is going we can leave - * the issue for our cyborg grandchildren to solve. + /** Compares the given specification version to the specification version of the platform. + * + * @param version a specification version of the form "major.minor" + * @return `true` iff the specification version of the current runtime + * is equal to or higher than the version denoted by the given string. + * @throws NumberFormatException if the given string is not a version string + * + * @example {{{ + * // In this example, the runtime's Java specification is assumed to be at version 1.7. + * isJavaAtLeast("1.6") // true + * isJavaAtLeast("1.7") // true + * isJavaAtLeast("1.8") // false + * }} */ - def isJavaAtLeast(version: String) = { - val okVersions = version match { - case "1.5" => List("1.5", "1.6", "1.7") - case "1.6" => List("1.6", "1.7") - case "1.7" => List("1.7") - case _ => Nil + def isJavaAtLeast(version: String): Boolean = { + def parts(x: String) = { + val i = x.indexOf('.') + if (i < 0) throw new NumberFormatException("Not a version: " + x) + (x.substring(0, i), x.substring(i+1, x.length)) } - okVersions exists (javaVersion startsWith _) + val (v, _v) = parts(version) + val (s, _s) = parts(javaSpecVersion) + s.toInt >= v.toInt && _s.toInt >= _v.toInt } // provide a main method so version info can be obtained by running this diff --git a/test/files/run/t7265.scala b/test/files/run/t7265.scala new file mode 100644 index 0000000000..c556930303 --- /dev/null +++ b/test/files/run/t7265.scala @@ -0,0 +1,27 @@ + +import scala.util.Properties._ + +object Test extends App { + + setProp("java.specification.version", "1.7") + + assert( isJavaAtLeast("1.5")) + assert( isJavaAtLeast("1.6")) + assert( isJavaAtLeast("1.7")) + assert(!isJavaAtLeast("1.8")) + assert(!isJavaAtLeast("1.71")) + + failing(isJavaAtLeast("1.a")) + failing(isJavaAtLeast("1")) + failing(isJavaAtLeast("")) + failing(isJavaAtLeast(".")) + failing(isJavaAtLeast(".5")) + failing(isJavaAtLeast("1.7.1")) + + def failing(u: =>Unit) = try { + u + assert(false, "Expected Exception") + } catch { + case _: NumberFormatException => + } +} diff --git a/test/pending/junit/scala/util/t7265.scala b/test/pending/junit/scala/util/t7265.scala new file mode 100644 index 0000000000..3b8fa80dbe --- /dev/null +++ b/test/pending/junit/scala/util/t7265.scala @@ -0,0 +1,46 @@ + +package scala.util +package test + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.util.PropertiesTrait + +/** The java version property uses the spec version + * and must work for all "major.minor" and fail otherwise. + */ +@RunWith(classOf[JUnit4]) +class SpecVersionTest { + val sut = new PropertiesTrait { + override def javaSpecVersion = "1.7" + + override protected def pickJarBasedOn: Class[_] = ??? + override protected def propCategory: String = "test" + + // override because of vals like releaseVersion + override lazy val scalaProps = new java.util.Properties + } + + @Test + def comparesCorrectly(): Unit = { + assert(sut isJavaAtLeast "1.5") + assert(sut isJavaAtLeast "1.6") + assert(sut isJavaAtLeast "1.7") + assert(!(sut isJavaAtLeast "1.8")) + } + @Test(expected = classOf[NumberFormatException]) + def badVersion(): Unit = { + sut isJavaAtLeast "1.a" + } + @Test(expected = classOf[NumberFormatException]) + def missingVersion(): Unit = { + sut isJavaAtLeast "1" + } + @Test(expected = classOf[NumberFormatException]) + def notASpec(): Unit = { + sut isJavaAtLeast "1.7.1" + } +} |