You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by br...@apache.org on 2009/08/15 16:30:21 UTC

svn commit: r804476 - in /incubator/pivot/trunk/core: src/org/apache/pivot/collections/MapList.java test/org/apache/pivot/collections/test/MapListTest.java

Author: brindy
Date: Sat Aug 15 14:30:21 2009
New Revision: 804476

URL: http://svn.apache.org/viewvc?rev=804476&view=rev
Log:
Added a test for MapList

Changed MapList:
- source is never null. previously it was easy to generate an NPE
- changed update so that it doesn't remove before doing a put from the source.  previously update on the map handler was never being called

Added:
    incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/MapListTest.java
Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/MapList.java

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/collections/MapList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/collections/MapList.java?rev=804476&r1=804475&r2=804476&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/collections/MapList.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/collections/MapList.java Sat Aug 15 14:30:21 2009
@@ -26,13 +26,13 @@
  * Decorates a {@link Map} to look like a {@link List} of key/value pairs. This
  * facilitates the use of a <tt>Map</tt> as table data in a
  * {@link org.apache.pivot.wtk.TableView}.
- *
+ * 
  * @author tvolkert
  */
 public class MapList<K, V> implements List<Pair<K, V>> {
     /**
      * Map list listener list.
-     *
+     * 
      * @author tvolkert
      */
     private static class MapListListenerList<K, V> extends ListenerList<MapListListener<K, V>>
@@ -48,6 +48,8 @@
 
     private ArrayList<Pair<K, V>> view = null;
 
+    // this flag is used to prevent recursion if the source is updated
+    // externally
     private boolean updating = false;
 
     private ListListenerList<Pair<K, V>> listListeners = new ListListenerList<Pair<K, V>>();
@@ -120,9 +122,9 @@
 
     /**
      * Creates a new map list that decorates the specified source map.
-     *
+     * 
      * @param source
-     * The map to present as a list
+     *            The map to present as a list
      */
     public MapList(Map<K, V> source) {
         setSource(source);
@@ -130,9 +132,8 @@
 
     /**
      * Gets the source map.
-     *
-     * @return
-     * The source map, or <tt>null</tt> if no source is set
+     * 
+     * @return The source map, or <tt>null</tt> if no source is set
      */
     public Map<K, V> getSource() {
         return source;
@@ -140,11 +141,15 @@
 
     /**
      * Sets the source map.
-     *
+     * 
      * @param source
-     * The source map, or <tt>null</tt> to clear the source
+     *            The source map, or <tt>null</tt> to clear the source
      */
     public void setSource(Map<K, V> source) {
+        if (source == null) {
+            source = new HashMap<K, V>();
+        }
+
         Map<K, V> previousSource = this.source;
 
         if (previousSource != source) {
@@ -168,14 +173,10 @@
             mapListListeners.sourceChanged(this, previousSource);
 
             // Refresh the view
-            if (source == null) {
-                view = null;
-            } else {
-                view = new ArrayList<Pair<K, V>>(source.count());
+            view = new ArrayList<Pair<K, V>>(source.count());
 
-                for (K key : source) {
-                    listListeners.itemInserted(this, view.add(new Pair<K, V>(key, source.get(key))));
-                }
+            for (K key : source) {
+                listListeners.itemInserted(this, view.add(new Pair<K, V>(key, source.get(key))));
             }
         }
     }
@@ -249,8 +250,7 @@
 
         Pair<K, V> previousPair = view.get(index);
 
-        if (!pair.key.equals(previousPair.key)
-            && source.containsKey(pair.key)) {
+        if (!pair.key.equals(previousPair.key) && source.containsKey(pair.key)) {
             throw new IllegalArgumentException("Duplicate keys not allowed.");
         }
 
@@ -260,7 +260,6 @@
         // Update the source
         updating = true;
         try {
-            source.remove(previousPair.key);
             source.put(pair.key, pair.value);
         } finally {
             updating = false;
@@ -280,7 +279,7 @@
         int index = indexOf(pair);
 
         if (index >= 0) {
-           remove(index, 1);
+            remove(index, 1);
         }
 
         return index;
@@ -328,15 +327,13 @@
     }
 
     /**
-     * Finds the specified pair in the view list by searching linearly
-     * and reporting an exact match only (bypasses the list's
-     * comparator).
-     *
+     * Finds the specified pair in the view list by searching linearly and
+     * reporting an exact match only (bypasses the list's comparator).
+     * 
      * @param pair
-     * The pair to search for
-     *
-     * @return
-     * The index of the pair in the list, or <tt>-1</tt> if not found
+     *            The pair to search for
+     * 
+     * @return The index of the pair in the list, or <tt>-1</tt> if not found
      */
     private int linearSearch(Pair<K, V> pair) {
         int index = -1;

Added: incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/MapListTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/MapListTest.java?rev=804476&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/MapListTest.java (added)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/MapListTest.java Sat Aug 15 14:30:21 2009
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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 org.apache.pivot.collections.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.util.Comparator;
+import java.util.Iterator;
+
+import org.apache.pivot.collections.HashMap;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.collections.MapList;
+import org.apache.pivot.collections.MapListListener;
+import org.apache.pivot.collections.Sequence;
+import org.apache.pivot.collections.Map.Pair;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MapListTest {
+
+    private TestMapListListener listener;
+
+    @Before
+    public void createListener() {
+        listener = new TestMapListListener();
+    }
+
+    @Test
+    public void basicTest() {
+        MapList<String, Integer> mapList = new MapList<String, Integer>();
+        assertMapList(mapList);
+
+        mapList = new MapList<String, Integer>(new HashMap<String, Integer>());
+        assertMapList(mapList);
+
+        Map<String, Integer> source = mapList.getSource();
+        mapList.getMapListListeners().add(listener);
+        mapList.setSource(null);
+        assertEquals(1, listener.calls);
+        assertEquals(mapList, listener.mapList);
+        assertEquals(source, listener.previousSource);
+        assertMapList(mapList);
+
+        TestComparator comparator = new TestComparator();
+        mapList.setComparator(comparator);
+        assertEquals(comparator, mapList.getComparator());
+    }
+
+    private void assertMapList(MapList<String, Integer> mapList) {
+        assertNull(mapList.getComparator());
+        assertNotNull(mapList.getListListeners());
+        assertNotNull(mapList.getMapListListeners());
+        assertEquals(0, mapList.getLength());
+
+        Pair<String, Integer> pair = new Pair<String, Integer>("a", 1);
+
+        assertEquals(-1, mapList.indexOf(pair));
+
+        mapList.add(pair);
+        assertEquals(1, mapList.getLength());
+
+        Map<String, Integer> newSource = new HashMap<String, Integer>();
+        newSource.put("b", 2);
+        newSource.put("c", 3);
+        mapList.setSource(newSource);
+
+        assertEquals(newSource, mapList.getSource());
+        assertEquals(2, mapList.getLength());
+
+        int iteratorCount = 0;
+        Iterator<Pair<String, Integer>> iter = mapList.iterator();
+        while (iter.hasNext()) {
+            pair = iter.next();
+            if (mapList.indexOf(pair) != iteratorCount) {
+                fail("Unexpected pair " + pair + " in iterator");
+            }
+            iteratorCount++;
+        }
+        assertEquals(2, iteratorCount);
+
+        Pair<String, Integer> newCPair = new Pair<String, Integer>("c", 33);
+        mapList.update(1, newCPair);
+        assertEquals(newCPair, mapList.get(1));
+
+        Pair<String, Integer> aaPair = new Pair<String, Integer>("aa", 11);
+        mapList.insert(aaPair, 1);
+        assertEquals(3, mapList.getLength());
+        assertEquals(aaPair, mapList.get(1));
+        assertEquals(newCPair, mapList.get(2));
+
+        Sequence<Pair<String, Integer>> sequence = mapList.remove(0, 1);
+        assertNotNull(sequence);
+        pair = sequence.get(0);
+        assertNotNull(pair);
+        assertEquals("b", pair.key);
+        assertEquals(2, pair.value);
+
+        assertEquals(2, mapList.getLength());
+
+        newSource.put("aa", 77);
+        pair = mapList.get(1);
+        assertEquals(pair.key, "aa");
+        assertEquals(pair.value, 77);
+
+        newSource.remove("aa");
+        assertEquals(1, mapList.getLength());
+
+        mapList.clear();
+        assertEquals(0, mapList.getLength());
+
+        pair = new Pair<String, Integer>("d", 4);
+        mapList.add(pair);
+        assertEquals(1, mapList.getLength());
+        assertEquals(pair, mapList.get(0));
+        
+        assertEquals(0, mapList.remove(pair));
+        assertEquals(0, mapList.getLength());
+
+        newSource.put("z", 24);
+        assertEquals(1, mapList.getLength());
+        
+        newSource.put("z", 24);
+        assertEquals(1, mapList.getLength());
+        
+        newSource.clear();
+        assertEquals(0, mapList.getLength());
+    }
+
+    private class TestMapListListener implements MapListListener<String, Integer> {
+        private MapList<String, Integer> mapList;
+        private Map<String, Integer> previousSource;
+        private int calls;
+
+        @Override
+        public void sourceChanged(MapList<String, Integer> mapList,
+            Map<String, Integer> previousSource) {
+            this.mapList = mapList;
+            this.previousSource = previousSource;
+            this.calls++;
+        }
+    }
+
+    private class TestComparator implements Comparator<Pair<String, Integer>> {
+        @Override
+        public int compare(Pair<String, Integer> o1, Pair<String, Integer> o2) {
+            return o1.key.compareTo(o2.key);
+        }
+    }
+
+}