1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import com.google.common.collect.ImmutableClassToInstanceMapTest.Impl; 20 import com.google.common.collect.ImmutableClassToInstanceMapTest.TestClassToInstanceMapGenerator; 21 import com.google.common.collect.testing.MapTestSuiteBuilder; 22 import com.google.common.collect.testing.features.CollectionFeature; 23 import com.google.common.collect.testing.features.CollectionSize; 24 import com.google.common.collect.testing.features.MapFeature; 25 import java.util.Map; 26 import java.util.Map.Entry; 27 import junit.framework.Test; 28 import junit.framework.TestCase; 29 import junit.framework.TestSuite; 30 31 /** 32 * Unit test of {@link MutableClassToInstanceMap}. 33 * 34 * @author Kevin Bourrillion 35 */ 36 public class MutableClassToInstanceMapTest extends TestCase { suite()37 public static Test suite() { 38 TestSuite suite = new TestSuite(); 39 suite.addTestSuite(MutableClassToInstanceMapTest.class); 40 41 suite.addTest( 42 MapTestSuiteBuilder.using( 43 new TestClassToInstanceMapGenerator() { 44 // Other tests will verify what real, warning-free usage looks like 45 // but here we have to do some serious fudging 46 @Override 47 @SuppressWarnings("unchecked") 48 public Map<Class, Impl> create(Object... elements) { 49 MutableClassToInstanceMap<Impl> map = MutableClassToInstanceMap.create(); 50 for (Object object : elements) { 51 Entry<Class, Impl> entry = (Entry<Class, Impl>) object; 52 map.putInstance(entry.getKey(), entry.getValue()); 53 } 54 return (Map) map; 55 } 56 }) 57 .named("MutableClassToInstanceMap") 58 .withFeatures( 59 MapFeature.GENERAL_PURPOSE, 60 MapFeature.RESTRICTS_KEYS, 61 MapFeature.ALLOWS_NULL_VALUES, 62 CollectionSize.ANY, 63 CollectionFeature.SERIALIZABLE, 64 CollectionFeature.SUPPORTS_ITERATOR_REMOVE, 65 MapFeature.ALLOWS_ANY_NULL_QUERIES) 66 .createTestSuite()); 67 68 return suite; 69 } 70 71 private ClassToInstanceMap<Number> map; 72 73 @Override setUp()74 protected void setUp() throws Exception { 75 map = MutableClassToInstanceMap.create(); 76 } 77 testConstraint()78 public void testConstraint() { 79 80 /** 81 * We'll give ourselves a pass on testing all the possible ways of breaking the constraint, 82 * because we know that newClassMap() is implemented using ConstrainedMap which is itself 83 * well-tested. A purist would object to this, but what can I say, we're dirty cheaters. 84 */ 85 map.put(Integer.class, new Integer(5)); 86 try { 87 map.put(Double.class, new Long(42)); 88 fail(); 89 } catch (ClassCastException expected) { 90 } 91 // Won't compile: map.put(String.class, "x"); 92 } 93 testPutAndGetInstance()94 public void testPutAndGetInstance() { 95 assertNull(map.putInstance(Integer.class, new Integer(5))); 96 97 Integer oldValue = map.putInstance(Integer.class, new Integer(7)); 98 assertEquals(5, (int) oldValue); 99 100 Integer newValue = map.getInstance(Integer.class); 101 assertEquals(7, (int) newValue); 102 103 // Won't compile: map.putInstance(Double.class, new Long(42)); 104 } 105 testNull()106 public void testNull() { 107 try { 108 map.put(null, new Integer(1)); 109 fail(); 110 } catch (NullPointerException expected) { 111 } 112 map.putInstance(Integer.class, null); 113 assertNull(map.get(Integer.class)); 114 assertNull(map.getInstance(Integer.class)); 115 116 map.put(Long.class, null); 117 assertNull(map.get(Long.class)); 118 assertNull(map.getInstance(Long.class)); 119 } 120 testPrimitiveAndWrapper()121 public void testPrimitiveAndWrapper() { 122 assertNull(map.getInstance(int.class)); 123 assertNull(map.getInstance(Integer.class)); 124 125 assertNull(map.putInstance(int.class, 0)); 126 assertNull(map.putInstance(Integer.class, 1)); 127 assertEquals(2, map.size()); 128 129 assertEquals(0, (int) map.getInstance(int.class)); 130 assertEquals(1, (int) map.getInstance(Integer.class)); 131 132 assertEquals(0, (int) map.putInstance(int.class, null)); 133 assertEquals(1, (int) map.putInstance(Integer.class, null)); 134 135 assertNull(map.getInstance(int.class)); 136 assertNull(map.getInstance(Integer.class)); 137 assertEquals(2, map.size()); 138 } 139 } 140