HashMapTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.util;
18
19import dalvik.annotation.TestInfo;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTarget;
22import dalvik.annotation.TestTargetClass;
23
24import junit.framework.TestCase;
25
26import java.util.HashMap;
27import java.util.Map;
28
29import org.apache.harmony.testframework.serialization.SerializationTest;
30
31@TestTargetClass(HashMap.class)
32public class HashMapTest extends TestCase {
33    class SubMap<K, V> extends HashMap<K, V> {
34        public SubMap(Map<? extends K, ? extends V> m) {
35            super(m);
36        }
37
38        public V put(K key, V value) {
39            throw new UnsupportedOperationException();
40        }
41    }
42
43    /**
44     * @tests java.util.HashMap#HashMap(java.util.Map)
45     */
46    @TestInfo(
47      level = TestLevel.PARTIAL,
48      purpose = "NullPointerException is not verified.",
49      targets = {
50        @TestTarget(
51          methodName = "HashMap",
52          methodArgs = {java.util.Map.class}
53        )
54    })
55    public void test_ConstructorLjava_util_Map() {
56        HashMap map = new HashMap();
57        map.put("a", "a");
58        SubMap map2 = new SubMap(map);
59        assertTrue(map2.containsKey("a"));
60        assertTrue(map2.containsValue("a"));
61    }
62
63    /**
64     * @tests serialization/deserialization.
65     */
66    @TestInfo(
67      level = TestLevel.COMPLETE,
68      purpose = "",
69      targets = {
70        @TestTarget(
71          methodName = "!SerializationSelf",
72          methodArgs = {}
73        )
74    })
75    public void testSerializationSelf() throws Exception {
76        HashMap<String, String> hm = new HashMap<String, String>();
77        hm.put("key", "value");
78
79        SerializationTest.verifySelf(hm);
80
81        //  regression for HARMONY-1583
82        hm.put(null, "null");
83        SerializationTest.verifySelf(hm);
84    }
85
86    /**
87     * @tests serialization/deserialization compatibility with RI.
88     */
89    @TestInfo(
90      level = TestLevel.COMPLETE,
91      purpose = "",
92      targets = {
93        @TestTarget(
94          methodName = "!SerializationGolden",
95          methodArgs = {}
96        )
97    })
98    public void testSerializationCompatibility() throws Exception {
99        HashMap<String, String> hm = new HashMap<String, String>();
100        hm.put("key", "value");
101
102        SerializationTest.verifyGolden(this, hm);
103    }
104}
105