From 8e2515d994b7cd9c799c989411f6d9d41b2722e2 Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Thu, 4 Mar 2010 13:03:59 +0000
Subject: [PATCH] [LMS-1412] primitive value comparator test

SVN: 15047
---
 .../resultset/CachedResultSetManager.java     |  9 +-
 .../shared/basic/dto/PrimitiveValueTest.java  | 99 +++++++++++++++++++
 2 files changed, 103 insertions(+), 5 deletions(-)
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/PrimitiveValueTest.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CachedResultSetManager.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CachedResultSetManager.java
index e460eb64746..fdcb552bb2c 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CachedResultSetManager.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CachedResultSetManager.java
@@ -26,8 +26,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.commons.collections.comparators.NullComparator;
-import org.apache.commons.collections.comparators.ReverseComparator;
+import org.apache.commons.collections.ComparatorUtils;
 import org.apache.commons.lang.text.StrMatcher;
 import org.apache.commons.lang.text.StrTokenizer;
 import org.apache.log4j.Logger;
@@ -266,8 +265,8 @@ public final class CachedResultSetManager<K> implements IResultSetManager<K>, Se
 
             };
 
-        // null values will be treated as smallestte
-        return applySortDir(sortDir, new NullComparator(comparator, false));
+        // null values will be treated as smallest
+        return applySortDir(sortDir, ComparatorUtils.nullLowComparator(comparator));
     }
 
     @SuppressWarnings("unchecked")
