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.util.*;
034: import junit.framework.TestCase;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class CollectionsTestCase extends TestCase {
040:
041: public void testCheckedCollection() throws Exception {
042: Collection strings = Collections.checkedCollection(
043: new ArrayList(), String.class);
044: strings.add("abc");
045: try {
046: strings.add(Boolean.TRUE);
047: fail();
048: } catch (ClassCastException e) {
049: //ok
050: }
051: }
052:
053: public void testCheckedSet() throws Exception {
054: Set strings = Collections.checkedSet(new HashSet(),
055: String.class);
056: strings.add("abc");
057: try {
058: strings.add(Boolean.TRUE);
059: fail();
060: } catch (ClassCastException e) {
061: //ok
062: }
063: }
064:
065: public void testCheckedSortedSet() throws Exception {
066: SortedSet strings = Collections.checkedSortedSet(new TreeSet(),
067: String.class);
068: strings.add("abc");
069: try {
070: strings.add(Boolean.TRUE);
071: fail();
072: } catch (ClassCastException e) {
073: //ok
074: }
075: }
076:
077: public void testCheckedList() throws Exception {
078: List strings = Collections.checkedList(new ArrayList(),
079: String.class);
080: strings.add("abc");
081: try {
082: strings.add(Boolean.TRUE);
083: fail();
084: } catch (ClassCastException e) {
085: //ok
086: }
087: }
088:
089: public void testCheckedMap() throws Exception {
090: Map strings = Collections.checkedMap(new HashMap(),
091: String.class, Integer.class);
092: strings.put("abc", Integer.MIN_VALUE);
093: try {
094: strings.put(Boolean.TRUE, Integer.MIN_VALUE);
095: fail();
096: } catch (ClassCastException e) {
097: //ok
098: }
099: try {
100: strings.put("abc", Boolean.TRUE);
101: fail();
102: } catch (ClassCastException e) {
103: //ok
104: }
105: Set set = strings.entrySet();
106: Map.Entry entry1 = (Map.Entry) set.iterator().next();
107: entry1.setValue(Integer.MAX_VALUE);
108: try {
109: entry1.setValue("fail");
110: fail();
111: } catch (ClassCastException e) {
112: //ok
113: }
114: Map.Entry entry2 = (Map.Entry) set.toArray()[0];
115: entry2.setValue(Integer.MAX_VALUE);
116: try {
117: entry2.setValue("fail");
118: fail();
119: } catch (ClassCastException e) {
120: //ok
121: }
122: }
123:
124: public void testCheckedSortedMap() throws Exception {
125: Map strings = Collections.checkedSortedMap(new TreeMap(),
126: String.class, Integer.class);
127: strings.put("abc", Integer.MIN_VALUE);
128: try {
129: strings.put(Boolean.TRUE, Integer.MIN_VALUE);
130: fail();
131: } catch (ClassCastException e) {
132: //ok
133: }
134: try {
135: strings.put("abc", Boolean.TRUE);
136: fail();
137: } catch (ClassCastException e) {
138: //ok
139: }
140: Set set = strings.entrySet();
141: Map.Entry entry1 = (Map.Entry) set.iterator().next();
142: entry1.setValue(Integer.MAX_VALUE);
143: try {
144: entry1.setValue("fail");
145: fail();
146: } catch (ClassCastException e) {
147: //ok
148: }
149: Map.Entry entry2 = (Map.Entry) set.toArray()[0];
150: entry2.setValue(Integer.MAX_VALUE);
151: try {
152: entry2.setValue("fail");
153: fail();
154: } catch (ClassCastException e) {
155: //ok
156: }
157: }
158:
159: public void testEmptySet() throws Exception {
160: assertSame(Collections.EMPTY_SET, Collections.emptySet());
161: }
162:
163: public void testEmptyList() throws Exception {
164: assertSame(Collections.EMPTY_LIST, Collections.emptyList());
165: }
166:
167: public void testEmptyMap() throws Exception {
168: assertSame(Collections.EMPTY_MAP, Collections.emptyMap());
169: }
170:
171: public void testReverseOrder() throws Exception {
172: String[] strings = { "a", "b" };
173: Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER);
174: assertEquals("a", strings[0]);
175: assertEquals("b", strings[1]);
176: Arrays.sort(strings, Collections
177: .reverseOrder(String.CASE_INSENSITIVE_ORDER));
178: assertEquals("b", strings[0]);
179: assertEquals("a", strings[1]);
180: }
181:
182: public void testFrequency() throws Exception {
183: assertEquals(2, Collections.frequency(Arrays.asList("a", "b",
184: "c", "b", "a"), "b"));
185: }
186:
187: public void testDisjoint() throws Exception {
188: assertTrue(Collections.disjoint(Arrays.asList("a", "b", "c"),
189: Arrays.asList("x", "y", "z")));
190: assertFalse(Collections.disjoint(Arrays.asList("a", "b", "c"),
191: Arrays.asList("x", "c", "z")));
192: }
193:
194: public void testAddAll() throws Exception {
195: List<String> list = new ArrayList<String>(Arrays.asList("a",
196: "b", "c"));
197: Collections.addAll(list, "x", "y");
198: assertEquals(5, list.size());
199: assertEquals("x", list.get(3));
200: }
201:
202: public void testExisting_singletonList() throws Exception {
203: List<String> list = Collections.singletonList("a");
204: assertEquals(1, list.size());
205: assertEquals("a", list.get(0));
206: }
207:
208: public void testExisting_synchronizedMap() throws Exception {
209: Map map = new HashMap();
210: Map syncMap = Collections.synchronizedMap(map);
211: syncMap.put("a", "b");
212: assertEquals(1, map.size());
213: assertEquals("b", map.get("a"));
214: }
215:
216: public void testList() throws Exception {
217: assertEquals(Arrays.asList("a", "b", "c"), Collections
218: .list(new StringTokenizer("a b c")));
219: }
220:
221: public void testReplaceAll() throws Exception {
222: List<String> list = Arrays.asList("a", "b", "c", "b", "c");
223: assertTrue(Collections.replaceAll(list, "c", "x"));
224: assertEquals(Arrays.asList("a", "b", "x", "b", "x"), list);
225: }
226:
227: public void testSwap() throws Exception {
228: List<String> list = Arrays.asList("a", "b", "c", "d", "e");
229: Collections.swap(list, 2, 4);
230: assertEquals(Arrays.asList("a", "b", "e", "d", "c"), list);
231: }
232:
233: }
|