1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.harmony.tests.java.util; 18 19 import junit.framework.TestCase; 20 import org.apache.harmony.testframework.serialization.SerializationTest; 21 import tests.util.SerializationTester; 22 import java.lang.reflect.Array; 23 import java.util.AbstractMap; 24 import java.util.AbstractMap.SimpleImmutableEntry; 25 import java.util.Map; 26 import java.util.Map.Entry; 27 import java.util.TreeMap; 28 29 public class SimpleImmutableEntryTest extends TestCase { 30 public void test_SimpleImmutableEntry_Constructor_K_V() throws Exception { 31 new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 32 new AbstractMap.SimpleImmutableEntry(null, null); 33 } 34 35 static class NullEntry implements Entry { 36 37 public Object getKey() { 38 return null; 39 } 40 41 public Object getValue() { 42 return null; 43 } 44 45 public Object setValue(Object object) { 46 return null; 47 } 48 } 49 50 public void test_SimpleImmutableEntry_Constructor_LEntry() throws Exception { 51 Map map = new TreeMap(); 52 map.put(1, "test"); 53 Entry entryToPut = (Entry) map.entrySet().iterator().next(); 54 Entry testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut); 55 assertEquals(1, testEntry.getKey()); 56 assertEquals("test", testEntry.getValue()); 57 map.clear(); 58 59 testEntry = new AbstractMap.SimpleImmutableEntry(new NullEntry()); 60 assertNull(testEntry.getKey()); 61 assertNull(testEntry.getValue()); 62 try { 63 new AbstractMap.SimpleImmutableEntry(null); 64 fail("Should throw NullPointerException"); 65 } catch (NullPointerException e) { 66 // expected 67 } 68 69 } 70 71 public void test_SimpleImmutableEntry_getKey() throws Exception { 72 Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 73 assertEquals(1, entry.getKey()); 74 entry = new AbstractMap.SimpleImmutableEntry(null, null); 75 assertNull(entry.getKey()); 76 } 77 78 public void test_SimpleImmutableEntry_getValue() throws Exception { 79 Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 80 assertEquals("test", entry.getValue()); 81 entry = new AbstractMap.SimpleImmutableEntry(null, null); 82 assertNull(entry.getValue()); 83 } 84 85 public void test_SimpleImmutableEntry_setValue() throws Exception { 86 Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 87 assertEquals("test", entry.getValue()); 88 try { 89 entry.setValue("Another String"); 90 fail("should throw UnsupportedOperationException"); 91 } catch (UnsupportedOperationException e) { 92 // expected 93 } 94 assertEquals("test", entry.getValue()); 95 try { 96 entry.setValue(null); 97 fail("should throw UnsupportedOperationException"); 98 } catch (UnsupportedOperationException e) { 99 // expected 100 } 101 } 102 103 public void test_SimpleImmutableEntry_equals() throws Exception { 104 Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 105 Map map = new TreeMap(); 106 map.put(1, "test"); 107 Entry entryToPut = (Entry) map.entrySet().iterator().next(); 108 Entry testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut); 109 assertEquals(entry, testEntry); 110 } 111 112 public void test_SimpleImmutableEntry_hashCode() throws Exception { 113 Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 114 assertEquals((e.getKey() == null ? 0 : e.getKey().hashCode()) 115 ^ (e.getValue() == null ? 0 : e.getValue().hashCode()), e 116 .hashCode()); 117 } 118 119 public void test_SimpleImmutableEntry_toString() throws Exception { 120 Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 121 assertEquals(e.getKey() + "=" + e.getValue(), e.toString()); 122 Object array = Array.newInstance((byte[].class).getComponentType(), 10); 123 assertEquals(10, ((byte[]) array).length); 124 } 125 126 /** 127 * serialization/deserialization. 128 */ 129 @SuppressWarnings({ "unchecked", "boxing" }) 130 public void testSerializationSelf_SimpleImmutableEntry() throws Exception { 131 Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 132 SerializationTest.verifySelf(e); 133 } 134 135 /** 136 * serialization/deserialization compatibility with RI. 137 */ 138 @SuppressWarnings({ "unchecked", "boxing" }) 139 public void testSerializationCompatibility_SimpleImmutableEntry() throws Exception { 140 SimpleImmutableEntry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 141 if (!(SerializationTester.readObject(e, "serialization/org/apache/harmony/tests/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser") instanceof SimpleImmutableEntry)) { 142 fail("should be SimpleImmutableEntry"); 143 } 144 SerializationTester.assertCompabilityEquals(e, "serialization/org/apache/harmony/tests/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser"); 145 } 146 } 147