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