Version { public static Version parse(String version) { int indexFirstDot = version.indexOf('.'); int indexSecondDot = version.indexOf('.', indexFirstDot + 1); if (indexFirstDot == -1 || indexSecondDot == -1) { throw new NumberFormatException("Invalid version format '" + version + "', expected two dots!"); } int indexFirstDash = version.indexOf('-'); int versionEnd; if (indexFirstDash == -1) { versionEnd = version.length(); } else { versionEnd = indexFirstDash; } boolean snapshot = version.endsWith(SNAPSHOT_MARKER); return new Version(Integer.parseInt(version.substring(0, indexFirstDot)), Integer.parseInt(version.substring(indexFirstDot + 1, indexSecondDot)), Integer.parseInt(version.substring(indexSecondDot + 1, versionEnd)), snapshot); } Version(int major, int minor, int revision, boolean snapshot);  Version(int major, int minor, int revision); int getMajor(); int getMinor(); int getRevision(); boolean isSnapshot(); @Override String toString(); @Override boolean equals(Object o); static Version parse(String version); static Version currentVersion(); static final Version DEV_VERSION; }
@Test public void testParse() { Version referenceVersion = Version.currentVersion(); assertEquals(referenceVersion, Version.parse(referenceVersion.toString())); assertEquals(new Version(1,5,2, false), Version.parse("1.5.2-incubating")); assertEquals(new Version(1,5,2, false), Version.parse("1.5.2")); }
@Test public void testParseSnapshot() { assertEquals(new Version(1,5,2, true), Version.parse("1.5.2-incubating-SNAPSHOT")); assertEquals(new Version(1,5,2, true), Version.parse("1.5.2-SNAPSHOT")); }
@Test public void testParseInvalidVersion() { try { Version.parse("1.5."); } catch (NumberFormatException e) { return; } assertTrue(false); }
@Test public void testParseInvalidVersion2() { try { Version.parse("1.5"); } catch (NumberFormatException e) { return; } assertTrue(false); }