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 */ 17package org.apache.harmony.luni.tests.java.util; 18 19import java.lang.reflect.Array; 20import java.util.AbstractMap; 21import java.util.Map; 22import java.util.TreeMap; 23import java.util.AbstractMap.SimpleImmutableEntry; 24import java.util.Map.Entry; 25 26import junit.framework.TestCase; 27 28import org.apache.harmony.testframework.serialization.SerializationTest; 29 30import tests.util.SerializationTester; 31 32public class SimpleImmutableEntryTest extends TestCase { 33 public void test_SimpleImmutableEntry_Constructor_K_V() throws Exception { 34 new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test"); 35 new AbstractMap.SimpleImmutableEntry(null,null); 36 } 37 38 public void test_SimpleImmutableEntry_Constructor_LEntry() throws Exception { 39 Map map = new TreeMap(); 40 map.put(1, "test"); 41 Entry entryToPut = (Entry)map.entrySet().iterator().next(); 42 Entry testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut); 43 assertEquals(1,testEntry.getKey()); 44 assertEquals("test",testEntry.getValue()); 45 map.clear(); 46 map.put(null, null); 47 entryToPut = (Entry)map.entrySet().iterator().next(); 48 testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut); 49 assertNull(testEntry.getKey()); 50 assertNull(testEntry.getValue()); 51 try { 52 new AbstractMap.SimpleImmutableEntry(null); 53 fail("Should throw NullPointerException"); 54 } catch (NullPointerException e) { 55 // expected 56 } 57 58 } 59 60 public void test_SimpleImmutableEntry_getKey() throws Exception { 61 Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test"); 62 assertEquals(1,entry.getKey()); 63 entry = new AbstractMap.SimpleImmutableEntry(null,null); 64 assertNull(entry.getKey()); 65 } 66 67 public void test_SimpleImmutableEntry_getValue() throws Exception { 68 Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test"); 69 assertEquals("test",entry.getValue()); 70 entry = new AbstractMap.SimpleImmutableEntry(null,null); 71 assertNull(entry.getValue()); 72 } 73 74 public void test_SimpleImmutableEntry_setValue() throws Exception { 75 Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test"); 76 assertEquals("test",entry.getValue()); 77 try{ 78 entry.setValue("Another String"); 79 fail("should throw UnsupportedOperationException"); 80 } catch (UnsupportedOperationException e){ 81 // expected 82 } 83 assertEquals("test",entry.getValue()); 84 try{ 85 entry.setValue(null); 86 fail("should throw UnsupportedOperationException"); 87 } catch (UnsupportedOperationException e){ 88 // expected 89 } 90 } 91 92 public void test_SimpleImmutableEntry_equals() throws Exception { 93 Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test"); 94 Map map = new TreeMap(); 95 map.put(1, "test"); 96 Entry entryToPut = (Entry)map.entrySet().iterator().next(); 97 Entry testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut); 98 assertEquals(entry,testEntry); 99 } 100 101 public void test_SimpleImmutableEntry_hashCode() throws Exception { 102 Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 103 assertEquals((e.getKey() == null ? 0 : e.getKey().hashCode()) 104 ^ (e.getValue() == null ? 0 : e.getValue().hashCode()), e 105 .hashCode()); 106 } 107 108 public void test_SimpleImmutableEntry_toString() throws Exception { 109 Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 110 assertEquals(e.getKey()+"="+e.getValue(),e.toString()); 111 Object array =Array.newInstance((byte[].class).getComponentType(), 10); 112 assertEquals(10,((byte[])array).length); 113 } 114 115 /** 116 * @tests serialization/deserialization. 117 */ 118 @SuppressWarnings({ "unchecked", "boxing" }) 119 public void testSerializationSelf_SimpleImmutableEntry() throws Exception { 120 Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 121 SerializationTest.verifySelf(e); 122 } 123 124 /** 125 * @tests serialization/deserialization compatibility with RI. 126 */ 127 @SuppressWarnings({ "unchecked", "boxing" }) 128 public void testSerializationCompatibility_SimpleImmutableEntry() throws Exception { 129 SimpleImmutableEntry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test"); 130 if (!(SerializationTester.readObject(e, "serialization/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser") instanceof SimpleImmutableEntry)){ 131 fail("should be SimpleImmutableEntry"); 132 } 133 SerializationTester.assertCompabilityEquals(e, "serialization/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser"); 134 } 135} 136