Skip to content

Commit 06606e8

Browse files
committed
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.
1 parent d0df4c5 commit 06606e8

File tree

3 files changed

+98
-10
lines changed

3 files changed

+98
-10
lines changed

src/library/scala/util/Properties.scala

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ private[scala] trait PropertiesTrait {
128128
def javaVmName = propOrEmpty("java.vm.name")
129129
def javaVmVendor = propOrEmpty("java.vm.vendor")
130130
def javaVmVersion = propOrEmpty("java.vm.version")
131+
// this property must remain less-well-known until 2.11
132+
private def javaSpecVersion = propOrEmpty("java.specification.version")
133+
//private def javaSpecVendor = propOrEmpty("java.specification.vendor")
134+
//private def javaSpecName = propOrEmpty("java.specification.name")
131135
def osName = propOrEmpty("os.name")
132136
def scalaHome = propOrEmpty("scala.home")
133137
def tmpDir = propOrEmpty("java.io.tmpdir")
@@ -152,18 +156,29 @@ private[scala] trait PropertiesTrait {
152156
def scalaCmd = if (isWin) "scala.bat" else "scala"
153157
def scalacCmd = if (isWin) "scalac.bat" else "scalac"
154158

155-
/** Can the java version be determined to be at least as high as the argument?
156-
* Hard to properly future proof this but at the rate 1.7 is going we can leave
157-
* the issue for our cyborg grandchildren to solve.
159+
/** Compares the given specification version to the specification version of the platform.
160+
*
161+
* @param version a specification version of the form "major.minor"
162+
* @return `true` iff the specification version of the current runtime
163+
* is equal to or higher than the version denoted by the given string.
164+
* @throws NumberFormatException if the given string is not a version string
165+
*
166+
* @example {{{
167+
* // In this example, the runtime's Java specification is assumed to be at version 1.7.
168+
* isJavaAtLeast("1.6") // true
169+
* isJavaAtLeast("1.7") // true
170+
* isJavaAtLeast("1.8") // false
171+
* }}
158172
*/
159-
def isJavaAtLeast(version: String) = {
160-
val okVersions = version match {
161-
case "1.5" => List("1.5", "1.6", "1.7")
162-
case "1.6" => List("1.6", "1.7")
163-
case "1.7" => List("1.7")
164-
case _ => Nil
173+
def isJavaAtLeast(version: String): Boolean = {
174+
def parts(x: String) = {
175+
val i = x.indexOf('.')
176+
if (i < 0) throw new NumberFormatException("Not a version: " + x)
177+
(x.substring(0, i), x.substring(i+1, x.length))
165178
}
166-
okVersions exists (javaVersion startsWith _)
179+
val (v, _v) = parts(version)
180+
val (s, _s) = parts(javaSpecVersion)
181+
s.toInt >= v.toInt && _s.toInt >= _v.toInt
167182
}
168183

169184
// provide a main method so version info can be obtained by running this

test/files/run/t7265.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import scala.util.Properties._
3+
4+
object Test extends App {
5+
6+
setProp("java.specification.version", "1.7")
7+
8+
assert( isJavaAtLeast("1.5"))
9+
assert( isJavaAtLeast("1.6"))
10+
assert( isJavaAtLeast("1.7"))
11+
assert(!isJavaAtLeast("1.8"))
12+
assert(!isJavaAtLeast("1.71"))
13+
14+
failing(isJavaAtLeast("1.a"))
15+
failing(isJavaAtLeast("1"))
16+
failing(isJavaAtLeast(""))
17+
failing(isJavaAtLeast("."))
18+
failing(isJavaAtLeast(".5"))
19+
failing(isJavaAtLeast("1.7.1"))
20+
21+
def failing(u: =>Unit) = try {
22+
u
23+
assert(false, "Expected Exception")
24+
} catch {
25+
case _: NumberFormatException =>
26+
}
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
package scala.util
3+
package test
4+
5+
import org.junit.Assert._
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
import org.junit.runners.JUnit4
9+
10+
import scala.util.PropertiesTrait
11+
12+
/** The java version property uses the spec version
13+
* and must work for all "major.minor" and fail otherwise.
14+
*/
15+
@RunWith(classOf[JUnit4])
16+
class SpecVersionTest {
17+
val sut = new PropertiesTrait {
18+
override def javaSpecVersion = "1.7"
19+
20+
override protected def pickJarBasedOn: Class[_] = ???
21+
override protected def propCategory: String = "test"
22+
23+
// override because of vals like releaseVersion
24+
override lazy val scalaProps = new java.util.Properties
25+
}
26+
27+
@Test
28+
def comparesCorrectly(): Unit = {
29+
assert(sut isJavaAtLeast "1.5")
30+
assert(sut isJavaAtLeast "1.6")
31+
assert(sut isJavaAtLeast "1.7")
32+
assert(!(sut isJavaAtLeast "1.8"))
33+
}
34+
@Test(expected = classOf[NumberFormatException])
35+
def badVersion(): Unit = {
36+
sut isJavaAtLeast "1.a"
37+
}
38+
@Test(expected = classOf[NumberFormatException])
39+
def missingVersion(): Unit = {
40+
sut isJavaAtLeast "1"
41+
}
42+
@Test(expected = classOf[NumberFormatException])
43+
def notASpec(): Unit = {
44+
sut isJavaAtLeast "1.7.1"
45+
}
46+
}

0 commit comments

Comments
 (0)