• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static org.junit.Assert.assertThrows;
20 
21 import com.google.common.collect.ImmutableClassToInstanceMapTest.Impl;
22 import com.google.common.collect.ImmutableClassToInstanceMapTest.TestClassToInstanceMapGenerator;
23 import com.google.common.collect.testing.MapTestSuiteBuilder;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26 import com.google.common.collect.testing.features.MapFeature;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32 
33 /**
34  * Unit test of {@link MutableClassToInstanceMap}.
35  *
36  * @author Kevin Bourrillion
37  */
38 public class MutableClassToInstanceMapTest extends TestCase {
suite()39   public static Test suite() {
40     TestSuite suite = new TestSuite();
41     suite.addTestSuite(MutableClassToInstanceMapTest.class);
42 
43     suite.addTest(
44         MapTestSuiteBuilder.using(
45                 new TestClassToInstanceMapGenerator() {
46                   // Other tests will verify what real, warning-free usage looks like
47                   // but here we have to do some serious fudging
48                   @Override
49                   @SuppressWarnings("unchecked")
50                   public Map<Class, Impl> create(Object... elements) {
51                     MutableClassToInstanceMap<Impl> map = MutableClassToInstanceMap.create();
52                     for (Object object : elements) {
53                       Entry<Class, Impl> entry = (Entry<Class, Impl>) object;
54                       map.putInstance(entry.getKey(), entry.getValue());
55                     }
56                     return (Map) map;
57                   }
58                 })
59             .named("MutableClassToInstanceMap")
60             .withFeatures(
61                 MapFeature.GENERAL_PURPOSE,
62                 MapFeature.RESTRICTS_KEYS,
63                 MapFeature.ALLOWS_NULL_VALUES,
64                 CollectionSize.ANY,
65                 CollectionFeature.SERIALIZABLE,
66                 CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
67                 MapFeature.ALLOWS_ANY_NULL_QUERIES)
68             .createTestSuite());
69 
70     return suite;
71   }
72 
73   private ClassToInstanceMap<Number> map;
74 
75   @Override
setUp()76   protected void setUp() throws Exception {
77     map = MutableClassToInstanceMap.create();
78   }
79 
testConstraint()80   public void testConstraint() {
81 
82     /**
83      * We'll give ourselves a pass on testing all the possible ways of breaking the constraint,
84      * because we know that newClassMap() is implemented using ConstrainedMap which is itself
85      * well-tested. A purist would object to this, but what can I say, we're dirty cheaters.
86      */
87     map.put(Integer.class, new Integer(5));
88     assertThrows(ClassCastException.class, () -> map.put(Double.class, new Long(42)));
89     // Won't compile: map.put(String.class, "x");
90   }
91 
testPutAndGetInstance()92   public void testPutAndGetInstance() {
93     assertNull(map.putInstance(Integer.class, new Integer(5)));
94 
95     Integer oldValue = map.putInstance(Integer.class, new Integer(7));
96     assertEquals(5, (int) oldValue);
97 
98     Integer newValue = map.getInstance(Integer.class);
99     assertEquals(7, (int) newValue);
100 
101     // Won't compile: map.putInstance(Double.class, new Long(42));
102   }
103 
testNull()104   public void testNull() {
105     assertThrows(NullPointerException.class, () -> map.put(null, new Integer(1)));
106     map.putInstance(Integer.class, null);
107     assertNull(map.get(Integer.class));
108     assertNull(map.getInstance(Integer.class));
109 
110     map.put(Long.class, null);
111     assertNull(map.get(Long.class));
112     assertNull(map.getInstance(Long.class));
113   }
114 
testPrimitiveAndWrapper()115   public void testPrimitiveAndWrapper() {
116     assertNull(map.getInstance(int.class));
117     assertNull(map.getInstance(Integer.class));
118 
119     assertNull(map.putInstance(int.class, 0));
120     assertNull(map.putInstance(Integer.class, 1));
121     assertEquals(2, map.size());
122 
123     assertEquals(0, (int) map.getInstance(int.class));
124     assertEquals(1, (int) map.getInstance(Integer.class));
125 
126     assertEquals(0, (int) map.putInstance(int.class, null));
127     assertEquals(1, (int) map.putInstance(Integer.class, null));
128 
129     assertNull(map.getInstance(int.class));
130     assertNull(map.getInstance(Integer.class));
131     assertEquals(2, map.size());
132   }
133 }
134