JavaVersionParser { public static JavaVersion parseJavaVersion(final String fullVersionString) { if (!StringUtils.isNullOrEmpty(fullVersionString)) { final Matcher matcher = VERSION_REGEX.matcher(fullVersionString); if (matcher.matches()) { final Integer majorVersionFamily = NumberUtils.tryParseInt(matcher.group(1)); final Integer majorVersion = NumberUtils.tryParseInt(matcher.group(2)); final Integer maintenanceNumber = NumberUtils.tryParseInt(matcher.group(3)); final Integer updateNumber = NumberUtils.tryParseInt(matcher.group(4)); return new JavaVersion(majorVersionFamily, majorVersion, maintenanceNumber, updateNumber); } } return JavaVersion.UNKNOWN; } private  JavaVersionParser(); static JavaVersion getCurrentJavaVersion(); static JavaVersion parseJavaVersion(final String fullVersionString); static final String JAVA_VERSION_PROPERTY; }
@Test public void nullVersion_ReturnsUnknownVersion() { JavaVersion version = JavaVersionParser.parseJavaVersion(null); assertJavaVersionsEqual(jv(null, null, null, null), version); assertEquals(KnownJavaVersions.UNKNOWN, version.getKnownVersion()); }
@Test public void emptyVersion_ReturnsUnknownVersion() { JavaVersion version = JavaVersionParser.parseJavaVersion(""); assertJavaVersionsEqual(jv(null, null, null, null), version); assertEquals(KnownJavaVersions.UNKNOWN, version.getKnownVersion()); }
@Test public void garbageVersion_ReturnsUnknownVersion() { JavaVersion version = JavaVersionParser.parseJavaVersion("invalid-version"); assertJavaVersionsEqual(jv(null, null, null, null), version); assertEquals(KnownJavaVersions.UNKNOWN, version.getKnownVersion()); }
@Test public void validJava6Version_WithUpdateNumber() { JavaVersion version = JavaVersionParser.parseJavaVersion("1.6.0_65"); assertJavaVersionsEqual(jv(1, 6, 0, 65), version); assertEquals(KnownJavaVersions.JAVA_6, version.getKnownVersion()); }
@Test public void validJava6Version_WithMultiDigitMaintenanceNumber() { JavaVersion version = JavaVersionParser.parseJavaVersion("1.6.101_65"); assertJavaVersionsEqual(jv(1, 6, 101, 65), version); assertEquals(KnownJavaVersions.JAVA_6, version.getKnownVersion()); }
@Test public void validJava6Version_WithoutUpdateNumber_WithIdentifier() { JavaVersion version = JavaVersionParser.parseJavaVersion("1.6.3-65"); assertJavaVersionsEqual(jv(1, 6, 3, null), version); assertEquals(KnownJavaVersions.JAVA_6, version.getKnownVersion()); }
@Test public void validJava7Version_WithUpdateNumber() { JavaVersion version = JavaVersionParser.parseJavaVersion("1.7.0_1234"); assertJavaVersionsEqual(jv(1, 7, 0, 1234), version); assertEquals(KnownJavaVersions.JAVA_7, version.getKnownVersion()); }
@Test public void validJava7Version_WithoutUpdateNumber() { JavaVersion version = JavaVersionParser.parseJavaVersion("1.7.0"); assertJavaVersionsEqual(jv(1, 7, 0, null), version); assertEquals(KnownJavaVersions.JAVA_7, version.getKnownVersion()); }
@Test public void validJava8Version_WithUpdateNumberAndIdentifier() { JavaVersion version = JavaVersionParser.parseJavaVersion("1.8.0_12-b24"); assertJavaVersionsEqual(jv(1, 8, 0, 12), version); assertEquals(KnownJavaVersions.JAVA_8, version.getKnownVersion()); }
@Test public void validJava9Version_NonZeroMaintenanceNumber_WithUpdateNumberAndIdentifier() { JavaVersion version = JavaVersionParser.parseJavaVersion("1.9.1_00-someIdentifier"); assertJavaVersionsEqual(jv(1, 9, 1, 0), version); assertEquals(KnownJavaVersions.JAVA_9, version.getKnownVersion()); }
@Test public void doubleDigitMajorVersion_ParsesMajorVersionCorrectly() { JavaVersion version = JavaVersionParser.parseJavaVersion("1.10.1_00"); assertJavaVersionsEqual(jv(1, 10, 1, 0), version); assertEquals(KnownJavaVersions.UNKNOWN, version.getKnownVersion()); }