VersionComparator implements Comparator<String> { @Override public int compare(String first, String second) { Long firstOrdinal = calculateOrdinal(first); Long secondOrdinal = calculateOrdinal(second); return firstOrdinal.compareTo(secondOrdinal); }  @Override int compare(String first, String second);  }
@Test public void testRegular() throws Exception { VersionComparator comparator = new VersionComparator(); assertTrue(comparator.compare("0.0.1", "0.0.1") == 0); assertTrue(comparator.compare("0.0.2", "0.0.1") == 1); assertTrue(comparator.compare("0.0.1", "0.0.2") == -1); assertTrue(comparator.compare("0.1", "0.1") == 0); assertTrue(comparator.compare("0.2", "0.1") == 1); assertTrue(comparator.compare("0.1", "0.2") == -1); assertTrue(comparator.compare("1.0", "1.0") == 0); assertTrue(comparator.compare("2.0", "1.0") == 1); assertTrue(comparator.compare("1.0", "2.0") == -1); assertTrue(comparator.compare("1.4", "1.4") == 0); assertTrue(comparator.compare("2.4", "2.3") == 1); assertTrue(comparator.compare("2.3", "2.4") == -1); assertTrue(comparator.compare("1.4.7", "1.4.7") == 0); assertTrue(comparator.compare("1.4.8", "1.4.7") == 1); assertTrue(comparator.compare("1.4.7", "1.4.8") == -1); }
@Test public void testAlpha() throws Exception { VersionComparator comparator = new VersionComparator(); assertTrue(comparator.compare("0.0.1alpha", "0.0.1alpha") == 0); assertTrue(comparator.compare("0.0.1", "0.0.1alpha") == 1); assertTrue(comparator.compare("0.0.1alpha", "0.0.1") == -1); assertTrue(comparator.compare("0.0.1alpha2", "0.0.1alpha2") == 0); assertTrue(comparator.compare("0.0.1alpha2", "0.0.1alpha1") == 1); assertTrue(comparator.compare("0.0.1alpha1", "0.0.1alpha2") == -1); assertTrue(comparator.compare("0.1alpha", "0.1alpha") == 0); assertTrue(comparator.compare("0.1", "0.1alpha") == 1); assertTrue(comparator.compare("0.2", "0.1alpha") == 1); assertTrue(comparator.compare("0.1alpha", "0.1") == -1); assertTrue(comparator.compare("0.4.6", "0.5.0alpha") == -1); }
@Test public void testBeta() throws Exception { VersionComparator comparator = new VersionComparator(); assertTrue(comparator.compare("0.0.1beta", "0.0.1beta") == 0); assertTrue(comparator.compare("0.0.1", "0.0.1beta") == 1); assertTrue(comparator.compare("0.0.1alpha", "0.0.1") == -1); assertTrue(comparator.compare("0.0.1beta2", "0.0.1beta2") == 0); assertTrue(comparator.compare("0.0.1beta2", "0.0.1beta1") == 1); assertTrue(comparator.compare("0.0.1beta1", "0.0.1beta2") == -1); assertTrue(comparator.compare("0.0.1alpha1", "0.0.1beta1") == -1); assertTrue(comparator.compare("0.0.1alpha2", "0.0.1beta1") == -1); }
@Test public void testReleaseCandidate() throws Exception { VersionComparator comparator = new VersionComparator(); assertTrue(comparator.compare("0.0.1rc", "0.0.1rc") == 0); assertTrue(comparator.compare("0.0.1", "0.0.1rc") == 1); assertTrue(comparator.compare("0.0.1rc", "0.0.1") == -1); assertTrue(comparator.compare("0.0.1rc2", "0.0.1rc2") == 0); assertTrue(comparator.compare("0.0.1rc2", "0.0.1rc1") == 1); assertTrue(comparator.compare("0.0.1rc1", "0.0.1rc2") == -1); assertTrue(comparator.compare("0.0.1rc1", "0.0.1beta1") == 1); assertTrue(comparator.compare("0.0.1rc1", "0.0.1beta2") == 1); assertTrue(comparator.compare("0.0.1rc2", "0.0.1alpha1") == 1); assertTrue(comparator.compare("0.0.1rc2", "0.0.1alpha2") == 1); }
@Test public void testSnapshot() throws Exception { VersionComparator comparator = new VersionComparator(); assertTrue(comparator.compare("0.4.23", "0.4.24-SNAPSHOT") == -1); assertTrue(comparator.compare("0.4.24-SNAPSHOT", "0.4.23") == 1); assertTrue(comparator.compare("0.4.24-SNAPSHOT", "0.4.24-SNAPSHOT") == 0); }