001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.ListIterator;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * @author gregw
026: *
027: */
028: public class LazyListTest extends TestCase {
029:
030: /**
031: * Constructor for LazyListTest.
032: * @param arg0
033: */
034: public LazyListTest(String arg0) {
035: super (arg0);
036: }
037:
038: /*
039: * Test for Object add(Object, Object)
040: */
041: public void testAddObjectObject() {
042: Object list = null;
043: assertEquals(0, LazyList.size(list));
044:
045: list = LazyList.add(list, "a");
046: assertEquals(1, LazyList.size(list));
047: assertEquals("a", LazyList.get(list, 0));
048:
049: list = LazyList.add(list, "b");
050: assertEquals(2, LazyList.size(list));
051: assertEquals("a", LazyList.get(list, 0));
052: assertEquals("b", LazyList.get(list, 1));
053:
054: list = null;
055: list = LazyList.add(list, null);
056: assertEquals(1, LazyList.size(list));
057: assertEquals(null, LazyList.get(list, 0));
058:
059: list = "a";
060: list = LazyList.add(list, null);
061: assertEquals(2, LazyList.size(list));
062: assertEquals("a", LazyList.get(list, 0));
063: assertEquals(null, LazyList.get(list, 1));
064:
065: list = LazyList.add(list, null);
066: assertEquals(3, LazyList.size(list));
067: assertEquals("a", LazyList.get(list, 0));
068: assertEquals(null, LazyList.get(list, 1));
069: assertEquals(null, LazyList.get(list, 2));
070:
071: list = LazyList.add(null, list);
072: assertEquals(1, LazyList.size(list));
073: assertTrue(LazyList.get(list, 0) instanceof List);
074: }
075:
076: /*
077: * Test for Object add(Object, int, Object)
078: */
079: public void testAddObjectintObject() {
080: Object list = null;
081: list = LazyList.add(list, 0, "c");
082: list = LazyList.add(list, 0, "a");
083: list = LazyList.add(list, 1, "b");
084: list = LazyList.add(list, 3, "d");
085:
086: assertEquals(4, LazyList.size(list));
087: assertEquals("a", LazyList.get(list, 0));
088: assertEquals("b", LazyList.get(list, 1));
089: assertEquals("c", LazyList.get(list, 2));
090: assertEquals("d", LazyList.get(list, 3));
091:
092: list = LazyList.add(null, 0, null);
093: assertTrue(list instanceof List);
094:
095: list = LazyList.add(null, 0, new ArrayList());
096: assertTrue(list instanceof List);
097: }
098:
099: public void testAddCollection() {
100: ArrayList l = new ArrayList();
101: l.add("a");
102: l.add("b");
103:
104: Object list = null;
105: list = LazyList.addCollection(list, l);
106: list = LazyList.addCollection(list, l);
107:
108: assertEquals(4, LazyList.size(list));
109: assertEquals("a", LazyList.get(list, 0));
110: assertEquals("b", LazyList.get(list, 1));
111: assertEquals("a", LazyList.get(list, 2));
112: assertEquals("b", LazyList.get(list, 3));
113: }
114:
115: public void testEnsureSize() {
116: assertTrue(LazyList.ensureSize(null, 10) != null);
117:
118: assertTrue(LazyList.ensureSize("a", 10) instanceof ArrayList);
119:
120: ArrayList l = new ArrayList();
121: l.add("a");
122: l.add("b");
123: assertTrue(LazyList.ensureSize(l, 10) != l);
124: assertTrue(LazyList.ensureSize(l, 1) == l);
125: }
126:
127: /*
128: * Test for Object remove(Object, Object)
129: */
130: public void testRemoveObjectObject() {
131: Object list = null;
132:
133: assertTrue(LazyList.remove(null, "a") == null);
134:
135: list = LazyList.add(list, "a");
136: assertEquals("a", LazyList.remove(list, "z"));
137: assertTrue(LazyList.remove(list, "a") == null);
138:
139: list = LazyList.add(list, "b");
140: list = LazyList.remove(list, "b");
141: list = LazyList.add(list, "b");
142: list = LazyList.add(list, "c");
143: list = LazyList.add(list, "d");
144: list = LazyList.add(list, "e");
145: list = LazyList.remove(list, "a");
146: list = LazyList.remove(list, "d");
147: list = LazyList.remove(list, "e");
148:
149: assertEquals(2, LazyList.size(list));
150: assertEquals("b", LazyList.get(list, 0));
151: assertEquals("c", LazyList.get(list, 1));
152:
153: list = LazyList.remove(list, "b");
154: list = LazyList.remove(list, "c");
155: assertEquals(null, list);
156: }
157:
158: /*
159: * Test for Object remove(Object, int)
160: */
161: public void testRemoveObjectint() {
162: Object list = null;
163: assertTrue(LazyList.remove(list, 0) == null);
164:
165: list = LazyList.add(list, "a");
166: assertEquals("a", LazyList.remove(list, 1));
167: assertTrue(LazyList.remove(list, 0) == null);
168:
169: list = LazyList.add(list, "b");
170: list = LazyList.remove(list, 1);
171: list = LazyList.add(list, "b");
172: list = LazyList.add(list, "c");
173: list = LazyList.add(list, "d");
174: list = LazyList.add(list, "e");
175: list = LazyList.remove(list, 0);
176: list = LazyList.remove(list, 2);
177: list = LazyList.remove(list, 2);
178:
179: assertEquals(2, LazyList.size(list));
180: assertEquals("b", LazyList.get(list, 0));
181: assertEquals("c", LazyList.get(list, 1));
182:
183: list = LazyList.remove(list, 0);
184: list = LazyList.remove(list, 0);
185: assertEquals(null, list);
186: }
187:
188: /*
189: * Test for List getList(Object)
190: */
191: public void testGetListObject() {
192: assertEquals(0, LazyList.getList(null).size());
193: assertEquals(1, LazyList.getList("a").size());
194:
195: ArrayList l = new ArrayList();
196: l.add("a");
197: l.add("b");
198: assertEquals(2, LazyList.getList(l).size());
199: }
200:
201: /*
202: * Test for List getList(Object, boolean)
203: */
204: public void testGetListObjectboolean() {
205: assertEquals(0, LazyList.getList(null, false).size());
206: assertEquals(null, LazyList.getList(null, true));
207: }
208:
209: public void testToStringArray() {
210: assertEquals(0, LazyList.toStringArray(null).length);
211:
212: assertEquals(1, LazyList.toStringArray("a").length);
213: assertEquals("a", LazyList.toStringArray("a")[0]);
214:
215: ArrayList l = new ArrayList();
216: l.add("a");
217: l.add(null);
218: l.add(new Integer(2));
219: String[] a = LazyList.toStringArray(l);
220:
221: assertEquals(3, a.length);
222: assertEquals("a", a[0]);
223: assertEquals(null, a[1]);
224: assertEquals("2", a[2]);
225:
226: }
227:
228: public void testSize() {
229: ArrayList l = new ArrayList();
230: l.add("a");
231: l.add("b");
232:
233: assertEquals(0, LazyList.size(null));
234: assertEquals(0, LazyList.size(new ArrayList()));
235: assertEquals(1, LazyList.size("a"));
236: assertEquals(2, LazyList.size(l));
237: }
238:
239: public void testGet() {
240: testAddObjectObject();
241:
242: assertEquals("a", LazyList.get("a", 0));
243:
244: try {
245: LazyList.get(null, 0);
246: assertTrue(false);
247: } catch (IndexOutOfBoundsException e) {
248: assertTrue(true);
249: }
250:
251: try {
252: LazyList.get("a", 1);
253: assertTrue(false);
254: } catch (IndexOutOfBoundsException e) {
255: assertTrue(true);
256: }
257: }
258:
259: public void testContains() {
260: ArrayList l = new ArrayList();
261: l.add("a");
262: l.add("b");
263:
264: assertFalse(LazyList.contains(null, "z"));
265: assertFalse(LazyList.contains("a", "z"));
266: assertFalse(LazyList.contains(l, "z"));
267:
268: assertTrue(LazyList.contains("a", "a"));
269: assertTrue(LazyList.contains(l, "b"));
270:
271: }
272:
273: public void testIterator() {
274: ArrayList l = new ArrayList();
275: l.add("a");
276: l.add("b");
277:
278: assertFalse(LazyList.iterator(null).hasNext());
279:
280: Iterator i = LazyList.iterator("a");
281: assertTrue(i.hasNext());
282: assertEquals("a", i.next());
283: assertFalse(i.hasNext());
284:
285: i = LazyList.iterator(l);
286: assertTrue(i.hasNext());
287: assertEquals("a", i.next());
288: assertTrue(i.hasNext());
289: assertEquals("b", i.next());
290: assertFalse(i.hasNext());
291: }
292:
293: public void testListIterator() {
294: ArrayList l = new ArrayList();
295: l.add("a");
296: l.add("b");
297:
298: assertFalse(LazyList.listIterator(null).hasNext());
299:
300: ListIterator i = LazyList.listIterator("a");
301: assertTrue(i.hasNext());
302: assertFalse(i.hasPrevious());
303: assertEquals("a", i.next());
304: assertFalse(i.hasNext());
305: assertTrue(i.hasPrevious());
306: assertEquals("a", i.previous());
307:
308: i = LazyList.listIterator(l);
309: assertTrue(i.hasNext());
310: assertFalse(i.hasPrevious());
311: assertEquals("a", i.next());
312: assertTrue(i.hasNext());
313: assertTrue(i.hasPrevious());
314: assertEquals("b", i.next());
315: assertFalse(i.hasNext());
316: assertTrue(i.hasPrevious());
317: assertEquals("b", i.previous());
318: assertEquals("a", i.previous());
319: }
320:
321: public void testCloneToString() {
322: ArrayList l = new ArrayList();
323: l.add("a");
324: l.add("b");
325:
326: assertEquals("[]", LazyList.toString(LazyList.clone(null)));
327: assertEquals("[a]", LazyList.toString(LazyList.clone("a")));
328: assertEquals("[a, b]", LazyList.toString(LazyList.clone(l)));
329: }
330:
331: }
|