1cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath/*
2cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  Licensed to the Apache Software Foundation (ASF) under one or more
3cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  contributor license agreements.  See the NOTICE file distributed with
4cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  this work for additional information regarding copyright ownership.
5cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  The ASF licenses this file to You under the Apache License, Version 2.0
6cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  (the "License"); you may not use this file except in compliance with
7cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  the License.  You may obtain a copy of the License at
8cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *
9cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *     http://www.apache.org/licenses/LICENSE-2.0
10cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *
11cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  Unless required by applicable law or agreed to in writing, software
12cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  distributed under the License is distributed on an "AS IS" BASIS,
13cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  See the License for the specific language governing permissions and
15cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  limitations under the License.
16cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */
17ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamathpackage org.apache.harmony.tests.java.util;
18cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
19cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport junit.framework.TestCase;
20cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport org.apache.harmony.testframework.serialization.SerializationTest;
21cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport tests.util.SerializationTester;
22665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport java.util.AbstractMap;
23665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport java.util.AbstractMap.SimpleEntry;
24665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport java.util.Map;
25665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport java.util.Map.Entry;
26665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport java.util.TreeMap;
27cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
28cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathpublic class SimpleEntryTest extends TestCase {
29cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_SimpleEntry_Constructor_K_V() throws Exception {
30cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        new AbstractMap.SimpleEntry<Integer, String>(1, "test");
31cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        new AbstractMap.SimpleEntry(null, null);
32cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
33cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
34665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath    static class NullEntry implements Entry {
35665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath
36665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath        public Object getKey() {
37665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath            return null;
38665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath        }
39665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath
40665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath        public Object getValue() {
41665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath            return null;
42665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath        }
43665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath
44665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath        public Object setValue(Object object) {
45665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath            return null;
46665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath        }
47665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath    }
48665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath
49cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_SimpleEntry_Constructor_LEntry() throws Exception {
50cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Map map = new TreeMap();
51cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        map.put(1, "test");
52cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry entryToPut = (Entry) map.entrySet().iterator().next();
53cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry testEntry = new AbstractMap.SimpleEntry(entryToPut);
54cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals(1, testEntry.getKey());
55cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("test", testEntry.getValue());
56cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        map.clear();
57665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath
58665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath        testEntry = new AbstractMap.SimpleEntry(new NullEntry());
59cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull(testEntry.getKey());
60cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull(testEntry.getValue());
61cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
62cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            new AbstractMap.SimpleEntry(null);
63cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            fail("Should throw NullPointerException");
64cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (NullPointerException e) {
65cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // expected
66cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
67cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
68cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
69cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
70cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_SimpleEntry_getKey() throws Exception {
71cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
72cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals(1, entry.getKey());
73cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        entry = new AbstractMap.SimpleEntry(null, null);
74cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull(entry.getKey());
75cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
76cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
77cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_SimpleEntry_getValue() throws Exception {
78cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
79cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("test", entry.getValue());
80cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        entry = new AbstractMap.SimpleEntry(null, null);
81cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull(entry.getValue());
82cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
83cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
84cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_SimpleEntry_setValue() throws Exception {
85cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
86cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("test", entry.getValue());
87cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        entry.setValue("Another String");
88cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Another String", entry.getValue());
89cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        entry = new AbstractMap.SimpleEntry(null, null);
90cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull(entry.getKey());
91cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
92cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
93cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_SimpleEntry_equals() throws Exception {
94cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
95cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Map map = new TreeMap();
96cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        map.put(1, "test");
97cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry entryToPut = (Entry) map.entrySet().iterator().next();
98cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry testEntry = new AbstractMap.SimpleEntry(entryToPut);
99cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals(entry, testEntry);
100cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry ent = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test");
101cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals(entry, ent);
102cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
103cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
104cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_SimpleEntry_hashCode() throws Exception {
105cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
106cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals((e.getKey() == null ? 0 : e.getKey().hashCode())
107cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                ^ (e.getValue() == null ? 0 : e.getValue().hashCode()), e
108cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .hashCode());
109cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
110cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
111cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_SimpleEntry_toString() throws Exception {
112cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
113cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals(e.getKey() + "=" + e.getValue(), e.toString());
114cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
115cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
116cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
117cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * serialization/deserialization.
118cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
119cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    @SuppressWarnings({ "unchecked", "boxing" })
120cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void testSerializationSelf_SimpleEntry() throws Exception {
121cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
122cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        SerializationTest.verifySelf(e);
123cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
124cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
125cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
126cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * serialization/deserialization compatibility with RI.
127cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
128cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    @SuppressWarnings({ "unchecked", "boxing" })
129cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void testSerializationCompatibility_SimpleEntry() throws Exception {
130cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        SimpleEntry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
131ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamath        SerializationTester.assertCompabilityEquals(e, "serialization/org/apache/harmony/tests/java/util/AbstractMapTest_SimpleEntry.golden.ser");
132cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
133cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath}
134