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.util.AbstractMap; 20import java.util.Map; 21import java.util.TreeMap; 22import java.util.AbstractMap.SimpleEntry; 23import java.util.Map.Entry; 24 25import junit.framework.TestCase; 26 27import org.apache.harmony.testframework.serialization.SerializationTest; 28 29import tests.util.SerializationTester; 30 31public class SimpleEntryTest extends TestCase { 32 public void test_SimpleEntry_Constructor_K_V() throws Exception { 33 new AbstractMap.SimpleEntry<Integer, String>(1,"test"); 34 new AbstractMap.SimpleEntry(null,null); 35 } 36 37 public void test_SimpleEntry_Constructor_LEntry() throws Exception { 38 Map map = new TreeMap(); 39 map.put(1, "test"); 40 Entry entryToPut = (Entry)map.entrySet().iterator().next(); 41 Entry testEntry = new AbstractMap.SimpleEntry(entryToPut); 42 assertEquals(1,testEntry.getKey()); 43 assertEquals("test",testEntry.getValue()); 44 map.clear(); 45 map.put(null, null); 46 entryToPut = (Entry)map.entrySet().iterator().next(); 47 testEntry = new AbstractMap.SimpleEntry(entryToPut); 48 assertNull(testEntry.getKey()); 49 assertNull(testEntry.getValue()); 50 try { 51 new AbstractMap.SimpleEntry(null); 52 fail("Should throw NullPointerException"); 53 } catch (NullPointerException e) { 54 // expected 55 } 56 57 } 58 59 public void test_SimpleEntry_getKey() throws Exception { 60 Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1,"test"); 61 assertEquals(1,entry.getKey()); 62 entry = new AbstractMap.SimpleEntry(null,null); 63 assertNull(entry.getKey()); 64 } 65 66 public void test_SimpleEntry_getValue() throws Exception { 67 Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1,"test"); 68 assertEquals("test",entry.getValue()); 69 entry = new AbstractMap.SimpleEntry(null,null); 70 assertNull(entry.getValue()); 71 } 72 73 public void test_SimpleEntry_setValue() throws Exception { 74 Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1,"test"); 75 assertEquals("test",entry.getValue()); 76 entry.setValue("Another String"); 77 assertEquals("Another String",entry.getValue()); 78 entry = new AbstractMap.SimpleEntry(null,null); 79 assertNull(entry.getKey()); 80 } 81 82 public void test_SimpleEntry_equals() throws Exception { 83 Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1,"test"); 84 Map map = new TreeMap(); 85 map.put(1, "test"); 86 Entry entryToPut = (Entry)map.entrySet().iterator().next(); 87 Entry testEntry = new AbstractMap.SimpleEntry(entryToPut); 88 assertEquals(entry,testEntry); 89 Entry ent = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test"); 90 assertEquals(entry,ent); 91 } 92 93 public void test_SimpleEntry_hashCode() throws Exception { 94 Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test"); 95 assertEquals((e.getKey() == null ? 0 : e.getKey().hashCode()) 96 ^ (e.getValue() == null ? 0 : e.getValue().hashCode()), e 97 .hashCode()); 98 } 99 100 public void test_SimpleEntry_toString() throws Exception { 101 Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test"); 102 assertEquals(e.getKey()+"="+e.getValue(),e.toString()); 103 } 104 105 /** 106 * @tests serialization/deserialization. 107 */ 108 @SuppressWarnings({ "unchecked", "boxing" }) 109 public void testSerializationSelf_SimpleEntry() throws Exception { 110 Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test"); 111 SerializationTest.verifySelf(e); 112 } 113 114 /** 115 * @tests serialization/deserialization compatibility with RI. 116 */ 117 @SuppressWarnings({ "unchecked", "boxing" }) 118 public void testSerializationCompatibility_SimpleEntry() throws Exception { 119 SimpleEntry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test"); 120 SerializationTester.assertCompabilityEquals(e, "serialization/java/util/AbstractMapTest_SimpleEntry.golden.ser"); 121 } 122} 123