--- jsr166/src/test/tck/AtomicIntegerArrayTest.java 2009/11/16 05:30:07 1.13 +++ jsr166/src/test/tck/AtomicIntegerArrayTest.java 2009/11/17 02:37:16 1.14 @@ -37,10 +37,8 @@ public class AtomicIntegerArrayTest exte try { int[] a = null; AtomicIntegerArray ai = new AtomicIntegerArray(a); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); - } + shouldThrow(); + } catch (NullPointerException success) {} } /** @@ -61,18 +59,22 @@ public class AtomicIntegerArrayTest exte AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); try { ai.get(SIZE); + shouldThrow(); } catch (IndexOutOfBoundsException success) { } try { ai.get(-1); + shouldThrow(); } catch (IndexOutOfBoundsException success) { } try { ai.set(SIZE, 0); + shouldThrow(); } catch (IndexOutOfBoundsException success) { } try { ai.set(-1, 0); + shouldThrow(); } catch (IndexOutOfBoundsException success) { } } @@ -128,23 +130,19 @@ public class AtomicIntegerArrayTest exte * compareAndSet in one thread enables another waiting for value * to succeed */ - public void testCompareAndSetInMultipleThreads() { + public void testCompareAndSetInMultipleThreads() throws Exception { final AtomicIntegerArray a = new AtomicIntegerArray(1); a.set(0, 1); Thread t = new Thread(new Runnable() { public void run() { while (!a.compareAndSet(0, 2, 3)) Thread.yield(); }}); - try { - t.start(); - assertTrue(a.compareAndSet(0, 1, 2)); - t.join(LONG_DELAY_MS); - assertFalse(t.isAlive()); - assertEquals(a.get(0), 3); - } - catch (Exception e) { - unexpectedException(); - } + + t.start(); + assertTrue(a.compareAndSet(0, 1, 2)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(a.get(0), 3); } /** @@ -293,50 +291,40 @@ public class AtomicIntegerArrayTest exte * Multiple threads using same array of counters successfully * update a number of times equal to total count */ - public void testCountingInMultipleThreads() { - try { - final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) - ai.set(i, COUNTDOWN); - Counter c1 = new Counter(ai); - Counter c2 = new Counter(ai); - Thread t1 = new Thread(c1); - Thread t2 = new Thread(c2); - t1.start(); - t2.start(); - t1.join(); - t2.join(); - assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testCountingInMultipleThreads() throws InterruptedException { + final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; ++i) + ai.set(i, COUNTDOWN); + Counter c1 = new Counter(ai); + Counter c2 = new Counter(ai); + Thread t1 = new Thread(c1); + Thread t2 = new Thread(c2); + t1.start(); + t2.start(); + t1.join(); + t2.join(); + assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); } /** * a deserialized serialized array holds same values */ - public void testSerialization() { + public void testSerialization() throws Exception { AtomicIntegerArray l = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) l.set(i, -i); - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(l); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - AtomicIntegerArray r = (AtomicIntegerArray) in.readObject(); - for (int i = 0; i < SIZE; ++i) { - assertEquals(l.get(i), r.get(i)); - } - } catch (Exception e) { - e.printStackTrace(); - unexpectedException(); + ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); + ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); + out.writeObject(l); + out.close(); + + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); + ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); + AtomicIntegerArray r = (AtomicIntegerArray) in.readObject(); + for (int i = 0; i < SIZE; ++i) { + assertEquals(l.get(i), r.get(i)); } }