@@ -275,7 +274,7 @@ public final class CachedResultSetManager<K> implements IResultSetManager<K>, Se
     {
         if (sortDir == SortDir.DESC)
         {
-            return new ReverseComparator(comparator);
+            return ComparatorUtils.reversedComparator(comparator);
         } else
         {
             return comparator;
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/PrimitiveValueTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/PrimitiveValueTest.java
new file mode 100644
index 00000000000..188c295ce38
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/PrimitiveValueTest.java
@@ -0,0 +1,99 @@
+package ch.systemsx.cisd.openbis.generic.shared.basic.dto;
+
+import org.testng.AssertJUnit;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
+
+/**
+ * @author Piotr Buczek
+ */
+public class PrimitiveValueTest extends AssertJUnit
+{
+    private enum ExpectedResult
+    {
+        LOWER, EQUAL, HIGHER;
+    }
+
+    @DataProvider(name = "values")
+    protected Object[][] getValues()
+    {
+        return new Object[][]
+            {
+                /* NULL == NULL == "" */
+                { PrimitiveValue.NULL, PrimitiveValue.NULL, ExpectedResult.EQUAL },
+                { PrimitiveValue.NULL, new PrimitiveValue(""), ExpectedResult.EQUAL },
+                /* NULL < anything else */
+                { PrimitiveValue.NULL, new PrimitiveValue("text"), ExpectedResult.LOWER },
+                { PrimitiveValue.NULL, new PrimitiveValue("5"), ExpectedResult.LOWER },
+                { PrimitiveValue.NULL, new PrimitiveValue(-10L), ExpectedResult.LOWER },
+                { PrimitiveValue.NULL, new PrimitiveValue(0L), ExpectedResult.LOWER },
+                { PrimitiveValue.NULL, new PrimitiveValue(10L), ExpectedResult.LOWER },
+                { PrimitiveValue.NULL, new PrimitiveValue(-10.0), ExpectedResult.LOWER },
+                { PrimitiveValue.NULL, new PrimitiveValue(0.0), ExpectedResult.LOWER },
+                { PrimitiveValue.NULL, new PrimitiveValue(10.0), ExpectedResult.LOWER },
+                /* String < Number */
+                { new PrimitiveValue("text"), new PrimitiveValue(-10L), ExpectedResult.LOWER },
+                { new PrimitiveValue("text"), new PrimitiveValue(0L), ExpectedResult.LOWER },
+                { new PrimitiveValue("text"), new PrimitiveValue(10L), ExpectedResult.LOWER },
+                { new PrimitiveValue("text"), new PrimitiveValue(-10.0), ExpectedResult.LOWER },
+                { new PrimitiveValue("text"), new PrimitiveValue(0.0), ExpectedResult.LOWER },
+                { new PrimitiveValue("text"), new PrimitiveValue(10.0), ExpectedResult.LOWER },
+                /* String number < Number */
+                { new PrimitiveValue("5"), new PrimitiveValue(5L), ExpectedResult.LOWER },
+                { new PrimitiveValue("5"), new PrimitiveValue(-10L), ExpectedResult.LOWER },
+                { new PrimitiveValue("5"), new PrimitiveValue(0L), ExpectedResult.LOWER },
+                { new PrimitiveValue("5"), new PrimitiveValue(10L), ExpectedResult.LOWER },
+                { new PrimitiveValue("5"), new PrimitiveValue(-10.0), ExpectedResult.LOWER },
+                { new PrimitiveValue("5"), new PrimitiveValue(0.0), ExpectedResult.LOWER },
+                { new PrimitiveValue("5"), new PrimitiveValue(10.0), ExpectedResult.LOWER },
+                { new PrimitiveValue("5.0"), new PrimitiveValue(5.0), ExpectedResult.LOWER },
+                { new PrimitiveValue("5.0"), new PrimitiveValue(-10L), ExpectedResult.LOWER },
+                { new PrimitiveValue("5.0"), new PrimitiveValue(0L), ExpectedResult.LOWER },
+                { new PrimitiveValue("5.0"), new PrimitiveValue(10L), ExpectedResult.LOWER },
+                { new PrimitiveValue("5.0"), new PrimitiveValue(-10.0), ExpectedResult.LOWER },
+                { new PrimitiveValue("5.0"), new PrimitiveValue(0.0), ExpectedResult.LOWER },
+                { new PrimitiveValue("5.0"), new PrimitiveValue(10.0), ExpectedResult.LOWER },
+                /* String vs String */
+                { new PrimitiveValue("aba"), new PrimitiveValue("aba"), ExpectedResult.EQUAL },
+                { new PrimitiveValue("aba"), new PrimitiveValue("abc"), ExpectedResult.LOWER },
+                { new PrimitiveValue("abc"), new PrimitiveValue("abca"), ExpectedResult.LOWER },
+                { new PrimitiveValue("aba"), new PrimitiveValue("abca"), ExpectedResult.LOWER },
+                /* Long vs Long */
+                { new PrimitiveValue(10L), new PrimitiveValue(10L), ExpectedResult.EQUAL },
+                { new PrimitiveValue(-10L), new PrimitiveValue(0L), ExpectedResult.LOWER },
+                { new PrimitiveValue(-10L), new PrimitiveValue(10L), ExpectedResult.LOWER },
+                { new PrimitiveValue(5L), new PrimitiveValue(10L), ExpectedResult.LOWER },
+                /* Double vs Double */
+                { new PrimitiveValue(9.9), new PrimitiveValue(9.9), ExpectedResult.EQUAL },
+                { new PrimitiveValue(9.9), new PrimitiveValue(10.0), ExpectedResult.LOWER },
+                /* Long vs Double */
+                { new PrimitiveValue(0L), new PrimitiveValue(0.0), ExpectedResult.EQUAL },
+                { new PrimitiveValue(10L), new PrimitiveValue(10.0), ExpectedResult.EQUAL },
+                { new PrimitiveValue(-10L), new PrimitiveValue(-5.0), ExpectedResult.LOWER },
+                { new PrimitiveValue(9.9), new PrimitiveValue(10L), ExpectedResult.LOWER },
+
+            };
+    }
+
+    @Test(dataProvider = "values")
+    public void testComparison(PrimitiveValue v1, PrimitiveValue v2, ExpectedResult expectedResult)
+    {
+        switch (expectedResult)
+        {
+            case EQUAL:
+                assertTrue(v1.compareTo(v2) == 0);
+                assertTrue(v2.compareTo(v1) == 0);
+                break;
+            case LOWER:
+                assertTrue(v1.compareTo(v2) < 0);
+                assertTrue(v2.compareTo(v1) > 0);
+                break;
+            case HIGHER:
+                assertTrue(v1.compareTo(v2) > 0);
+                assertTrue(v2.compareTo(v1) < 0);
+                break;
+        }
+    }
+}
-- 
GitLab