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