harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r439252 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/ test/java/tests/api/java/util/ test/resources/serialization/tests/ test/resources/serialization/tests/api/ test/resources/serialization/tests/api...
Date Fri, 01 Sep 2006 09:50:50 GMT
Author: pyang
Date: Fri Sep  1 02:50:49 2006
New Revision: 439252

URL: http://svn.apache.org/viewvc?rev=439252&view=rev
Log:
Patch applied for HARMONY-1356 ([classlib][luni] new method clone(), equals(Object) writeObject(ObjectOutputStream)
and readObject(ObjectInputStream) in java.util.EnumMap)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/tests/
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/tests/api/
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/tests/api/java/
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/tests/api/java/util/
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/tests/api/java/util/EnumMapTest.golden.ser
  (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java?rev=439252&r1=439251&r2=439252&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java
Fri Sep  1 02:50:49 2006
@@ -15,11 +15,12 @@
 
 package java.util;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.lang.reflect.Array;
 
-import org.apache.harmony.luni.util.NotYetImplementedException;
-
 public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V> implements
         Map<K, V>, Serializable, Cloneable {
     private static final long serialVersionUID = 458661240069192865L;
@@ -442,8 +443,15 @@
      * 
      * @return a shallow copy of this map
      */
-    public EnumMap<K,V> clone() {
-        throw new NotYetImplementedException();
+    @Override
+    public EnumMap<K, V> clone() {
+        try {
+            EnumMap<K, V> enumMap = (EnumMap<K, V>) super.clone();
+            enumMap.initialization(this);
+            return enumMap;
+        } catch (CloneNotSupportedException e) {
+            return null;
+        }
     }
 
     /**
@@ -512,8 +520,20 @@
      *            the object to be compared with this map
      * @return true if the given object is equal to this map.
      */
+    @Override
     public boolean equals(Object object) {
-        throw new NotYetImplementedException();
+        if (this == object) {
+            return true;
+        }
+        if (!(object instanceof EnumMap)) {
+            return super.equals(object);
+        }
+        EnumMap<K, V> enumMap = (EnumMap<K, V>) object;
+        if (keyType != enumMap.keyType || size() != enumMap.size()) {
+            return false;
+        }
+        return Arrays.equals(hasMapping, enumMap.hasMapping) &&
+            Arrays.equals(values, enumMap.values);
     }
 
     /**
@@ -636,6 +656,31 @@
             valuesCollection = new EnumMapValueCollection<K, V>(this);
         }
         return valuesCollection;
+    }
+
+    private void readObject(ObjectInputStream stream) throws IOException,
+            ClassNotFoundException {
+        stream.defaultReadObject();
+        initialization(keyType);
+        int elementCount = stream.readInt();
+        Enum<K> enumKey;
+        Object value;
+        for (int i = elementCount; i > 0; i--) {
+            enumKey = (Enum<K>) stream.readObject();
+            value = stream.readObject();
+            putImpl((K) enumKey, (V) value);
+        }
+    }
+
+    private void writeObject(ObjectOutputStream stream) throws IOException {
+        stream.defaultWriteObject();
+        stream.writeInt(mappingsCount);
+        Iterator iterator = entrySet().iterator();
+        while (iterator.hasNext()) {
+            Map.Entry<K, V> entry = (Map.Entry<K, V>) iterator.next();
+            stream.writeObject(entry.getKey());
+            stream.writeObject(entry.getValue());
+        }
     }
 
     private boolean isValidKeyType(Object key) {

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java?rev=439252&r1=439251&r2=439252&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java
Fri Sep  1 02:50:49 2006
@@ -24,6 +24,8 @@
 import java.util.NoSuchElementException;
 import java.util.Set;
 
+import org.apache.harmony.testframework.serialization.SerializationTest;
+
 import junit.framework.TestCase;
 
 public class EnumMapTest extends TestCase {
@@ -70,7 +72,6 @@
         }
     }
 
-
     /**
      * @tests java.util.EnumMap#EnumMap(Class)
      */
@@ -228,7 +229,7 @@
             // Expected
         }
     }
-    
+
     /**
      * @tests java.util.EnumMap#clear()
      */
@@ -265,6 +266,28 @@
     }
 
     /**
+     * @tests java.util.EnumMap#clone()
+     */
+    @SuppressWarnings( { "unchecked", "boxing" })
+    public void test_clone() {
+        EnumMap enumSizeMap = new EnumMap(Size.class);
+        Integer integer = new Integer("3"); //$NON-NLS-1$
+        enumSizeMap.put(Size.Small, integer);
+        EnumMap enumSizeMapClone = enumSizeMap.clone();
+        assertNotSame("Should not be same", enumSizeMap, enumSizeMapClone); //$NON-NLS-1$
+        assertEquals("Clone answered unequal EnumMap", enumSizeMap, //$NON-NLS-1$
+                enumSizeMapClone);
+
+        assertSame("Should be same", enumSizeMap.get(Size.Small), //$NON-NLS-1$
+                enumSizeMapClone.get(Size.Small));
+        assertSame("Clone is not shallow clone", integer, enumSizeMapClone //$NON-NLS-1$
+                .get(Size.Small));
+        enumSizeMap.remove(Size.Small);
+        assertSame("Clone is not shallow clone", integer, enumSizeMapClone //$NON-NLS-1$
+                .get(Size.Small));
+    }
+
+    /**
      * @tests java.util.EnumMap#containsValue(Object)
      */
     @SuppressWarnings( { "unchecked", "boxing" })
