View Javadoc
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   */
17  package org.apache.commons.collections4.map;
18  
19  import static org.apache.commons.collections4.map.LazyMap.lazyMap;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.collections4.Factory;
25  import org.apache.commons.collections4.FactoryUtils;
26  import org.apache.commons.collections4.Transformer;
27  import org.junit.Test;
28  
29  /**
30   * Extension of {@link AbstractMapTest} for exercising the
31   * {@link LazyMap} implementation.
32   *
33   * @since 3.0
34   */
35  @SuppressWarnings("boxing")
36  public class LazyMapTest<K, V> extends AbstractIterableMapTest<K, V> {
37  
38      private static final Factory<Integer> oneFactory = FactoryUtils.constantFactory(1);
39  
40      public LazyMapTest(final String testName) {
41          super(testName);
42      }
43  
44      @Override
45      public LazyMap<K,V> makeObject() {
46          return lazyMap(new HashMap<K,V>(), FactoryUtils.<V>nullFactory());
47      }
48  
49      //-----------------------------------------------------------------------
50      @Override
51      public void testMapGet() {
52          //TODO eliminate need for this via superclass - see svn history.
53      }
54  
55      @Test
56      public void mapGetWithFactory() {
57          Map<Integer, Number> map = lazyMap(new HashMap<Integer,Number>(), oneFactory);
58          assertEquals(0, map.size());
59          final Number i1 = map.get("Five");
60          assertEquals(1, i1);
61          assertEquals(1, map.size());
62          final Number i2 = map.get(new String(new char[] {'F','i','v','e'}));
63          assertEquals(1, i2);
64          assertEquals(1, map.size());
65          assertSame(i1, i2);
66  
67          map = lazyMap(new HashMap<Integer,Number>(), FactoryUtils.<Long>nullFactory());
68          final Object o = map.get("Five");
69          assertEquals(null,o);
70          assertEquals(1, map.size());
71      }
72  
73      @Test
74      public void mapGetWithTransformer() {
75          final Transformer<Number, Integer> intConverter = new Transformer<Number, Integer>(){
76              @Override
77              public Integer transform(final Number input) {
78                  return input.intValue();
79              }
80          };
81          final Map<Long, Number> map = lazyMap(new HashMap<Long,Number>(), intConverter );
82          assertEquals(0, map.size());
83          final Number i1 = map.get(123L);
84          assertEquals(123, i1);
85          assertEquals(1, map.size());
86      }
87  
88  
89      @Override
90      public String getCompatibilityVersion() {
91          return "4";
92      }
93  
94  //    public void testCreate() throws Exception {
95  //        resetEmpty();
96  //        writeExternalFormToDisk(
97  //            (java.io.Serializable) map,
98  //            "src/test/resources/data/test/LazyMap.emptyCollection.version4.obj");
99  //        resetFull();
100 //        writeExternalFormToDisk(
101 //            (java.io.Serializable) map,
102 //            "src/test/resources/data/test/LazyMap.fullCollection.version4.obj");
103 //    }
104 
105 }