diff options
author | Pavel Petlinsky <p.petlinsky@rambler-co.ru> | 2016-07-08 15:37:21 +0300 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2016-07-13 12:12:22 -0700 |
commit | 72076e59257da72f962d4101d87ff5507da28e4f (patch) | |
tree | 4d4d9377328371780119bba67d9596e28180ab2c | |
parent | 6612ba010b0e70c53550d1e47141c8dc89a55f23 (diff) | |
download | scala-72076e59257da72f962d4101d87ff5507da28e4f.tar.gz scala-72076e59257da72f962d4101d87ff5507da28e4f.tar.bz2 scala-72076e59257da72f962d4101d87ff5507da28e4f.zip |
SI-9750 scala.util.Properties.isJavaAtLeast works with JDK9
The utility method compares javaSpecVersion, which has the
form "1.8" previously and "9" going forward.
The method accepts "1.n" for n < 9. More correctly, the string
argument should be a single number.
Supports JEP-223.
-rw-r--r-- | src/library/scala/util/Properties.scala | 26 | ||||
-rw-r--r-- | test/junit/scala/util/SpecVersionTest.scala | 56 |
2 files changed, 54 insertions, 28 deletions
diff --git a/src/library/scala/util/Properties.scala b/src/library/scala/util/Properties.scala index a176748cd6..6995f452fa 100644 --- a/src/library/scala/util/Properties.scala +++ b/src/library/scala/util/Properties.scala @@ -168,27 +168,31 @@ private[scala] trait PropertiesTrait { /** Compares the given specification version to the specification version of the platform. * - * @param version a specification version of the form "major.minor" + * @param version a specification version number (legacy forms acceptable) * @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. + * // In this example, the runtime's Java specification is assumed to be at version 8. * isJavaAtLeast("1.6") // true - * isJavaAtLeast("1.7") // true - * isJavaAtLeast("1.8") // false + * isJavaAtLeast("1.8") // true + * isJavaAtLeast("8") // true + * isJavaAtLeast("9") // false + * isJavaAtLeast("1.9") // throws * }}} */ 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)) + def versionOf(s: String): Int = s.indexOf('.') match { + case 1 if s.charAt(0) == '1' => + val v = versionOf(s.substring(2)) + if (v < 9) v else -1 + case -1 => s.toInt + case _ => -1 } - val (v, _v) = parts(version) - val (s, _s) = parts(javaSpecVersion) - s.toInt >= v.toInt && _s.toInt >= _v.toInt + val v = versionOf(version) + if (v < 0) throw new NumberFormatException(s"Not a version: $version") + versionOf(javaSpecVersion) >= v } // provide a main method so version info can be obtained by running this diff --git a/test/junit/scala/util/SpecVersionTest.scala b/test/junit/scala/util/SpecVersionTest.scala index e3e7a978f2..2b69f288fa 100644 --- a/test/junit/scala/util/SpecVersionTest.scala +++ b/test/junit/scala/util/SpecVersionTest.scala @@ -6,13 +6,16 @@ import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +import scala.tools.testing.AssertUtil._ + /** The java version property uses the spec version - * and must work for all "major.minor" and fail otherwise. + * and must work for legacy "major.minor" and plain version_number, + * and fail otherwise. */ @RunWith(classOf[JUnit4]) class SpecVersionTest { - val sut = new PropertiesTrait { - override def javaSpecVersion = "1.7" + class TestProperties(versionAt: String) extends PropertiesTrait { + override def javaSpecVersion = versionAt override protected def pickJarBasedOn: Class[_] = ??? override protected def propCategory: String = "test" @@ -20,38 +23,57 @@ class SpecVersionTest { // override because of vals like releaseVersion override lazy val scalaProps = new java.util.Properties } + val sut7 = new TestProperties("1.7") + val sut9 = new TestProperties("9") + + @Test + def comparesJDK9Correctly(): Unit = { + assert(sut9 isJavaAtLeast "1") + assert(sut9 isJavaAtLeast "1.5") + assert(sut9 isJavaAtLeast "5") + assert(sut9 isJavaAtLeast "1.8") + assert(sut9 isJavaAtLeast "8") + assert(sut9 isJavaAtLeast "9") + } // SI-7265 @Test def comparesCorrectly(): Unit = { - assert(sut isJavaAtLeast "1.5") - assert(sut isJavaAtLeast "1.6") - assert(sut isJavaAtLeast "1.7") - assert(!(sut isJavaAtLeast "1.8")) - assert(!(sut isJavaAtLeast "1.71")) + assert(sut7 isJavaAtLeast "1") + assert(sut7 isJavaAtLeast "1.5") + assert(sut7 isJavaAtLeast "5") + assert(sut7 isJavaAtLeast "1.6") + assert(sut7 isJavaAtLeast "1.7") + assertFalse(sut7 isJavaAtLeast "1.8") + assertFalse(sut7 isJavaAtLeast "9") + assertFalse(sut7 isJavaAtLeast "10") } - @Test(expected = classOf[NumberFormatException]) - def badVersion(): Unit = { - sut isJavaAtLeast "1.a" + + @Test def variousBadVersionStrings(): Unit = { + assertThrows[NumberFormatException] { sut7 isJavaAtLeast "1.9" } + assertThrows[NumberFormatException] { sut9 isJavaAtLeast "1.9" } + assertThrows[NumberFormatException] { sut7 isJavaAtLeast "9.1" } + assertThrows[NumberFormatException] { sut9 isJavaAtLeast "9.1" } } + @Test(expected = classOf[NumberFormatException]) - def missingVersion(): Unit = { - sut isJavaAtLeast "1" + def badVersion(): Unit = { + sut7 isJavaAtLeast "1.a" } @Test(expected = classOf[NumberFormatException]) def noVersion(): Unit = { - sut isJavaAtLeast "" + sut7 isJavaAtLeast "" } @Test(expected = classOf[NumberFormatException]) def dotOnly(): Unit = { - sut isJavaAtLeast "." + sut7 isJavaAtLeast "." } @Test(expected = classOf[NumberFormatException]) def leadingDot(): Unit = { - sut isJavaAtLeast ".5" + sut7 isJavaAtLeast ".5" } @Test(expected = classOf[NumberFormatException]) def notASpec(): Unit = { - sut isJavaAtLeast "1.7.1" + sut7 isJavaAtLeast "1.7.1" } } |