Repository: ambari
Updated Branches:
  refs/heads/trunk eebca68f7 -> aeb5076ad


AMBARI-20221 Ambari db schema update should be able to compare Ambari versions 
with build text (dili)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/aeb5076a
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/aeb5076a
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/aeb5076a

Branch: refs/heads/trunk
Commit: aeb5076ad66692a43af531c7c5377bdb72dc0f37
Parents: eebca68
Author: Di Li <d...@apache.org>
Authored: Tue Feb 28 14:55:26 2017 -0500
Committer: Di Li <d...@apache.org>
Committed: Tue Feb 28 14:55:26 2017 -0500

----------------------------------------------------------------------
 .../ambari/server/utils/VersionUtils.java       |  6 ++-
 .../ambari/server/utils/TestVersionUtils.java   | 46 ++++++++++++++++++++
 2 files changed, 50 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/aeb5076a/ambari-server/src/main/java/org/apache/ambari/server/utils/VersionUtils.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/utils/VersionUtils.java 
b/ambari-server/src/main/java/org/apache/ambari/server/utils/VersionUtils.java
index d3d8592..af0e030 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/utils/VersionUtils.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/utils/VersionUtils.java
@@ -80,8 +80,10 @@ public class VersionUtils {
       return 0;
     }
 
-    String[] version1Parts = version1.split("\\.");
-    String[] version2Parts = version2.split("\\.");
+    //String pattern = "^([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+).*";
+    String pattern = "([0-9]+).([0-9]+).([0-9]+).([0-9]+)?.*";
+    String[] version1Parts = version1.replaceAll(pattern, 
"$1.$2.$3.$4").split("\\.");
+    String[] version2Parts = version2.replaceAll(pattern, 
"$1.$2.$3.$4").split("\\.");
 
     int length = Math.max(version1Parts.length, version2Parts.length);
     length = maxLengthToCompare == 0 || maxLengthToCompare > length ? length : 
maxLengthToCompare;

http://git-wip-us.apache.org/repos/asf/ambari/blob/aeb5076a/ambari-server/src/test/java/org/apache/ambari/server/utils/TestVersionUtils.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/utils/TestVersionUtils.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/utils/TestVersionUtils.java
index 821565e..ab55518 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/utils/TestVersionUtils.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/utils/TestVersionUtils.java
@@ -97,6 +97,52 @@ public class TestVersionUtils {
   }
 
   @Test