@@ -524,6 +547,46 @@
     }
 
     /**
+     * @tests java.util.EnumMap#equals(Object)
+     */
+    @SuppressWarnings( { "unchecked", "boxing" })
+    public void test_equalsLjava_lang_Object() {
+        EnumMap enumMap = new EnumMap(Size.class);
+        enumMap.put(Size.Small, 1);
+
+        EnumMap enumSizeMap = new EnumMap(Size.class);
+        assertFalse("Returned true for unequal EnumMap", enumSizeMap //$NON-NLS-1$
+                .equals(enumMap));
+        enumSizeMap.put(Size.Small, 1);
+        assertTrue("Returned false for equal EnumMap", enumSizeMap //$NON-NLS-1$
+                .equals(enumMap));
+        enumSizeMap.put(Size.Big, null);
+        assertFalse("Returned true for unequal EnumMap", enumSizeMap //$NON-NLS-1$
+                .equals(enumMap));
+
+        enumMap.put(Size.Middle, null);
+        assertFalse("Returned true for unequal EnumMap", enumSizeMap //$NON-NLS-1$
+                .equals(enumMap));
+        enumMap.remove(Size.Middle);
+        enumMap.put(Size.Big, 3);
+        assertFalse("Returned true for unequal EnumMap", enumSizeMap //$NON-NLS-1$
+                .equals(enumMap));
+        enumMap.put(Size.Big, null);
+        assertTrue("Returned false for equal EnumMap", enumSizeMap //$NON-NLS-1$
+                .equals(enumMap));
+
+        HashMap hashMap = new HashMap();
+        hashMap.put(Size.Small, 1);
+        assertFalse("Returned true for unequal EnumMap", hashMap //$NON-NLS-1$
+                .equals(enumMap));
+        hashMap.put(Size.Big, null);
+        assertTrue("Returned false for equal EnumMap", enumMap.equals(hashMap)); //$NON-NLS-1$
+
+        assertFalse("Should return false", enumSizeMap //$NON-NLS-1$
+                .equals(new Integer(1)));
+    }
+
+    /**
      * @tests java.util.EnumMap#keySet()
      */
     @SuppressWarnings( { "unchecked", "boxing" })
@@ -1069,6 +1132,27 @@
         } catch (NoSuchElementException e) {
             // Expected
         }
+    }
+
+    /**
+     * @tests serialization/deserialization.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationSelf() throws Exception {
+        EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class);
+        enumColorMap.put(Color.Blue, 3);
+        SerializationTest.verifySelf(enumColorMap);
+    }
+
+    /**
+     * @tests serialization/deserialization compatibility with RI.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationCompatibility() throws Exception {
+        EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class);
+        enumColorMap.put(Color.Red, 1);
+        enumColorMap.put(Color.Blue, 3);
+        SerializationTest.verifyGolden(this, enumColorMap);
     }
 
     /**

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/tests/api/java/util/EnumMapTest.golden.ser
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/tests/api/java/util/EnumMapTest.golden.ser?rev=439252&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/tests/api/java/util/EnumMapTest.golden.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message