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.assertInstanceOf;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.util.HashMap;
24
25 import org.apache.commons.collections4.BoundedMap;
26 import org.apache.commons.collections4.KeyValue;
27 import org.apache.commons.collections4.OrderedMap;
28 import org.junit.jupiter.api.Test;
29
30 /**
31 * JUnit tests.
32 *
33 * @param <K> the key type.
34 * @param <V> the value type.
35 */
36 public class SingletonMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
37
38 private static final Integer ONE = Integer.valueOf(1);
39 private static final Integer TWO = Integer.valueOf(2);
40 private static final String TEN = "10";
41
42 @Override
43 public String getCompatibilityVersion() {
44 return "4";
45 }
46
47 @Override
48 @SuppressWarnings("unchecked")
49 public V[] getNewSampleValues() {
50 return (V[]) new Object[] { TEN };
51 }
52
53 @Override
54 @SuppressWarnings("unchecked")
55 public K[] getSampleKeys() {
56 return (K[]) new Object[] { ONE };
57 }
58
59 @Override
60 @SuppressWarnings("unchecked")
61 public V[] getSampleValues() {
62 return (V[]) new Object[] { TWO };
63 }
64
65 @Override
66 public boolean isPutAddSupported() {
67 return false;
68 }
69
70 @Override
71 public boolean isRemoveSupported() {
72 return false;
73 }
74
75 @Override
76 @SuppressWarnings("unchecked")
77 public SingletonMap<K, V> makeFullMap() {
78 return new SingletonMap<>((K) ONE, (V) TWO);
79 }
80
81 @Override
82 public OrderedMap<K, V> makeObject() {
83 // need an empty singleton map, but that's not possible
84 // use a ridiculous fake instead to make the tests pass
85 return UnmodifiableOrderedMap.unmodifiableOrderedMap(ListOrderedMap.listOrderedMap(new HashMap<>()));
86 }
87
88 @Test
89 public void testBoundedMap() {
90 final SingletonMap<K, V> map = makeFullMap();
91 assertEquals(1, map.size());
92 assertTrue(map.isFull());
93 assertEquals(1, map.maxSize());
94 assertInstanceOf(BoundedMap.class, map);
95 }
96
97 @Test
98 public void testClone() {
99 final SingletonMap<K, V> map = makeFullMap();
100 assertEquals(1, map.size());
101 final SingletonMap<K, V> cloned = map.clone();
102 assertEquals(1, cloned.size());
103 assertTrue(cloned.containsKey(ONE));
104 assertTrue(cloned.containsValue(TWO));
105 }
106
107 // public BulkTest bulkTestMapIterator() {
108 // return new TestFlatMapIterator();
109 // }
110 //
111 // public class TestFlatMapIterator extends AbstractTestOrderedMapIterator {
112 // public TestFlatMapIterator() {
113 // super("TestFlatMapIterator");
114 // }
115 //
116 // public Object[] addSetValues() {
117 // return TestSingletonMap.this.getNewSampleValues();
118 // }
119 //
120 // public boolean supportsRemove() {
121 // return TestSingletonMap.this.isRemoveSupported();
122 // }
123 //
124 // public boolean supportsSetValue() {
125 // return TestSingletonMap.this.isSetValueSupported();
126 // }
127 //
128 // public MapIterator makeEmptyMapIterator() {
129 // resetEmpty();
130 // return ((Flat3Map) TestSingletonMap.this.map).mapIterator();
131 // }
132 //
133 // public MapIterator makeFullMapIterator() {
134 // resetFull();
135 // return ((Flat3Map) TestSingletonMap.this.map).mapIterator();
136 // }
137 //
138 // public Map getMap() {
139 // // assumes makeFullMapIterator() called first
140 // return TestSingletonMap.this.map;
141 // }
142 //
143 // public Map getConfirmedMap() {
144 // // assumes makeFullMapIterator() called first
145 // return TestSingletonMap.this.confirmed;
146 // }
147 //
148 // public void verify() {
149 // super.verify();
150 // TestSingletonMap.this.verify();
151 // }
152 // }
153
154 @Test
155 public void testKeyValue() {
156 final SingletonMap<K, V> map = makeFullMap();
157 assertEquals(1, map.size());
158 assertEquals(ONE, map.getKey());
159 assertEquals(TWO, map.getValue());
160 assertInstanceOf(KeyValue.class, map);
161 }
162
163 // public void testCreate() throws Exception {
164 // resetEmpty();
165 // writeExternalFormToDisk(
166 // (java.io.Serializable) map,
167 // "src/test/resources/data/test/SingletonMap.emptyCollection.version4.obj");
168 // resetFull();
169 // writeExternalFormToDisk(
170 // (java.io.Serializable) map,
171 // "src/test/resources/data/test/SingletonMap.fullCollection.version4.obj");
172 // }
173
174 }