diff options
author | bvosburgh | 2009-10-28 17:46:10 +0000 |
---|---|---|
committer | bvosburgh | 2009-10-28 17:46:10 +0000 |
commit | 267dda422e19750a8746f265dc0d6469e3aac014 (patch) | |
tree | 0afb6f533e2c7fed4907a5d2eeeb479661fdbce1 /jpa/tests/org.eclipse.jpt.utility.tests | |
parent | b9beac12764ad3ce59b39a98fc7a0665e8af36f4 (diff) | |
download | webtools.dali-267dda422e19750a8746f265dc0d6469e3aac014.tar.gz webtools.dali-267dda422e19750a8746f265dc0d6469e3aac014.tar.xz webtools.dali-267dda422e19750a8746f265dc0d6469e3aac014.zip |
rework JPA project locking
Diffstat (limited to 'jpa/tests/org.eclipse.jpt.utility.tests')
5 files changed, 471 insertions, 13 deletions
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SimpleQueueTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SimpleQueueTests.java new file mode 100644 index 0000000000..8ac603921f --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SimpleQueueTests.java @@ -0,0 +1,149 @@ +/******************************************************************************* + * Copyright (c) 2009 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.utility.tests.internal; + +import java.util.NoSuchElementException; + +import junit.framework.TestCase; + +import org.eclipse.jpt.utility.internal.Queue; +import org.eclipse.jpt.utility.internal.SimpleQueue; + +@SuppressWarnings("nls") +public class SimpleQueueTests extends TestCase { + + public SimpleQueueTests(String name) { + super(name); + } + + Queue<String> buildQueue() { + return new SimpleQueue<String>(); + } + + public void testIsEmpty() { + Queue<String> queue = this.buildQueue(); + assertTrue(queue.isEmpty()); + queue.enqueue("first"); + assertFalse(queue.isEmpty()); + queue.enqueue("second"); + assertFalse(queue.isEmpty()); + queue.dequeue(); + assertFalse(queue.isEmpty()); + queue.dequeue(); + assertTrue(queue.isEmpty()); + } + + public void testEnqueueAndDequeue() { + Queue<String> queue = this.buildQueue(); + String first = "first"; + String second = "second"; + + queue.enqueue(first); + queue.enqueue(second); + assertEquals(first, queue.dequeue()); + assertEquals(second, queue.dequeue()); + } + + public void testEnqueueAndPeek() { + Queue<String> queue = this.buildQueue(); + String first = "first"; + String second = "second"; + + queue.enqueue(first); + queue.enqueue(second); + assertEquals(first, queue.peek()); + assertEquals(first, queue.peek()); + assertEquals(first, queue.dequeue()); + assertEquals(second, queue.peek()); + assertEquals(second, queue.peek()); + assertEquals(second, queue.dequeue()); + } + + public void testEmptyQueueExceptionPeek() { + Queue<String> queue = this.buildQueue(); + String first = "first"; + String second = "second"; + + queue.enqueue(first); + queue.enqueue(second); + assertEquals(first, queue.peek()); + assertEquals(first, queue.dequeue()); + assertEquals(second, queue.peek()); + assertEquals(second, queue.dequeue()); + + boolean exCaught = false; + try { + queue.peek(); + fail(); + } catch (NoSuchElementException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testEmptyQueueExceptionDequeue() { + Queue<String> queue = this.buildQueue(); + String first = "first"; + String second = "second"; + + queue.enqueue(first); + queue.enqueue(second); + assertEquals(first, queue.peek()); + assertEquals(first, queue.dequeue()); + assertEquals(second, queue.peek()); + assertEquals(second, queue.dequeue()); + + boolean exCaught = false; + try { + queue.dequeue(); + fail(); + } catch (NoSuchElementException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testClone() { + SimpleQueue<String> queue = (SimpleQueue<String>) this.buildQueue(); + queue.enqueue("first"); + queue.enqueue("second"); + queue.enqueue("third"); + + this.verifyClone(queue, queue.clone()); + } + + public void testSerialization() throws Exception { + Queue<String> queue = this.buildQueue(); + queue.enqueue("first"); + queue.enqueue("second"); + queue.enqueue("third"); + + this.verifyClone(queue, TestTools.serialize(queue)); + } + + private void verifyClone(Queue<String> original, Queue<String> clone) { + assertNotSame(original, clone); + assertEquals(original.peek(), clone.peek()); + assertEquals(original.dequeue(), clone.dequeue()); + assertEquals(original.peek(), clone.peek()); + assertEquals(original.dequeue(), clone.dequeue()); + assertEquals(original.isEmpty(), clone.isEmpty()); + assertEquals(original.peek(), clone.peek()); + assertEquals(original.dequeue(), clone.dequeue()); + assertTrue(original.isEmpty()); + assertEquals(original.isEmpty(), clone.isEmpty()); + + original.enqueue("fourth"); + assertFalse(original.isEmpty()); + // clone should still be empty + assertTrue(clone.isEmpty()); + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SimpleStackTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SimpleStackTests.java index f1e97895c4..4d3de7b5bf 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SimpleStackTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SimpleStackTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Oracle. All rights reserved. + * Copyright (c) 2007, 2009 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -14,14 +14,19 @@ import junit.framework.TestCase; import org.eclipse.jpt.utility.internal.SimpleStack; import org.eclipse.jpt.utility.internal.Stack; +@SuppressWarnings("nls") public class SimpleStackTests extends TestCase { public SimpleStackTests(String name) { super(name); } + Stack<String> buildStack() { + return new SimpleStack<String>(); + } + public void testIsEmpty() { - Stack<String> stack = new SimpleStack<String>(); + Stack<String> stack = this.buildStack(); assertTrue(stack.isEmpty()); stack.push("first"); assertFalse(stack.isEmpty()); @@ -34,7 +39,7 @@ public class SimpleStackTests extends TestCase { } public void testPushAndPop() { - Stack<String> stack = new SimpleStack<String>(); + Stack<String> stack = this.buildStack(); String first = "first"; String second = "second"; @@ -45,7 +50,7 @@ public class SimpleStackTests extends TestCase { } public void testPushAndPeek() { - Stack<String> stack = new SimpleStack<String>(); + Stack<String> stack = this.buildStack(); String first = "first"; String second = "second"; @@ -60,7 +65,7 @@ public class SimpleStackTests extends TestCase { } public void testEmptyStackExceptionPeek() { - Stack<String> stack = new SimpleStack<String>(); + Stack<String> stack = this.buildStack(); String first = "first"; String second = "second"; @@ -74,6 +79,7 @@ public class SimpleStackTests extends TestCase { boolean exCaught = false; try { stack.peek(); + fail(); } catch (EmptyStackException ex) { exCaught = true; } @@ -81,7 +87,7 @@ public class SimpleStackTests extends TestCase { } public void testEmptyStackExceptionPop() { - Stack<String> stack = new SimpleStack<String>(); + Stack<String> stack = this.buildStack(); String first = "first"; String second = "second"; @@ -95,6 +101,7 @@ public class SimpleStackTests extends TestCase { boolean exCaught = false; try { stack.pop(); + fail(); } catch (EmptyStackException ex) { exCaught = true; } @@ -102,7 +109,7 @@ public class SimpleStackTests extends TestCase { } public void testClone() { - SimpleStack<String> stack = new SimpleStack<String>(); + SimpleStack<String> stack = (SimpleStack<String>) this.buildStack(); stack.push("first"); stack.push("second"); stack.push("third"); @@ -111,7 +118,7 @@ public class SimpleStackTests extends TestCase { } public void testSerialization() throws Exception { - SimpleStack<String> stack = new SimpleStack<String>(); + Stack<String> stack = this.buildStack(); stack.push("first"); stack.push("second"); stack.push("third"); diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SynchronizedQueueTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SynchronizedQueueTests.java new file mode 100644 index 0000000000..c95262682d --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SynchronizedQueueTests.java @@ -0,0 +1,285 @@ +/******************************************************************************* + * Copyright (c) 2009 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.utility.tests.internal; + +import java.util.NoSuchElementException; + +import org.eclipse.jpt.utility.internal.Queue; +import org.eclipse.jpt.utility.internal.SimpleQueue; +import org.eclipse.jpt.utility.internal.SynchronizedQueue; + +@SuppressWarnings("nls") +public class SynchronizedQueueTests extends SimpleQueueTests { + private volatile SynchronizedQueue<String> sq; + private volatile boolean exCaught; + private volatile boolean timeoutOccurred; + private volatile long startTime; + private volatile long endTime; + private volatile Object dequeuedObject; + + static final String ITEM_1 = new String(); + static final String ITEM_2 = new String(); + + public SynchronizedQueueTests(String name) { + super(name); + } + + @Override + Queue<String> buildQueue() { + return new SynchronizedQueue<String>(); + } + + @Override + public void testClone() { + // synchronized queue is not cloneable + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.sq = new SynchronizedQueue<String>(); + this.exCaught = false; + this.timeoutOccurred = false; + this.startTime = 0; + this.endTime = 0; + this.dequeuedObject = null; + } + + /** + * test first with an unsynchronized queue, + * then with a synchronized queue + */ + public void testConcurrentAccess() throws Exception { + this.verifyConcurrentAccess(new SlowSimpleQueue<String>(), "first"); + this.verifyConcurrentAccess(new SlowSynchronizedQueue<String>(), "second"); + } + + private void verifyConcurrentAccess(SlowQueue<String> slowQueue, String expected) throws Exception { + slowQueue.enqueue("first"); + slowQueue.enqueue("second"); + + Thread thread = new Thread(this.buildRunnable(slowQueue)); + thread.start(); + Thread.sleep(200); + + assertEquals(expected, slowQueue.dequeue()); + thread.join(); + assertTrue(slowQueue.isEmpty()); + } + + private Runnable buildRunnable(final SlowQueue<String> slowQueue) { + return new Runnable() { + public void run() { + slowQueue.slowDequeue(); + } + }; + } + + + private interface SlowQueue<E> extends Queue<E> { + Object slowDequeue(); + } + + private class SlowSimpleQueue<E> extends SimpleQueue<E> implements SlowQueue<E> { + SlowSimpleQueue() { + super(); + } + public Object slowDequeue() { + try { + Thread.sleep(500); + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + return this.dequeue(); + } + + } + + private class SlowSynchronizedQueue<E> extends SynchronizedQueue<E> implements SlowQueue<E> { + SlowSynchronizedQueue() { + super(); + } + public synchronized Object slowDequeue() { + try { + Thread.sleep(500); + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + return this.dequeue(); + } + + } + + + // ********** waits ********** + + public void testWaitToDequeue() throws Exception { + this.verifyWaitToDequeue(0); + // no timeout occurs... + assertFalse(this.timeoutOccurred); + // ...and an item should have been dequeued by t2... + assertSame(ITEM_1, this.dequeuedObject); + // ...and the queue should be empty + assertTrue(this.sq.isEmpty()); + // make a reasonable guess about how long t2 took + assertTrue(this.elapsedTime() > 150); + } + + public void testWaitToDequeueTimeout() throws Exception { + this.verifyWaitToDequeue(20); + // timeout occurs... + assertTrue(this.timeoutOccurred); + // ...and the queue was never dequeued... + assertNull(this.dequeuedObject); + // ...and it still holds the item + assertSame(ITEM_1, this.sq.peek()); + // make a reasonable guess about how long t2 took + assertTrue(this.elapsedTime() < 150); + } + + private void verifyWaitToDequeue(long timeout) throws Exception { + Runnable r1 = this.buildRunnable(this.buildEnqueueCommand(), this.sq, 200); + Runnable r2 = this.buildRunnable(this.buildWaitToDequeueCommand(timeout), this.sq, 0); + Thread t1 = new Thread(r1); + Thread t2 = new Thread(r2); + t1.start(); + t2.start(); + while (t1.isAlive() || t2.isAlive()) { + Thread.sleep(50); + } + assertFalse(this.exCaught); + } + + public void testWaitToEnqueue() throws Exception { + this.verifyWaitToEnqueue(0); + // no timeout occurs... + assertFalse(this.timeoutOccurred); + // ...and the queue gets dequeued by t1... + assertSame(ITEM_1, this.dequeuedObject); + // ...and an item is enqueued on to the queue by t2 + assertFalse(this.sq.isEmpty()); + assertSame(ITEM_2, this.sq.peek()); + // make a reasonable guess about how long t2 took + assertTrue(this.elapsedTime() > 150); + } + + public void testWaitToEnqueueTimeout() throws Exception { + this.verifyWaitToEnqueue(20); + // timeout occurs... + assertTrue(this.timeoutOccurred); + // ...and the queue is eventually dequeued by t1... + assertSame(ITEM_1, this.dequeuedObject); + // ...but nothing is enqueued on to the queue by t2 + assertTrue(this.sq.isEmpty()); + // make a reasonable guess about how long t2 took + assertTrue(this.elapsedTime() < 150); + } + + private void verifyWaitToEnqueue(long timeout) throws Exception { + this.sq.enqueue(ITEM_1); + Runnable r1 = this.buildRunnable(this.buildDequeueCommand(), this.sq, 200); + Runnable r2 = this.buildRunnable(this.buildWaitToEnqueueCommand(timeout), this.sq, 0); + Thread t1 = new Thread(r1); + Thread t2 = new Thread(r2); + t1.start(); + t2.start(); + while (t1.isAlive() || t2.isAlive()) { + Thread.sleep(50); + } + assertFalse(this.exCaught); + } + + private Command buildEnqueueCommand() { + return new Command() { + public void execute(SynchronizedQueue<String> synchronizedQueue) { + synchronizedQueue.enqueue(ITEM_1); + } + }; + } + + private Command buildWaitToDequeueCommand(final long timeout) { + return new Command() { + public void execute(SynchronizedQueue<String> synchronizedQueue) throws Exception { + SynchronizedQueueTests.this.setStartTime(System.currentTimeMillis()); + try { + SynchronizedQueueTests.this.setDequeuedObject(synchronizedQueue.waitToDequeue(timeout)); + } catch (NoSuchElementException ex) { + SynchronizedQueueTests.this.setTimeoutOccurred(true); + } + SynchronizedQueueTests.this.setEndTime(System.currentTimeMillis()); + } + }; + } + + private Command buildDequeueCommand() { + return new Command() { + public void execute(SynchronizedQueue<String> synchronizedQueue) { + SynchronizedQueueTests.this.setDequeuedObject(synchronizedQueue.dequeue()); + } + }; + } + + private Command buildWaitToEnqueueCommand(final long timeout) { + return new Command() { + public void execute(SynchronizedQueue<String> synchronizedQueue) throws Exception { + SynchronizedQueueTests.this.setStartTime(System.currentTimeMillis()); + SynchronizedQueueTests.this.setTimeoutOccurred( ! synchronizedQueue.waitToEnqueue(ITEM_2, timeout)); + SynchronizedQueueTests.this.setEndTime(System.currentTimeMillis()); + } + }; + } + + private Runnable buildRunnable(final Command command, final SynchronizedQueue<String> synchronizedQueue, final long sleep) { + return new Runnable() { + public void run() { + try { + if (sleep != 0) { + Thread.sleep(sleep); + } + command.execute(synchronizedQueue); + } catch (Exception ex) { + SynchronizedQueueTests.this.setExCaught(true); + } + } + }; + } + + void setExCaught(boolean exCaught) { + this.exCaught = exCaught; + } + + void setTimeoutOccurred(boolean timeoutOccurred) { + this.timeoutOccurred = timeoutOccurred; + } + + void setStartTime(long startTime) { + this.startTime = startTime; + } + + void setEndTime(long endTime) { + this.endTime = endTime; + } + + void setDequeuedObject(Object dequeuedObject) { + this.dequeuedObject = dequeuedObject; + } + + long elapsedTime() { + return this.endTime - this.startTime; + } + + + // ********** Command interface ********** + + private interface Command { + void execute(SynchronizedQueue<String> synchronizedQueue) throws Exception; + } + +} diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SynchronizedStackTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SynchronizedStackTests.java index 50c353c2de..ca187b5cb3 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SynchronizedStackTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/SynchronizedStackTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Oracle. All rights reserved. + * Copyright (c) 2007, 2009 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -14,6 +14,7 @@ import org.eclipse.jpt.utility.internal.SimpleStack; import org.eclipse.jpt.utility.internal.Stack; import org.eclipse.jpt.utility.internal.SynchronizedStack; +@SuppressWarnings("nls") public class SynchronizedStackTests extends SimpleStackTests { private volatile SynchronizedStack<String> ss; private volatile boolean exCaught; @@ -30,6 +31,16 @@ public class SynchronizedStackTests extends SimpleStackTests { } @Override + Stack<String> buildStack() { + return new SynchronizedStack<String>(); + } + + @Override + public void testClone() { + // synchronized stack is not cloneable + } + + @Override protected void setUp() throws Exception { super.setUp(); this.ss = new SynchronizedStack<String>(); @@ -53,10 +64,13 @@ public class SynchronizedStackTests extends SimpleStackTests { slowStack.push("first"); slowStack.push("second"); - new Thread(this.buildRunnable(slowStack)).start(); + Thread thread = new Thread(this.buildRunnable(slowStack)); + thread.start(); Thread.sleep(200); assertEquals(expected, slowStack.pop()); + thread.join(); + assertTrue(slowStack.isEmpty()); } private Runnable buildRunnable(final SlowStack<String> slowStack) { @@ -93,7 +107,7 @@ public class SynchronizedStackTests extends SimpleStackTests { } public synchronized Object slowPop() { try { - Thread.sleep(100); + Thread.sleep(500); } catch (InterruptedException ex) { throw new RuntimeException(ex); } diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/synchronizers/AsynchronousSynchronizerTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/synchronizers/AsynchronousSynchronizerTests.java index 9c16348a64..7a6a82f517 100644 --- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/synchronizers/AsynchronousSynchronizerTests.java +++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/synchronizers/AsynchronousSynchronizerTests.java @@ -14,6 +14,7 @@ import junit.framework.TestCase; import org.eclipse.jpt.utility.Command; import org.eclipse.jpt.utility.internal.ClassTools; import org.eclipse.jpt.utility.internal.CompositeException; +import org.eclipse.jpt.utility.internal.ConsumerThreadCoordinator; import org.eclipse.jpt.utility.internal.synchronizers.AsynchronousSynchronizer; import org.eclipse.jpt.utility.internal.synchronizers.Synchronizer; @@ -57,7 +58,8 @@ public class AsynchronousSynchronizerTests extends TestCase { } protected static void stop(Synchronizer synchronizer) { - if (ClassTools.fieldValue(synchronizer, "thread") != null) { + ConsumerThreadCoordinator ctc = (ConsumerThreadCoordinator) ClassTools.fieldValue(synchronizer, "consumerThreadCoordinator"); + if (ClassTools.fieldValue(ctc, "thread") != null) { synchronizer.stop(); } } @@ -166,7 +168,8 @@ public class AsynchronousSynchronizerTests extends TestCase { public void testThreadName() { Synchronizer s = new AsynchronousSynchronizer(this.command1, "sync"); s.start(); - Thread t = (Thread) ClassTools.fieldValue(s, "thread"); + ConsumerThreadCoordinator ctc = (ConsumerThreadCoordinator) ClassTools.fieldValue(s, "consumerThreadCoordinator"); + Thread t = (Thread) ClassTools.fieldValue(ctc, "thread"); assertEquals("sync", t.getName()); s.stop(); } |