diff options
11 files changed, 826 insertions, 103 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedObject.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedObject.java index 037cb64482..b6d9309ad4 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedObject.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedObject.java @@ -241,7 +241,8 @@ public class SynchronizedObject<V> return thisValue; // nothing changes } other.setChangedValue_(thisValue); - return this.setChangedValue_(otherValue); + this.setChangedValue_(otherValue); + return otherValue; } } } @@ -573,7 +574,7 @@ public class SynchronizedObject<V> * Return the previous value. * If the time-out is zero, wait indefinitely. */ - public boolean waitToSwap(V newValue, V expectedValue, long timeout) throws InterruptedException { + public boolean waitToCommit(V newValue, V expectedValue, long timeout) throws InterruptedException { synchronized (this.mutex) { boolean success = this.waitUntil_(PredicateTools.isEqual(expectedValue), timeout); if (success) { diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractBooleanReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractBooleanReferenceTests.java index 12be6594f0..92233241f6 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractBooleanReferenceTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractBooleanReferenceTests.java @@ -10,6 +10,7 @@ package org.eclipse.jpt.common.utility.tests.internal.reference; import org.eclipse.jpt.common.utility.internal.reference.AbstractBooleanReference; +import org.eclipse.jpt.common.utility.reference.BooleanReference; import junit.framework.TestCase; @SuppressWarnings("nls") @@ -43,8 +44,10 @@ public class AbstractBooleanReferenceTests } public void testIsFalse() { - LocalBooleanReference br = new LocalBooleanReference(); + BooleanReference br = new LocalBooleanReference(); assertFalse(br.isFalse()); + br = new FalseLocalBooleanReference(); + assertTrue(br.isFalse()); } public void testEquals() { @@ -74,4 +77,15 @@ public class AbstractBooleanReferenceTests return true; } } + + private class FalseLocalBooleanReference + extends AbstractBooleanReference + { + FalseLocalBooleanReference() { + super(); + } + public boolean getValue() { + return false; + } + } } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractIntReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractIntReferenceTests.java index 9926fb90a7..3e48ef12e7 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractIntReferenceTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractIntReferenceTests.java @@ -9,7 +9,9 @@ ******************************************************************************/ package org.eclipse.jpt.common.utility.tests.internal.reference; +import org.eclipse.jpt.common.utility.internal.predicate.int_.IntPredicateTools; import org.eclipse.jpt.common.utility.internal.reference.AbstractIntReference; +import org.eclipse.jpt.common.utility.internal.reference.SimpleIntReference; import junit.framework.TestCase; @SuppressWarnings("nls") @@ -21,57 +23,147 @@ public class AbstractIntReferenceTests } public void testGetValue() { - LocalIntReference br = new LocalIntReference(); + LocalIntReference br = new LocalIntReference(42); assertEquals(42, br.getValue()); } public void testEqualsInt() { - LocalIntReference br = new LocalIntReference(); + LocalIntReference br = new LocalIntReference(42); assertTrue(br.equals(42)); assertFalse(br.equals(0)); } public void testNotEqual() { - LocalIntReference br = new LocalIntReference(); + LocalIntReference br = new LocalIntReference(42); assertFalse(br.notEqual(42)); assertTrue(br.notEqual(0)); } public void testIsZero() { - LocalIntReference br = new LocalIntReference(); + LocalIntReference br = new LocalIntReference(42); assertFalse(br.isZero()); + br = new LocalIntReference(0); + assertTrue(br.isZero()); } public void testIsNotZero() { - LocalIntReference br = new LocalIntReference(); + LocalIntReference br = new LocalIntReference(42); assertTrue(br.isNotZero()); + br = new LocalIntReference(0); + assertFalse(br.isNotZero()); + } + + public void testIsGreaterThan() { + LocalIntReference br = new LocalIntReference(42); + assertTrue(br.isGreaterThan(22)); + assertFalse(br.isGreaterThan(2222)); + } + + public void testIsGreaterThanOrEqual() { + LocalIntReference br = new LocalIntReference(42); + assertTrue(br.isGreaterThanOrEqual(22)); + assertTrue(br.isGreaterThanOrEqual(42)); + assertFalse(br.isGreaterThanOrEqual(2222)); + } + + public void testIsLessThan() { + LocalIntReference br = new LocalIntReference(42); + assertTrue(br.isLessThan(2222)); + assertFalse(br.isLessThan(22)); + } + + public void testIsLessThanOrEqual() { + LocalIntReference br = new LocalIntReference(42); + assertTrue(br.isLessThanOrEqual(2222)); + assertTrue(br.isLessThanOrEqual(42)); + assertFalse(br.isLessThanOrEqual(22)); + } + + public void testIsPositive() { + LocalIntReference br = new LocalIntReference(42); + assertTrue(br.isPositive()); + br = new LocalIntReference(0); + assertFalse(br.isPositive()); + br = new LocalIntReference(-42); + assertFalse(br.isPositive()); + } + + public void testIsNotPositive() { + LocalIntReference br = new LocalIntReference(-42); + assertTrue(br.isNotPositive()); + br = new LocalIntReference(0); + assertTrue(br.isNotPositive()); + br = new LocalIntReference(42); + assertFalse(br.isNotPositive()); + } + + public void testIsNegative() { + LocalIntReference br = new LocalIntReference(-42); + assertTrue(br.isNegative()); + br = new LocalIntReference(0); + assertFalse(br.isNegative()); + br = new LocalIntReference(42); + assertFalse(br.isNegative()); + } + + public void testIsNotNegative() { + LocalIntReference br = new LocalIntReference(42); + assertTrue(br.isNotNegative()); + br = new LocalIntReference(0); + assertTrue(br.isNotNegative()); + br = new LocalIntReference(-42); + assertFalse(br.isNotNegative()); + } + + public void testIsMemberOf() { + LocalIntReference br = new LocalIntReference(42); + assertTrue(br.isMemberOf(IntPredicateTools.isEven())); + assertFalse(br.isMemberOf(IntPredicateTools.isOdd())); + } + + public void testIsNotMemberOf() { + LocalIntReference br = new LocalIntReference(42); + assertFalse(br.isNotMemberOf(IntPredicateTools.isEven())); + assertTrue(br.isNotMemberOf(IntPredicateTools.isOdd())); + } + + public void testCompareToIntReference() { + LocalIntReference ir1 = new LocalIntReference(44); + SimpleIntReference ir2 = new SimpleIntReference(44); + assertTrue(ir1.compareTo(ir2) == 0); + ir2 = new SimpleIntReference(55); + assertTrue(ir1.compareTo(ir2) < 0); + ir2 = new SimpleIntReference(33); + assertTrue(ir1.compareTo(ir2) > 0); } public void testEquals() { - LocalIntReference br1 = new LocalIntReference(); - LocalIntReference br2 = new LocalIntReference(); + LocalIntReference br1 = new LocalIntReference(42); + LocalIntReference br2 = new LocalIntReference(42); assertTrue(br1.equals(br1)); assertFalse(br1.equals(br2)); } public void testHashCode() { - LocalIntReference br = new LocalIntReference(); + LocalIntReference br = new LocalIntReference(42); assertEquals(br.hashCode(), br.hashCode()); } public void testToString() { - LocalIntReference br = new LocalIntReference(); + LocalIntReference br = new LocalIntReference(42); assertEquals("[42]", br.toString()); } private class LocalIntReference extends AbstractIntReference { - LocalIntReference() { + private int value; + LocalIntReference(int value) { super(); + this.value = value; } public int getValue() { - return 42; + return this.value; } } } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractModifiableObjectReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractModifiableObjectReferenceTests.java new file mode 100644 index 0000000000..b03fe3984b --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractModifiableObjectReferenceTests.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2015 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.reference; + +import org.eclipse.jpt.common.utility.internal.reference.AbstractModifiableObjectReference; +import org.eclipse.jpt.common.utility.reference.ModifiableObjectReference; + +public class AbstractModifiableObjectReferenceTests + extends ModifiableObjectReferenceTests +{ + public AbstractModifiableObjectReferenceTests(String name) { + super(name); + } + + @Override + protected ModifiableObjectReference<String> buildObjectReference(String value) { + return new LocalObjectReference(value); + } + + private static class LocalObjectReference + extends AbstractModifiableObjectReference<String> + { + protected String value; + protected LocalObjectReference(String value) { + super(); + this.value = value; + } + public String getValue() { + return this.value; + } + public String setValue(String value) { + String old = this.value; + this.value = value; + return old; + } + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractObjectReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractObjectReferenceTests.java new file mode 100644 index 0000000000..2e2aea25a8 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractObjectReferenceTests.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2015 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.reference; + +import org.eclipse.jpt.common.utility.internal.reference.AbstractObjectReference; +import org.eclipse.jpt.common.utility.reference.ObjectReference; + +public class AbstractObjectReferenceTests + extends ObjectReferenceTests +{ + public AbstractObjectReferenceTests(String name) { + super(name); + } + + @Override + protected ObjectReference<String> buildObjectReference(String value) { + return new LocalObjectReference(value); + } + + protected static class LocalObjectReference + extends AbstractObjectReference<String> + { + protected String value; + protected LocalObjectReference(String value) { + super(); + this.value = value; + } + public String getValue() { + return this.value; + } + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java index 08fbd16f7a..0440070901 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java @@ -22,6 +22,8 @@ public class JptCommonUtilityReferenceTests { suite.addTestSuite(AbstractBooleanReferenceTests.class); suite.addTestSuite(AbstractIntReferenceTests.class); + suite.addTestSuite(AbstractModifiableObjectReferenceTests.class); + suite.addTestSuite(AbstractObjectReferenceTests.class); suite.addTestSuite(SimpleBooleanReferenceTests.class); suite.addTestSuite(SimpleIntReferenceTests.class); suite.addTestSuite(SimpleObjectReferenceTests.class); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/ModifiableObjectReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/ModifiableObjectReferenceTests.java new file mode 100644 index 0000000000..df02e35f92 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/ModifiableObjectReferenceTests.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2008, 2015 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.reference; + +import org.eclipse.jpt.common.utility.internal.reference.SimpleObjectReference; +import org.eclipse.jpt.common.utility.reference.ModifiableObjectReference; + +@SuppressWarnings("nls") +public abstract class ModifiableObjectReferenceTests + extends ObjectReferenceTests +{ + public ModifiableObjectReferenceTests(String name) { + super(name); + } + + @Override + protected ModifiableObjectReference<String> buildObjectReference() { + return (ModifiableObjectReference<String>) super.buildObjectReference(); + } + + @Override + protected abstract ModifiableObjectReference<String> buildObjectReference(String value); + + public void testSetNull() { + ModifiableObjectReference<String> or = this.buildObjectReference(); + assertNull(or.getValue()); + or.setValue("foo"); + assertEquals("foo", or.getValue()); + or.setNull(); + assertNull(or.getValue()); + } + + public void testCommit() throws Exception { + String v1 = "foo"; + ModifiableObjectReference<String> or = this.buildObjectReference(); + assertTrue(or.commit(v1, null)); + assertEquals(v1, or.getValue()); + assertFalse(or.commit(v1, null)); + assertEquals(v1, or.getValue()); + + String v2 = "bar"; + assertTrue(or.commit(v2, v1)); + assertEquals(v2, or.getValue()); + assertFalse(or.commit(v2, v1)); + assertEquals(v2, or.getValue()); + } + + public void testSwapRef() throws Exception { + ModifiableObjectReference<String> or = this.buildObjectReference(); + ModifiableObjectReference<String> temp = or; + assertEquals(null, or.swap(temp)); + + ModifiableObjectReference<String> ref = new SimpleObjectReference<>("foo"); + assertEquals("foo", or.swap(ref)); + assertEquals("foo", or.getValue()); + assertEquals(null, ref.getValue()); + + or.setValue("foo"); + ref.setValue("foo"); + assertEquals("foo", or.swap(ref)); + assertEquals("foo", or.getValue()); + assertEquals("foo", ref.getValue()); + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/ObjectReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/ObjectReferenceTests.java new file mode 100644 index 0000000000..3cdb1f2bbb --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/ObjectReferenceTests.java @@ -0,0 +1,127 @@ +/******************************************************************************* + * Copyright (c) 2008, 2015 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.reference; + +import org.eclipse.jpt.common.utility.internal.predicate.PredicateTools; +import org.eclipse.jpt.common.utility.reference.ObjectReference; +import junit.framework.TestCase; + +@SuppressWarnings("nls") +public abstract class ObjectReferenceTests + extends TestCase +{ + public ObjectReferenceTests(String name) { + super(name); + } + + protected ObjectReference<String> buildObjectReference() { + return this.buildObjectReference(null); + } + + protected abstract ObjectReference<String> buildObjectReference(String value); + + public void testGetValue() { + ObjectReference<String> or = this.buildObjectReference(); + assertNull(or.getValue()); + or = this.buildObjectReference("foo"); + assertEquals("foo", or.getValue()); + } + + public void testValueEqualsObject() { + ObjectReference<String> or = this.buildObjectReference(); + assertTrue(or.valueEquals(null)); + assertFalse(or.valueEquals("foo")); + + or = this.buildObjectReference("foo"); + assertFalse(or.valueEquals(null)); + assertTrue(or.valueEquals("foo")); + assertTrue(or.valueEquals(new String("foo"))); + } + + public void testValueNotEqualObject() { + ObjectReference<String> or = this.buildObjectReference(); + assertFalse(or.valueNotEqual(null)); + assertTrue(or.valueNotEqual("foo")); + + or = this.buildObjectReference("foo"); + assertTrue(or.valueNotEqual(null)); + assertFalse(or.valueNotEqual("foo")); + assertFalse(or.valueNotEqual(new String("foo"))); + } + + public void testIsObject() { + ObjectReference<String> or = this.buildObjectReference(); + assertTrue(or.is(null)); + assertFalse(or.is("foo")); + + or = this.buildObjectReference("foo"); + assertFalse(or.is(null)); + assertTrue(or.is("foo")); + assertFalse(or.is(new String("foo"))); + } + + public void testIsNotObject() { + ObjectReference<String> or = this.buildObjectReference(); + assertFalse(or.isNot(null)); + assertTrue(or.isNot("foo")); + + or = this.buildObjectReference("foo"); + assertTrue(or.isNot(null)); + assertFalse(or.isNot("foo")); + assertTrue(or.isNot(new String("foo"))); + } + + public void testIsNull() { + ObjectReference<String> or = this.buildObjectReference(); + assertTrue(or.isNull()); + or = this.buildObjectReference("foo"); + assertFalse(or.isNull()); + } + + public void testIsNotNull() { + ObjectReference<String> or = this.buildObjectReference(); + assertFalse(or.isNotNull()); + or = this.buildObjectReference("foo"); + assertTrue(or.isNotNull()); + } + + public void testIsMemberOfPredicate() { + ObjectReference<String> or = this.buildObjectReference(); + assertTrue(or.isMemberOf(PredicateTools.isNull())); + or = this.buildObjectReference("foo"); + assertFalse(or.isMemberOf(PredicateTools.isNull())); + } + + public void testIsNotMemberOfPredicate() { + ObjectReference<String> or = this.buildObjectReference(); + assertTrue(or.isNotMemberOf(PredicateTools.isNotNull())); + or = this.buildObjectReference("foo"); + assertFalse(or.isNotMemberOf(PredicateTools.isNotNull())); + } + + public void testEqualsObject() { + ObjectReference<String> or1 = this.buildObjectReference(); + assertTrue(or1.equals(or1)); + ObjectReference<String> or2 = this.buildObjectReference(); + assertFalse(or1.equals(or2)); + } + + public void testHashCode() { + ObjectReference<String> or1 = this.buildObjectReference(); + assertEquals(or1.hashCode(), or1.hashCode()); + } + + public void testToString() { + ObjectReference<String> or = this.buildObjectReference(); + assertEquals("[null]", or.toString()); + or = this.buildObjectReference("foo"); + assertEquals("[foo]", or.toString()); + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleObjectReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleObjectReferenceTests.java index 9a77a629fd..4f5ff38b66 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleObjectReferenceTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleObjectReferenceTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2010 Oracle. All rights reserved. + * Copyright (c) 2008, 2015 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,80 +9,34 @@ ******************************************************************************/ package org.eclipse.jpt.common.utility.tests.internal.reference; -import junit.framework.TestCase; - +import org.eclipse.jpt.common.utility.internal.ObjectTools; import org.eclipse.jpt.common.utility.internal.reference.SimpleObjectReference; +import org.eclipse.jpt.common.utility.reference.ModifiableObjectReference; +import org.eclipse.jpt.common.utility.reference.ObjectReference; @SuppressWarnings("nls") -public class SimpleObjectReferenceTests extends TestCase { - +public class SimpleObjectReferenceTests + extends ModifiableObjectReferenceTests +{ public SimpleObjectReferenceTests(String name) { super(name); } - public void testGetValue() { - SimpleObjectReference<String> or = new SimpleObjectReference<String>(); - assertNull(or.getValue()); - or.setValue("foo"); - assertEquals("foo", or.getValue()); - } - - public void testValueEqualsObject() { - SimpleObjectReference<String> or = new SimpleObjectReference<String>(); - assertTrue(or.valueEquals(null)); - assertFalse(or.valueEquals("foo")); - - or.setValue("foo"); - assertFalse(or.valueEquals(null)); - assertTrue(or.valueEquals("foo")); - } - - public void testValueNotEqualObject() { - SimpleObjectReference<String> or = new SimpleObjectReference<String>(); - assertFalse(or.valueNotEqual(null)); - assertTrue(or.valueNotEqual("foo")); - - or.setValue("foo"); - assertTrue(or.valueNotEqual(null)); - assertFalse(or.valueNotEqual("foo")); + @Override + protected ModifiableObjectReference<String> buildObjectReference() { + return new SimpleObjectReference<>(); } - public void testIsNull() { - SimpleObjectReference<String> or = new SimpleObjectReference<String>(); - assertTrue(or.isNull()); - or.setValue("foo"); - assertFalse(or.isNull()); - } - - public void testIsNotNull() { - SimpleObjectReference<String> or = new SimpleObjectReference<String>(); - assertFalse(or.isNotNull()); - or.setValue("foo"); - assertTrue(or.isNotNull()); - } - - public void testSetNull() { - SimpleObjectReference<String> or = new SimpleObjectReference<String>(); - assertNull(or.getValue()); - or.setValue("foo"); - assertEquals("foo", or.getValue()); - or.setNull(); - assertNull(or.getValue()); + @Override + protected ModifiableObjectReference<String> buildObjectReference(String value) { + return new SimpleObjectReference<>(value); } public void testClone() { - SimpleObjectReference<String> or = new SimpleObjectReference<String>("foo"); - @SuppressWarnings("cast") - SimpleObjectReference<String> clone = (SimpleObjectReference<String>) or.clone(); + ObjectReference<String> or = this.buildObjectReference("foo"); + @SuppressWarnings("unchecked") + ObjectReference<String> clone = (ObjectReference<String>) ObjectTools.execute(or, "clone"); assertEquals("foo", clone.getValue()); assertNotSame(or, clone); } - - public void testToString() { - SimpleObjectReference<String> or = new SimpleObjectReference<String>(); - assertEquals("[null]", or.toString()); - or.setValue("foo"); - assertEquals("[foo]", or.toString()); - } - } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java index a3791e3f08..f0d7cf7fdc 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java @@ -517,9 +517,6 @@ public class SynchronizedIntTests assertSame(this.si, this.si.getMutex()); } - - // ********** indefinite waits ********** - /** * t2 will wait indefinitely until t1 sets the value to 0 */ @@ -775,7 +772,7 @@ public class SynchronizedIntTests } /** - * t2 will time out waiting for t1 to set the value to 0 + * t2 will NOT time out waiting for t1 to set the value to 0 */ public void testWaitToSetValueTimeout2() throws Exception { this.verifyWaitToSetValue(THREE_TICKS); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedObjectTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedObjectTests.java index ef5207487d..25ebdf01ec 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedObjectTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedObjectTests.java @@ -9,8 +9,12 @@ ******************************************************************************/ package org.eclipse.jpt.common.utility.tests.internal.reference; +import org.eclipse.jpt.common.utility.internal.predicate.PredicateTools; +import org.eclipse.jpt.common.utility.internal.reference.SimpleObjectReference; import org.eclipse.jpt.common.utility.internal.reference.SynchronizedObject; +import org.eclipse.jpt.common.utility.reference.ModifiableObjectReference; import org.eclipse.jpt.common.utility.tests.internal.MultiThreadedTestCase; +import org.eclipse.jpt.common.utility.tests.internal.TestTools; @SuppressWarnings("nls") public class SynchronizedObjectTests @@ -31,13 +35,21 @@ public class SynchronizedObjectTests @Override protected void setUp() throws Exception { super.setUp(); - this.so = new SynchronizedObject<Object>(); + this.so = new SynchronizedObject<>(); this.timeoutOccurred = false; this.startTime = 0; this.endTime = 0; this.soValue = null; } + public void testCtorObjectObject() throws Exception { + String v = "foo"; + String mutex = "lock"; + this.so = new SynchronizedObject<>(v, mutex); + assertEquals(v, this.so.getValue()); + assertEquals(mutex, this.so.getMutex()); + } + public void testAccessors() throws Exception { this.so.setValue(null); assertNull(this.so.getValue()); @@ -57,11 +69,162 @@ public class SynchronizedObjectTests assertSame(this.so, this.so.getMutex()); } + public void testValueEqualsObject() throws Exception { + String v = "foo"; + this.so.setValue(null); + assertTrue(this.so.valueEquals(null)); + assertFalse(this.so.valueEquals(v)); + + this.so.setValue(v); + assertTrue(this.so.valueEquals(v)); + assertFalse(this.so.valueEquals(null)); + } + + public void testValueNotEqualObject() throws Exception { + String v = "foo"; + this.so.setValue(null); + assertFalse(this.so.valueNotEqual(null)); + assertTrue(this.so.valueNotEqual(v)); + + this.so.setValue(v); + assertFalse(this.so.valueNotEqual(v)); + assertTrue(this.so.valueNotEqual(null)); + } + + public void testIsObject() throws Exception { + String v = "foo"; + this.so.setValue(null); + assertTrue(this.so.is(null)); + assertFalse(this.so.is(v)); + + this.so.setValue(v); + assertTrue(this.so.is(v)); + assertFalse(this.so.is(null)); + assertFalse(this.so.is(new String(v))); + } + + public void testIsNotObject() throws Exception { + String v = "foo"; + this.so.setValue(null); + assertFalse(this.so.isNot(null)); + assertTrue(this.so.isNot(v)); + + this.so.setValue(v); + assertFalse(this.so.isNot(v)); + assertTrue(this.so.isNot(null)); + assertTrue(this.so.isNot(new String(v))); + } + + public void testIsMemberOfPredicate() throws Exception { + String v = "foo"; + this.so.setValue(null); + assertTrue(this.so.isMemberOf(PredicateTools.isNull())); + assertFalse(this.so.isMemberOf(PredicateTools.isNotNull())); + + this.so.setValue(v); + assertFalse(this.so.isMemberOf(PredicateTools.isNull())); + assertTrue(this.so.isMemberOf(PredicateTools.isNotNull())); + } + + public void testIsNotMemberOfPredicate() throws Exception { + String v = "foo"; + this.so.setValue(null); + assertFalse(this.so.isNotMemberOf(PredicateTools.isNull())); + assertTrue(this.so.isNotMemberOf(PredicateTools.isNotNull())); + + this.so.setValue(v); + assertTrue(this.so.isNotMemberOf(PredicateTools.isNull())); + assertFalse(this.so.isNotMemberOf(PredicateTools.isNotNull())); + } + + public void testCommit() throws Exception { + String v1 = "foo"; + this.so.setValue(null); + assertTrue(this.so.commit(v1, null)); + assertEquals(v1, this.so.getValue()); + assertFalse(this.so.commit(v1, null)); + assertEquals(v1, this.so.getValue()); + + String v2 = "bar"; + assertTrue(this.so.commit(v2, v1)); + assertEquals(v2, this.so.getValue()); + assertFalse(this.so.commit(v2, v1)); + assertEquals(v2, this.so.getValue()); + } + + public void testSwapRef() throws Exception { + this.so.setValue(null); + ModifiableObjectReference<Object> temp = this.so; + assertEquals(null, this.so.swap(temp)); + + ModifiableObjectReference<Object> ref = new SimpleObjectReference<>("foo"); + assertEquals("foo", this.so.swap(ref)); + assertEquals("foo", this.so.getValue()); + assertEquals(null, ref.getValue()); + + this.so.setValue("foo"); + ref.setValue("foo"); + assertEquals("foo", this.so.swap(ref)); + assertEquals("foo", this.so.getValue()); + assertEquals("foo", ref.getValue()); + } + + public void testSwapSyncObject() throws Exception { + this.so.setValue(null); + assertEquals(null, this.so.swap(this.so)); + + ModifiableObjectReference<Object> ref = new SynchronizedObject<>("foo"); + assertEquals("foo", this.so.swap(ref)); + assertEquals("foo", this.so.getValue()); + assertEquals(null, ref.getValue()); + + this.so.setValue("foo"); + ref.setValue("foo"); + assertEquals("foo", this.so.swap(ref)); + assertEquals("foo", this.so.getValue()); + assertEquals("foo", ref.getValue()); + + ref.setValue("bar"); + assertEquals("foo", ref.swap(this.so)); + assertEquals("bar", this.so.getValue()); + assertEquals("foo", ref.getValue()); + } + + public void testSwapSyncInt2() throws Exception { + this.so.setValue(null); + assertEquals(null, this.so.swap(this.so)); + + SynchronizedObject<Object> ref = new SynchronizedObject<>("foo"); + assertEquals("foo", this.so.swap(ref)); + assertEquals("foo", this.so.getValue()); + assertEquals(null, ref.getValue()); + + this.so.setValue("foo"); + ref.setValue("foo"); + assertEquals("foo", this.so.swap(ref)); + assertEquals("foo", this.so.getValue()); + assertEquals("foo", ref.getValue()); + } + /** * t2 will wait indefinitely until t1 sets the value to null */ public void testWaitUntilNull() throws Exception { - this.verifyWaitUntilNull(0); + this.verifyWaitUntilNull(-1); // explicit indefinite wait + // no timeout occurs... + assertFalse(this.timeoutOccurred); + // ...and the value should be set to null by t2 + assertNull(this.so.getValue()); + // make a reasonable guess about how long t2 took + long time = this.calculateElapsedTime(); + assertTrue("t2 finished a bit early (expected value should be > " + TICK + "): " + time, time > TICK); + } + + /** + * t2 will wait indefinitely until t1 sets the value to null + */ + public void testWaitUntilNul2l() throws Exception { + this.verifyWaitUntilNull(0); // 0 = indefinite wait // no timeout occurs... assertFalse(this.timeoutOccurred); // ...and the value should be set to null by t2 @@ -85,16 +248,124 @@ public class SynchronizedObjectTests assertTrue("t2 finished a bit late (expected value should be < " + THREE_TICKS + "): " + time, time < THREE_TICKS); } + /** + * t2 will NOT time out waiting for t1 to set the value to null + */ + public void testWaitUntilNullTimeout2() throws Exception { + this.verifyWaitUntilNull(THREE_TICKS); + // no timeout occurs... + assertFalse(this.timeoutOccurred); + // ...and the value should be set to null by t2 + assertNull(this.so.getValue()); + // make a reasonable guess about how long t2 took + long time = this.calculateElapsedTime(); + assertTrue("t2 finished a bit early (expected value should be >= " + TWO_TICKS + "): " + time, time >= TWO_TICKS); + } + private void verifyWaitUntilNull(long t2Timeout) throws Exception { this.executeThreads(this.buildSetNullCommand(), this.buildWaitUntilNullCommand(t2Timeout)); } + private Command buildWaitUntilNullCommand(final long timeout) { + return new Command() { + public void execute(SynchronizedObject<Object> sObject) throws InterruptedException { + SynchronizedObjectTests.this.startTime = System.currentTimeMillis(); + SynchronizedObjectTests.this.timeoutOccurred = this.timeoutOccurred(sObject); + SynchronizedObjectTests.this.endTime = System.currentTimeMillis(); + } + private boolean timeoutOccurred(SynchronizedObject<Object> sObject) throws InterruptedException { + if (timeout < 0) { + sObject.waitUntilNull(); + return false; + } + return ! sObject.waitUntilNull(timeout); + } + }; + } + + public void testWaitUntilNotPredicate() throws Exception { + this.so.setValue(this.value); + this.so.waitUntilNot(PredicateTools.isNull()); + assertEquals(this.value, this.so.getValue()); + } + + public void testWaitUntilValueEqualsObject() throws Exception { + String v1 = "foo"; + this.so.setValue(v1); + String v2 = new String(v1); + this.so.waitUntilValueEquals(v2); + assertEquals(v2, this.so.getValue()); + } + + public void testWaitUntilValueNotEqualObject() throws Exception { + String v = "foo"; + this.so.setValue(v); + this.so.waitUntilValueNotEqual(null); + assertEquals(v, this.so.getValue()); + } + + public void testWaitUntilValueIsObject() throws Exception { + String v1 = "foo"; + this.so.setValue(v1); + this.so.waitUntilValueIs(v1); + assertEquals(v1, this.so.getValue()); + } + + public void testWaitUntilValueIsNotObject() throws Exception { + String v1 = "foo"; + this.so.setValue(v1); + String v2 = new String(v1); + this.so.waitUntilValueIsNot(v2); + assertSame(v1, this.so.getValue()); + assertNotSame(v2, this.so.getValue()); + } + + public void testWaitUntilNotNull() throws Exception { + String v1 = "foo"; + this.so.setValue(v1); + this.so.waitUntilNotNull(); + assertEquals(v1, this.so.getValue()); + } + + public void testWaitToSetValueObject() throws Exception { + this.so.setValue(null); + assertNull(this.so.waitToSetValue(this.value)); + assertEquals(this.value, this.so.getValue()); + } + + public void testWaitToSetNull() throws Exception { + this.so.setValue(this.value); + assertEquals(this.value, this.so.waitToSetNull()); + assertNull(this.so.getValue()); + } + + public void testWaitToCommit() throws Exception { + this.so.setValue(this.value); + assertEquals(this.value, this.so.waitToCommit("foo", this.value)); + assertEquals("foo", this.so.getValue()); + } + /** * t2 will wait indefinitely until t1 sets the value to null; * then t2 will set the value to an object */ public void testWaitToSetValue() throws Exception { - this.verifyWaitToSetValue(0); + this.verifyWaitToSetValue(-1); // explicit indefinite wait + // no timeout occurs... + assertFalse(this.timeoutOccurred); + // ...and the value should be set to an object by t2 + assertTrue(this.so.isNotNull()); + // make a reasonable guess about how long t2 took + long time = this.calculateElapsedTime(); + assertTrue("t2 finished a bit early (expected value should be > " + TICK + "): " + time, time > TICK); + } + + /** + * t2 will wait indefinitely until t1 sets the value to null; + * then t2 will set the value to an object + */ + public void testWaitToSetValue2() throws Exception { + this.verifyWaitToSetValue(0); // 0 = indefinite wait // no timeout occurs... assertFalse(this.timeoutOccurred); // ...and the value should be set to an object by t2 @@ -118,10 +389,41 @@ public class SynchronizedObjectTests assertTrue("t2 finished a bit late (expected value should be < " + THREE_TICKS + "): " + time, time < THREE_TICKS); } + /** + * t2 will NOT time out waiting for t1 to set the value to null + */ + public void testWaitToSetValueTimeout2() throws Exception { + this.verifyWaitToSetValue(THREE_TICKS); + // no timeout occurs... + assertFalse(this.timeoutOccurred); + // ...and the value should be set to a value by t2 + assertFalse(this.so.isNull()); + // make a reasonable guess about how long t2 took + long time = this.calculateElapsedTime(); + assertTrue("t2 finished a bit early (expected value should be >= " + TWO_TICKS + "): " + time, time >= TWO_TICKS); + } + private void verifyWaitToSetValue(long t2Timeout) throws Exception { this.executeThreads(this.buildSetNullCommand(), this.buildWaitToSetValueCommand(t2Timeout)); } + private Command buildWaitToSetValueCommand(final long timeout) { + return new Command() { + public void execute(SynchronizedObject<Object> sObject) throws InterruptedException { + SynchronizedObjectTests.this.startTime = System.currentTimeMillis(); + SynchronizedObjectTests.this.timeoutOccurred = this.timeoutOccurred(sObject); + SynchronizedObjectTests.this.endTime = System.currentTimeMillis(); + } + private boolean timeoutOccurred(SynchronizedObject<Object> sObject) throws InterruptedException { + if (timeout < 0) { + sObject.waitToSetValue(SynchronizedObjectTests.this.value); + return false; + } + return ! sObject.waitToSetValue(SynchronizedObjectTests.this.value, timeout); + } + }; + } + /** * t2 will wait until t1 is finished "initializing" the value; * then t2 will get the newly-initialized value ("foo") @@ -164,26 +466,6 @@ public class SynchronizedObjectTests }; } - private Command buildWaitUntilNullCommand(final long timeout) { - return new Command() { - public void execute(SynchronizedObject<Object> sObject) throws InterruptedException { - SynchronizedObjectTests.this.startTime = System.currentTimeMillis(); - SynchronizedObjectTests.this.timeoutOccurred = ! sObject.waitUntilNull(timeout); - SynchronizedObjectTests.this.endTime = System.currentTimeMillis(); - } - }; - } - - private Command buildWaitToSetValueCommand(final long timeout) { - return new Command() { - public void execute(SynchronizedObject<Object> sObject) throws InterruptedException { - SynchronizedObjectTests.this.startTime = System.currentTimeMillis(); - SynchronizedObjectTests.this.timeoutOccurred = ! sObject.waitToSetValue(SynchronizedObjectTests.this.value, timeout); - SynchronizedObjectTests.this.endTime = System.currentTimeMillis(); - } - }; - } - private Command buildInitializeValueCommand() { return new Command() { public void execute(final SynchronizedObject<Object> sObject) throws InterruptedException { @@ -230,10 +512,110 @@ public class SynchronizedObjectTests return this.endTime - this.startTime; } + public void testWaitUntilNotPredicateTimeout() throws Exception { + this.so.setValue(this.value); + this.so.waitUntilNot(PredicateTools.isNull(), TWO_TICKS); + assertEquals(this.value, this.so.getValue()); + } + + public void testWaitUntilValueEqualsObjectTimeout() throws Exception { + String v1 = "foo"; + this.so.setValue(v1); + String v2 = new String(v1); + this.so.waitUntilValueEquals(v2, TWO_TICKS); + assertEquals(v2, this.so.getValue()); + } + + public void testWaitUntilValueNotEqualObjectTimeout() throws Exception { + String v = "foo"; + this.so.setValue(v); + this.so.waitUntilValueNotEqual(null, TWO_TICKS); + assertEquals(v, this.so.getValue()); + } + + public void testWaitUntilValueIsObjectTimeout() throws Exception { + String v1 = "foo"; + this.so.setValue(v1); + this.so.waitUntilValueIs(v1, TWO_TICKS); + assertEquals(v1, this.so.getValue()); + } + + public void testWaitUntilValueIsNotObjectTimeout() throws Exception { + String v1 = "foo"; + this.so.setValue(v1); + String v2 = new String(v1); + this.so.waitUntilValueIsNot(v2, TWO_TICKS); + assertSame(v1, this.so.getValue()); + assertNotSame(v2, this.so.getValue()); + } + + public void testWaitUntilNotNullTimeout() throws Exception { + String v1 = "foo"; + this.so.setValue(v1); + this.so.waitUntilNotNull(TWO_TICKS); + assertEquals(v1, this.so.getValue()); + } + + public void testWaitToSetValueObjectTimeout() throws Exception { + this.so.setValue(null); + assertTrue(this.so.waitToSetValue(this.value, TWO_TICKS)); + assertEquals(this.value, this.so.getValue()); + } + + public void testWaitToSetNullTimeout() throws Exception { + this.so.setValue(this.value); + assertTrue(this.so.waitToSetNull(TWO_TICKS)); + assertNull(this.so.getValue()); + } + + public void testWaitToCommitTimeout() throws Exception { + this.so.setValue(this.value); + assertTrue(this.so.waitToCommit("foo", this.value, TWO_TICKS)); + assertEquals("foo", this.so.getValue()); + } + // ********** Command interface ********** private interface Command { void execute(SynchronizedObject<Object> so) throws InterruptedException; } + + + // ********** standard methods ********** + + public void testEquals() { + SynchronizedObject<Object> so2 = new SynchronizedObject<>(); + assertTrue(this.so.equals(this.so)); + assertFalse(this.so.equals(so2)); + } + + public void testHashCode() { + assertEquals(this.so.hashCode(), this.so.hashCode()); + } + + public void testClone() { + this.so.setValue(null); + SynchronizedObject<Object> clone = this.so.clone(); + assertNull(clone.getValue()); + assertNotSame(clone, this.so); + + this.so.setValue("foo"); + clone = this.so.clone(); + assertEquals("foo", clone.getValue()); + assertNotSame(clone, this.so); + } + + public void testSerialization() throws Exception { + this.so.setValue("foo"); + SynchronizedObject<Object> clone = TestTools.serialize(this.so); + assertNotSame(this.so, clone); + assertEquals("foo", clone.getValue()); + } + + public void testToString() { + assertEquals("[null]", this.so.toString()); + this.so.setValue("foo"); + assertEquals("[foo]", this.so.toString()); + } } |