1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
33
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
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
95
96
97
98
99
100
101
102
103
104
105 }