Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Vosburgh2016-07-20 15:18:39 +0000
committerBrian Vosburgh2017-05-18 22:38:09 +0000
commitf91968e110d50306c5d8144ea401ba888c66abf7 (patch)
treef08428c2acdfd2b60917445443ce84ef237a6a32 /common/tests
parent9f7381368b8b5df01ae18db6d6d7dbb1870298e8 (diff)
downloadwebtools.dali-f91968e110d50306c5d8144ea401ba888c66abf7.tar.gz
webtools.dali-f91968e110d50306c5d8144ea401ba888c66abf7.tar.xz
webtools.dali-f91968e110d50306c5d8144ea401ba888c66abf7.zip
add BiClosure
Diffstat (limited to 'common/tests')
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java4
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BiClosureToolsTests.java726
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/ClosureToolsTests.java1854
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java2
4 files changed, 2585 insertions, 1 deletions
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java
index ab7b8c52ec..a0e2cd0d9e 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2015 Oracle. All rights reserved.
+ * Copyright (c) 2005, 2016 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.
@@ -9,6 +9,7 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal;
+import org.eclipse.jpt.common.utility.tests.internal.closure.JptCommonUtilityClosureTests;
import org.eclipse.jpt.common.utility.tests.internal.collection.JptCommonUtilityCollectionTests;
import org.eclipse.jpt.common.utility.tests.internal.command.JptCommonUtilityCommandTests;
import org.eclipse.jpt.common.utility.tests.internal.comparator.JptCommonUtilityComparatorTests;
@@ -38,6 +39,7 @@ public class JptCommonUtilityTests {
public static Test suite() {
TestSuite suite = new TestSuite(JptCommonUtilityTests.class.getPackage().getName());
+ suite.addTest(JptCommonUtilityClosureTests.suite());
suite.addTest(JptCommonUtilityCollectionTests.suite());
suite.addTest(JptCommonUtilityCommandTests.suite());
suite.addTest(JptCommonUtilityComparatorTests.suite());
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BiClosureToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BiClosureToolsTests.java
new file mode 100644
index 0000000000..da20ae3efa
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BiClosureToolsTests.java
@@ -0,0 +1,726 @@
+/*******************************************************************************
+ * Copyright (c) 2016 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.common.utility.tests.internal.closure;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.command.Command;
+import org.eclipse.jpt.common.utility.command.InterruptibleCommand;
+import org.eclipse.jpt.common.utility.factory.Factory;
+import org.eclipse.jpt.common.utility.factory.InterruptibleFactory;
+import org.eclipse.jpt.common.utility.internal.ClassTools;
+import org.eclipse.jpt.common.utility.internal.closure.BiClosureAdapter;
+import org.eclipse.jpt.common.utility.internal.closure.BiClosureTools;
+import org.eclipse.jpt.common.utility.internal.closure.BiClosureWrapper;
+import org.eclipse.jpt.common.utility.internal.closure.InterruptibleBiClosureAdapter;
+import org.eclipse.jpt.common.utility.internal.closure.InterruptibleBiClosureWrapper;
+import org.eclipse.jpt.common.utility.internal.closure.ThreadLocalBiClosure;
+import org.eclipse.jpt.common.utility.internal.closure.ThreadLocalInterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.internal.exception.CollectingExceptionHandler;
+import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
+import org.eclipse.jpt.common.utility.internal.reference.SimpleIntReference;
+import org.eclipse.jpt.common.utility.reference.ModifiableIntReference;
+import org.eclipse.jpt.common.utility.tests.internal.TestTools;
+import org.eclipse.jpt.common.utility.tests.internal.closure.ClosureToolsTests.LocalCommand;
+import org.eclipse.jpt.common.utility.tests.internal.closure.ClosureToolsTests.LocalFactory;
+import org.eclipse.jpt.common.utility.tests.internal.closure.ClosureToolsTests.LocalInterruptibleCommand;
+import org.eclipse.jpt.common.utility.tests.internal.closure.ClosureToolsTests.LocalInterruptibleFactory;
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class BiClosureToolsTests
+ extends TestCase
+{
+ public BiClosureToolsTests(String name) {
+ super(name);
+ }
+
+ public void testAdaptCommand_execute() {
+ LocalCommand command = new LocalCommand();
+ BiClosure<Boolean, Boolean> closure = BiClosureTools.adapt(command);
+ closure.execute(Boolean.FALSE, Boolean.TRUE);
+ assertEquals(1, command.count);
+ }
+
+ public void testAdaptCommand_toString() {
+ LocalCommand command = new LocalCommand();
+ BiClosure<Boolean, Boolean> closure = BiClosureTools.adapt(command);
+ assertTrue(closure.toString().indexOf("CommandBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalCommand") != -1);
+ }
+
+ public void testAdaptCommand_NPE() {
+ boolean exCaught = false;
+ try {
+ BiClosure<Boolean, Boolean> closure = BiClosureTools.adapt((Command) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptInterruptibleCommand_execute() throws Exception {
+ LocalInterruptibleCommand command = new LocalInterruptibleCommand();
+ InterruptibleBiClosure<Boolean, Boolean> closure = BiClosureTools.adapt(command);
+ closure.execute(Boolean.FALSE, Boolean.TRUE);
+ assertEquals(1, command.count);
+ }
+
+ public void testAdaptInterruptibleCommand_toString() throws Exception {
+ LocalInterruptibleCommand command = new LocalInterruptibleCommand();
+ InterruptibleBiClosure<Boolean, Boolean> closure = BiClosureTools.adapt(command);
+ assertTrue(closure.toString().indexOf("InterruptibleCommandBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleCommand") != -1);
+ }
+
+ public void testAdaptInterruptibleCommand_NPE() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Boolean, Boolean> closure = BiClosureTools.adapt((InterruptibleCommand) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptFactory_execute() throws Exception {
+ LocalFactory<Boolean> factory = new LocalFactory<>();
+ BiClosure<Boolean, Boolean> closure = BiClosureTools.adapt(factory);
+ closure.execute(Boolean.FALSE, Boolean.TRUE);
+ assertEquals(1, factory.count);
+ }
+
+ public void testAdaptFactory_toString() throws Exception {
+ LocalFactory<Boolean> factory = new LocalFactory<>();
+ BiClosure<Boolean, Boolean> closure = BiClosureTools.adapt(factory);
+ assertTrue(closure.toString().indexOf("FactoryBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalFactory") != -1);
+ }
+
+ public void testAdaptFactory_NPE() {
+ boolean exCaught = false;
+ try {
+ BiClosure<Boolean, Boolean> closure = BiClosureTools.adapt((Factory<Boolean>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptInterruptibleFactory_execute() throws Exception {
+ LocalInterruptibleFactory<Boolean> factory = new LocalInterruptibleFactory<>();
+ InterruptibleBiClosure<Boolean, Boolean> closure = BiClosureTools.adapt(factory);
+ closure.execute(Boolean.FALSE, Boolean.TRUE);
+ assertEquals(1, factory.count);
+ }
+
+ public void testAdaptInterruptibleFactory_toString() throws Exception {
+ LocalInterruptibleFactory<Boolean> factory = new LocalInterruptibleFactory<>();
+ InterruptibleBiClosure<Boolean, Boolean> closure = BiClosureTools.adapt(factory);
+ assertTrue(closure.toString().indexOf("InterruptibleFactoryBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleFactory") != -1);
+ }
+
+ public void testAdaptInterruptibleFactory_NPE() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Boolean, Boolean> closure = BiClosureTools.adapt((InterruptibleFactory<Boolean>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testNullBiClosure_execute() throws Exception {
+ BiClosure<Boolean, Object> closure = BiClosureTools.nullBiClosure();
+ closure.execute(Boolean.FALSE, null);
+ assertNotNull(closure);
+ }
+
+ public void testNullBiClosure_toString() throws Exception {
+ BiClosure<Boolean, Object> closure = BiClosureTools.nullBiClosure();
+ assertTrue(closure.toString().indexOf("NullBiClosure") != -1);
+ }
+
+ public void testNullBiClosure_serialization() throws Exception {
+ BiClosure<Boolean, Object> closure = BiClosureTools.nullBiClosure();
+ Object clone = TestTools.serialize(closure);
+ assertSame(closure, clone);
+ }
+
+ public void testThreadLocalBiClosure_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ final ThreadLocalBiClosure<Integer, Integer> closure = BiClosureTools.threadLocalBiClosure();
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(0, ref.getValue());
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ closure.set(new BiClosureAdapter<Integer, Integer>() {
+ @Override
+ public void execute(Integer integer1, Integer integer2) {
+ ref.add(integer1.intValue());
+ ref.add(integer2.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ }
+ };
+ Thread thread = new Thread(r);
+ thread.start();
+ thread.join();
+ assertEquals(5, ref.getValue());
+ }
+
+ public void testThreadLocalBiClosure_toString() {
+ ThreadLocalBiClosure<Boolean, Boolean> closure = BiClosureTools.threadLocalBiClosure();
+ assertTrue(closure.toString().indexOf("NullBiClosure") != -1);
+ }
+
+ public void testThreadLocalBiClosure_NPE() {
+ boolean exCaught = false;
+ try {
+ ThreadLocalBiClosure<Boolean, Boolean> closure = BiClosureTools.threadLocalBiClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testThreadLocalInterruptibleBiClosure_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ final ThreadLocalInterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.threadLocalInterruptibleBiClosure();
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(0, ref.getValue());
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ closure.set(new BiClosureAdapter<Integer, Integer>() {
+ @Override
+ public void execute(Integer integer1, Integer integer2) {
+ ref.add(integer1.intValue());
+ ref.add(integer2.intValue());
+ }
+ });
+ try {
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ } catch (InterruptedException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ };
+ Thread thread = new Thread(r);
+ thread.start();
+ thread.join();
+ assertEquals(5, ref.getValue());
+ }
+
+ public void testThreadLocalInterruptibleBiClosure_toString() {
+ ThreadLocalInterruptibleBiClosure<Boolean, Boolean> closure = BiClosureTools.threadLocalInterruptibleBiClosure();
+ assertTrue(closure.toString().indexOf("NullBiClosure") != -1);
+ }
+
+ public void testThreadLocalInterruptibleBiClosure_NPE() {
+ boolean exCaught = false;
+ try {
+ ThreadLocalInterruptibleBiClosure<Boolean, Boolean> closure = BiClosureTools.threadLocalInterruptibleBiClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrap_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ BiClosureWrapper<Integer, Integer> closure = BiClosureTools.wrap(new BiClosureAdapter<Integer, Integer>() {
+ @Override
+ public void execute(Integer argument1, Integer argument2) {
+ ref.add(argument1.intValue());
+ ref.add(argument2.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, ref.getValue());
+ closure.setClosure(new BiClosureAdapter<Integer, Integer>() {
+ @Override
+ public void execute(Integer argument1, Integer argument2) {
+ ref.add(2 * argument1.intValue());
+ ref.add(2 * argument2.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(15, ref.getValue());
+ }
+
+ public void testWrap_toString() {
+ BiClosureWrapper<Integer, Integer> closure = BiClosureTools.wrap(new BiClosureAdapter<Integer, Integer>() {
+ @Override
+ public void execute(Integer argument1, Integer argument2) {
+ // NOP
+ }
+ });
+ assertTrue(closure.toString().indexOf("BiClosureWrapper") != -1);
+ }
+
+ public void testWrap_NPE1() {
+ boolean exCaught = false;
+ try {
+ BiClosure<Integer, Integer> closure = BiClosureTools.wrap((BiClosure<Integer, Integer>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrap_NPE2() {
+ BiClosureWrapper<Integer, Integer> closure = BiClosureTools.wrap(new BiClosureAdapter<Integer, Integer>());
+ boolean exCaught = false;
+ try {
+ closure.setClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrapInterruptible_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ InterruptibleBiClosureWrapper<Integer, Integer> closure = BiClosureTools.wrap(new InterruptibleBiClosureAdapter<Integer, Integer>() {
+ @Override
+ public void execute(Integer argument1, Integer argument2) throws InterruptedException {
+ ref.add(argument1.intValue());
+ ref.add(argument2.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, ref.getValue());
+ closure.setClosure(new InterruptibleBiClosureAdapter<Integer, Integer>() {
+ @Override
+ public void execute(Integer argument1, Integer argument2) throws InterruptedException {
+ ref.add(2 * argument1.intValue());
+ ref.add(2 * argument2.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(15, ref.getValue());
+ }
+
+ public void testWrapInterruptible_toString() {
+ InterruptibleBiClosureWrapper<Integer, Integer> closure = BiClosureTools.wrap(new InterruptibleBiClosureAdapter<Integer, Integer>() {
+ @Override
+ public void execute(Integer argument1, Integer argument2) throws InterruptedException {
+ // NOP
+ }
+ });
+ assertTrue(closure.toString().indexOf("InterruptibleBiClosureWrapper") != -1);
+ }
+
+ public void testWrapInterruptible_NPE1() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.wrap((InterruptibleBiClosure<Integer, Integer>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrapInterruptible_NPE2() {
+ InterruptibleBiClosureWrapper<Integer, Integer> closure = BiClosureTools.wrap(new InterruptibleBiClosureAdapter<Integer, Integer>());
+ boolean exCaught = false;
+ try {
+ closure.setClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSafeClosure_execute() throws Exception {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ BiClosure<Integer, Integer> closure2 = BiClosureTools.safeBiClosure(closure1);
+ closure2.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, closure1.count);
+ CollectingExceptionHandler handler = new CollectingExceptionHandler();
+ closure2 = BiClosureTools.safeBiClosure(closure1, handler);
+ closure2.execute(null, null);
+ assertEquals(1, IterableTools.size(handler.getExceptions()));
+ }
+
+ public void testSafeBiClosure_toString() {
+ BiClosure<Integer, Integer> closure = BiClosureTools.safeBiClosure(new LocalBiClosure());
+ assertTrue(closure.toString().indexOf("SafeBiClosureWrapper") != -1);
+ assertTrue(closure.toString().indexOf("LocalBiClosure") != -1);
+ }
+
+ public void testSafeBiClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ BiClosure<Integer, Integer> closure = BiClosureTools.safeBiClosure((BiClosure<Integer, Integer>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSafeBiClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ BiClosure<Integer, Integer> closure = BiClosureTools.safeBiClosure(new LocalBiClosure(), null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSafeInterruptibleClosure_execute() throws Exception {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ InterruptibleBiClosure<Integer, Integer> closure2 = BiClosureTools.safeBiClosure(closure1);
+ closure2.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, closure1.count);
+ CollectingExceptionHandler handler = new CollectingExceptionHandler();
+ closure2 = BiClosureTools.safeBiClosure(closure1, handler);
+ closure2.execute(null, null);
+ assertEquals(1, IterableTools.size(handler.getExceptions()));
+ }
+
+ public void testSafeInterruptibleBiClosure_toString() {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.safeBiClosure(new LocalInterruptibleBiClosure());
+ assertTrue(closure.toString().indexOf("SafeInterruptibleBiClosureWrapper") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleBiClosure") != -1);
+ }
+
+ public void testSafeInterruptibleBiClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.safeBiClosure((InterruptibleBiClosure<Integer, Integer>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSafeInterruptibleBiClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.safeBiClosure(new LocalInterruptibleBiClosure(), null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCompositeClosure_execute1() throws Exception {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ LocalBiClosure closure2 = new LocalBiClosure();
+ BiClosure<Integer, Integer> closure = BiClosureTools.compositeBiClosure(closure1, closure2);
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, closure1.count);
+ assertEquals(5, closure2.count);
+ @SuppressWarnings("unchecked")
+ BiClosure<Integer, Integer>[] closureArray = new BiClosure[0];
+ closure = BiClosureTools.compositeBiClosure(closureArray);
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ }
+
+ public void testCompositeBiClosure_execute2() throws Exception {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ LocalBiClosure closure2 = new LocalBiClosure();
+ ArrayList<LocalBiClosure> list = new ArrayList<>();
+ list.add(closure1);
+ list.add(closure2);
+ BiClosure<Integer, Integer> closure = BiClosureTools.compositeBiClosure(list);
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, closure1.count);
+ assertEquals(5, closure2.count);
+ list.clear();
+ closure = BiClosureTools.compositeBiClosure(list);
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ }
+
+ public void testCompositeBiClosure_toString() {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ LocalBiClosure closure2 = new LocalBiClosure();
+ BiClosure<Integer, Integer> closure = BiClosureTools.compositeBiClosure(closure1, closure2);
+ assertTrue(closure.toString().indexOf("CompositeBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalBiClosure") != -1);
+ }
+
+ public void testCompositeBiClosure_NPE1() {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ boolean exCaught = false;
+ try {
+ BiClosure<Integer, Integer> closure = BiClosureTools.compositeBiClosure(closure1, null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCompositeBiClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ BiClosure<Integer, Integer> closure = BiClosureTools.compositeBiClosure((Iterable<BiClosure<Integer, Integer>>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCompositeInterruptibleClosure_execute1() throws Exception {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ LocalInterruptibleBiClosure closure2 = new LocalInterruptibleBiClosure();
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.compositeInterruptibleBiClosure(closure1, closure2);
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, closure1.count);
+ assertEquals(5, closure2.count);
+ @SuppressWarnings("unchecked")
+ InterruptibleBiClosure<Integer, Integer>[] closureArray = new InterruptibleBiClosure[0];
+ closure = BiClosureTools.compositeInterruptibleBiClosure(closureArray);
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ }
+
+ public void testCompositeInterruptibleBiClosure_execute2() throws Exception {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ LocalInterruptibleBiClosure closure2 = new LocalInterruptibleBiClosure();
+ ArrayList<LocalInterruptibleBiClosure> list = new ArrayList<>();
+ list.add(closure1);
+ list.add(closure2);
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.compositeInterruptibleBiClosure(list);
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, closure1.count);
+ assertEquals(5, closure2.count);
+ list.clear();
+ closure = BiClosureTools.compositeInterruptibleBiClosure(list);
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ }
+
+ public void testCompositeInterruptibleBiClosure_toString() {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ LocalInterruptibleBiClosure closure2 = new LocalInterruptibleBiClosure();
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.compositeInterruptibleBiClosure(closure1, closure2);
+ assertTrue(closure.toString().indexOf("CompositeInterruptibleBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleBiClosure") != -1);
+ }
+
+ public void testCompositeInterruptibleBiClosure_NPE1() {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.compositeInterruptibleBiClosure(closure1, null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCompositeInterruptibleBiClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.compositeInterruptibleBiClosure((Iterable<InterruptibleBiClosure<Integer, Integer>>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testDisabledBiClosure_execute() {
+ BiClosure<Integer, Integer> closure = BiClosureTools.disabledBiClosure();
+ boolean exCaught = false;
+ try {
+ closure.execute(null, null);
+ fail();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testDisabledBiClosure_toString() {
+ BiClosure<Integer, Integer> closure = BiClosureTools.disabledBiClosure();
+ assertTrue(closure.toString().indexOf("DisabledBiClosure") != -1);
+ }
+
+ public void testDisabledBiClosure_serialization() throws Exception {
+ BiClosure<Integer, Integer> closure = BiClosureTools.disabledBiClosure();
+ assertSame(closure, TestTools.serialize(closure));
+ }
+
+ public void testInterruptedBiClosure_execute() {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.interruptedBiClosure();
+ boolean exCaught = false;
+ try {
+ closure.execute(null, null);
+ fail();
+ } catch (InterruptedException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testInterruptedBiClosure_toString() {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.interruptedBiClosure();
+ assertTrue(closure.toString().indexOf("InterruptedBiClosure") != -1);
+ }
+
+ public void testInterruptedBiClosure_serialization() throws Exception {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.interruptedBiClosure();
+ assertSame(closure, TestTools.serialize(closure));
+ }
+
+ public void testRepeatingBiClosure_execute1() throws Exception {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ BiClosure<Integer, Integer> closure2 = BiClosureTools.repeatingBiClosure(closure1, 3);
+ closure2.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(15, closure1.count);
+ }
+
+ public void testRepeatingBiClosure_execute2() throws Exception {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ BiClosure<Integer, Integer> closure2 = BiClosureTools.repeatingBiClosure(closure1, 0);
+ closure2.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(0, closure1.count);
+ }
+
+ public void testRepeatingBiClosure_toString() {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ BiClosure<Integer, Integer> closure2 = BiClosureTools.repeatingBiClosure(closure1, 3);
+ assertTrue(closure2.toString().indexOf("RepeatingBiClosure") != -1);
+ assertTrue(closure2.toString().indexOf("LocalBiClosure") != -1);
+ }
+
+ public void testRepeatingBiClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ BiClosure<Integer, Integer> closure = BiClosureTools.repeatingBiClosure((BiClosure<Integer, Integer>) null, 3);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testRepeatingBiClosure_NPE2() {
+ LocalBiClosure closure1 = new LocalBiClosure();
+ boolean exCaught = false;
+ try {
+ BiClosure<Integer, Integer> closure2 = BiClosureTools.repeatingBiClosure(closure1, -3);
+ fail("bogus: " + closure2);
+ } catch (IndexOutOfBoundsException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testRepeatingInterruptibleBiClosure_execute1() throws Exception {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ InterruptibleBiClosure<Integer, Integer> closure2 = BiClosureTools.repeatingBiClosure(closure1, 3);
+ closure2.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(15, closure1.count);
+ }
+
+ public void testRepeatingInterruptibleBiClosure_execute2() throws Exception {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ InterruptibleBiClosure<Integer, Integer> closure2 = BiClosureTools.repeatingBiClosure(closure1, 0);
+ closure2.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(0, closure1.count);
+ }
+
+ public void testRepeatingInterruptibleBiClosure_toString() {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ InterruptibleBiClosure<Integer, Integer> closure2 = BiClosureTools.repeatingBiClosure(closure1, 3);
+ assertTrue(closure2.toString().indexOf("RepeatingInterruptibleBiClosure") != -1);
+ assertTrue(closure2.toString().indexOf("LocalInterruptibleBiClosure") != -1);
+ }
+
+ public void testRepeatingInterruptibleBiClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Integer, Integer> closure = BiClosureTools.repeatingBiClosure((InterruptibleBiClosure<Integer, Integer>) null, 3);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testRepeatingInterruptibleBiClosure_NPE2() {
+ LocalInterruptibleBiClosure closure1 = new LocalInterruptibleBiClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure<Integer, Integer> closure2 = BiClosureTools.repeatingBiClosure(closure1, -3);
+ fail("bogus: " + closure2);
+ } catch (IndexOutOfBoundsException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testConstructor() {
+ boolean exCaught = false;
+ try {
+ Object at = ClassTools.newInstance(BiClosureTools.class);
+ fail("bogus: " + at);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof InvocationTargetException) {
+ if (ex.getCause().getCause() instanceof UnsupportedOperationException) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ // ********** test classes **********
+
+ public static class LocalBiClosure
+ extends BiClosureAdapter<Integer, Integer>
+ {
+ public int count = 0;
+
+ @Override
+ public void execute(Integer argument1, Integer argument2) {
+ this.count += argument1.intValue(); // possible NPE
+ this.count += argument2.intValue(); // possible NPE
+ }
+ }
+
+ public static class LocalInterruptibleBiClosure
+ extends InterruptibleBiClosureAdapter<Integer, Integer>
+ {
+ public int count = 0;
+
+ @Override
+ public void execute(Integer argument1, Integer argument2) throws InterruptedException {
+ this.count += argument1.intValue(); // possible NPE
+ this.count += argument2.intValue(); // possible NPE
+ }
+ }
+}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/ClosureToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/ClosureToolsTests.java
new file mode 100644
index 0000000000..242d28fa84
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/ClosureToolsTests.java
@@ -0,0 +1,1854 @@
+/*******************************************************************************
+ * Copyright (c) 2016 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.common.utility.tests.internal.closure;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.List;
+import org.eclipse.jpt.common.utility.Association;
+import org.eclipse.jpt.common.utility.closure.Closure;
+import org.eclipse.jpt.common.utility.closure.InterruptibleClosure;
+import org.eclipse.jpt.common.utility.command.Command;
+import org.eclipse.jpt.common.utility.command.InterruptibleCommand;
+import org.eclipse.jpt.common.utility.factory.Factory;
+import org.eclipse.jpt.common.utility.factory.InterruptibleFactory;
+import org.eclipse.jpt.common.utility.internal.ClassTools;
+import org.eclipse.jpt.common.utility.internal.SimpleAssociation;
+import org.eclipse.jpt.common.utility.internal.closure.ClosureAdapter;
+import org.eclipse.jpt.common.utility.internal.closure.ClosureTools;
+import org.eclipse.jpt.common.utility.internal.closure.ClosureWrapper;
+import org.eclipse.jpt.common.utility.internal.closure.InterruptibleClosureAdapter;
+import org.eclipse.jpt.common.utility.internal.closure.InterruptibleClosureWrapper;
+import org.eclipse.jpt.common.utility.internal.closure.ThreadLocalClosure;
+import org.eclipse.jpt.common.utility.internal.closure.ThreadLocalInterruptibleClosure;
+import org.eclipse.jpt.common.utility.internal.command.CommandAdapter;
+import org.eclipse.jpt.common.utility.internal.command.CommandTools;
+import org.eclipse.jpt.common.utility.internal.command.InterruptibleCommandAdapter;
+import org.eclipse.jpt.common.utility.internal.exception.CollectingExceptionHandler;
+import org.eclipse.jpt.common.utility.internal.factory.FactoryAdapter;
+import org.eclipse.jpt.common.utility.internal.factory.InterruptibleFactoryAdapter;
+import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
+import org.eclipse.jpt.common.utility.internal.predicate.PredicateAdapter;
+import org.eclipse.jpt.common.utility.internal.predicate.PredicateTools;
+import org.eclipse.jpt.common.utility.internal.reference.SimpleIntReference;
+import org.eclipse.jpt.common.utility.internal.transformer.InterruptibleTransformerAdapter;
+import org.eclipse.jpt.common.utility.internal.transformer.TransformerAdapter;
+import org.eclipse.jpt.common.utility.predicate.Predicate;
+import org.eclipse.jpt.common.utility.reference.ModifiableIntReference;
+import org.eclipse.jpt.common.utility.tests.internal.TestTools;
+import org.eclipse.jpt.common.utility.transformer.InterruptibleTransformer;
+import org.eclipse.jpt.common.utility.transformer.Transformer;
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class ClosureToolsTests
+ extends TestCase
+{
+ public ClosureToolsTests(String name) {
+ super(name);
+ }
+
+ public void testAdaptCommand_execute() {
+ LocalCommand command = new LocalCommand();
+ Closure<Boolean> closure = ClosureTools.adapt(command);
+ closure.execute(Boolean.FALSE);
+ assertEquals(1, command.count);
+ }
+
+ public void testAdaptCommand_toString() {
+ LocalCommand command = new LocalCommand();
+ Closure<Boolean> closure = ClosureTools.adapt(command);
+ assertTrue(closure.toString().indexOf("CommandClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalCommand") != -1);
+ }
+
+ public void testAdaptCommand_NPE() {
+ boolean exCaught = false;
+ try {
+ Closure<Boolean> closure = ClosureTools.adapt((Command) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptInterruptibleCommand_execute() throws Exception {
+ LocalInterruptibleCommand command = new LocalInterruptibleCommand();
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt(command);
+ closure.execute(Boolean.FALSE);
+ assertEquals(1, command.count);
+ }
+
+ public void testAdaptInterruptibleCommand_toString() {
+ LocalInterruptibleCommand command = new LocalInterruptibleCommand();
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt(command);
+ assertTrue(closure.toString().indexOf("InterruptibleCommandClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleCommand") != -1);
+ }
+
+ public void testAdaptInterruptibleCommand_NPE() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt((InterruptibleCommand) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptFactory_execute() {
+ LocalFactory<?> factory = new LocalFactory<>();
+ Closure<Boolean> closure = ClosureTools.adapt(factory);
+ closure.execute(Boolean.FALSE);
+ assertEquals(1, factory.count);
+ }
+
+ public void testAdaptFactory_toString() {
+ LocalFactory<?> factory = new LocalFactory<>();
+ Closure<Boolean> closure = ClosureTools.adapt(factory);
+ assertTrue(closure.toString().indexOf("FactoryClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalFactory") != -1);
+ }
+
+ public void testAdaptFactory_NPE() {
+ boolean exCaught = false;
+ try {
+ Closure<Boolean> closure = ClosureTools.adapt((Factory<?>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptInterruptibleFactory_execute() throws Exception {
+ LocalInterruptibleFactory<?> factory = new LocalInterruptibleFactory<>();
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt(factory);
+ closure.execute(Boolean.FALSE);
+ assertEquals(1, factory.count);
+ }
+
+ public void testAdaptInterruptibleFactory_toString() {
+ LocalInterruptibleFactory<?> factory = new LocalInterruptibleFactory<>();
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt(factory);
+ assertTrue(closure.toString().indexOf("InterruptibleFactoryClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleFactory") != -1);
+ }
+
+ public void testAdaptInterruptibleFactory_NPE() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt((InterruptibleFactory<?>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptTransformer_execute() {
+ LocalTransformer<Boolean, ?> transformer = new LocalTransformer<>();
+ Closure<Boolean> closure = ClosureTools.adapt(transformer);
+ closure.execute(Boolean.FALSE);
+ assertEquals(1, transformer.count);
+ }
+
+ public void testAdaptTransformer_toString() {
+ LocalTransformer<Boolean, ?> transformer = new LocalTransformer<>();
+ Closure<Boolean> closure = ClosureTools.adapt(transformer);
+ assertTrue(closure.toString().indexOf("TransformerClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalTransformer") != -1);
+ }
+
+ public void testAdaptTransformer_NPE() {
+ boolean exCaught = false;
+ try {
+ Closure<Boolean> closure = ClosureTools.adapt((Transformer<Boolean, ?>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptInterruptibleTransformer_execute() throws Exception {
+ LocalInterruptibleTransformer<Boolean, ?> transformer = new LocalInterruptibleTransformer<>();
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt(transformer);
+ closure.execute(Boolean.FALSE);
+ assertEquals(1, transformer.count);
+ }
+
+ public void testAdaptInterruptibleTransformer_toString() {
+ LocalInterruptibleTransformer<Boolean, ?> transformer = new LocalInterruptibleTransformer<>();
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt(transformer);
+ assertTrue(closure.toString().indexOf("InterruptibleTransformerClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleTransformer") != -1);
+ }
+
+ public void testAdaptInterruptibleTransformer_NPE() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Boolean> closure = ClosureTools.adapt((InterruptibleTransformer<Boolean, ?>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testNullClosure_execute() throws Exception {
+ Closure<Boolean> closure = ClosureTools.nullClosure();
+ closure.execute(Boolean.FALSE);
+ assertNotNull(closure);
+ }
+
+ public void testNullClosure_toString() throws Exception {
+ Closure<Boolean> closure = ClosureTools.nullClosure();
+ assertTrue(closure.toString().indexOf("NullClosure") != -1);
+ }
+
+ public void testNullClosure_serialization() throws Exception {
+ Closure<Boolean> closure = ClosureTools.nullClosure();
+ Object clone = TestTools.serialize(closure);
+ assertSame(closure, clone);
+ }
+
+ public void testThreadLocalClosure_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ final ThreadLocalClosure<Integer> closure = ClosureTools.threadLocalClosure();
+ closure.execute(Integer.valueOf(3));
+ assertEquals(0, ref.getValue());
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ closure.set(new ClosureAdapter<Integer>() {
+ @Override
+ public void execute(Integer integer) {
+ ref.add(integer.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3));
+ }
+ };
+ Thread thread = new Thread(r);
+ thread.start();
+ thread.join();
+ assertEquals(3, ref.getValue());
+ }
+
+ public void testThreadLocalClosure_toString() {
+ ThreadLocalClosure<Boolean> closure = ClosureTools.threadLocalClosure();
+ assertTrue(closure.toString().indexOf("NullClosure") != -1);
+ }
+
+ public void testThreadLocalClosure_NPE() {
+ boolean exCaught = false;
+ try {
+ ThreadLocalClosure<Boolean> closure = ClosureTools.threadLocalClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testThreadLocalInterruptibleClosure_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ final ThreadLocalInterruptibleClosure<Integer> closure = ClosureTools.threadLocalInterruptibleClosure();
+ closure.execute(Integer.valueOf(3));
+ assertEquals(0, ref.getValue());
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ closure.set(new ClosureAdapter<Integer>() {
+ @Override
+ public void execute(Integer integer) {
+ ref.add(integer.intValue());
+ }
+ });
+ try {
+ closure.execute(Integer.valueOf(3));
+ } catch (InterruptedException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ };
+ Thread thread = new Thread(r);
+ thread.start();
+ thread.join();
+ assertEquals(3, ref.getValue());
+ }
+
+ public void testThreadLocalInterruptibleClosure_toString() {
+ ThreadLocalInterruptibleClosure<Boolean> closure = ClosureTools.threadLocalInterruptibleClosure();
+ assertTrue(closure.toString().indexOf("NullClosure") != -1);
+ }
+
+ public void testThreadLocalInterruptibleClosure_NPE() {
+ boolean exCaught = false;
+ try {
+ ThreadLocalInterruptibleClosure<Boolean> closure = ClosureTools.threadLocalInterruptibleClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testNullCheck_execute() throws Exception {
+ LocalClosure closure1 = new LocalClosure();
+ Closure<Integer> closure2 = ClosureTools.nullCheck(closure1);
+ closure2.execute(null);
+ assertEquals(0, closure1.count);
+ closure2.execute(Integer.valueOf(3));
+ assertEquals(3, closure1.count);
+ }
+
+ public void testNullCheck_toString() {
+ Closure<Integer> closure1 = new LocalClosure();
+ Closure<Integer> closure2 = ClosureTools.nullCheck(closure1);
+ assertTrue(closure2.toString().indexOf("NullCheckClosure") != -1);
+ assertTrue(closure2.toString().indexOf("LocalClosure") != -1);
+ }
+
+ public void testNullCheck_NPE1() {
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.nullCheck((Closure<Integer>) null, CommandTools.nullCommand());
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testNullCheck_NPE2() {
+ Closure<Integer> closure1 = new LocalClosure();
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure2 = ClosureTools.nullCheck(closure1, null);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testNullCheckInterruptible_execute() throws Exception {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ InterruptibleClosure<Integer> closure2 = ClosureTools.nullCheck(closure1);
+ closure2.execute(null);
+ assertEquals(0, closure1.count);
+ closure2.execute(Integer.valueOf(3));
+ assertEquals(3, closure1.count);
+ }
+
+ public void testNullCheckInterruptible_toString() {
+ InterruptibleClosure<Integer> closure1 = new LocalInterruptibleClosure();
+ InterruptibleClosure<Integer> closure2 = ClosureTools.nullCheck(closure1);
+ assertTrue(closure2.toString().indexOf("NullCheckInterruptibleClosure") != -1);
+ assertTrue(closure2.toString().indexOf("LocalInterruptibleClosure") != -1);
+ }
+
+ public void testNullCheckInterruptible_NPE1() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.nullCheck((InterruptibleClosure<Integer>) null, CommandTools.nullCommand());
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testNullCheckInterruptible_NPE2() {
+ InterruptibleClosure<Integer> closure1 = new LocalInterruptibleClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure2 = ClosureTools.nullCheck(closure1, null);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrap_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ ClosureWrapper<Integer> closure = ClosureTools.wrap(new ClosureAdapter<Integer>() {
+ @Override
+ public void execute(Integer argument) {
+ ref.add(argument.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3));
+ assertEquals(3, ref.getValue());
+ closure.setClosure(new ClosureAdapter<Integer>() {
+ @Override
+ public void execute(Integer argument) {
+ ref.add(2 * argument.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3));
+ assertEquals(9, ref.getValue());
+ }
+
+ public void testWrap_toString() {
+ ClosureWrapper<Integer> closure = ClosureTools.wrap(new ClosureAdapter<Integer>() {
+ @Override
+ public void execute(Integer argument) {
+ // NOP
+ }
+ });
+ assertTrue(closure.toString().indexOf("ClosureWrapper") != -1);
+ }
+
+ public void testWrap_NPE1() {
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.wrap((Closure<Integer>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrap_NPE2() {
+ ClosureWrapper<Integer> closure = ClosureTools.wrap(new LocalClosure());
+ boolean exCaught = false;
+ try {
+ closure.setClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrapInterruptible_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ InterruptibleClosureWrapper<Integer> closure = ClosureTools.wrap(new InterruptibleClosureAdapter<Integer>() {
+ @Override
+ public void execute(Integer argument) throws InterruptedException {
+ ref.add(argument.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3));
+ assertEquals(3, ref.getValue());
+ closure.setClosure(new InterruptibleClosureAdapter<Integer>() {
+ @Override
+ public void execute(Integer argument) throws InterruptedException {
+ ref.add(2 * argument.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3));
+ assertEquals(9, ref.getValue());
+ }
+
+ public void testWrapInterruptible_toString() {
+ InterruptibleClosureWrapper<Integer> closure = ClosureTools.wrap(new InterruptibleClosureAdapter<Integer>() {
+ @Override
+ public void execute(Integer argument) throws InterruptedException {
+ // NOP
+ }
+ });
+ assertTrue(closure.toString().indexOf("InterruptibleClosureWrapper") != -1);
+ }
+
+ public void testWrapInterruptible_NPE1() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.wrap((InterruptibleClosure<Integer>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrapInterruptible_NPE2() {
+ InterruptibleClosureWrapper<Integer> closure = ClosureTools.wrap(new LocalInterruptibleClosure());
+ boolean exCaught = false;
+ try {
+ closure.setClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSafeClosure_execute() throws Exception {
+ LocalClosure closure1 = new LocalClosure();
+ Closure<Integer> closure2 = ClosureTools.safeClosure(closure1);
+ closure2.execute(Integer.valueOf(3));
+ assertEquals(3, closure1.count);
+ CollectingExceptionHandler handler = new CollectingExceptionHandler();
+ closure2 = ClosureTools.safeClosure(closure1, handler);
+ closure2.execute(null);
+ assertEquals(1, IterableTools.size(handler.getExceptions()));
+ }
+
+ public void testSafeClosure_toString() {
+ Closure<Integer> closure = ClosureTools.safeClosure(new LocalClosure());
+ assertTrue(closure.toString().indexOf("SafeClosureWrapper") != -1);
+ assertTrue(closure.toString().indexOf("LocalClosure") != -1);
+ }
+
+ public void testSafeClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.safeClosure((Closure<Integer>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSafeClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.safeClosure(new LocalClosure(), null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSafeInterruptibleClosure_execute() throws Exception {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ InterruptibleClosure<Integer> closure2 = ClosureTools.safeClosure(closure1);
+ closure2.execute(Integer.valueOf(3));
+ assertEquals(3, closure1.count);
+ CollectingExceptionHandler handler = new CollectingExceptionHandler();
+ closure2 = ClosureTools.safeClosure(closure1, handler);
+ closure2.execute(null);
+ assertEquals(1, IterableTools.size(handler.getExceptions()));
+ }
+
+ public void testSafeInterruptibleClosure_toString() {
+ InterruptibleClosure<Integer> closure = ClosureTools.safeClosure(new LocalInterruptibleClosure());
+ assertTrue(closure.toString().indexOf("SafeInterruptibleClosureWrapper") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleClosure") != -1);
+ }
+
+ public void testSafeInterruptibleClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.safeClosure((InterruptibleClosure<Integer>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSafeInterruptibleClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.safeClosure(new LocalInterruptibleClosure(), null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testMethodClosure_execute() throws Exception {
+ Closure<Object> closure = ClosureTools.methodClosure("execute");
+ LocalCommand cmd = new LocalCommand();
+ assertEquals(0, cmd.count);
+ closure.execute(cmd);
+ assertEquals(1, cmd.count);
+ }
+
+ public void testMethodClosure_execute_arg() throws Exception {
+ LocalCommand cmd = new LocalCommand();
+ Closure<Object> closure = ClosureTools.methodClosure("equals", Object.class, cmd);
+ assertEquals(0, cmd.count);
+ closure.execute(cmd);
+ assertEquals(0, cmd.count);
+ }
+
+ public void testMethodClosure_equals() throws Exception {
+ Closure<Object> closure1 = ClosureTools.methodClosure("equals", Object.class, "foo");
+ Closure<Object> closure2 = ClosureTools.methodClosure("equals", Object.class, "foo");
+ assertFalse(closure1.equals(null));
+ assertFalse(closure1.equals("foo"));
+ assertTrue(closure1.equals(closure1));
+ assertTrue(closure1.equals(closure2));
+ closure2 = ClosureTools.methodClosure("equals", Object.class, "bar");
+ assertFalse(closure1.equals(closure2));
+ closure2 = ClosureTools.methodClosure("equals", String.class, "foo");
+ assertFalse(closure1.equals(closure2));
+ closure2 = ClosureTools.methodClosure("toString", Object.class, "foo");
+ assertFalse(closure1.equals(closure2));
+ }
+
+ public void testMethodClosure_hashCode() throws Exception {
+ Closure<Object> closure1 = ClosureTools.methodClosure("equals", Object.class, "foo");
+ Closure<Object> closure2 = ClosureTools.methodClosure("equals", Object.class, "foo");
+ assertEquals(closure1.hashCode(), closure1.hashCode());
+ assertEquals(closure1.hashCode(), closure2.hashCode());
+ }
+
+ public void testMethodClosure_toString() {
+ Closure<Object> closure = ClosureTools.methodClosure("execute");
+ assertTrue(closure.toString().indexOf("MethodClosure") != -1);
+ assertTrue(closure.toString().indexOf("execute()") != -1);
+ }
+
+ public void testMethodClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ Closure<Object> closure = ClosureTools.methodClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testMethodClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ Closure<Object> closure = ClosureTools.methodClosure("execute", (Class[]) null, new Object[0]);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testMethodClosure_NPE2a() {
+ boolean exCaught = false;
+ try {
+ Closure<Object> closure = ClosureTools.methodClosure("execute", new Class[] {Object.class, null}, new Object[2]);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testMethodClosure_NPE3() {
+ boolean exCaught = false;
+ try {
+ Closure<Object> closure = ClosureTools.methodClosure("execute", new Class[0], null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCompositeClosure_execute1() throws Exception {
+ LocalClosure closure1 = new LocalClosure();
+ LocalClosure closure2 = new LocalClosure();
+ Closure<Integer> closure = ClosureTools.compositeClosure(closure1, closure2);
+ closure.execute(Integer.valueOf(3));
+ assertEquals(3, closure1.count);
+ assertEquals(3, closure2.count);
+ @SuppressWarnings("unchecked")
+ Closure<Integer>[] closureArray = new Closure[0];
+ closure = ClosureTools.compositeClosure(closureArray);
+ closure.execute(Integer.valueOf(3));
+ }
+
+ public void testCompositeClosure_execute2() throws Exception {
+ LocalClosure closure1 = new LocalClosure();
+ LocalClosure closure2 = new LocalClosure();
+ ArrayList<LocalClosure> list = new ArrayList<>();
+ list.add(closure1);
+ list.add(closure2);
+ Closure<Integer> closure = ClosureTools.compositeClosure(list);
+ closure.execute(Integer.valueOf(3));
+ assertEquals(3, closure1.count);
+ assertEquals(3, closure2.count);
+ list.clear();
+ closure = ClosureTools.compositeClosure(list);
+ closure.execute(Integer.valueOf(3));
+ }
+
+ public void testCompositeClosure_toString() {
+ LocalClosure closure1 = new LocalClosure();
+ LocalClosure closure2 = new LocalClosure();
+ Closure<Integer> closure = ClosureTools.compositeClosure(closure1, closure2);
+ assertTrue(closure.toString().indexOf("CompositeClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalClosure") != -1);
+ }
+
+ public void testCompositeClosure_NPE1() {
+ LocalClosure closure1 = new LocalClosure();
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.compositeClosure(closure1, null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCompositeClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.compositeClosure((Iterable<Closure<Integer>>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCompositeInterruptibleClosure_execute1() throws Exception {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ InterruptibleClosure<Integer> closure = ClosureTools.compositeInterruptibleClosure(closure1, closure2);
+ closure.execute(Integer.valueOf(3));
+ assertEquals(3, closure1.count);
+ assertEquals(3, closure2.count);
+ @SuppressWarnings("unchecked")
+ InterruptibleClosure<Integer>[] closureArray = new InterruptibleClosure[0];
+ closure = ClosureTools.compositeInterruptibleClosure(closureArray);
+ closure.execute(Integer.valueOf(3));
+ }
+
+ public void testCompositeInterruptibleClosure_execute2() throws Exception {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ ArrayList<LocalInterruptibleClosure> list = new ArrayList<>();
+ list.add(closure1);
+ list.add(closure2);
+ InterruptibleClosure<Integer> closure = ClosureTools.compositeInterruptibleClosure(list);
+ closure.execute(Integer.valueOf(3));
+ assertEquals(3, closure1.count);
+ assertEquals(3, closure2.count);
+ list.clear();
+ closure = ClosureTools.compositeInterruptibleClosure(list);
+ closure.execute(Integer.valueOf(3));
+ }
+
+ public void testCompositeInterruptibleClosure_toString() {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ InterruptibleClosure<Integer> closure = ClosureTools.compositeInterruptibleClosure(closure1, closure2);
+ assertTrue(closure.toString().indexOf("CompositeInterruptibleClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleClosure") != -1);
+ }
+
+ public void testCompositeInterruptibleClosure_NPE1() {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.compositeInterruptibleClosure(closure1, null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCompositeInterruptibleClosure_NPE2() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.compositeInterruptibleClosure((Iterable<InterruptibleClosure<Integer>>) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testConditionalClosure_execute1() throws Exception {
+ LocalClosure closure1 = new LocalClosure();
+ Predicate<Integer> predicate = new PredicateAdapter<Integer>() {
+ @Override
+ public boolean evaluate(Integer variable) {
+ return variable.intValue() > 3;
+ }
+ };
+ Closure<Integer> closure = ClosureTools.conditionalClosure(predicate, closure1);
+ closure.execute(Integer.valueOf(3));
+ assertEquals(0, closure1.count);
+ closure.execute(Integer.valueOf(4));
+ assertEquals(4, closure1.count);
+ }
+
+ public void testConditionalClosure_execute2() throws Exception {
+ LocalClosure closure1 = new LocalClosure();
+ LocalClosure closure2 = new LocalClosure();
+ Predicate<Integer> predicate = new PredicateAdapter<Integer>() {
+ @Override
+ public boolean evaluate(Integer variable) {
+ return variable.intValue() > 3;
+ }
+ };
+ Closure<Integer> closure = ClosureTools.conditionalClosure(predicate, closure1, closure2);
+ closure.execute(Integer.valueOf(3));
+ assertEquals(0, closure1.count);
+ assertEquals(3, closure2.count);
+ closure.execute(Integer.valueOf(4));
+ assertEquals(4, closure1.count);
+ assertEquals(3, closure2.count);
+ }
+
+ public void testConditionalClosure_toString() {
+ LocalClosure closure1 = new LocalClosure();
+ LocalClosure closure2 = new LocalClosure();
+ Predicate<Integer> predicate = new PredicateAdapter<Integer>() {
+ @Override
+ public boolean evaluate(Integer variable) {
+ return variable.intValue() > 3;
+ }
+ };
+ Closure<Integer> closure = ClosureTools.conditionalClosure(predicate, closure1, closure2);
+ assertTrue(closure.toString().indexOf("ConditionalClosure") != -1);
+ assertTrue(closure.toString().indexOf("Predicate") != -1);
+ }
+
+ public void testConditionalClosure_NPE1() {
+ LocalClosure closure1 = new LocalClosure();
+ LocalClosure closure2 = new LocalClosure();
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.conditionalClosure(null, closure1, closure2);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testConditionalClosure_NPE2() {
+ LocalClosure closure1 = new LocalClosure();
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.conditionalClosure(null, closure1, null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testConditionalClosure_NPE3() {
+ LocalClosure closure2 = new LocalClosure();
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.conditionalClosure(PredicateTools.true_(), null, closure2);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testConditionalInterruptibleClosure_execute1() throws Exception {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ Predicate<Integer> predicate = new PredicateAdapter<Integer>() {
+ @Override
+ public boolean evaluate(Integer variable) {
+ return variable.intValue() > 3;
+ }
+ };
+ InterruptibleClosure<Integer> closure = ClosureTools.conditionalClosure(predicate, closure1);
+ closure.execute(Integer.valueOf(3));
+ assertEquals(0, closure1.count);
+ closure.execute(Integer.valueOf(4));
+ assertEquals(4, closure1.count);
+ }
+
+ public void testConditionalInterruptibleClosure_execute2() throws Exception {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ Predicate<Integer> predicate = new PredicateAdapter<Integer>() {
+ @Override
+ public boolean evaluate(Integer variable) {
+ return variable.intValue() > 3;
+ }
+ };
+ InterruptibleClosure<Integer> closure = ClosureTools.conditionalClosure(predicate, closure1, closure2);
+ closure.execute(Integer.valueOf(3));
+ assertEquals(0, closure1.count);
+ assertEquals(3, closure2.count);
+ closure.execute(Integer.valueOf(4));
+ assertEquals(4, closure1.count);
+ assertEquals(3, closure2.count);
+ }
+
+ public void testConditionalInterruptibleClosure_toString() {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ Predicate<Integer> predicate = new PredicateAdapter<Integer>() {
+ @Override
+ public boolean evaluate(Integer variable) {
+ return variable.intValue() > 3;
+ }
+ };
+ InterruptibleClosure<Integer> closure = ClosureTools.conditionalClosure(predicate, closure1, closure2);
+ assertTrue(closure.toString().indexOf("ConditionalInterruptibleClosure") != -1);
+ assertTrue(closure.toString().indexOf("Predicate") != -1);
+ }
+
+ public void testConditionalInterruptibleClosure_NPE1() {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.conditionalClosure(null, closure1, closure2);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testConditionalInterruptibleClosure_NPE2() {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.conditionalClosure(null, closure1, null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testConditionalInterruptibleClosure_NPE3() {
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.conditionalClosure(PredicateTools.true_(), null, closure2);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSwitchClosure_execute1() throws Exception {
+ List<Association<Predicate<Integer>, LocalClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalClosure closure1 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalClosure closure2 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalClosure closure3 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+
+ Closure<Integer> closure = ClosureTools.switchClosure(list);
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(0));
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(4));
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(1));
+ assertEquals(1, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(2));
+ assertEquals(1, closure1.count);
+ assertEquals(2, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(2));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(3));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(3, closure3.count);
+ }
+
+ public void testSwitchClosure_execute2() throws Exception {
+ List<Association<Predicate<Integer>, LocalClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalClosure closure1 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalClosure closure2 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalClosure closure3 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+
+ LocalClosure closureX = new LocalClosure();
+
+ Closure<Integer> closure = ClosureTools.switchClosure(list, closureX);
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(0, closureX.count);
+
+ closure.execute(Integer.valueOf(0));
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(0, closureX.count);
+
+ closure.execute(Integer.valueOf(4));
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(1));
+ assertEquals(1, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(2));
+ assertEquals(1, closure1.count);
+ assertEquals(2, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(2));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(3));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(3, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(-7));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(3, closure3.count);
+ assertEquals(-3, closureX.count);
+ }
+
+ public void testSwitchClosure_toString() {
+ List<Association<Predicate<Integer>, LocalClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalClosure closure1 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalClosure closure2 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalClosure closure3 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+
+ LocalClosure closureX = new LocalClosure();
+
+ Closure<Integer> closure = ClosureTools.switchClosure(list, closureX);
+
+ assertTrue(closure.toString().indexOf("SwitchClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalClosure") != -1);
+ }
+
+ public void testSwitchClosure_NPE1() {
+ List<Association<Predicate<Integer>, LocalClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalClosure closure1 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalClosure closure2 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalClosure closure3 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.switchClosure(list, null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSwitchClosure_NPE2() {
+ List<Association<Predicate<Integer>, LocalClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalClosure closure1 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalClosure closure2 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalClosure closure3 = new LocalClosure();
+ Association<Predicate<Integer>, LocalClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+ list.add(null);
+
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.switchClosure(list);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSwitchClosure_NPE3() {
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.switchClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSwitchInterruptibleClosure_execute1() throws Exception {
+ List<Association<Predicate<Integer>, LocalInterruptibleClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalInterruptibleClosure closure3 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+
+ InterruptibleClosure<Integer> closure = ClosureTools.switchInterruptibleClosure(list);
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(0));
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(4));
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(1));
+ assertEquals(1, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(2));
+ assertEquals(1, closure1.count);
+ assertEquals(2, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(2));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(0, closure3.count);
+
+ closure.execute(Integer.valueOf(3));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(3, closure3.count);
+ }
+
+ public void testSwitchInterruptibleClosure_execute2() throws Exception {
+ List<Association<Predicate<Integer>, LocalInterruptibleClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalInterruptibleClosure closure3 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+
+ LocalInterruptibleClosure closureX = new LocalInterruptibleClosure();
+
+ InterruptibleClosure<Integer> closure = ClosureTools.switchInterruptibleClosure(list, closureX);
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(0, closureX.count);
+
+ closure.execute(Integer.valueOf(0));
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(0, closureX.count);
+
+ closure.execute(Integer.valueOf(4));
+ assertEquals(0, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(1));
+ assertEquals(1, closure1.count);
+ assertEquals(0, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(2));
+ assertEquals(1, closure1.count);
+ assertEquals(2, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(2));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(0, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(3));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(3, closure3.count);
+ assertEquals(4, closureX.count);
+
+ closure.execute(Integer.valueOf(-7));
+ assertEquals(1, closure1.count);
+ assertEquals(4, closure2.count);
+ assertEquals(3, closure3.count);
+ assertEquals(-3, closureX.count);
+ }
+
+ public void testSwitchInterruptibleClosure_toString() {
+ List<Association<Predicate<Integer>, LocalInterruptibleClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalInterruptibleClosure closure3 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+
+ LocalInterruptibleClosure closureX = new LocalInterruptibleClosure();
+
+ InterruptibleClosure<Integer> closure = ClosureTools.switchInterruptibleClosure(list, closureX);
+
+ assertTrue(closure.toString().indexOf("SwitchInterruptibleClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleClosure") != -1);
+ }
+
+ public void testSwitchInterruptibleClosure_NPE1() {
+ List<Association<Predicate<Integer>, LocalInterruptibleClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalInterruptibleClosure closure3 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.switchInterruptibleClosure(list, null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSwitchInterruptibleClosure_NPE2() {
+ List<Association<Predicate<Integer>, LocalInterruptibleClosure>> list = new ArrayList<>();
+
+ Predicate<Integer> predicate1 = PredicateTools.isEqual(Integer.valueOf(1));
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association1 = new SimpleAssociation<>(predicate1, closure1);
+ list.add(association1);
+
+ Predicate<Integer> predicate2 = PredicateTools.isEqual(Integer.valueOf(2));
+ LocalInterruptibleClosure closure2 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association2 = new SimpleAssociation<>(predicate2, closure2);
+ list.add(association2);
+
+ Predicate<Integer> predicate3 = PredicateTools.isEqual(Integer.valueOf(3));
+ LocalInterruptibleClosure closure3 = new LocalInterruptibleClosure();
+ Association<Predicate<Integer>, LocalInterruptibleClosure> association3 = new SimpleAssociation<>(predicate3, closure3);
+ list.add(association3);
+ list.add(null);
+
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.switchInterruptibleClosure(list);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testSwitchInterruptibleClosure_NPE3() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.switchInterruptibleClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testDisabledClosure_execute() {
+ Closure<Integer> closure = ClosureTools.disabledClosure();
+ boolean exCaught = false;
+ try {
+ closure.execute(null);
+ fail();
+ } catch (UnsupportedOperationException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testDisabledClosure_toString() {
+ Closure<Integer> closure = ClosureTools.disabledClosure();
+ assertTrue(closure.toString().indexOf("DisabledClosure") != -1);
+ }
+
+ public void testDisabledClosure_serialization() throws Exception {
+ Closure<Integer> closure = ClosureTools.disabledClosure();
+ assertSame(closure, TestTools.serialize(closure));
+ }
+
+ public void testInterruptedClosure_execute() throws Exception {
+ InterruptibleClosure<Integer> closure = ClosureTools.interruptedClosure();
+ boolean exCaught = false;
+ try {
+ closure.execute(null);
+ fail();
+ } catch (InterruptedException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testInterruptedClosure_toString() {
+ InterruptibleClosure<Integer> closure = ClosureTools.interruptedClosure();
+ assertTrue(closure.toString().indexOf("InterruptedClosure") != -1);
+ }
+
+ public void testInterruptedClosure_serialization() throws Exception {
+ InterruptibleClosure<Integer> closure = ClosureTools.interruptedClosure();
+ assertSame(closure, TestTools.serialize(closure));
+ }
+
+ public void testRepeatingClosure_execute1() throws Exception {
+ LocalClosure closure1 = new LocalClosure();
+ Closure<Integer> closure2 = ClosureTools.repeatingClosure(closure1, 3);
+ closure2.execute(Integer.valueOf(3));
+ assertEquals(9, closure1.count);
+ }
+
+ public void testRepeatingClosure_execute2() throws Exception {
+ LocalClosure closure1 = new LocalClosure();
+ Closure<Integer> closure2 = ClosureTools.repeatingClosure(closure1, 0);
+ closure2.execute(Integer.valueOf(3));
+ assertEquals(0, closure1.count);
+ }
+
+ public void testRepeatingClosure_toString() {
+ LocalClosure closure1 = new LocalClosure();
+ Closure<Integer> closure2 = ClosureTools.repeatingClosure(closure1, 3);
+ assertTrue(closure2.toString().indexOf("RepeatingClosure") != -1);
+ assertTrue(closure2.toString().indexOf("LocalClosure") != -1);
+ }
+
+ public void testRepeatingClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure = ClosureTools.repeatingClosure((Closure<Integer>) null, 3);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testRepeatingClosure_NPE2() {
+ LocalClosure closure1 = new LocalClosure();
+ boolean exCaught = false;
+ try {
+ Closure<Integer> closure2 = ClosureTools.repeatingClosure(closure1, -3);
+ fail("bogus: " + closure2);
+ } catch (IndexOutOfBoundsException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testRepeatingInterruptibleClosure_execute1() throws Exception {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ InterruptibleClosure<Integer> closure2 = ClosureTools.repeatingClosure(closure1, 3);
+ closure2.execute(Integer.valueOf(3));
+ assertEquals(9, closure1.count);
+ }
+
+ public void testRepeatingInterruptibleClosure_execute2() throws Exception {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ InterruptibleClosure<Integer> closure2 = ClosureTools.repeatingClosure(closure1, 0);
+ closure2.execute(Integer.valueOf(3));
+ assertEquals(0, closure1.count);
+ }
+
+ public void testRepeatingInterruptibleClosure_toString() {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ InterruptibleClosure<Integer> closure2 = ClosureTools.repeatingClosure(closure1, 3);
+ assertTrue(closure2.toString().indexOf("RepeatingInterruptibleClosure") != -1);
+ assertTrue(closure2.toString().indexOf("LocalInterruptibleClosure") != -1);
+ }
+
+ public void testRepeatingInterruptibleClosure_NPE1() {
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure = ClosureTools.repeatingClosure((InterruptibleClosure<Integer>) null, 3);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testRepeatingInterruptibleClosure_NPE2() {
+ LocalInterruptibleClosure closure1 = new LocalInterruptibleClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<Integer> closure2 = ClosureTools.repeatingClosure(closure1, -3);
+ fail("bogus: " + closure2);
+ } catch (IndexOutOfBoundsException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWhileClosure_execute() throws Exception {
+ ModifiableIntReference ref = new SimpleIntReference(0);
+ IntRefClosure closure1 = new IntRefClosure();
+ Predicate<ModifiableIntReference> predicate = new IntRefValueNotEqualPredicate(3);
+ Closure<ModifiableIntReference> closure2 = ClosureTools.whileClosure(predicate, closure1);
+ closure2.execute(ref);
+ assertEquals(6, closure1.count);
+ ref.setValue(-6);
+ closure2.execute(ref);
+ assertEquals(24, closure1.count);
+ }
+
+ public void testWhileClosure_toString() {
+ IntRefClosure closure1 = new IntRefClosure();
+ Predicate<ModifiableIntReference> predicate = new IntRefValueNotEqualPredicate(3);
+ Closure<ModifiableIntReference> closure2 = ClosureTools.whileClosure(predicate, closure1);
+ assertTrue(closure2.toString().indexOf("WhileClosure") != -1);
+ assertTrue(closure2.toString().indexOf("IntRefValueNotEqualPredicate") != -1);
+ }
+
+ public void testWhileClosure_NPE1() {
+ IntRefClosure closure1 = new IntRefClosure();
+ boolean exCaught = false;
+ try {
+ Closure<ModifiableIntReference> closure2 = ClosureTools.whileClosure(null, closure1);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWhileClosure_NPE2() {
+ Predicate<ModifiableIntReference> predicate = new IntRefValueNotEqualPredicate(3);
+ boolean exCaught = false;
+ try {
+ Closure<ModifiableIntReference> closure2 = ClosureTools.whileClosure(predicate, (IntRefClosure) null);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public static class IntRefClosure
+ extends ClosureAdapter<ModifiableIntReference>
+ {
+ public int count = 0;
+ @Override
+ public void execute(ModifiableIntReference argument) {
+ this.count += 2;
+ argument.increment();
+ }
+ }
+
+ public void testWhileInterruptibleClosure_execute() throws Exception {
+ ModifiableIntReference ref = new SimpleIntReference(0);
+ IntRefInterruptibleClosure closure1 = new IntRefInterruptibleClosure();
+ Predicate<ModifiableIntReference> predicate = new IntRefValueNotEqualPredicate(3);
+ InterruptibleClosure<ModifiableIntReference> closure2 = ClosureTools.whileClosure(predicate, closure1);
+ closure2.execute(ref);
+ assertEquals(6, closure1.count);
+ ref.setValue(-6);
+ closure2.execute(ref);
+ assertEquals(24, closure1.count);
+ }
+
+ public void testWhileInterruptibleClosure_toString() {
+ IntRefInterruptibleClosure closure1 = new IntRefInterruptibleClosure();
+ Predicate<ModifiableIntReference> predicate = new IntRefValueNotEqualPredicate(3);
+ InterruptibleClosure<ModifiableIntReference> closure2 = ClosureTools.whileClosure(predicate, closure1);
+ assertTrue(closure2.toString().indexOf("WhileInterruptibleClosure") != -1);
+ assertTrue(closure2.toString().indexOf("IntRefValueNotEqualPredicate") != -1);
+ }
+
+ public void testWhileInterruptibleClosure_NPE1() {
+ IntRefInterruptibleClosure closure1 = new IntRefInterruptibleClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<ModifiableIntReference> closure2 = ClosureTools.whileClosure(null, closure1);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWhileInterruptibleClosure_NPE2() {
+ Predicate<ModifiableIntReference> predicate = new IntRefValueNotEqualPredicate(3);
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<ModifiableIntReference> closure2 = ClosureTools.whileClosure(predicate, (IntRefInterruptibleClosure) null);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public static class IntRefInterruptibleClosure
+ extends InterruptibleClosureAdapter<ModifiableIntReference>
+ {
+ public int count = 0;
+ @Override
+ public void execute(ModifiableIntReference argument) throws InterruptedException {
+ this.count += 2;
+ argument.increment();
+ }
+ }
+
+ public static class IntRefValueNotEqualPredicate
+ extends PredicateAdapter<ModifiableIntReference>
+ {
+ private final int count;
+ public IntRefValueNotEqualPredicate(int count) {
+ super();
+ this.count = count;
+ }
+ @Override
+ public boolean evaluate(ModifiableIntReference variable) {
+ return variable.notEqual(this.count);
+ }
+ }
+
+ public void testUntilClosure_execute() throws Exception {
+ ModifiableIntReference ref = new SimpleIntReference(0);
+ IntRefClosure closure1 = new IntRefClosure();
+ Predicate<ModifiableIntReference> predicate = new IntRefValueEqualPredicate(3);
+ Closure<ModifiableIntReference> closure2 = ClosureTools.untilClosure(closure1, predicate);
+ closure2.execute(ref);
+ assertEquals(6, closure1.count);
+ ref.setValue(-6);
+ closure2.execute(ref);
+ assertEquals(24, closure1.count);
+ }
+
+ public void testUntilClosure_toString() {
+ IntRefClosure closure1 = new IntRefClosure();
+ Predicate<ModifiableIntReference> predicate = new IntRefValueEqualPredicate(3);
+ Closure<ModifiableIntReference> closure2 = ClosureTools.untilClosure(closure1, predicate);
+ assertTrue(closure2.toString().indexOf("UntilClosure") != -1);
+ assertTrue(closure2.toString().indexOf("IntRefValueEqualPredicate") != -1);
+ }
+
+ public void testUntilClosure_NPE1() {
+ IntRefClosure closure1 = new IntRefClosure();
+ boolean exCaught = false;
+ try {
+ Closure<ModifiableIntReference> closure2 = ClosureTools.untilClosure(closure1, null);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testUntilClosure_NPE2() {
+ Predicate<ModifiableIntReference> predicate = new IntRefValueEqualPredicate(3);
+ boolean exCaught = false;
+ try {
+ Closure<ModifiableIntReference> closure2 = ClosureTools.untilClosure((IntRefClosure) null, predicate);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testUntilInterruptibleClosure_execute() throws Exception {
+ ModifiableIntReference ref = new SimpleIntReference(0);
+ IntRefInterruptibleClosure closure1 = new IntRefInterruptibleClosure();
+ Predicate<ModifiableIntReference> predicate = new IntRefValueEqualPredicate(3);
+ InterruptibleClosure<ModifiableIntReference> closure2 = ClosureTools.untilClosure(closure1, predicate);
+ closure2.execute(ref);
+ assertEquals(6, closure1.count);
+ ref.setValue(-6);
+ closure2.execute(ref);
+ assertEquals(24, closure1.count);
+ }
+
+ public void testUntilInterruptibleClosure_toString() {
+ IntRefInterruptibleClosure closure1 = new IntRefInterruptibleClosure();
+ Predicate<ModifiableIntReference> predicate = new IntRefValueEqualPredicate(3);
+ InterruptibleClosure<ModifiableIntReference> closure2 = ClosureTools.untilClosure(closure1, predicate);
+ assertTrue(closure2.toString().indexOf("UntilInterruptibleClosure") != -1);
+ assertTrue(closure2.toString().indexOf("IntRefValueEqualPredicate") != -1);
+ }
+
+ public void testUntilInterruptibleClosure_NPE1() {
+ IntRefInterruptibleClosure closure1 = new IntRefInterruptibleClosure();
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<ModifiableIntReference> closure2 = ClosureTools.untilClosure(closure1, null);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testUntilInterruptibleClosure_NPE2() {
+ Predicate<ModifiableIntReference> predicate = new IntRefValueEqualPredicate(3);
+ boolean exCaught = false;
+ try {
+ InterruptibleClosure<ModifiableIntReference> closure2 = ClosureTools.untilClosure((IntRefInterruptibleClosure) null, predicate);
+ fail("bogus: " + closure2);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public static class IntRefValueEqualPredicate
+ extends PredicateAdapter<ModifiableIntReference>
+ {
+ private final int count;
+ public IntRefValueEqualPredicate(int count) {
+ super();
+ this.count = count;
+ }
+ @Override
+ public boolean evaluate(ModifiableIntReference variable) {
+ return variable.equals(this.count);
+ }
+ }
+
+ public void testConstructor() {
+ boolean exCaught = false;
+ try {
+ Object at = ClassTools.newInstance(ClosureTools.class);
+ fail("bogus: " + at);
+ } catch (RuntimeException ex) {
+ if (ex.getCause() instanceof InvocationTargetException) {
+ if (ex.getCause().getCause() instanceof UnsupportedOperationException) {
+ exCaught = true;
+ }
+ }
+ }
+ assertTrue(exCaught);
+ }
+
+ // ********** test classes **********
+
+ public static class LocalClosure
+ extends ClosureAdapter<Integer>
+ {
+ public int count = 0;
+
+ @Override
+ public void execute(Integer argument) {
+ this.count += argument.intValue(); // possible NPE
+ }
+ }
+
+ public static class LocalClosure2
+ extends ClosureAdapter<Integer>
+ {
+ public int count = 0;
+
+ @Override
+ public void execute(Integer argument) {
+ this.count += (2 * argument.intValue()); // possible NPE
+ }
+ }
+
+ public static class LocalInterruptibleClosure
+ extends InterruptibleClosureAdapter<Integer>
+ {
+ public int count = 0;
+
+ @Override
+ public void execute(Integer argument) throws InterruptedException {
+ this.count += argument.intValue(); // possible NPE
+ }
+ }
+
+ public static class LocalCommand
+ extends CommandAdapter
+ {
+ public int count = 0;
+
+ @Override
+ public void execute() {
+ this.count++;
+ }
+ }
+
+ public static class LocalInterruptibleCommand
+ extends InterruptibleCommandAdapter
+ {
+ public int count = 0;
+
+ @Override
+ public void execute() {
+ this.count++;
+ }
+ }
+
+ public static class LocalFactory<T>
+ extends FactoryAdapter<T>
+ {
+ public int count = 0;
+
+ @Override
+ public T create() {
+ this.count++;
+ return null;
+ }
+ }
+
+ public static class LocalInterruptibleFactory<T>
+ extends InterruptibleFactoryAdapter<T>
+ {
+ public int count = 0;
+
+ @Override
+ public T create() throws InterruptedException {
+ this.count++;
+ return null;
+ }
+ }
+
+ public static class LocalTransformer<I, O>
+ extends TransformerAdapter<I, O>
+ {
+ public int count = 0;
+
+ @Override
+ public O transform(I input) {
+ this.count++;
+ return null;
+ }
+ }
+
+ public static class LocalInterruptibleTransformer<I, O>
+ extends InterruptibleTransformerAdapter<I, O>
+ {
+ public int count = 0;
+
+ @Override
+ public O transform(I input) throws InterruptedException {
+ this.count++;
+ return null;
+ }
+ }
+}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java
index 40f8b348ec..cf0c333f35 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java
@@ -20,7 +20,9 @@ public class JptCommonUtilityClosureTests {
public static Test suite() {
TestSuite suite = new TestSuite(JptCommonUtilityClosureTests.class.getPackage().getName());
+ suite.addTestSuite(BiClosureToolsTests.class);
suite.addTestSuite(BooleanClosureTests.class);
+ suite.addTestSuite(ClosureToolsTests.class);
suite.addTestSuite(NullableBooleanClosureTests.class);
return suite;

Back to the top