You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/24 05:42:33 UTC
svn commit: r434296 [13/19] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/concurrent/ modules/concurrent/.settings/
modules/concurrent/META-INF/ modules/concurrent/make/
modules/concurrent/src/ modules/concurrent/src/main/ modules/conc...
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentHashMapTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentHashMapTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentHashMapTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentHashMapTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,560 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes,
+ * Pat Fisher, Mike Judd.
+ */
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.Enumeration;
+import java.io.*;
+
+public class ConcurrentHashMapTest extends JSR166TestCase{
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(ConcurrentHashMapTest.class);
+ }
+
+ /**
+ * Create a map from Integers 1-5 to Strings "A"-"E".
+ */
+ private static ConcurrentHashMap map5() {
+ ConcurrentHashMap map = new ConcurrentHashMap(5);
+ assertTrue(map.isEmpty());
+ map.put(one, "A");
+ map.put(two, "B");
+ map.put(three, "C");
+ map.put(four, "D");
+ map.put(five, "E");
+ assertFalse(map.isEmpty());
+ assertEquals(5, map.size());
+ return map;
+ }
+
+ /**
+ * clear removes all pairs
+ */
+ public void testClear() {
+ ConcurrentHashMap map = map5();
+ map.clear();
+ assertEquals(map.size(), 0);
+ }
+
+ /**
+ * Maps with same contents are equal
+ */
+ public void testEquals() {
+ ConcurrentHashMap map1 = map5();
+ ConcurrentHashMap map2 = map5();
+ assertEquals(map1, map2);
+ assertEquals(map2, map1);
+ map1.clear();
+ assertFalse(map1.equals(map2));
+ assertFalse(map2.equals(map1));
+ }
+
+ /**
+ * contains returns true for contained value
+ */
+ public void testContains() {
+ ConcurrentHashMap map = map5();
+ assertTrue(map.contains("A"));
+ assertFalse(map.contains("Z"));
+ }
+
+ /**
+ * containsKey returns true for contained key
+ */
+ public void testContainsKey() {
+ ConcurrentHashMap map = map5();
+ assertTrue(map.containsKey(one));
+ assertFalse(map.containsKey(zero));
+ }
+
+ /**
+ * containsValue returns true for held values
+ */
+ public void testContainsValue() {
+ ConcurrentHashMap map = map5();
+ assertTrue(map.contains("A"));
+ assertFalse(map.contains("Z"));
+ }
+
+ /**
+ * enumeration returns an enumeration containing the correct
+ * elements
+ */
+ public void testEnumeration() {
+ ConcurrentHashMap map = map5();
+ Enumeration e = map.elements();
+ int count = 0;
+ while(e.hasMoreElements()){
+ count++;
+ e.nextElement();
+ }
+ assertEquals(5, count);
+ }
+
+ /**
+ * Clone creates an equal map
+ */
+ public void testClone() {
+ ConcurrentHashMap map = map5();
+ ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone());
+ assertEquals(map, m2);
+ }
+
+ /**
+ * get returns the correct element at the given key,
+ * or null if not present
+ */
+ public void testGet() {
+ ConcurrentHashMap map = map5();
+ assertEquals("A", (String)map.get(one));
+ ConcurrentHashMap empty = new ConcurrentHashMap();
+ assertNull(map.get("anything"));
+ }
+
+ /**
+ * isEmpty is true of empty map and false for non-empty
+ */
+ public void testIsEmpty() {
+ ConcurrentHashMap empty = new ConcurrentHashMap();
+ ConcurrentHashMap map = map5();
+ assertTrue(empty.isEmpty());
+ assertFalse(map.isEmpty());
+ }
+
+ /**
+ * keys returns an enumeration containing all the keys from the map
+ */
+ public void testKeys() {
+ ConcurrentHashMap map = map5();
+ Enumeration e = map.keys();
+ int count = 0;
+ while(e.hasMoreElements()){
+ count++;
+ e.nextElement();
+ }
+ assertEquals(5, count);
+ }
+
+ /**
+ * keySet returns a Set containing all the keys
+ */
+ public void testKeySet() {
+ ConcurrentHashMap map = map5();
+ Set s = map.keySet();
+ assertEquals(5, s.size());
+ assertTrue(s.contains(one));
+ assertTrue(s.contains(two));
+ assertTrue(s.contains(three));
+ assertTrue(s.contains(four));
+ assertTrue(s.contains(five));
+ }
+
+ /**
+ * values collection contains all values
+ */
+ public void testValues() {
+ ConcurrentHashMap map = map5();
+ Collection s = map.values();
+ assertEquals(5, s.size());
+ assertTrue(s.contains("A"));
+ assertTrue(s.contains("B"));
+ assertTrue(s.contains("C"));
+ assertTrue(s.contains("D"));
+ assertTrue(s.contains("E"));
+ }
+
+ /**
+ * entrySet contains all pairs
+ */
+ public void testEntrySet() {
+ ConcurrentHashMap map = map5();
+ Set s = map.entrySet();
+ assertEquals(5, s.size());
+ Iterator it = s.iterator();
+ while (it.hasNext()) {
+ Map.Entry e = (Map.Entry) it.next();
+ assertTrue(
+ (e.getKey().equals(one) && e.getValue().equals("A")) ||
+ (e.getKey().equals(two) && e.getValue().equals("B")) ||
+ (e.getKey().equals(three) && e.getValue().equals("C")) ||
+ (e.getKey().equals(four) && e.getValue().equals("D")) ||
+ (e.getKey().equals(five) && e.getValue().equals("E")));
+ }
+ }
+
+ /**
+ * putAll adds all key-value pairs from the given map
+ */
+ public void testPutAll() {
+ ConcurrentHashMap empty = new ConcurrentHashMap();
+ ConcurrentHashMap map = map5();
+ empty.putAll(map);
+ assertEquals(5, empty.size());
+ assertTrue(empty.containsKey(one));
+ assertTrue(empty.containsKey(two));
+ assertTrue(empty.containsKey(three));
+ assertTrue(empty.containsKey(four));
+ assertTrue(empty.containsKey(five));
+ }
+
+ /**
+ * putIfAbsent works when the given key is not present
+ */
+ public void testPutIfAbsent() {
+ ConcurrentHashMap map = map5();
+ map.putIfAbsent(six, "Z");
+ assertTrue(map.containsKey(six));
+ }
+
+ /**
+ * putIfAbsent does not add the pair if the key is already present
+ */
+ public void testPutIfAbsent2() {
+ ConcurrentHashMap map = map5();
+ assertEquals("A", map.putIfAbsent(one, "Z"));
+ }
+
+ /**
+ * replace fails when the given key is not present
+ */
+ public void testReplace() {
+ ConcurrentHashMap map = map5();
+ assertNull(map.replace(six, "Z"));
+ assertFalse(map.containsKey(six));
+ }
+
+ /**
+ * replace succeeds if the key is already present
+ */
+ public void testReplace2() {
+ ConcurrentHashMap map = map5();
+ assertNotNull(map.replace(one, "Z"));
+ assertEquals("Z", map.get(one));
+ }
+
+
+ /**
+ * replace value fails when the given key not mapped to expected value
+ */
+ public void testReplaceValue() {
+ ConcurrentHashMap map = map5();
+ assertEquals("A", map.get(one));
+ assertFalse(map.replace(one, "Z", "Z"));
+ assertEquals("A", map.get(one));
+ }
+
+ /**
+ * replace value succeeds when the given key mapped to expected value
+ */
+ public void testReplaceValue2() {
+ ConcurrentHashMap map = map5();
+ assertEquals("A", map.get(one));
+ assertTrue(map.replace(one, "A", "Z"));
+ assertEquals("Z", map.get(one));
+ }
+
+
+ /**
+ * remove removes the correct key-value pair from the map
+ */
+ public void testRemove() {
+ ConcurrentHashMap map = map5();
+ map.remove(five);
+ assertEquals(4, map.size());
+ assertFalse(map.containsKey(five));
+ }
+
+ /**
+ * remove(key,value) removes only if pair present
+ */
+ public void testRemove2() {
+ ConcurrentHashMap map = map5();
+ map.remove(five, "E");
+ assertEquals(4, map.size());
+ assertFalse(map.containsKey(five));
+ map.remove(four, "A");
+ assertEquals(4, map.size());
+ assertTrue(map.containsKey(four));
+
+ }
+
+ /**
+ * size returns the correct values
+ */
+ public void testSize() {
+ ConcurrentHashMap map = map5();
+ ConcurrentHashMap empty = new ConcurrentHashMap();
+ assertEquals(0, empty.size());
+ assertEquals(5, map.size());
+ }
+
+ /**
+ * toString contains toString of elements
+ */
+ public void testToString() {
+ ConcurrentHashMap map = map5();
+ String s = map.toString();
+ for (int i = 1; i <= 5; ++i) {
+ assertTrue(s.indexOf(String.valueOf(i)) >= 0);
+ }
+ }
+
+ // Exception tests
+
+ /**
+ * Cannot create with negative capacity
+ */
+ public void testConstructor1() {
+ try {
+ new ConcurrentHashMap(-1,0,1);
+ shouldThrow();
+ } catch(IllegalArgumentException e){}
+ }
+
+ /**
+ * Cannot create with negative concurrency level
+ */
+ public void testConstructor2() {
+ try {
+ new ConcurrentHashMap(1,0,-1);
+ shouldThrow();
+ } catch(IllegalArgumentException e){}
+ }
+
+ /**
+ * Cannot create with only negative capacity
+ */
+ public void testConstructor3() {
+ try {
+ new ConcurrentHashMap(-1);
+ shouldThrow();
+ } catch(IllegalArgumentException e){}
+ }
+
+ /**
+ * get(null) throws NPE
+ */
+ public void testGet_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.get(null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * containsKey(null) throws NPE
+ */
+ public void testContainsKey_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.containsKey(null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * containsValue(null) throws NPE
+ */
+ public void testContainsValue_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.containsValue(null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * contains(null) throws NPE
+ */
+ public void testContains_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.contains(null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * put(null,x) throws NPE
+ */
+ public void testPut1_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.put(null, "whatever");
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * put(x, null) throws NPE
+ */
+ public void testPut2_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.put("whatever", null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * putIfAbsent(null, x) throws NPE
+ */
+ public void testPutIfAbsent1_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.putIfAbsent(null, "whatever");
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * replace(null, x) throws NPE
+ */
+ public void testReplace_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.replace(null, "whatever");
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * replace(null, x, y) throws NPE
+ */
+ public void testReplaceValue_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.replace(null, one, "whatever");
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * putIfAbsent(x, null) throws NPE
+ */
+ public void testPutIfAbsent2_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.putIfAbsent("whatever", null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+
+ /**
+ * replace(x, null) throws NPE
+ */
+ public void testReplace2_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.replace("whatever", null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * replace(x, null, y) throws NPE
+ */
+ public void testReplaceValue2_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.replace("whatever", null, "A");
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * replace(x, y, null) throws NPE
+ */
+ public void testReplaceValue3_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.replace("whatever", one, null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+
+ /**
+ * remove(null) throws NPE
+ */
+ public void testRemove1_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.put("sadsdf", "asdads");
+ c.remove(null);
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * remove(null, x) throws NPE
+ */
+ public void testRemove2_NullPointerException() {
+ try {
+ ConcurrentHashMap c = new ConcurrentHashMap(5);
+ c.put("sadsdf", "asdads");
+ c.remove(null, "whatever");
+ shouldThrow();
+ } catch(NullPointerException e){}
+ }
+
+ /**
+ * A deserialized map equals original
+ */
+ public void testSerialization() {
+ ConcurrentHashMap q = map5();
+
+ try {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(q);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
+ assertEquals(q.size(), r.size());
+ assertTrue(q.equals(r));
+ assertTrue(r.equals(q));
+ } catch(Exception e){
+ e.printStackTrace();
+ unexpectedException();
+ }
+ }
+
+
+ /**
+ * SetValue of an EntrySet entry sets value in the map.
+ */
+ public void testSetValueWriteThrough() {
+ // Adapted from a bug report by Eric Zoerner
+ ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
+ assertTrue(map.isEmpty());
+ for (int i = 0; i < 20; i++)
+ map.put(new Integer(i), new Integer(i));
+ assertFalse(map.isEmpty());
+ Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
+
+ // assert that entry1 is not 16
+ assertTrue("entry is 16, test not valid",
+ !entry1.getKey().equals(new Integer(16)));
+
+ // remove 16 (a different key) from map
+ // which just happens to cause entry1 to be cloned in map
+ map.remove(new Integer(16));
+ entry1.setValue("XYZ");
+ assertTrue(map.containsValue("XYZ")); // fails
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentHashMapTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentLinkedQueueTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentLinkedQueueTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentLinkedQueueTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentLinkedQueueTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,535 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes,
+ * Pat Fisher, Mike Judd.
+ */
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.io.*;
+
+public class ConcurrentLinkedQueueTest extends JSR166TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+
+ public static Test suite() {
+ return new TestSuite(ConcurrentLinkedQueueTest.class);
+ }
+
+ /**
+ * Create a queue of given size containing consecutive
+ * Integers 0 ... n.
+ */
+ private ConcurrentLinkedQueue populatedQueue(int n) {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ assertTrue(q.isEmpty());
+ for(int i = 0; i < n; ++i)
+ assertTrue(q.offer(new Integer(i)));
+ assertFalse(q.isEmpty());
+ assertEquals(n, q.size());
+ return q;
+ }
+
+ /**
+ * new queue is empty
+ */
+ public void testConstructor1() {
+ assertEquals(0, new ConcurrentLinkedQueue().size());
+ }
+
+ /**
+ * Initializing from null Collection throws NPE
+ */
+ public void testConstructor3() {
+ try {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
+ shouldThrow();
+ }
+ catch (NullPointerException success) {}
+ }
+
+ /**
+ * Initializing from Collection of null elements throws NPE
+ */
+ public void testConstructor4() {
+ try {
+ Integer[] ints = new Integer[SIZE];
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
+ shouldThrow();
+ }
+ catch (NullPointerException success) {}
+ }
+
+ /**
+ * Initializing from Collection with some null elements throws NPE
+ */
+ public void testConstructor5() {
+ try {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE-1; ++i)
+ ints[i] = new Integer(i);
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
+ shouldThrow();
+ }
+ catch (NullPointerException success) {}
+ }
+
+ /**
+ * Queue contains all elements of collection used to initialize
+ */
+ public void testConstructor6() {
+ try {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE; ++i)
+ ints[i] = new Integer(i);
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
+ for (int i = 0; i < SIZE; ++i)
+ assertEquals(ints[i], q.poll());
+ }
+ finally {}
+ }
+
+ /**
+ * isEmpty is true before add, false after
+ */
+ public void testEmpty() {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ assertTrue(q.isEmpty());
+ q.add(one);
+ assertFalse(q.isEmpty());
+ q.add(two);
+ q.remove();
+ q.remove();
+ assertTrue(q.isEmpty());
+ }
+
+ /**
+ * size changes when elements added and removed
+ */
+ public void testSize() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(SIZE-i, q.size());
+ q.remove();
+ }
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(i, q.size());
+ q.add(new Integer(i));
+ }
+ }
+
+ /**
+ * offer(null) throws NPE
+ */
+ public void testOfferNull() {
+ try {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ q.offer(null);
+ shouldThrow();
+ } catch (NullPointerException success) { }
+ }
+
+ /**
+ * add(null) throws NPE
+ */
+ public void testAddNull() {
+ try {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ q.add(null);
+ shouldThrow();
+ } catch (NullPointerException success) { }
+ }
+
+
+ /**
+ * Offer returns true
+ */
+ public void testOffer() {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ assertTrue(q.offer(zero));
+ assertTrue(q.offer(one));
+ }
+
+ /**
+ * add returns true
+ */
+ public void testAdd() {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(i, q.size());
+ assertTrue(q.add(new Integer(i)));
+ }
+ }
+
+ /**
+ * addAll(null) throws NPE
+ */
+ public void testAddAll1() {
+ try {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ q.addAll(null);
+ shouldThrow();
+ }
+ catch (NullPointerException success) {}
+ }
+
+ /**
+ * addAll(this) throws IAE
+ */
+ public void testAddAllSelf() {
+ try {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ q.addAll(q);
+ shouldThrow();
+ }
+ catch (IllegalArgumentException success) {}
+ }
+
+ /**
+ * addAll of a collection with null elements throws NPE
+ */
+ public void testAddAll2() {
+ try {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ Integer[] ints = new Integer[SIZE];
+ q.addAll(Arrays.asList(ints));
+ shouldThrow();
+ }
+ catch (NullPointerException success) {}
+ }
+ /**
+ * addAll of a collection with any null elements throws NPE after
+ * possibly adding some elements
+ */
+ public void testAddAll3() {
+ try {
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE-1; ++i)
+ ints[i] = new Integer(i);
+ q.addAll(Arrays.asList(ints));
+ shouldThrow();
+ }
+ catch (NullPointerException success) {}
+ }
+
+ /**
+ * Queue contains all elements, in traversal order, of successful addAll
+ */
+ public void testAddAll5() {
+ try {
+ Integer[] empty = new Integer[0];
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE; ++i)
+ ints[i] = new Integer(i);
+ ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ assertFalse(q.addAll(Arrays.asList(empty)));
+ assertTrue(q.addAll(Arrays.asList(ints)));
+ for (int i = 0; i < SIZE; ++i)
+ assertEquals(ints[i], q.poll());
+ }
+ finally {}
+ }
+
+ /**
+ * poll succeeds unless empty
+ */
+ public void testPoll() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(i, ((Integer)q.poll()).intValue());
+ }
+ assertNull(q.poll());
+ }
+
+ /**
+ * peek returns next element, or null if empty
+ */
+ public void testPeek() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(i, ((Integer)q.peek()).intValue());
+ q.poll();
+ assertTrue(q.peek() == null ||
+ i != ((Integer)q.peek()).intValue());
+ }
+ assertNull(q.peek());
+ }
+
+ /**
+ * element returns next element, or throws NSEE if empty
+ */
+ public void testElement() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(i, ((Integer)q.element()).intValue());
+ q.poll();
+ }
+ try {
+ q.element();
+ shouldThrow();
+ }
+ catch (NoSuchElementException success) {}
+ }
+
+ /**
+ * remove removes next element, or throws NSEE if empty
+ */
+ public void testRemove() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(i, ((Integer)q.remove()).intValue());
+ }
+ try {
+ q.remove();
+ shouldThrow();
+ } catch (NoSuchElementException success){
+ }
+ }
+
+ /**
+ * remove(x) removes x and returns true if present
+ */
+ public void testRemoveElement() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ for (int i = 1; i < SIZE; i+=2) {
+ assertTrue(q.remove(new Integer(i)));
+ }
+ for (int i = 0; i < SIZE; i+=2) {
+ assertTrue(q.remove(new Integer(i)));
+ assertFalse(q.remove(new Integer(i+1)));
+ }
+ assertTrue(q.isEmpty());
+ }
+
+ /**
+ * contains(x) reports true when elements added but not yet removed
+ */
+ public void testContains() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ assertTrue(q.contains(new Integer(i)));
+ q.poll();
+ assertFalse(q.contains(new Integer(i)));
+ }
+ }
+
+ /**
+ * clear removes all elements
+ */
+ public void testClear() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ q.clear();
+ assertTrue(q.isEmpty());
+ assertEquals(0, q.size());
+ q.add(one);
+ assertFalse(q.isEmpty());
+ q.clear();
+ assertTrue(q.isEmpty());
+ }
+
+ /**
+ * containsAll(c) is true when c contains a subset of elements
+ */
+ public void testContainsAll() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
+ for (int i = 0; i < SIZE; ++i) {
+ assertTrue(q.containsAll(p));
+ assertFalse(p.containsAll(q));
+ p.add(new Integer(i));
+ }
+ assertTrue(p.containsAll(q));
+ }
+
+ /**
+ * retainAll(c) retains only those elements of c and reports true if change
+ */
+ public void testRetainAll() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ ConcurrentLinkedQueue p = populatedQueue(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ boolean changed = q.retainAll(p);
+ if (i == 0)
+ assertFalse(changed);
+ else
+ assertTrue(changed);
+
+ assertTrue(q.containsAll(p));
+ assertEquals(SIZE-i, q.size());
+ p.remove();
+ }
+ }
+
+ /**
+ * removeAll(c) removes only those elements of c and reports true if changed
+ */
+ public void testRemoveAll() {
+ for (int i = 1; i < SIZE; ++i) {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ ConcurrentLinkedQueue p = populatedQueue(i);
+ assertTrue(q.removeAll(p));
+ assertEquals(SIZE-i, q.size());
+ for (int j = 0; j < i; ++j) {
+ Integer I = (Integer)(p.remove());
+ assertFalse(q.contains(I));
+ }
+ }
+ }
+
+ /**
+ * toArray contains all elements
+ */
+ public void testToArray() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ Object[] o = q.toArray();
+ Arrays.sort(o);
+ for(int i = 0; i < o.length; i++)
+ assertEquals(o[i], q.poll());
+ }
+
+ /**
+ * toArray(a) contains all elements
+ */
+ public void testToArray2() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ Integer[] ints = new Integer[SIZE];
+ ints = (Integer[])q.toArray(ints);
+ Arrays.sort(ints);
+ for(int i = 0; i < ints.length; i++)
+ assertEquals(ints[i], q.poll());
+ }
+
+ /**
+ * toArray(null) throws NPE
+ */
+ public void testToArray_BadArg() {
+ try {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ Object o[] = q.toArray(null);
+ shouldThrow();
+ } catch(NullPointerException success){}
+ }
+
+ /**
+ * toArray with incompatible array type throws CCE
+ */
+ public void testToArray1_BadArg() {
+ try {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ Object o[] = q.toArray(new String[10] );
+ shouldThrow();
+ } catch(ArrayStoreException success){}
+ }
+
+ /**
+ * iterator iterates through all elements
+ */
+ public void testIterator() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ int i = 0;
+ Iterator it = q.iterator();
+ while(it.hasNext()) {
+ assertTrue(q.contains(it.next()));
+ ++i;
+ }
+ assertEquals(i, SIZE);
+ }
+
+ /**
+ * iterator ordering is FIFO
+ */
+ public void testIteratorOrdering() {
+ final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ q.add(one);
+ q.add(two);
+ q.add(three);
+
+ int k = 0;
+ for (Iterator it = q.iterator(); it.hasNext();) {
+ int i = ((Integer)(it.next())).intValue();
+ assertEquals(++k, i);
+ }
+
+ assertEquals(3, k);
+ }
+
+ /**
+ * Modifications do not cause iterators to fail
+ */
+ public void testWeaklyConsistentIteration () {
+ final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ q.add(one);
+ q.add(two);
+ q.add(three);
+
+ try {
+ for (Iterator it = q.iterator(); it.hasNext();) {
+ q.remove();
+ it.next();
+ }
+ }
+ catch (ConcurrentModificationException e) {
+ shouldThrow();
+ }
+
+ assertEquals("queue should be empty again", 0, q.size());
+ }
+
+ /**
+ * iterator.remove removes current element
+ */
+ public void testIteratorRemove () {
+ final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
+ q.add(one);
+ q.add(two);
+ q.add(three);
+ Iterator it = q.iterator();
+ it.next();
+ it.remove();
+ it = q.iterator();
+ assertEquals(it.next(), two);
+ assertEquals(it.next(), three);
+ assertFalse(it.hasNext());
+ }
+
+
+ /**
+ * toString contains toStrings of elements
+ */
+ public void testToString() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ String s = q.toString();
+ for (int i = 0; i < SIZE; ++i) {
+ assertTrue(s.indexOf(String.valueOf(i)) >= 0);
+ }
+ }
+
+ /**
+ * A deserialized serialized queue has same elements in same order
+ */
+ public void testSerialization() {
+ ConcurrentLinkedQueue q = populatedQueue(SIZE);
+ try {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(q);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
+ assertEquals(q.size(), r.size());
+ while (!q.isEmpty())
+ assertEquals(q.remove(), r.remove());
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/ConcurrentLinkedQueueTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArrayListTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArrayListTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArrayListTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArrayListTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,621 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes,
+ * Pat Fisher, Mike Judd.
+ */
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.io.*;
+
+public class CopyOnWriteArrayListTest extends JSR166TestCase{
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+
+ public static Test suite() {
+ return new TestSuite(CopyOnWriteArrayListTest.class);
+ }
+
+ static CopyOnWriteArrayList populatedArray(int n){
+ CopyOnWriteArrayList a = new CopyOnWriteArrayList();
+ assertTrue(a.isEmpty());
+ for (int i = 0; i < n; ++i)
+ a.add(new Integer(i));
+ assertFalse(a.isEmpty());
+ assertEquals(n, a.size());
+ return a;
+ }
+
+
+ /**
+ * a new list is empty
+ */
+ public void testConstructor() {
+ CopyOnWriteArrayList a = new CopyOnWriteArrayList();
+ assertTrue(a.isEmpty());
+ }
+
+ /**
+ * new list contains all elements of initializing array
+ */
+ public void testConstructor2() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE-1; ++i)
+ ints[i] = new Integer(i);
+ CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
+ for (int i = 0; i < SIZE; ++i)
+ assertEquals(ints[i], a.get(i));
+ }
+
+ /**
+ * new list contains all elements of initializing collection
+ */
+ public void testConstructor3() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE-1; ++i)
+ ints[i] = new Integer(i);
+ CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
+ for (int i = 0; i < SIZE; ++i)
+ assertEquals(ints[i], a.get(i));
+ }
+
+
+ /**
+ * addAll adds each element from the given collection
+ */
+ public void testAddAll() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ Vector v = new Vector();
+ v.add(three);
+ v.add(four);
+ v.add(five);
+ full.addAll(v);
+ assertEquals(6, full.size());
+ }
+
+ /**
+ * addAllAbsent adds each element from the given collection that did not
+ * already exist in the List
+ */
+ public void testAddAllAbsent() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ Vector v = new Vector();
+ v.add(three);
+ v.add(four);
+ v.add(one); // will not add this element
+ full.addAllAbsent(v);
+ assertEquals(5, full.size());
+ }
+
+ /**
+ * addIfAbsent will not add the element if it already exists in the list
+ */
+ public void testAddIfAbsent() {
+ CopyOnWriteArrayList full = populatedArray(SIZE);
+ full.addIfAbsent(one);
+ assertEquals(SIZE, full.size());
+ }
+
+ /**
+ * addIfAbsent adds the element when it does not exist in the list
+ */
+ public void testAddIfAbsent2() {
+ CopyOnWriteArrayList full = populatedArray(SIZE);
+ full.addIfAbsent(three);
+ assertTrue(full.contains(three));
+ }
+
+ /**
+ * clear removes all elements from the list
+ */
+ public void testClear() {
+ CopyOnWriteArrayList full = populatedArray(SIZE);
+ full.clear();
+ assertEquals(0, full.size());
+ }
+
+
+ /**
+ * Cloned list is equal
+ */
+ public void testClone() {
+ CopyOnWriteArrayList l1 = populatedArray(SIZE);
+ CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
+ assertEquals(l1, l2);
+ l1.clear();
+ assertFalse(l1.equals(l2));
+ }
+
+ /**
+ * contains is true for added elements
+ */
+ public void testContains() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ assertTrue(full.contains(one));
+ assertFalse(full.contains(five));
+ }
+
+ /**
+ * adding at an index places it in the indicated index
+ */
+ public void testAddIndex() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ full.add(0, m1);
+ assertEquals(4, full.size());
+ assertEquals(m1, full.get(0));
+ assertEquals(zero, full.get(1));
+
+ full.add(2, m2);
+ assertEquals(5, full.size());
+ assertEquals(m2, full.get(2));
+ assertEquals(two, full.get(4));
+ }
+
+ /**
+ * lists with same elements are equal and have same hashCode
+ */
+ public void testEquals() {
+ CopyOnWriteArrayList a = populatedArray(3);
+ CopyOnWriteArrayList b = populatedArray(3);
+ assertTrue(a.equals(b));
+ assertTrue(b.equals(a));
+ assertEquals(a.hashCode(), b.hashCode());
+ a.add(m1);
+ assertFalse(a.equals(b));
+ assertFalse(b.equals(a));
+ b.add(m1);
+ assertTrue(a.equals(b));
+ assertTrue(b.equals(a));
+ assertEquals(a.hashCode(), b.hashCode());
+ }
+
+
+ /**
+ * containsAll returns true for collection with subset of elements
+ */
+ public void testContainsAll() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ Vector v = new Vector();
+ v.add(one);
+ v.add(two);
+ assertTrue(full.containsAll(v));
+ v.add(six);
+ assertFalse(full.containsAll(v));
+ }
+
+ /**
+ * get returns the value at the given index
+ */
+ public void testGet() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ assertEquals(0, ((Integer)full.get(0)).intValue());
+ }
+
+ /**
+ * indexOf gives the index for the given object
+ */
+ public void testIndexOf() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ assertEquals(1, full.indexOf(one));
+ assertEquals(-1, full.indexOf("puppies"));
+ }
+
+ /**
+ * indexOf gives the index based on the given index
+ * at which to start searching
+ */
+ public void testIndexOf2() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ assertEquals(1, full.indexOf(one, 0));
+ assertEquals(-1, full.indexOf(one, 2));
+ }
+
+ /**
+ * isEmpty returns true when empty, else false
+ */
+ public void testIsEmpty() {
+ CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
+ CopyOnWriteArrayList full = populatedArray(SIZE);
+ assertTrue(empty.isEmpty());
+ assertFalse(full.isEmpty());
+ }
+
+ /**
+ * iterator() returns an iterator containing the elements of the list
+ */
+ public void testIterator() {
+ CopyOnWriteArrayList full = populatedArray(SIZE);
+ Iterator i = full.iterator();
+ int j;
+ for(j = 0; i.hasNext(); j++)
+ assertEquals(j, ((Integer)i.next()).intValue());
+ assertEquals(SIZE, j);
+ }
+
+ /**
+ * iterator.remove throws UnsupportedOperationException
+ */
+ public void testIteratorRemove () {
+ CopyOnWriteArrayList full = populatedArray(SIZE);
+ Iterator it = full.iterator();
+ it.next();
+ try {
+ it.remove();
+ shouldThrow();
+ }
+ catch (UnsupportedOperationException success) {}
+ }
+
+ /**
+ * toString contains toString of elements
+ */
+ public void testToString() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ String s = full.toString();
+ for (int i = 0; i < 3; ++i) {
+ assertTrue(s.indexOf(String.valueOf(i)) >= 0);
+ }
+ }
+
+ /**
+ * lastIndexOf returns the index for the given object
+ */
+ public void testLastIndexOf1() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ full.add(one);
+ full.add(three);
+ assertEquals(3, full.lastIndexOf(one));
+ assertEquals(-1, full.lastIndexOf(six));
+ }
+
+ /**
+ * lastIndexOf returns the index from the given starting point
+ */
+ public void testlastIndexOf2() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ full.add(one);
+ full.add(three);
+ assertEquals(3, full.lastIndexOf(one, 4));
+ assertEquals(-1, full.lastIndexOf(three, 3));
+ }
+
+ /**
+ * listIterator traverses all elements
+ */
+ public void testListIterator1() {
+ CopyOnWriteArrayList full = populatedArray(SIZE);
+ ListIterator i = full.listIterator();
+ int j;
+ for(j = 0; i.hasNext(); j++)
+ assertEquals(j, ((Integer)i.next()).intValue());
+ assertEquals(SIZE, j);
+ }
+
+ /**
+ * listIterator only returns those elements after the given index
+ */
+ public void testListIterator2() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ ListIterator i = full.listIterator(1);
+ int j;
+ for(j = 0; i.hasNext(); j++)
+ assertEquals(j+1, ((Integer)i.next()).intValue());
+ assertEquals(2, j);
+ }
+
+ /**
+ * remove removes and returns the object at the given index
+ */
+ public void testRemove() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ assertEquals(two, full.remove(2));
+ assertEquals(2, full.size());
+ }
+
+ /**
+ * removeAll removes all elements from the given collection
+ */
+ public void testRemoveAll() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ Vector v = new Vector();
+ v.add(one);
+ v.add(two);
+ full.removeAll(v);
+ assertEquals(1, full.size());
+ }
+
+ /**
+ * set changes the element at the given index
+ */
+ public void testSet() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ assertEquals(two, full.set(2, four));
+ assertEquals(4, ((Integer)full.get(2)).intValue());
+ }
+
+ /**
+ * size returns the number of elements
+ */
+ public void testSize() {
+ CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
+ CopyOnWriteArrayList full = populatedArray(SIZE);
+ assertEquals(SIZE, full.size());
+ assertEquals(0, empty.size());
+ }
+
+ /**
+ * toArray returns an Object array containing all elements from the list
+ */
+ public void testToArray() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ Object[] o = full.toArray();
+ assertEquals(3, o.length);
+ assertEquals(0, ((Integer)o[0]).intValue());
+ assertEquals(1, ((Integer)o[1]).intValue());
+ assertEquals(2, ((Integer)o[2]).intValue());
+ }
+
+ /**
+ * toArray returns an Integer array containing all elements from
+ * the list
+ */
+ public void testToArray2() {
+ CopyOnWriteArrayList full = populatedArray(3);
+ Integer[] i = new Integer[3];
+ i = (Integer[])full.toArray(i);
+ assertEquals(3, i.length);
+ assertEquals(0, i[0].intValue());
+ assertEquals(1, i[1].intValue());
+ assertEquals(2, i[2].intValue());
+ }
+
+
+ /**
+ * sublists contains elements at indexes offset from their base
+ */
+ public void testSubList() {
+ CopyOnWriteArrayList a = populatedArray(10);
+ assertTrue(a.subList(1,1).isEmpty());
+ for(int j = 0; j < 9; ++j) {
+ for(int i = j ; i < 10; ++i) {
+ List b = a.subList(j,i);
+ for(int k = j; k < i; ++k) {
+ assertEquals(new Integer(k), b.get(k-j));
+ }
+ }
+ }
+
+ List s = a.subList(2, 5);
+ assertEquals(s.size(), 3);
+ s.set(2, m1);
+ assertEquals(a.get(4), m1);
+ s.clear();
+ assertEquals(a.size(), 7);
+ }
+
+ // Exception tests
+
+ /**
+ * toArray throws an ArrayStoreException when the given array
+ * can not store the objects inside the list
+ */
+ public void testToArray_ArrayStoreException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add("zfasdfsdf");
+ c.add("asdadasd");
+ c.toArray(new Long[5]);
+ shouldThrow();
+ } catch(ArrayStoreException e){}
+ }
+
+ /**
+ * get throws an IndexOutOfBoundsException on a negative index
+ */
+ public void testGet1_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.get(-1);
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * get throws an IndexOutOfBoundsException on a too high index
+ */
+ public void testGet2_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add("asdasd");
+ c.add("asdad");
+ c.get(100);
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * set throws an IndexOutOfBoundsException on a negative index
+ */
+ public void testSet1_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.set(-1,"qwerty");
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * set throws an IndexOutOfBoundsException on a too high index
+ */
+ public void testSet2() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add("asdasd");
+ c.add("asdad");
+ c.set(100, "qwerty");
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * add throws an IndexOutOfBoundsException on a negative index
+ */
+ public void testAdd1_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add(-1,"qwerty");
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * add throws an IndexOutOfBoundsException on a too high index
+ */
+ public void testAdd2_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add("asdasd");
+ c.add("asdasdasd");
+ c.add(100, "qwerty");
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * remove throws an IndexOutOfBoundsException on a negative index
+ */
+ public void testRemove1_IndexOutOfBounds() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.remove(-1);
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * remove throws an IndexOutOfBoundsException on a too high index
+ */
+ public void testRemove2_IndexOutOfBounds() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add("asdasd");
+ c.add("adasdasd");
+ c.remove(100);
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * addAll throws an IndexOutOfBoundsException on a negative index
+ */
+ public void testAddAll1_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.addAll(-1,new LinkedList());
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * addAll throws an IndexOutOfBoundsException on a too high index
+ */
+ public void testAddAll2_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add("asdasd");
+ c.add("asdasdasd");
+ c.addAll(100, new LinkedList());
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * listIterator throws an IndexOutOfBoundsException on a negative index
+ */
+ public void testListIterator1_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.listIterator(-1);
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * listIterator throws an IndexOutOfBoundsException on a too high index
+ */
+ public void testListIterator2_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add("adasd");
+ c.add("asdasdas");
+ c.listIterator(100);
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * subList throws an IndexOutOfBoundsException on a negative index
+ */
+ public void testSubList1_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.subList(-1,100);
+
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * subList throws an IndexOutOfBoundsException on a too high index
+ */
+ public void testSubList2_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.add("asdasd");
+ c.subList(1,100);
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * subList throws IndexOutOfBoundsException when the second index
+ * is lower then the first
+ */
+ public void testSubList3_IndexOutOfBoundsException() {
+ try {
+ CopyOnWriteArrayList c = new CopyOnWriteArrayList();
+ c.subList(3,1);
+
+ shouldThrow();
+ } catch(IndexOutOfBoundsException e){}
+ }
+
+ /**
+ * a deserialized serialiszed list is equal
+ */
+ public void testSerialization() {
+ CopyOnWriteArrayList q = populatedArray(SIZE);
+
+ try {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(q);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
+ assertEquals(q.size(), r.size());
+ assertTrue(q.equals(r));
+ assertTrue(r.equals(q));
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArrayListTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArraySetTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArraySetTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArraySetTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArraySetTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,294 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes,
+ * Pat Fisher, Mike Judd.
+ */
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.io.*;
+
+public class CopyOnWriteArraySetTest extends JSR166TestCase {
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(CopyOnWriteArraySetTest.class);
+ }
+
+ static CopyOnWriteArraySet populatedSet(int n){
+ CopyOnWriteArraySet a = new CopyOnWriteArraySet();
+ assertTrue(a.isEmpty());
+ for (int i = 0; i < n; ++i)
+ a.add(new Integer(i));
+ assertFalse(a.isEmpty());
+ assertEquals(n, a.size());
+ return a;
+ }
+
+ /**
+ * Default-constructed set is empty
+ */
+ public void testConstructor() {
+ CopyOnWriteArraySet a = new CopyOnWriteArraySet();
+ assertTrue(a.isEmpty());
+ }
+
+ /**
+ * Collection-constructed set holds all of its elements
+ */
+ public void testConstructor3() {
+ Integer[] ints = new Integer[SIZE];
+ for (int i = 0; i < SIZE-1; ++i)
+ ints[i] = new Integer(i);
+ CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
+ for (int i = 0; i < SIZE; ++i)
+ assertTrue(a.contains(ints[i]));
+ }
+
+
+ /**
+ * addAll adds each element from the given collection
+ */
+ public void testAddAll() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ Vector v = new Vector();
+ v.add(three);
+ v.add(four);
+ v.add(five);
+ full.addAll(v);
+ assertEquals(6, full.size());
+ }
+
+ /**
+ * addAll adds each element from the given collection that did not
+ * already exist in the set
+ */
+ public void testAddAll2() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ Vector v = new Vector();
+ v.add(three);
+ v.add(four);
+ v.add(one); // will not add this element
+ full.addAll(v);
+ assertEquals(5, full.size());
+ }
+
+ /**
+ * add will not add the element if it already exists in the set
+ */
+ public void testAdd2() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ full.add(one);
+ assertEquals(3, full.size());
+ }
+
+ /**
+ * add adds the element when it does not exist
+ * in the set
+ */
+ public void testAdd3() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ full.add(three);
+ assertTrue(full.contains(three));
+ }
+
+ /**
+ * clear removes all elements from the set
+ */
+ public void testClear() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ full.clear();
+ assertEquals(0, full.size());
+ }
+
+ /**
+ * contains returns true for added elements
+ */
+ public void testContains() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ assertTrue(full.contains(one));
+ assertFalse(full.contains(five));
+ }
+
+ /**
+ * Sets with equal elements are equal
+ */
+ public void testEquals() {
+ CopyOnWriteArraySet a = populatedSet(3);
+ CopyOnWriteArraySet b = populatedSet(3);
+ assertTrue(a.equals(b));
+ assertTrue(b.equals(a));
+ assertEquals(a.hashCode(), b.hashCode());
+ a.add(m1);
+ assertFalse(a.equals(b));
+ assertFalse(b.equals(a));
+ b.add(m1);
+ assertTrue(a.equals(b));
+ assertTrue(b.equals(a));
+ assertEquals(a.hashCode(), b.hashCode());
+ }
+
+
+ /**
+ * containsAll returns true for collections with subset of elements
+ */
+ public void testContainsAll() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ Vector v = new Vector();
+ v.add(one);
+ v.add(two);
+ assertTrue(full.containsAll(v));
+ v.add(six);
+ assertFalse(full.containsAll(v));
+ }
+
+ /**
+ * isEmpty is true when empty, else false
+ */
+ public void testIsEmpty() {
+ CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
+ CopyOnWriteArraySet full = populatedSet(3);
+ assertTrue(empty.isEmpty());
+ assertFalse(full.isEmpty());
+ }
+
+ /**
+ * iterator() returns an iterator containing the elements of the set
+ */
+ public void testIterator() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ Iterator i = full.iterator();
+ int j;
+ for(j = 0; i.hasNext(); j++)
+ assertEquals(j, ((Integer)i.next()).intValue());
+ assertEquals(3, j);
+ }
+
+ /**
+ * iterator remove is unsupported
+ */
+ public void testIteratorRemove () {
+ CopyOnWriteArraySet full = populatedSet(3);
+ Iterator it = full.iterator();
+ it.next();
+ try {
+ it.remove();
+ shouldThrow();
+ }
+ catch (UnsupportedOperationException success) {}
+ }
+
+ /**
+ * toString holds toString of elements
+ */
+ public void testToString() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ String s = full.toString();
+ for (int i = 0; i < 3; ++i) {
+ assertTrue(s.indexOf(String.valueOf(i)) >= 0);
+ }
+ }
+
+
+ /**
+ * removeAll removes all elements from the given collection
+ */
+ public void testRemoveAll() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ Vector v = new Vector();
+ v.add(one);
+ v.add(two);
+ full.removeAll(v);
+ assertEquals(1, full.size());
+ }
+
+
+ /**
+ * remove removes an element
+ */
+ public void testRemove() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ full.remove(one);
+ assertFalse(full.contains(one));
+ assertEquals(2, full.size());
+ }
+
+ /**
+ * size returns the number of elements
+ */
+ public void testSize() {
+ CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
+ CopyOnWriteArraySet full = populatedSet(3);
+ assertEquals(3, full.size());
+ assertEquals(0, empty.size());
+ }
+
+ /**
+ * toArray returns an Object array containing all elements from the set
+ */
+ public void testToArray() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ Object[] o = full.toArray();
+ assertEquals(3, o.length);
+ assertEquals(0, ((Integer)o[0]).intValue());
+ assertEquals(1, ((Integer)o[1]).intValue());
+ assertEquals(2, ((Integer)o[2]).intValue());
+ }
+
+ /**
+ * toArray returns an Integer array containing all elements from
+ * the set
+ */
+ public void testToArray2() {
+ CopyOnWriteArraySet full = populatedSet(3);
+ Integer[] i = new Integer[3];
+ i = (Integer[])full.toArray(i);
+ assertEquals(3, i.length);
+ assertEquals(0, i[0].intValue());
+ assertEquals(1, i[1].intValue());
+ assertEquals(2, i[2].intValue());
+ }
+
+
+ /**
+ * toArray throws an ArrayStoreException when the given array can
+ * not store the objects inside the set
+ */
+ public void testToArray_ArrayStoreException() {
+ try {
+ CopyOnWriteArraySet c = new CopyOnWriteArraySet();
+ c.add("zfasdfsdf");
+ c.add("asdadasd");
+ c.toArray(new Long[5]);
+ shouldThrow();
+ } catch(ArrayStoreException e){}
+ }
+
+ /**
+ * A deserialized serialized set is equal
+ */
+ public void testSerialization() {
+ CopyOnWriteArraySet q = populatedSet(SIZE);
+
+ try {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(q);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
+ assertEquals(q.size(), r.size());
+ assertTrue(q.equals(r));
+ assertTrue(r.equals(q));
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CopyOnWriteArraySetTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CountDownLatchTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CountDownLatchTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CountDownLatchTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CountDownLatchTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,204 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes,
+ * Pat Fisher, Mike Judd.
+ */
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+public class CountDownLatchTest extends JSR166TestCase {
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(CountDownLatchTest.class);
+ }
+
+ /**
+ * negative constructor argument throws IAE
+ */
+ public void testConstructor() {
+ try {
+ new CountDownLatch(-1);
+ shouldThrow();
+ } catch(IllegalArgumentException success){}
+ }
+
+ /**
+ * getCount returns initial count and decreases after countDown
+ */
+ public void testGetCount() {
+ final CountDownLatch l = new CountDownLatch(2);
+ assertEquals(2, l.getCount());
+ l.countDown();
+ assertEquals(1, l.getCount());
+ }
+
+ /**
+ * countDown decrements count when positive and has no effect when zero
+ */
+ public void testCountDown() {
+ final CountDownLatch l = new CountDownLatch(1);
+ assertEquals(1, l.getCount());
+ l.countDown();
+ assertEquals(0, l.getCount());
+ l.countDown();
+ assertEquals(0, l.getCount());
+ }
+
+ /**
+ * await returns after countDown to zero, but not before
+ */
+ public void testAwait() {
+ final CountDownLatch l = new CountDownLatch(2);
+
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ threadAssertTrue(l.getCount() > 0);
+ l.await();
+ threadAssertTrue(l.getCount() == 0);
+ } catch(InterruptedException e){
+ threadUnexpectedException();
+ }
+ }
+ });
+ t.start();
+ try {
+ assertEquals(l.getCount(), 2);
+ Thread.sleep(SHORT_DELAY_MS);
+ l.countDown();
+ assertEquals(l.getCount(), 1);
+ l.countDown();
+ assertEquals(l.getCount(), 0);
+ t.join();
+ } catch (InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+
+ /**
+ * timed await returns after countDown to zero
+ */
+ public void testTimedAwait() {
+ final CountDownLatch l = new CountDownLatch(2);
+
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ threadAssertTrue(l.getCount() > 0);
+ threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
+ } catch(InterruptedException e){
+ threadUnexpectedException();
+ }
+ }
+ });
+ t.start();
+ try {
+ assertEquals(l.getCount(), 2);
+ Thread.sleep(SHORT_DELAY_MS);
+ l.countDown();
+ assertEquals(l.getCount(), 1);
+ l.countDown();
+ assertEquals(l.getCount(), 0);
+ t.join();
+ } catch (InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * await throws IE if interrupted before counted down
+ */
+ public void testAwait_InterruptedException() {
+ final CountDownLatch l = new CountDownLatch(1);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ threadAssertTrue(l.getCount() > 0);
+ l.await();
+ threadShouldThrow();
+ } catch(InterruptedException success){}
+ }
+ });
+ t.start();
+ try {
+ assertEquals(l.getCount(), 1);
+ t.interrupt();
+ t.join();
+ } catch (InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * timed await throws IE if interrupted before counted down
+ */
+ public void testTimedAwait_InterruptedException() {
+ final CountDownLatch l = new CountDownLatch(1);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ threadAssertTrue(l.getCount() > 0);
+ l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
+ threadShouldThrow();
+ } catch(InterruptedException success){}
+ }
+ });
+ t.start();
+ try {
+ Thread.sleep(SHORT_DELAY_MS);
+ assertEquals(l.getCount(), 1);
+ t.interrupt();
+ t.join();
+ } catch (InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * timed await times out if not counted down before timeout
+ */
+ public void testAwaitTimeout() {
+ final CountDownLatch l = new CountDownLatch(1);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ threadAssertTrue(l.getCount() > 0);
+ threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
+ threadAssertTrue(l.getCount() > 0);
+ } catch(InterruptedException ie){
+ threadUnexpectedException();
+ }
+ }
+ });
+ t.start();
+ try {
+ assertEquals(l.getCount(), 1);
+ t.join();
+ } catch (InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * toString indicates current count
+ */
+ public void testToString() {
+ CountDownLatch s = new CountDownLatch(2);
+ String us = s.toString();
+ assertTrue(us.indexOf("Count = 2") >= 0);
+ s.countDown();
+ String s1 = s.toString();
+ assertTrue(s1.indexOf("Count = 1") >= 0);
+ s.countDown();
+ String s2 = s.toString();
+ assertTrue(s2.indexOf("Count = 0") >= 0);
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CountDownLatchTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CyclicBarrierTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CyclicBarrierTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CyclicBarrierTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CyclicBarrierTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,377 @@
+/*
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ * Other contributors include Andrew Wright, Jeffrey Hayes,
+ * Pat Fisher, Mike Judd.
+ */
+
+import junit.framework.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+public class CyclicBarrierTest extends JSR166TestCase{
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(CyclicBarrierTest.class);
+ }
+
+ private volatile int countAction;
+ private class MyAction implements Runnable {
+ public void run() { ++countAction; }
+ }
+
+ /**
+ * Creating with negative parties throws IAE
+ */
+ public void testConstructor1() {
+ try {
+ new CyclicBarrier(-1, (Runnable)null);
+ shouldThrow();
+ } catch(IllegalArgumentException e){}
+ }
+
+ /**
+ * Creating with negative parties and no action throws IAE
+ */
+ public void testConstructor2() {
+ try {
+ new CyclicBarrier(-1);
+ shouldThrow();
+ } catch(IllegalArgumentException e){}
+ }
+
+ /**
+ * getParties returns the number of parties given in constructor
+ */
+ public void testGetParties() {
+ CyclicBarrier b = new CyclicBarrier(2);
+ assertEquals(2, b.getParties());
+ assertEquals(0, b.getNumberWaiting());
+ }
+
+ /**
+ * A 1-party barrier triggers after single await
+ */
+ public void testSingleParty() {
+ try {
+ CyclicBarrier b = new CyclicBarrier(1);
+ assertEquals(1, b.getParties());
+ assertEquals(0, b.getNumberWaiting());
+ b.await();
+ b.await();
+ assertEquals(0, b.getNumberWaiting());
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * The supplied barrier action is run at barrier
+ */
+ public void testBarrierAction() {
+ try {
+ countAction = 0;
+ CyclicBarrier b = new CyclicBarrier(1, new MyAction());
+ assertEquals(1, b.getParties());
+ assertEquals(0, b.getNumberWaiting());
+ b.await();
+ b.await();
+ assertEquals(0, b.getNumberWaiting());
+ assertEquals(countAction, 2);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * A 2-party/thread barrier triggers after both threads invoke await
+ */
+ public void testTwoParties() {
+ final CyclicBarrier b = new CyclicBarrier(2);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ b.await();
+ b.await();
+ b.await();
+ b.await();
+ } catch(Exception e){
+ threadUnexpectedException();
+ }}});
+
+ try {
+ t.start();
+ b.await();
+ b.await();
+ b.await();
+ b.await();
+ t.join();
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+
+ /**
+ * An interruption in one party causes others waiting in await to
+ * throw BrokenBarrierException
+ */
+ public void testAwait1_Interrupted_BrokenBarrier() {
+ final CyclicBarrier c = new CyclicBarrier(3);
+ Thread t1 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await();
+ threadShouldThrow();
+ } catch(InterruptedException success){}
+ catch(Exception b){
+ threadUnexpectedException();
+ }
+ }
+ });
+ Thread t2 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await();
+ threadShouldThrow();
+ } catch(BrokenBarrierException success){
+ } catch(Exception i){
+ threadUnexpectedException();
+ }
+ }
+ });
+ try {
+ t1.start();
+ t2.start();
+ Thread.sleep(SHORT_DELAY_MS);
+ t1.interrupt();
+ t1.join();
+ t2.join();
+ } catch(InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * An interruption in one party causes others waiting in timed await to
+ * throw BrokenBarrierException
+ */
+ public void testAwait2_Interrupted_BrokenBarrier() {
+ final CyclicBarrier c = new CyclicBarrier(3);
+ Thread t1 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+ threadShouldThrow();
+ } catch(InterruptedException success){
+ } catch(Exception b){
+ threadUnexpectedException();
+ }
+ }
+ });
+ Thread t2 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
+ threadShouldThrow();
+ } catch(BrokenBarrierException success){
+ } catch(Exception i){
+ threadUnexpectedException();
+ }
+ }
+ });
+ try {
+ t1.start();
+ t2.start();
+ Thread.sleep(SHORT_DELAY_MS);
+ t1.interrupt();
+ t1.join();
+ t2.join();
+ } catch(InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * A timeout in timed await throws TimeoutException
+ */
+ public void testAwait3_TimeOutException() {
+ final CyclicBarrier c = new CyclicBarrier(2);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
+ threadShouldThrow();
+ } catch(TimeoutException success){
+ } catch(Exception b){
+ threadUnexpectedException();
+
+ }
+ }
+ });
+ try {
+ t.start();
+ t.join();
+ } catch(InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * A timeout in one party causes others waiting in timed await to
+ * throw BrokenBarrierException
+ */
+ public void testAwait4_Timeout_BrokenBarrier() {
+ final CyclicBarrier c = new CyclicBarrier(3);
+ Thread t1 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
+ threadShouldThrow();
+ } catch(TimeoutException success){
+ } catch(Exception b){
+ threadUnexpectedException();
+ }
+ }
+ });
+ Thread t2 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
+ threadShouldThrow();
+ } catch(BrokenBarrierException success){
+ } catch(Exception i){
+ threadUnexpectedException();
+ }
+ }
+ });
+ try {
+ t1.start();
+ t2.start();
+ t1.join();
+ t2.join();
+ } catch(InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * A timeout in one party causes others waiting in await to
+ * throw BrokenBarrierException
+ */
+ public void testAwait5_Timeout_BrokenBarrier() {
+ final CyclicBarrier c = new CyclicBarrier(3);
+ Thread t1 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
+ threadShouldThrow();
+ } catch(TimeoutException success){
+ } catch(Exception b){
+ threadUnexpectedException();
+ }
+ }
+ });
+ Thread t2 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await();
+ threadShouldThrow();
+ } catch(BrokenBarrierException success){
+ } catch(Exception i){
+ threadUnexpectedException();
+ }
+ }
+ });
+ try {
+ t1.start();
+ t2.start();
+ t1.join();
+ t2.join();
+ } catch(InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * A reset of an active barrier causes waiting threads to throw
+ * BrokenBarrierException
+ */
+ public void testReset_BrokenBarrier() {
+ final CyclicBarrier c = new CyclicBarrier(3);
+ Thread t1 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await();
+ threadShouldThrow();
+ } catch(BrokenBarrierException success){}
+ catch(Exception b){
+ threadUnexpectedException();
+ }
+ }
+ });
+ Thread t2 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await();
+ threadShouldThrow();
+ } catch(BrokenBarrierException success){
+ } catch(Exception i){
+ threadUnexpectedException();
+ }
+ }
+ });
+ try {
+ t1.start();
+ t2.start();
+ Thread.sleep(SHORT_DELAY_MS);
+ c.reset();
+ t1.join();
+ t2.join();
+ } catch(InterruptedException e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * A reset before threads enter barrier does not throw
+ * BrokenBarrierException
+ */
+ public void testReset_NoBrokenBarrier() {
+ final CyclicBarrier c = new CyclicBarrier(3);
+ Thread t1 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await();
+ } catch(Exception b){
+ threadUnexpectedException();
+ }
+ }
+ });
+ Thread t2 = new Thread(new Runnable() {
+ public void run() {
+ try {
+ c.await();
+ } catch(Exception i){
+ threadUnexpectedException();
+ }
+ }
+ });
+ try {
+ c.reset();
+ t1.start();
+ t2.start();
+ c.await();
+ t1.join();
+ t2.join();
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/CyclicBarrierTest.java
------------------------------------------------------------------------------
svn:eol-style = native