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