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