From 148dfde78ec9109ad9faf6a7ada02eb3b57ca996 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Tue, 5 Feb 2008 20:56:54 +0000
Subject: [PATCH] add: unit test for TableMap fix: KEEP_FIRST strategy and
 thrown exception

SVN: 4028
---
 .../cisd/common/collections/TableMap.java     |   6 +-
 .../cisd/common/collections/TableMapTest.java | 109 ++++++++++++++++++
 2 files changed, 113 insertions(+), 2 deletions(-)
 create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/collections/TableMapTest.java

diff --git a/common/source/java/ch/systemsx/cisd/common/collections/TableMap.java b/common/source/java/ch/systemsx/cisd/common/collections/TableMap.java
index 498c20f5b40..e7eb7c741eb 100644
--- a/common/source/java/ch/systemsx/cisd/common/collections/TableMap.java
+++ b/common/source/java/ch/systemsx/cisd/common/collections/TableMap.java
@@ -108,10 +108,12 @@ public class TableMap<K, E> implements Iterable<E>
                     map.put(key, row);
                     break;
                 case ERROR:
-                    throw new IllegalStateException();
+                    throw new UniqueKeyViolationException("Key '" + key.toString() + "' already in the map.");
             }
+        } else
+        {
+            map.put(key, row);
         }
-        map.put(key, row);
     }
 
     /**
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/collections/TableMapTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/collections/TableMapTest.java
new file mode 100644
index 00000000000..28a2f3e10ae
--- /dev/null
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/collections/TableMapTest.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2008 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.common.collections;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import static org.testng.AssertJUnit.*;
+
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.common.collections.TableMap.UniqueKeyViolationStrategy;
+
+/**
+ * Test cases for the {@link TableMap}
+ * 
+ * @author Bernd Rinn
+ */
+public class TableMapTest
+{
+
+    final IKeyExtractor<Integer, String> integerExtractor = new IKeyExtractor<Integer, String>()
+        {
+            public Integer getKey(String e)
+            {
+                final int i = e.indexOf(':');
+                if (i >= 0)
+                {
+                    return Integer.parseInt(e.substring(i + 1));
+                } else
+                {
+                    return Integer.parseInt(e);
+                }
+            }
+        };
+
+    @Test
+    public void testIteration()
+    {
+        final TableMap<Integer, String> tableMap =
+                new TableMap<Integer, String>(Arrays.asList("1", "7", "0"), integerExtractor);
+        Iterator<String> it = tableMap.iterator();
+        assertEquals("1", it.next());
+        assertEquals("7", it.next());
+        assertEquals("0", it.next());
+        assertFalse(it.hasNext());
+    }
+
+    @Test
+    public void testTryGet()
+    {
+        final TableMap<Integer, String> tableMap =
+                new TableMap<Integer, String>(Arrays.asList("1", "7", "0"), integerExtractor);
+        assertNull(tableMap.tryGet(10));
+        assertEquals("0", tableMap.tryGet(0));
+        assertEquals("1", tableMap.tryGet(1));
+        assertEquals("7", tableMap.tryGet(7));
+    }
+
+    @Test(expectedExceptions = TableMap.UniqueKeyViolationException.class)
+    public void testUniqueKeyViolationError()
+    {
+        final TableMap<Integer, String> tableMap =
+                new TableMap<Integer, String>(Arrays.asList("1", "7", "0", "1"), integerExtractor);
+        assertNull(tableMap.tryGet(10));
+        assertEquals("0", tableMap.tryGet(0));
+        assertEquals("1", tableMap.tryGet(1));
+        assertEquals("7", tableMap.tryGet(7));
+    }
+
+    @Test
+    public void testUniqueKeyViolationKeepFirst()
+    {
+        final TableMap<Integer, String> tableMap =
+                new TableMap<Integer, String>(Arrays.asList("a:1", "7", "0", "b:1"), integerExtractor,
+                        UniqueKeyViolationStrategy.KEEP_FIRST);
+        assertNull(tableMap.tryGet(10));
+        assertEquals("0", tableMap.tryGet(0));
+        assertEquals("a:1", tableMap.tryGet(1));
+        assertEquals("7", tableMap.tryGet(7));
+    }
+
+    @Test
+    public void testUniqueKeyViolationKeepLast()
+    {
+        final TableMap<Integer, String> tableMap =
+                new TableMap<Integer, String>(Arrays.asList("a:1", "7", "0", "b:1"), integerExtractor,
+                        UniqueKeyViolationStrategy.KEEP_LAST);
+        assertNull(tableMap.tryGet(10));
+        assertEquals("0", tableMap.tryGet(0));
+        assertEquals("b:1", tableMap.tryGet(1));
+        assertEquals("7", tableMap.tryGet(7));
+    }
+
+}
-- 
GitLab