001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.util;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: import java.util.Collection;
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.EnumMap;
027:
028: /**
029: * Tests EnumMap.
030: * TODO(tobyr) Consider using Apache collections tests.
031: */
032: public class EnumMapTest extends GWTTestCase {
033:
034: enum Number {
035:
036: Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen, Twenty, TwentyOne, TwentyTwo, TwentyThree, TwentyFour, TwentyFive, TwentySix, TwentySeven, TwentyEight, TwentyNine, Thirty,
037: }
038:
039: private static <E extends Enum<E>> void enumTests(Class<E> e) {
040: E[] enums = e.getEnumConstants();
041:
042: EnumMap<E, Integer> numbers = new EnumMap<E, Integer>(e);
043: HashMap<E, Integer> numberMap = new HashMap<E, Integer>();
044: assertEquals(numberMap, numbers);
045:
046: numbers.put(enums[1], 1);
047: numberMap.put(enums[1], 1);
048: numbers.put(enums[2], 2);
049: numberMap.put(enums[2], 2);
050: assertEquals(numberMap, numbers);
051:
052: numbers.put(enums[23], 23);
053: numberMap.put(enums[23], 23);
054: assertEquals(numberMap, numbers);
055:
056: numbers.remove(enums[1]);
057: numberMap.remove(enums[1]);
058: assertEquals(numberMap, numbers);
059:
060: // Attempt an add at the beginning
061: numbers.put(enums[0], 0);
062: numberMap.put(enums[0], 0);
063: assertEquals(numberMap, numbers);
064:
065: // Attempt an add at the end
066: numbers.put(enums[enums.length - 1], enums.length - 1);
067: numberMap.put(enums[enums.length - 1], enums.length - 1);
068: assertEquals(numberMap, numbers);
069:
070: // Attempt to remove something bogus
071: numbers.remove(enums[15]);
072: numberMap.remove(enums[15]);
073: assertEquals(numberMap, numbers);
074:
075: // Attempt to add a duplicate value
076: numbers.put(enums[23], 23);
077: numberMap.put(enums[23], 23);
078: assertEquals(numberMap, numbers);
079:
080: numbers.clear();
081: numberMap.clear();
082: for (E val : enums) {
083: numbers.put(val, val.ordinal());
084: numberMap.put(val, val.ordinal());
085: }
086: assertEquals(numberMap, numbers);
087:
088: assertEquals(numberMap, numbers.clone());
089: assertEquals(numberMap, new EnumMap<E, Integer>(numberMap));
090:
091: // Test entrySet, keySet, and values
092: // Make sure that modifications through these views works correctly
093: Set<Map.Entry<E, Integer>> numbersEntrySet = numbers.entrySet();
094: Set<Map.Entry<E, Integer>> mapEntrySet = numberMap.entrySet();
095: assertEquals(mapEntrySet, numbersEntrySet);
096:
097: final Map.Entry<E, Integer> entry = numbers.entrySet()
098: .iterator().next();
099: /*
100: * Copy entry because it is no longer valid after
101: * numbersEntrySet.remove(entry).
102: */
103: Map.Entry<E, Integer> entryCopy = new Map.Entry<E, Integer>() {
104: E key = entry.getKey();
105: Integer value = entry.getValue();
106:
107: public E getKey() {
108: return key;
109: }
110:
111: public Integer getValue() {
112: return value;
113: }
114:
115: public Integer setValue(Integer value) {
116: Integer oldValue = this .value;
117: this .value = value;
118: return oldValue;
119: }
120: };
121: numbersEntrySet.remove(entry);
122: mapEntrySet.remove(entryCopy);
123: assertEquals(mapEntrySet, numbersEntrySet);
124: assertEquals(numberMap, numbers);
125:
126: Set<E> numbersKeySet = numbers.keySet();
127: Set<E> mapKeySet = numberMap.keySet();
128: assertEquals(mapKeySet, numbersKeySet);
129: numbersKeySet.remove(enums[2]);
130: mapKeySet.remove(enums[2]);
131: assertEquals(mapKeySet, numbersKeySet);
132: assertEquals(numberMap, numbers);
133:
134: Collection<Integer> numbersValues = numbers.values();
135: Collection<Integer> mapValues = numberMap.values();
136: assertEquals(sort(mapValues), sort(numbersValues));
137: numbersValues.remove(23);
138: mapValues.remove(23);
139: assertEquals(sort(mapValues), sort(numbersValues));
140: assertEquals(numberMap, numbers);
141: }
142:
143: private static <T extends Comparable<T>> Collection<T> sort(
144: Collection<T> col) {
145: ArrayList<T> list = new ArrayList<T>(col);
146: Collections.sort(list);
147: return list;
148: }
149:
150: public String getModuleName() {
151: return "com.google.gwt.emultest.EmulSuite";
152: }
153:
154: public void testBasics() {
155: enumTests(Number.class);
156: }
157:
158: public void testNulls() {
159: EnumMap<Number, Integer> numbers = new EnumMap<Number, Integer>(
160: Number.class);
161:
162: assertFalse("Should not contain null value", numbers
163: .containsValue(null));
164: assertFalse("Should not contain null key", numbers
165: .containsKey(null));
166:
167: numbers.put(Number.Two, null);
168: assertTrue("Should contain a null value", numbers
169: .containsValue(null));
170:
171: try {
172: numbers.put(null, 3);
173: fail("Should not be able to insert a null key.");
174: } catch (NullPointerException ex) {
175: }
176: }
177:
178: public void testOrdering() {
179: EnumMap<Number, Integer> numbers = new EnumMap<Number, Integer>(
180: Number.class);
181: Number[] enums = Number.values();
182:
183: for (int i = enums.length - 1; i >= 0; --i) {
184: numbers.put(enums[i], i);
185: }
186:
187: int lastOrdinal = -1;
188: for (Map.Entry<Number, Integer> val : numbers.entrySet()) {
189: int newOrdinal = val.getKey().ordinal();
190: assertTrue("EnumMap must maintain Enums in order",
191: lastOrdinal < newOrdinal);
192: lastOrdinal = newOrdinal;
193: }
194: }
195: }
|