001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.util;
032:
033: import java.lang.annotation.ElementType;
034: import java.util.*;
035: import junit.framework.TestCase;
036:
037: /**
038: * @author Taras Puchko
039: */
040: public class EnumMap_TestCase extends TestCase {
041:
042: private static enum Day {
043: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIEDAY, SATURDAY
044: }
045:
046: public void testClear() {
047: EnumMap<Day, String> map = getSampleMap();
048: assertFalse(map.isEmpty());
049: assertEquals(3, map.size());
050: map.clear();
051: assertTrue(map.isEmpty());
052: assertEquals(0, map.size());
053: }
054:
055: public void testClone() {
056: EnumMap<Day, String> clone = getSampleMap().clone();
057: assertTrue(clone.equals(getSampleMap()));
058: }
059:
060: public void testConstructors() {
061: EnumMap<Day, String> map = getSampleMap();
062: assertEquals(3, new EnumMap<Day, String>(map).size());
063: assertEquals(3,
064: new EnumMap<Day, String>((Map<Day, String>) map).size());
065: assertEquals(3, new EnumMap<Day, String>(
066: new HashMap<Day, String>(map)).size());
067: try {
068: new EnumMap(String.class);
069: fail();
070: } catch (NullPointerException e) {
071: //ok
072: }
073: try {
074: new EnumMap<Day, String>(new HashMap<Day, String>());
075: fail();
076: } catch (IllegalArgumentException e) {
077: //ok
078: }
079: try {
080: HashMap m = new HashMap();
081: m.put("a", "b");
082: new EnumMap(m);
083: fail();
084: } catch (ClassCastException e) {
085: //ok
086: }
087: }
088:
089: public void testEntrySet() {
090: EnumMap<Day, String> map = getSampleMap();
091: Set<Map.Entry<Day, String>> entries = map.entrySet();
092: assertEquals(3, entries.size());
093: assertFalse(entries.contains("a"));
094: assertTrue(entries.contains(new MapEntry(Day.SUNDAY, "Sunday")));
095: assertFalse(entries.contains(new MapEntry(Day.WEDNESDAY,
096: "Frieday")));
097: Iterator<Map.Entry<Day, String>> iterator = entries.iterator();
098: Map.Entry<Day, String> first = iterator.next();
099: assertEquals(Day.SUNDAY, first.getKey());
100: assertEquals("Sunday", first.getValue());
101: Map.Entry<Day, String> second = iterator.next();
102: assertEquals(Day.WEDNESDAY, second.getKey());
103: assertEquals("Wednesday", second.getValue());
104: iterator.remove();
105: Map.Entry<Day, String> third = iterator.next();
106: assertEquals(Day.FRIEDAY, third.getKey());
107: assertEquals("Frieday", third.getValue());
108: assertFalse(iterator.hasNext());
109: assertFalse(entries.remove("a"));
110: assertFalse(entries.remove(new MapEntry(Day.FRIEDAY, "Sunday")));
111: assertTrue(entries.remove(new MapEntry(Day.SUNDAY, "Sunday")));
112: assertEquals(1, entries.size());
113: assertEquals(1, map.size());
114: }
115:
116: public void testEquals() {
117: assertTrue(getSampleMap().equals(getSampleMap()));
118: assertTrue(getSampleMap().equals(new HashMap(getSampleMap())));
119: }
120:
121: public void testGet() {
122: assertEquals("Frieday", getSampleMap().get(Day.FRIEDAY));
123: assertEquals(null, getSampleMap().get("s"));
124: assertEquals(null, getSampleMap().get(null));
125: }
126:
127: public void testKeySet() {
128: EnumMap<Day, String> map = getSampleMap();
129: Set<Day> days = map.keySet();
130: assertFalse(days.isEmpty());
131: assertEquals(3, days.size());
132: assertTrue(days.contains(Day.SUNDAY));
133: assertFalse(days.contains(Day.MONDAY));
134: assertFalse(days.contains(Day.TUESDAY));
135: assertTrue(days.contains(Day.WEDNESDAY));
136: assertFalse(days.contains(Day.THURSDAY));
137: assertTrue(days.contains(Day.FRIEDAY));
138: assertFalse(days.contains(Day.SATURDAY));
139: Iterator<Day> iterator = days.iterator();
140: assertEquals(Day.SUNDAY, iterator.next());
141: assertEquals(Day.WEDNESDAY, iterator.next());
142: iterator.remove();
143: assertEquals(Day.FRIEDAY, iterator.next());
144: assertFalse(iterator.hasNext());
145: assertTrue(days.remove(Day.FRIEDAY));
146: assertEquals(1, days.size());
147: assertEquals(1, map.size());
148: }
149:
150: public void testPut() {
151: assertEquals("Sunday", getSampleMap().put(Day.SUNDAY, "v"));
152: try {
153: ((Map) getSampleMap()).put("k", "v");
154: fail();
155: } catch (ClassCastException e) {
156: //ok
157: }
158: try {
159: getSampleMap().put(null, "null");
160: fail();
161: } catch (NullPointerException e) {
162: //ok
163: }
164: }
165:
166: public void testPutAll() {
167: EnumMap<Day, String> map = new EnumMap<Day, String>(Day.class);
168: map.putAll(getSampleMap());
169: assertEquals(3, map.size());
170: map.clear();
171: map.putAll(new HashMap<Day, String>(getSampleMap()));
172: assertEquals(3, map.size());
173: EnumMap strings = new EnumMap(ElementType.class);
174: map.putAll(strings);
175: strings.put(ElementType.ANNOTATION_TYPE, "a");
176: try {
177: map.putAll(strings);
178: fail();
179: } catch (ClassCastException e) {
180: //ok
181: }
182: }
183:
184: public void testRemove() {
185: assertEquals("Wednesday", getSampleMap().remove(Day.WEDNESDAY));
186: assertNull(getSampleMap().remove(Day.MONDAY));
187: assertNull(getSampleMap().remove(null));
188: assertNull(getSampleMap().remove("k"));
189: }
190:
191: public void testValues() {
192: EnumMap<Day, String> map = getSampleMap();
193: Collection<String> values = map.values();
194: assertEquals(3, values.size());
195: assertTrue(values.contains("Sunday"));
196: assertFalse(values.contains("Saturday"));
197: Iterator<String> iterator = values.iterator();
198: assertEquals("Sunday", iterator.next());
199: assertEquals("Wednesday", iterator.next());
200: iterator.remove();
201: assertEquals("Frieday", iterator.next());
202: assertFalse(iterator.hasNext());
203: values.remove("Sunday");
204: assertEquals(1, values.size());
205: assertEquals(1, map.size());
206: }
207:
208: private EnumMap<Day, String> getSampleMap() {
209: EnumMap<Day, String> map = new EnumMap<Day, String>(Day.class);
210: assertNull(map.put(Day.SUNDAY, "Sunday"));
211: assertNull(map.put(Day.FRIEDAY, "Frieday"));
212: assertNull(map.put(Day.WEDNESDAY, "Wednesday"));
213: return map;
214: }
215:
216: private static class MapEntry implements Map.Entry {
217:
218: private Object key;
219: private Object value;
220:
221: public MapEntry(Object key, Object value) {
222: this .key = key;
223: this .value = value;
224: }
225:
226: public Object getKey() {
227: return key;
228: }
229:
230: public Object getValue() {
231: return value;
232: }
233:
234: public Object setValue(Object value) {
235: Object previous = this.value;
236: this.value = value;
237: return previous;
238: }
239: }
240: }
|