VersionUtil { public static boolean isNewerVersion(String ver1, String ver2) { boolean ret=false; try { long v1=Long.parseLong(ver1); long v2=Long.parseLong(ver2); ret = (v2 > v1); } catch (Exception e) { if (ver1.indexOf('.') != -1 && ver2.indexOf('.') != -1) { String[] v1=ver1.split("\\."); String[] v2=ver2.split("\\."); try { int cur=0; while (!ret && cur < v1.length && cur < v2.length) { try { long p1=Long.parseLong(v1[cur]); long p2=Long.parseLong(v2[cur]); if (p2 < p1) { break; } ret = (p2 > p1); } catch (Exception e2) { ret = v2[cur].compareTo(v1[cur]) > 0; } cur++; } } catch (Exception e2) { if (LOG.isLoggable(Level.FINEST)) { LOG.log(Level.FINEST, "Skipping version check for '" +ver1+"' against '"+ver2+"'", e); } } } else { ret = (ver2.compareTo(ver1) > 0); } } return (ret); } private  VersionUtil(); static boolean isNewerVersion(String ver1, String ver2);  }
@Test public void testIsNewerVersionNumeric() { if (!VersionUtil.isNewerVersion("1", "2")) { fail("Wrong result 1"); } if (!VersionUtil.isNewerVersion(""+System.currentTimeMillis(), ""+System.currentTimeMillis()+2000)) { fail("Wrong result 2"); } }
@Test public void testIsNewerVersionLexical() { if (!VersionUtil.isNewerVersion("1.1.0.Final", "1.1.1.Final")) { fail("Wrong result 1"); } if (!VersionUtil.isNewerVersion("1.1.0.Final", "1.10.0.Final")) { fail("Wrong result 2"); } if (!VersionUtil.isNewerVersion("1.2.0.Final", "1.10.0.Final")) { fail("Wrong result 3"); } if (!VersionUtil.isNewerVersion("1.2.0.Final", "2.1.0.Final")) { fail("Wrong result 4"); } if (!VersionUtil.isNewerVersion("1.2.0.Final", "1.2.0.GA")) { fail("Wrong result 5"); } }