+  public void testVersionCompareSuccessCustomVersion() {
+    Assert.assertTrue(VersionUtils.areVersionsEqual("1.2.3_MYAMBARI_000000", 
"1.2.3_MYAMBARI_000000", false));
+    Assert.assertTrue(VersionUtils.areVersionsEqual("1.2.3_MYAMBARI_000000", 
"1.2.3_MYAMBARI_000000", true));
+    Assert.assertTrue(VersionUtils.areVersionsEqual("", "", true));
+    Assert.assertTrue(VersionUtils.areVersionsEqual(null, null, true));
+    Assert.assertTrue(VersionUtils.areVersionsEqual(BootStrapImpl.DEV_VERSION, 
"1.2.3_MYAMBARI_000000", false));
+    Assert.assertTrue(VersionUtils.areVersionsEqual(BootStrapImpl.DEV_VERSION, 
"", true));
+    Assert.assertTrue(VersionUtils.areVersionsEqual(BootStrapImpl.DEV_VERSION, 
null, true));
+
+    
Assert.assertFalse(VersionUtils.areVersionsEqual("1.2.3.1_MYAMBARI_000000", 
"1.2.3_MYAMBARI_000000", false));
+    Assert.assertFalse(VersionUtils.areVersionsEqual("2.1.3_MYAMBARI_000000", 
"1.2.3_MYAMBARI_000000", false));
+    
Assert.assertFalse(VersionUtils.areVersionsEqual("1.2.3.1_MYAMBARI_000000", 
"1.2.3_MYAMBARI_000000", true));
+    Assert.assertFalse(VersionUtils.areVersionsEqual("2.1.3_MYAMBARI_000000", 
"1.2.3_MYAMBARI_000000", true));
+    Assert.assertFalse(VersionUtils.areVersionsEqual("", 
"1.2.3_MYAMBARI_000000", true));
+    Assert.assertFalse(VersionUtils.areVersionsEqual("", null, true));
+    Assert.assertFalse(VersionUtils.areVersionsEqual(null, "", true));
+    Assert.assertFalse(VersionUtils.areVersionsEqual(null, 
"1.2.3_MYAMBARI_000000", true));
+
+    //Assert.assertEquals(-1, 
VersionUtils.compareVersions("1.2.3_MYAMBARI_000000", "1.2.4_MYAMBARI_000000"));
+    Assert.assertEquals(1, 
VersionUtils.compareVersions("1.2.4_MYAMBARI_000000", "1.2.3_MYAMBARI_000000"));
+    Assert.assertEquals(0, 
VersionUtils.compareVersions("1.2.3_MYAMBARI_000000", "1.2.3_MYAMBARI_000000"));
+
+    Assert.assertEquals(-1, 
VersionUtils.compareVersions("1.2.3_MYAMBARI_000000", "1.2.4_MYAMBARI_000000", 
3));
+    Assert.assertEquals(1, 
VersionUtils.compareVersions("1.2.4_MYAMBARI_000000", "1.2.3_MYAMBARI_000000", 
3));
+    Assert.assertEquals(0, 
VersionUtils.compareVersions("1.2.3_MYAMBARI_000000", "1.2.3_MYAMBARI_000000", 
3));
+
+    Assert.assertEquals(-1, 
VersionUtils.compareVersions("1.2.3.9_MYAMBARI_000000", 
"1.2.4.6_MYAMBARI_000000", 3));
+    Assert.assertEquals(-1, 
VersionUtils.compareVersions("1.2.3_MYAMBARI_000000", 
"1.2.4.6_MYAMBARI_000000", 3));
+    Assert.assertEquals(-1, 
VersionUtils.compareVersions("1.2_MYAMBARI_000000", "1.2.4.6_MYAMBARI_000000", 
3));
+    Assert.assertEquals(1, 
VersionUtils.compareVersions("1.2.4.8_MYAMBARI_000000", 
"1.2.3.6.7_MYAMBARI_000000", 3));
+    Assert.assertEquals(0, 
VersionUtils.compareVersions("1.2.3_MYAMBARI_000000", 
"1.2.3.4_MYAMBARI_000000", 3));
+    Assert.assertEquals(0, 
VersionUtils.compareVersions("1.2.3.6.7_MYAMBARI_000000", 
"1.2.3.4_MYAMBARI_000000", 3));
+    Assert.assertEquals(1, 
VersionUtils.compareVersions("1.2.3.6.7_MYAMBARI_000000", 
"1.2.3.4_MYAMBARI_000000", 4));
+    Assert.assertEquals(0, 
VersionUtils.compareVersions("1.2.3_MYAMBARI_000000", 
"1.2.3.0_MYAMBARI_000000", 4));
+    Assert.assertEquals(-1, 
VersionUtils.compareVersions("1.2.3_MYAMBARI_000000", 
"1.2.3.1_MYAMBARI_000000", 4));
+    Assert.assertEquals(1, 
VersionUtils.compareVersions("1.2.3.6.7_MYAMBARI_000000\n", 
"1.2.3.4_MYAMBARI_000000\n", 4)); //test version trimming
+
+    Assert.assertEquals(1, 
VersionUtils.compareVersions("1.2.3.1_MYAMBARI_000000", 
"1.2.3_MYAMBARI_000000", true));
+    Assert.assertEquals(1, 
VersionUtils.compareVersions("2.1.3_MYAMBARI_000000", "1.2.3_MYAMBARI_000000", 
true));
+    Assert.assertEquals(-1, VersionUtils.compareVersions("", 
"1.2.3_MYAMBARI_000000", true));
+    Assert.assertEquals(1, VersionUtils.compareVersions("", null, true));
+    Assert.assertEquals(-1, VersionUtils.compareVersions(null, "", true));
+    Assert.assertEquals(-1, VersionUtils.compareVersions(null, 
"1.2.3_MYAMBARI_000000", true));
+  }
+
+  @Test
   public void testVersionCompareError() {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("version2 cannot be empty");

Reply via email to