01: /*
02: * <copyright>
03: *
04: * Copyright 2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.util;
28:
29: import java.util.*;
30:
31: import junit.framework.TestCase;
32: import junit.framework.*;
33:
34: public class TestRarelyModifiedList extends TestCase {
35:
36: public void test_DBL() {
37: List x, y;
38: RarelyModifiedList l = new RarelyModifiedList();
39: assertTrue(l.size() == 0);
40:
41: x = l.getUnmodifiableList();
42: assertTrue(x.size() == 0);
43:
44: l.add("A");
45: y = l.getUnmodifiableList();
46: assertTrue(y != x); // different unmodifiable lists
47: assertTrue(x.size() == 0); // x is still empty
48: assertTrue(y.size() == 1); // y has an element
49: assertTrue(y != l); // not ==
50: assertEquals(y, l); // but .equals
51: assertEquals(l, y); // but .equals
52:
53: Iterator it = l.iterator();
54: assertTrue(it.hasNext()); // has an element
55: Object o = it.next();
56: assertEquals("A", o); // the element is "A"
57: assertTrue(!it.hasNext()); // nothing else in the iteration
58:
59: assertEquals("A", l.get(0)); // get(int) works correctly
60:
61: comod(new ArrayList(), false);
62: comod(new RarelyModifiedList(), true);
63: }
64:
65: void comod(List l, boolean expectation) {
66: String cname = l.getClass().getName();
67: try {
68: l.add("A");
69: l.add("B");
70:
71: Iterator it = l.iterator();
72:
73: l.add("C");
74:
75: assertEquals("A", it.next());
76: assertEquals("B", it.next());
77:
78: assertTrue(cname + " didn't cause CME", expectation);
79: } catch (ConcurrentModificationException e) {
80: assertTrue(cname + " caused CME", !expectation);
81: }
82: }
83: }
|