Skip to main content
summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorBrian Vosburgh2016-07-13 18:16:36 +0000
committerBrian Vosburgh2017-05-18 22:37:52 +0000
commitdb9742d4e12b21208d30c7cd66c0fc3037739076 (patch)
tree6e9bafd0a703a2cf809c9e6f491f414aaad2ea8e /common
parent6c41d233cde62d444e7b7b6edf442f552fbbc366 (diff)
downloadwebtools.dali-db9742d4e12b21208d30c7cd66c0fc3037739076.tar.gz
webtools.dali-db9742d4e12b21208d30c7cd66c0fc3037739076.tar.xz
webtools.dali-db9742d4e12b21208d30c7cd66c0fc3037739076.zip
add some PVMTools tests
Diffstat (limited to 'common')
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java22
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/PropertyValueModelToolsTests.java865
2 files changed, 884 insertions, 3 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java
index 9e677be6e7..3932494f74 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java
@@ -111,7 +111,7 @@ public final class PropertyValueModelTools {
* @see #valueEquals_(PropertyValueModel, Object)
*/
public static PropertyValueModel<Boolean> valueNotEquals_(PropertyValueModel<?> propertyModel, Object value) {
- return valueAffirms_(propertyModel, PredicateTools.isNotEqual(value));
+ return valueAffirms(propertyModel, PredicateTools.isNotEqual(value));
}
/**
@@ -369,7 +369,7 @@ public final class PropertyValueModelTools {
* of the specified class, it will return the specified default value.
* <p>
* <strong>NB:</strong> If the wrapped value is <code>null</code>,
- * the model returns the specified default value.
+ * the model returns a <code>null</code> value.
*
* @see #filter(PropertyValueModel, Class)
* @see PredicateTools#instanceOf(Class)
@@ -379,6 +379,22 @@ public final class PropertyValueModelTools {
}
/**
+ * Construct a property value model that filters the specified
+ * property value model to return the wrapped value only if it is an instance
+ * of the specified class. If the wrapped value is <em>not</em> an instance
+ * of the specified class, it will return the specified default value.
+ * <p>
+ * <strong>NB:</strong> If the wrapped value is <code>null</code>,
+ * the model returns the specified default value.
+ *
+ * @see #filter(PropertyValueModel, Class)
+ * @see PredicateTools#instanceOf(Class)
+ */
+ public static <V> PropertyValueModel<V> filter_(PropertyValueModel<?> propertyModel, Class<V> clazz, V defaultValue) {
+ return transform_(propertyModel, TransformerTools.cast(TransformerTools.filteringTransformer(PredicateTools.instanceOf(clazz), defaultValue)));
+ }
+
+ /**
* Construct a property value model that wraps the specified
* property value model and filters its value with the specified
* filter. If the wrapped value passes the filter,
@@ -449,7 +465,7 @@ public final class PropertyValueModelTools {
* @see PluggablePropertyValueModel
*/
public static <V> PropertyValueModel<V> filter_(PropertyValueModel<? extends V> propertyModel, Predicate<? super V> filter, V defaultValue) {
- return transform(propertyModel, TransformerTools.filteringTransformer(filter, defaultValue));
+ return transform_(propertyModel, TransformerTools.filteringTransformer(filter, defaultValue));
}
/**
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/PropertyValueModelToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/PropertyValueModelToolsTests.java
index a501b5a9f4..ce91c5575e 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/PropertyValueModelToolsTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/PropertyValueModelToolsTests.java
@@ -12,6 +12,10 @@ package org.eclipse.jpt.common.utility.tests.internal.model.value;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.jpt.common.utility.closure.Closure;
import org.eclipse.jpt.common.utility.internal.ClassTools;
+import org.eclipse.jpt.common.utility.internal.StringTools;
+import org.eclipse.jpt.common.utility.internal.closure.ClosureAdapter;
+import org.eclipse.jpt.common.utility.internal.exception.RuntimeExceptionHandler;
+import org.eclipse.jpt.common.utility.internal.model.ChangeSupport;
import org.eclipse.jpt.common.utility.internal.model.value.CollectionValueModelTools;
import org.eclipse.jpt.common.utility.internal.model.value.PluggableModifiablePropertyValueModel;
import org.eclipse.jpt.common.utility.internal.model.value.PluggableModifiablePropertyValueModel.Adapter;
@@ -19,11 +23,14 @@ import org.eclipse.jpt.common.utility.internal.model.value.PluggablePropertyValu
import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools;
import org.eclipse.jpt.common.utility.internal.model.value.SimpleCollectionValueModel;
import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel;
+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.transformer.TransformerAdapter;
import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
+import org.eclipse.jpt.common.utility.predicate.Predicate;
import org.eclipse.jpt.common.utility.transformer.Transformer;
import junit.framework.TestCase;
@@ -145,6 +152,43 @@ public class PropertyValueModelToolsTests
assertNull(listener.event);
}
+ public void testValueEquals_() {
+ String string = "foo";
+ ModifiablePropertyValueModel<String> stringModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<Boolean> booleanModel = PropertyValueModelTools.valueEquals_(stringModel, string);
+ LocalListener listener = new LocalListener();
+ booleanModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals(Boolean.FALSE, booleanModel.getValue());
+ assertEquals("", stringModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ stringModel.setValue("foo");
+ assertEquals(Boolean.TRUE, booleanModel.getValue());
+ assertEquals("foo", stringModel.getValue());
+ assertEquals(Boolean.TRUE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue("bar");
+ assertEquals(Boolean.FALSE, booleanModel.getValue());
+ assertEquals("bar", stringModel.getValue());
+ assertEquals(Boolean.FALSE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue(null);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ booleanModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event);
+ }
+
public void testValueNotEquals() {
String string = "foo";
ModifiablePropertyValueModel<String> stringModel = new SimplePropertyValueModel<>("");
@@ -182,6 +226,43 @@ public class PropertyValueModelToolsTests
assertNull(listener.event);
}
+ public void testValueNotEquals_() {
+ String string = "foo";
+ ModifiablePropertyValueModel<String> stringModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<Boolean> booleanModel = PropertyValueModelTools.valueNotEquals_(stringModel, string);
+ LocalListener listener = new LocalListener();
+ booleanModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals(Boolean.TRUE, booleanModel.getValue());
+ assertEquals("", stringModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ stringModel.setValue("foo");
+ assertEquals(Boolean.FALSE, booleanModel.getValue());
+ assertEquals("foo", stringModel.getValue());
+ assertEquals(Boolean.FALSE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue("bar");
+ assertEquals(Boolean.TRUE, booleanModel.getValue());
+ assertEquals("bar", stringModel.getValue());
+ assertEquals(Boolean.TRUE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue(null);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ booleanModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event);
+ }
+
public void testValueIsIdentical() {
Object object0 = new Object();
Object object1 = new Object();
@@ -221,6 +302,45 @@ public class PropertyValueModelToolsTests
assertNull(listener.event);
}
+ public void testValueIsIdentical_() {
+ Object object0 = new Object();
+ Object object1 = new Object();
+ Object object2 = new Object();
+ ModifiablePropertyValueModel<Object> stringModel = new SimplePropertyValueModel<>(object0);
+ PropertyValueModel<Boolean> booleanModel = PropertyValueModelTools.valueIsIdentical_(stringModel, object1);
+ LocalListener listener = new LocalListener();
+ booleanModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals(Boolean.FALSE, booleanModel.getValue());
+ assertEquals(object0, stringModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ stringModel.setValue(object1);
+ assertEquals(Boolean.TRUE, booleanModel.getValue());
+ assertEquals(object1, stringModel.getValue());
+ assertEquals(Boolean.TRUE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue(object2);
+ assertEquals(Boolean.FALSE, booleanModel.getValue());
+ assertEquals(object2, stringModel.getValue());
+ assertEquals(Boolean.FALSE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue(null);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ booleanModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event);
+ }
+
public void testValueIsNotIdentical() {
Object object0 = new Object();
Object object1 = new Object();
@@ -260,6 +380,731 @@ public class PropertyValueModelToolsTests
assertNull(listener.event);
}
+ public void testValueIsNotIdentical_() {
+ Object object0 = new Object();
+ Object object1 = new Object();
+ Object object2 = new Object();
+ ModifiablePropertyValueModel<Object> stringModel = new SimplePropertyValueModel<>(object0);
+ PropertyValueModel<Boolean> booleanModel = PropertyValueModelTools.valueIsNotIdentical_(stringModel, object1);
+ LocalListener listener = new LocalListener();
+ booleanModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals(Boolean.TRUE, booleanModel.getValue());
+ assertEquals(object0, stringModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ stringModel.setValue(object1);
+ assertEquals(Boolean.FALSE, booleanModel.getValue());
+ assertEquals(object1, stringModel.getValue());
+ assertEquals(Boolean.FALSE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue(object2);
+ assertEquals(Boolean.TRUE, booleanModel.getValue());
+ assertEquals(object2, stringModel.getValue());
+ assertEquals(Boolean.TRUE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue(null);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ booleanModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testValueAffirms() {
+ String string = "foo";
+ ModifiablePropertyValueModel<String> stringModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<Boolean> booleanModel = PropertyValueModelTools.valueAffirms(stringModel, PredicateTools.isEqual(string), true);
+ LocalListener listener = new LocalListener();
+ booleanModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals(Boolean.FALSE, booleanModel.getValue());
+ assertEquals("", stringModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ stringModel.setValue("foo");
+ assertEquals(Boolean.TRUE, booleanModel.getValue());
+ assertEquals("foo", stringModel.getValue());
+ assertEquals(Boolean.TRUE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue("bar");
+ assertEquals(Boolean.FALSE, booleanModel.getValue());
+ assertEquals("bar", stringModel.getValue());
+ assertEquals(Boolean.FALSE, listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.setValue(null);
+ assertEquals(Boolean.TRUE, booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertEquals(Boolean.TRUE, listener.event.getNewValue());
+
+ listener.event = null;
+ booleanModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(booleanModel.getValue());
+ assertNull(stringModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilterModelClass() {
+ Class<String> clazz = String.class;
+ ModifiablePropertyValueModel<Object> objectModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<String> stringModel = PropertyValueModelTools.filter(objectModel, clazz);
+ LocalListener listener = new LocalListener();
+ stringModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals("", stringModel.getValue());
+ assertEquals("", objectModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ objectModel.setValue("foo");
+ assertEquals("foo", stringModel.getValue());
+ assertEquals("foo", objectModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ objectModel.setValue(Integer.valueOf(0));
+ assertNull(stringModel.getValue());
+ assertEquals(Integer.valueOf(0), objectModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ objectModel.setValue(null);
+ assertNull(stringModel.getValue());
+ assertNull(objectModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ stringModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(stringModel.getValue());
+ assertNull(objectModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilterModelClassObject() {
+ Class<String> clazz = String.class;
+ ModifiablePropertyValueModel<Object> objectModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<String> stringModel = PropertyValueModelTools.filter(objectModel, clazz, "XXX");
+ LocalListener listener = new LocalListener();
+ stringModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals("", stringModel.getValue());
+ assertEquals("", objectModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ objectModel.setValue("foo");
+ assertEquals("foo", stringModel.getValue());
+ assertEquals("foo", objectModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ objectModel.setValue(Integer.valueOf(0));
+ assertEquals("XXX", stringModel.getValue());
+ assertEquals(Integer.valueOf(0), objectModel.getValue());
+ assertEquals("XXX", listener.event.getNewValue());
+
+ listener.event = null;
+ objectModel.setValue(null);
+ assertNull(stringModel.getValue());
+ assertNull(objectModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ stringModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(stringModel.getValue());
+ assertNull(objectModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilter_ModelClassObject() {
+ Class<String> clazz = String.class;
+ ModifiablePropertyValueModel<Object> objectModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<String> stringModel = PropertyValueModelTools.filter_(objectModel, clazz, "XXX");
+ LocalListener listener = new LocalListener();
+ stringModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals("", stringModel.getValue());
+ assertEquals("", objectModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ objectModel.setValue("foo");
+ assertEquals("foo", stringModel.getValue());
+ assertEquals("foo", objectModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ objectModel.setValue(Integer.valueOf(0));
+ assertEquals("XXX", stringModel.getValue());
+ assertEquals(Integer.valueOf(0), objectModel.getValue());
+ assertEquals("XXX", listener.event.getNewValue());
+
+ listener.event = null;
+ objectModel.setValue(null);
+ assertEquals("XXX", stringModel.getValue());
+ assertNull(objectModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ stringModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(stringModel.getValue());
+ assertNull(objectModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilterModelPredicate() {
+ Predicate<String> predicate = new LengthIs(3);
+ ModifiablePropertyValueModel<String> innerModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<String> outerModel = PropertyValueModelTools.filter(innerModel, predicate);
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertNull(outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("foo");
+ assertEquals("foo", outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertNull(outerModel.getValue());
+ assertEquals("asdfasdf", innerModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue(null);
+ assertNull(outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("bar");
+ assertEquals("bar", outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertEquals("bar", listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilter_ModelPredicate() {
+ Predicate<String> predicate = new LengthIs_NullCheck(3);
+ ModifiablePropertyValueModel<String> innerModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<String> outerModel = PropertyValueModelTools.filter_(innerModel, predicate);
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertNull(outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("foo");
+ assertEquals("foo", outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertNull(outerModel.getValue());
+ assertEquals("asdfasdf", innerModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue(null);
+ assertNull(outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("bar");
+ assertEquals("bar", outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertEquals("bar", listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilter_ModelPredicate_NPE() {
+ Predicate<String> predicate = new LengthIs(3);
+ ModifiablePropertyValueModel<String> innerModel = new LocalSimplePropertyValueModel<>("");
+ PropertyValueModel<String> outerModel = PropertyValueModelTools.filter_(innerModel, predicate);
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertNull(outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("foo");
+ assertEquals("foo", outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertNull(outerModel.getValue());
+ assertEquals("asdfasdf", innerModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ boolean exCaught = true;
+ try {
+ innerModel.setValue(null);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ assertNull(outerModel.getValue()); // stays null...
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("bar");
+ assertEquals("bar", outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertEquals("bar", listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilterModelPredicateObject() {
+ Predicate<String> predicate = new LengthIs(3);
+ ModifiablePropertyValueModel<String> innerModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<String> outerModel = PropertyValueModelTools.filter(innerModel, predicate, "XXX");
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals("XXX", outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("foo");
+ assertEquals("foo", outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertEquals("XXX", outerModel.getValue());
+ assertEquals("XXX", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue(null);
+ assertEquals("XXX", outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("bar");
+ assertEquals("bar", outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertEquals("bar", listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilter_ModelPredicateObject() {
+ Predicate<String> predicate = new LengthIs_NullCheck(3);
+ ModifiablePropertyValueModel<String> innerModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<String> outerModel = PropertyValueModelTools.filter_(innerModel, predicate, "XXX");
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals("XXX", outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("foo");
+ assertEquals("foo", outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertEquals("XXX", outerModel.getValue());
+ assertEquals("XXX", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue(null);
+ assertEquals("XXX", outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("bar");
+ assertEquals("bar", outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertEquals("bar", listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testFilter_ModelPredicateObject_NPE() {
+ Predicate<String> predicate = new LengthIs(3);
+ ModifiablePropertyValueModel<String> innerModel = new LocalSimplePropertyValueModel<>("");
+ PropertyValueModel<String> outerModel = PropertyValueModelTools.filter_(innerModel, predicate, "XXX");
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals("XXX", outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("foo");
+ assertEquals("foo", outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertEquals("XXX", outerModel.getValue());
+ assertEquals("XXX", listener.event.getNewValue());
+
+ listener.event = null;
+ listener.event = null;
+ boolean exCaught = true;
+ try {
+ innerModel.setValue(null);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ assertEquals("XXX", outerModel.getValue()); // unchanged
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("bar");
+ assertEquals("bar", outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertEquals("bar", listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testNullCheck() {
+ ModifiablePropertyValueModel<String> innerModel = new SimplePropertyValueModel<>("");
+ PropertyValueModel<String> outerModel = PropertyValueModelTools.nullCheck(innerModel, "null");
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals("", outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("foo");
+ assertEquals("foo", outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertEquals("foo", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue("bar");
+ assertEquals("bar", outerModel.getValue());
+ assertEquals("bar", innerModel.getValue());
+ assertEquals("bar", listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue(null);
+ assertEquals("null", outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertEquals("null", listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public static class LengthIs
+ extends PredicateAdapter<String>
+ {
+ private final int length;
+ public LengthIs(int length) {
+ super();
+ this.length = length;
+ }
+ @Override
+ public boolean evaluate(String string) {
+ return string.length() == this.length;
+ }
+ }
+
+ public static class LengthIs_NullCheck
+ extends LengthIs
+ {
+ public LengthIs_NullCheck(int length) {
+ super(length);
+ }
+ @Override
+ public boolean evaluate(String string) {
+ return (string != null) && super.evaluate(string);
+ }
+ }
+
+ public static class LocalSimplePropertyValueModel<V>
+ extends SimplePropertyValueModel<V>
+ {
+ public LocalSimplePropertyValueModel(V value) {
+ super(value);
+ }
+ @Override
+ protected ChangeSupport buildChangeSupport() {
+ return new ChangeSupport(this, RuntimeExceptionHandler.instance());
+ }
+ }
+
+ public void testTransformModelTransformerClosure() {
+ ModifiablePropertyValueModel<String> innerModel = new SimplePropertyValueModel<>("foo");
+ Closure<Integer> closure = new SetStringModelClosure(innerModel);
+ ModifiablePropertyValueModel<Integer> outerModel = PropertyValueModelTools.transform(innerModel, LENGTH_TRANSFORMER, closure);
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals(Integer.valueOf(3), outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("");
+ assertEquals(Integer.valueOf(0), outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertEquals(Integer.valueOf(0), listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue(null);
+ assertNull(outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertNull(listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertEquals(Integer.valueOf(8), outerModel.getValue());
+ assertEquals("asdfasdf", innerModel.getValue());
+ assertEquals(Integer.valueOf(8), listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.setValue(Integer.valueOf(5));
+ assertEquals(Integer.valueOf(5), outerModel.getValue());
+ assertEquals("XXXXX", innerModel.getValue());
+ assertEquals(Integer.valueOf(5), listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("XXXXX", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testTransform_ModelTransformerClosure() {
+ ModifiablePropertyValueModel<String> innerModel = new LocalSimplePropertyValueModel<>("foo");
+ Closure<Integer> closure = new SetStringModelClosure_NullCheck(innerModel);
+ ModifiablePropertyValueModel<Integer> outerModel = PropertyValueModelTools.transform_(innerModel, NULL_CHECK_LENGTH_TRANSFORMER, closure);
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals(Integer.valueOf(3), outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("");
+ assertEquals(Integer.valueOf(0), outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertEquals(Integer.valueOf(0), listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue(null);
+ assertEquals(Integer.valueOf(0), outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertEquals(Integer.valueOf(8), outerModel.getValue());
+ assertEquals("asdfasdf", innerModel.getValue());
+ assertEquals(Integer.valueOf(8), listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.setValue(Integer.valueOf(5));
+ assertEquals(Integer.valueOf(5), outerModel.getValue());
+ assertEquals("XXXXX", innerModel.getValue());
+ assertEquals(Integer.valueOf(5), listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.setValue(null);
+ assertEquals(Integer.valueOf(0), outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertEquals(Integer.valueOf(0), listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public void testTransform_ModelTransformerClosure_NPE() {
+ ModifiablePropertyValueModel<String> innerModel = new LocalSimplePropertyValueModel<>("foo");
+ Closure<Integer> closure = new SetStringModelClosure(innerModel);
+ ModifiablePropertyValueModel<Integer> outerModel = PropertyValueModelTools.transform_(innerModel, NULL_CHECK_LENGTH_TRANSFORMER, closure);
+ LocalListener listener = new LocalListener();
+ outerModel.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+
+ listener.event = null;
+ assertEquals(Integer.valueOf(3), outerModel.getValue());
+ assertEquals("foo", innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("");
+ assertEquals(Integer.valueOf(0), outerModel.getValue());
+ assertEquals("", innerModel.getValue());
+ assertEquals(Integer.valueOf(0), listener.event.getNewValue());
+
+ listener.event = null;
+ innerModel.setValue(null);
+ assertEquals(Integer.valueOf(0), outerModel.getValue());
+ assertNull(innerModel.getValue());
+ assertNull(listener.event);
+
+ listener.event = null;
+ innerModel.setValue("asdfasdf");
+ assertEquals(Integer.valueOf(8), outerModel.getValue());
+ assertEquals("asdfasdf", innerModel.getValue());
+ assertEquals(Integer.valueOf(8), listener.event.getNewValue());
+
+ listener.event = null;
+ outerModel.setValue(Integer.valueOf(5));
+ assertEquals(Integer.valueOf(5), outerModel.getValue());
+ assertEquals("XXXXX", innerModel.getValue());
+ assertEquals(Integer.valueOf(5), listener.event.getNewValue());
+
+ listener.event = null;
+ boolean exCaught = false;
+ try {
+ outerModel.setValue(null);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ assertEquals(Integer.valueOf(5), outerModel.getValue()); // unchanged
+ assertEquals("XXXXX", innerModel.getValue()); // unchanged
+ assertNull(listener.event);
+
+ listener.event = null;
+ outerModel.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertNull(outerModel.getValue());
+ assertEquals("XXXXX", innerModel.getValue());
+ assertNull(listener.event);
+ }
+
+ public static final Transformer<String, Integer> LENGTH_TRANSFORMER = new LengthTransformer();
+ public static class LengthTransformer
+ extends TransformerAdapter<String, Integer>
+ {
+ @Override
+ public Integer transform(String string) {
+ return Integer.valueOf(string.length());
+ }
+ }
+
+ public static final Transformer<String, Integer> NULL_CHECK_LENGTH_TRANSFORMER = new NullCheckLengthTransformer();
+ public static class NullCheckLengthTransformer
+ extends TransformerAdapter<String, Integer>
+ {
+ @Override
+ public Integer transform(String string) {
+ return Integer.valueOf((string != null) ? string.length() : 0);
+ }
+ }
+
+ public static class SetStringModelClosure
+ extends ClosureAdapter<Integer>
+ {
+ private final ModifiablePropertyValueModel<? super String> model;
+ public SetStringModelClosure(ModifiablePropertyValueModel<? super String> model) {
+ super();
+ this.model = model;
+ }
+ @Override
+ public void execute(Integer integer) {
+ this.model.setValue(StringTools.repeat("X", integer.intValue()));
+ }
+ }
+
+ public static class SetStringModelClosure_NullCheck
+ extends SetStringModelClosure
+ {
+ public SetStringModelClosure_NullCheck(ModifiablePropertyValueModel<? super String> model) {
+ super(model);
+ }
+ @Override
+ public void execute(Integer integer) {
+ if (integer == null) {
+ integer = Integer.valueOf(0);
+ }
+ super.execute(integer);
+ }
+ }
+
public void testModifiablePropertyValueModel() {
ModifiablePropertyValueModel<String> doubleStringModel = new SimplePropertyValueModel<>("foofoo");
PluggableModifiablePropertyValueModel.Adapter.Factory<String> factory = new HalfStringModelAdapter.Factory(doubleStringModel);
@@ -428,6 +1273,26 @@ public class PropertyValueModelToolsTests
}
}
+ public void testNullCheckValueTransformer() {
+ SimplePropertyValueModel<String> stringModel = new SimplePropertyValueModel<>("foo");
+ Transformer<PropertyValueModel<String>, String> transformer = PropertyValueModelTools.nullCheckValueTransformer();
+ assertEquals("foo", transformer.transform(stringModel));
+ assertNull(transformer.transform(null));
+ }
+
+ public void testNullCheckValueTransformerObject() {
+ SimplePropertyValueModel<String> stringModel = new SimplePropertyValueModel<>("foo");
+ Transformer<PropertyValueModel<String>, String> transformer = PropertyValueModelTools.nullCheckValueTransformer("XXX");
+ assertEquals("foo", transformer.transform(stringModel));
+ assertEquals("XXX", transformer.transform(null));
+ }
+
+ public void testValueTransformer() {
+ SimplePropertyValueModel<String> stringModel = new SimplePropertyValueModel<>("foo");
+ Transformer<PropertyValueModel<String>, String> transformer = PropertyValueModelTools.valueTransformer();
+ assertEquals("foo", transformer.transform(stringModel));
+ }
+
public void testConstructor() {
boolean exCaught = false;
try {

Back to the top