VersionComparator implements Comparator<Version> { @Override public int compare(Version version1, Version version2) { if (version1.equals(version2)) { return 0; } if (version1.getBigNumber() > version2.getBigNumber()) { return 1; } else if (version1.getBigNumber() == version2.getBigNumber()) { if (version1.getIntermediateNumber() > version2.getIntermediateNumber()) { return 1; } else if (version1.getIntermediateNumber() == version2.getIntermediateNumber()) { if (version1.getLowNumber() > version2.getLowNumber()) { return 1; } else if (version1.getLowNumber() == version2.getLowNumber()) { return version1.getCustomName().compareTo(version2.getCustomName()); } else { return -1; } } else { return -1; } } else { return -1; } }  @Override int compare(Version version1, Version version2);  }
@Test public void testCompareEqualsVersion() { assertEquals(0, versionComparator.compare(new Version("4.2.1"), new Version("4.2.1"))); }
@Test public void testCompareHigherVersion() { assertTrue(versionComparator.compare(new Version("4.2.2"), new Version("4.2.1")) > 0); assertTrue(versionComparator.compare(new Version("4.3.1"), new Version("4.2.1")) > 0); assertTrue(versionComparator.compare(new Version("5.2.1"), new Version("4.2.1")) > 0); assertTrue(versionComparator.compare(new Version("4.2.2"), new Version("4.1.3")) > 0); }
@Test public void testCompareWithManyPatches() { assertTrue(versionComparator.compare(new Version("4.2.1-dev-patch2"), new Version("4.2.1")) > 0); assertTrue(versionComparator.compare(new Version("4.2.1"), new Version("4.2.1-dev-patch2")) < 0); }
@Test public void testCompareInvalidVersion() { assertTrue(versionComparator.compare(new Version("1.4.rc6-xliveless-no_xinput2"), new Version("1.4")) > 0); assertTrue(versionComparator.compare(new Version("1.4"), new Version("1.4.rc6-xliveless-no_xinput2")) < 0); }
@Test public void testCompareWithPatches() { assertTrue(versionComparator.compare(new Version("4.2.1-dev"), new Version("4.2.1")) > 0); assertTrue(versionComparator.compare(new Version("4.2.1"), new Version("4.2.1-dev")) < 0); }
@Test public void testCompareWithPatchesOnlyTwo() { assertTrue(versionComparator.compare(new Version("4.2-dev"), new Version("4.2")) > 0); assertTrue(versionComparator.compare(new Version("4.2"), new Version("4.2-rc6")) < 0); }
@Test public void testCompareLowerVersion() { assertTrue(versionComparator.compare(new Version("4.2.1"), new Version("4.2.2")) < 0); assertTrue(versionComparator.compare(new Version("4.2.1"), new Version("4.3.1")) < 0); assertTrue(versionComparator.compare(new Version("4.2.1"), new Version("5.2.1")) < 0); assertTrue(versionComparator.compare(new Version("4.1.3"), new Version("4.2.2")) < 0); }