KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > util > CollectionsTestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.util;
33
34 import java.util.*;
35 import junit.framework.TestCase;
36
37 /**
38  * @author Taras Puchko
39  */

40 public class CollectionsTestCase extends TestCase {
41
42     public void testCheckedCollection() throws Exception JavaDoc {
43         Collection strings = Collections.checkedCollection(new ArrayList(), String JavaDoc.class);
44         strings.add("abc");
45         try {
46             strings.add(Boolean.TRUE);
47             fail();
48         } catch (ClassCastException JavaDoc e) {
49             //ok
50
}
51     }
52
53     public void testCheckedSet() throws Exception JavaDoc {
54         Set strings = Collections.checkedSet(new HashSet(), String JavaDoc.class);
55         strings.add("abc");
56         try {
57             strings.add(Boolean.TRUE);
58             fail();
59         } catch (ClassCastException JavaDoc e) {
60             //ok
61
}
62     }
63
64     public void testCheckedSortedSet() throws Exception JavaDoc {
65         SortedSet strings = Collections.checkedSortedSet(new TreeSet(), String JavaDoc.class);
66         strings.add("abc");
67         try {
68             strings.add(Boolean.TRUE);
69             fail();
70         } catch (ClassCastException JavaDoc e) {
71             //ok
72
}
73     }
74
75     public void testCheckedList() throws Exception JavaDoc {
76         List strings = Collections.checkedList(new ArrayList(), String JavaDoc.class);
77         strings.add("abc");
78         try {
79             strings.add(Boolean.TRUE);
80             fail();
81         } catch (ClassCastException JavaDoc e) {
82             //ok
83
}
84     }
85
86     public void testCheckedMap() throws Exception JavaDoc {
87         Map strings = Collections.checkedMap(new HashMap(), String JavaDoc.class, Integer JavaDoc.class);
88         strings.put("abc", Integer.MIN_VALUE);
89         try {
90             strings.put(Boolean.TRUE, Integer.MIN_VALUE);
91             fail();
92         } catch (ClassCastException JavaDoc e) {
93             //ok
94
}
95         try {
96             strings.put("abc", Boolean.TRUE);
97             fail();
98         } catch (ClassCastException JavaDoc e) {
99             //ok
100
}
101         Set set = strings.entrySet();
102         Map.Entry entry1 = (Map.Entry) set.iterator().next();
103         entry1.setValue(Integer.MAX_VALUE);
104         try {
105             entry1.setValue("fail");
106             fail();
107         } catch (ClassCastException JavaDoc e) {
108             //ok
109
}
110         Map.Entry entry2 = (Map.Entry) set.toArray()[0];
111         entry2.setValue(Integer.MAX_VALUE);
112         try {
113             entry2.setValue("fail");
114             fail();
115         } catch (ClassCastException JavaDoc e) {
116             //ok
117
}
118     }
119
120     public void testCheckedSortedMap() throws Exception JavaDoc {
121         Map strings = Collections.checkedSortedMap(new TreeMap(), String JavaDoc.class, Integer JavaDoc.class);
122         strings.put("abc", Integer.MIN_VALUE);
123         try {
124             strings.put(Boolean.TRUE, Integer.MIN_VALUE);
125             fail();
126         } catch (ClassCastException JavaDoc e) {
127             //ok
128
}
129         try {
130             strings.put("abc", Boolean.TRUE);
131             fail();
132         } catch (ClassCastException JavaDoc e) {
133             //ok
134
}
135         Set set = strings.entrySet();
136         Map.Entry entry1 = (Map.Entry) set.iterator().next();
137         entry1.setValue(Integer.MAX_VALUE);
138         try {
139             entry1.setValue("fail");
140             fail();
141         } catch (ClassCastException JavaDoc e) {
142             //ok
143
}
144         Map.Entry entry2 = (Map.Entry) set.toArray()[0];
145         entry2.setValue(Integer.MAX_VALUE);
146         try {
147             entry2.setValue("fail");
148             fail();
149         } catch (ClassCastException JavaDoc e) {
150             //ok
151
}
152     }
153
154     public void testEmptySet() throws Exception JavaDoc {
155         assertSame(Collections.EMPTY_SET, Collections.emptySet());
156     }
157
158     public void testEmptyList() throws Exception JavaDoc {
159         assertSame(Collections.EMPTY_LIST, Collections.emptyList());
160     }
161
162     public void testEmptyMap() throws Exception JavaDoc {
163         assertSame(Collections.EMPTY_MAP, Collections.emptyMap());
164     }
165
166     public void testReverseOrder() throws Exception JavaDoc {
167         String JavaDoc[] strings = {"a", "b"};
168         Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER);
169         assertEquals("a", strings[0]);
170         assertEquals("b", strings[1]);
171         Arrays.sort(strings, Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER));
172         assertEquals("b", strings[0]);
173         assertEquals("a", strings[1]);
174     }
175
176     public void testFrequency() throws Exception JavaDoc {
177         assertEquals(2, Collections.frequency(Arrays.asList("a", "b", "c", "b", "a"), "b"));
178     }
179
180     public void testDisjoint() throws Exception JavaDoc {
181         assertTrue(Collections.disjoint(Arrays.asList("a", "b", "c"), Arrays.asList("x", "y", "z")));
182         assertFalse(Collections.disjoint(Arrays.asList("a", "b", "c"), Arrays.asList("x", "c", "z")));
183     }
184
185     public void testAddAll() throws Exception JavaDoc {
186         List<String JavaDoc> list = new ArrayList<String JavaDoc>(Arrays.asList("a", "b", "c"));
187         Collections.addAll(list, "x", "y");
188         assertEquals(5, list.size());
189         assertEquals("x", list.get(3));
190     }
191
192     public void testExisting_singletonList() throws Exception JavaDoc {
193         List<String JavaDoc> list = Collections.singletonList("a");
194         assertEquals(1, list.size());
195         assertEquals("a", list.get(0));
196     }
197
198     public void testExisting_synchronizedMap() throws Exception JavaDoc {
199         Map map = new HashMap();
200         Map syncMap = Collections.synchronizedMap(map);
201         syncMap.put("a", "b");
202         assertEquals(1, map.size());
203         assertEquals("b", map.get("a"));
204     }
205
206 }
207
Popular Tags