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 19665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport junit.framework.TestCase; 20665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport org.apache.harmony.testframework.serialization.SerializationTest; 21665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport tests.util.SerializationTester; 22cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.lang.reflect.Array; 23cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.AbstractMap; 24cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.AbstractMap.SimpleImmutableEntry; 25665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport java.util.Map; 26cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.Map.Entry; 27665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamathimport java.util.TreeMap; 28cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 29cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathpublic class SimpleImmutableEntryTest extends TestCase { 30cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_SimpleImmutableEntry_Constructor_K_V() throws Exception { 31cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 32cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new AbstractMap.SimpleImmutableEntry(null, null); 33cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 34cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 35665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath static class NullEntry implements Entry { 36665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath 37665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath public Object getKey() { 38665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath return null; 39665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath } 40665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath 41665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath public Object getValue() { 42665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath return null; 43665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath } 44665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath 45665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath public Object setValue(Object object) { 46665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath return null; 47665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath } 48665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath } 49665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath 50cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_SimpleImmutableEntry_Constructor_LEntry() throws Exception { 51cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Map map = new TreeMap(); 52cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath map.put(1, "test"); 53cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry entryToPut = (Entry) map.entrySet().iterator().next(); 54cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut); 55cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals(1, testEntry.getKey()); 56cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals("test", testEntry.getValue()); 57cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath map.clear(); 58665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath 59665c45e473c041dc2b1e8d85fbaf7008daa69c06Narayan Kamath testEntry = new AbstractMap.SimpleImmutableEntry(new NullEntry()); 60cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertNull(testEntry.getKey()); 61cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertNull(testEntry.getValue()); 62cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath try { 63cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new AbstractMap.SimpleImmutableEntry(null); 64cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath fail("Should throw NullPointerException"); 65cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } catch (NullPointerException e) { 66cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath // expected 67cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 68cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 69cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 70cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 71cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_SimpleImmutableEntry_getKey() throws Exception { 72cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 73cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals(1, entry.getKey()); 74cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath entry = new AbstractMap.SimpleImmutableEntry(null, null); 75cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertNull(entry.getKey()); 76cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 77cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 78cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_SimpleImmutableEntry_getValue() throws Exception { 79cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 80cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals("test", entry.getValue()); 81cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath entry = new AbstractMap.SimpleImmutableEntry(null, null); 82cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertNull(entry.getValue()); 83cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 84cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 85cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_SimpleImmutableEntry_setValue() throws Exception { 86cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 87cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals("test", entry.getValue()); 88cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath try { 89cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath entry.setValue("Another String"); 90cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath fail("should throw UnsupportedOperationException"); 91cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } catch (UnsupportedOperationException e) { 92cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath // expected 93cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 94cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals("test", entry.getValue()); 95cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath try { 96cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath entry.setValue(null); 97cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath fail("should throw UnsupportedOperationException"); 98cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } catch (UnsupportedOperationException e) { 99cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath // expected 100cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 101cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 102cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 103cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_SimpleImmutableEntry_equals() throws Exception { 104cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 105cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Map map = new TreeMap(); 106cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath map.put(1, "test"); 107cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry entryToPut = (Entry) map.entrySet().iterator().next(); 108cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut); 109cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals(entry, testEntry); 110cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 111cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 112cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_SimpleImmutableEntry_hashCode() throws Exception { 113cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 114cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals((e.getKey() == null ? 0 : e.getKey().hashCode()) 115cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath ^ (e.getValue() == null ? 0 : e.getValue().hashCode()), e 116cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath .hashCode()); 117cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 118cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 119cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_SimpleImmutableEntry_toString() throws Exception { 120cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 121cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals(e.getKey() + "=" + e.getValue(), e.toString()); 122cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Object array = Array.newInstance((byte[].class).getComponentType(), 10); 123cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals(10, ((byte[]) array).length); 124cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 125cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 126cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 127cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * serialization/deserialization. 128cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 129cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath @SuppressWarnings({ "unchecked", "boxing" }) 130cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void testSerializationSelf_SimpleImmutableEntry() throws Exception { 131cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 132cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath SerializationTest.verifySelf(e); 133cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 134cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 135cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 136cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * serialization/deserialization compatibility with RI. 137cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 138cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath @SuppressWarnings({ "unchecked", "boxing" }) 139cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void testSerializationCompatibility_SimpleImmutableEntry() throws Exception { 140cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath SimpleImmutableEntry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 141ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamath if (!(SerializationTester.readObject(e, "serialization/org/apache/harmony/tests/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser") instanceof SimpleImmutableEntry)) { 142cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath fail("should be SimpleImmutableEntry"); 143cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 144ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamath SerializationTester.assertCompabilityEquals(e, "serialization/org/apache/harmony/tests/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser"); 145cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 146cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath} 147