diff options
Diffstat (limited to 'common/tests')
13 files changed, 2272 insertions, 201 deletions
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CollectionPropertyValueModelAdapterTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CollectionPropertyValueModelAdapterTests.java index bdf7a7e20b..bc9ae272a0 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CollectionPropertyValueModelAdapterTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CollectionPropertyValueModelAdapterTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2015 Oracle. All rights reserved. + * Copyright (c) 2007, 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. @@ -10,13 +10,12 @@ package org.eclipse.jpt.common.utility.tests.internal.model.value; import java.util.Collection; - -import junit.framework.TestCase; - +import org.eclipse.jpt.common.utility.internal.ObjectTools; import org.eclipse.jpt.common.utility.internal.collection.CollectionTools; -import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools; import org.eclipse.jpt.common.utility.internal.model.AbstractModel; -import org.eclipse.jpt.common.utility.internal.model.value.CollectionPropertyValueModelAdapter; +import org.eclipse.jpt.common.utility.internal.model.value.CollectionPluggablePropertyValueModelAdapter; +import org.eclipse.jpt.common.utility.internal.model.value.CollectionValueModelTools; +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.model.event.PropertyChangeEvent; import org.eclipse.jpt.common.utility.model.listener.ChangeAdapter; @@ -24,13 +23,16 @@ import org.eclipse.jpt.common.utility.model.listener.ChangeListener; import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener; import org.eclipse.jpt.common.utility.model.value.CollectionValueModel; import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; -import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; import org.eclipse.jpt.common.utility.tests.internal.TestTools; +import org.eclipse.jpt.common.utility.transformer.Transformer; +import junit.framework.TestCase; @SuppressWarnings("nls") -public class CollectionPropertyValueModelAdapterTests extends TestCase { - private ModifiablePropertyValueModel<Boolean> adapter; - private SimpleCollectionValueModel<String> wrappedCollectionHolder; +public class CollectionPropertyValueModelAdapterTests + extends TestCase +{ + private PropertyValueModel<Boolean> adapter; + private SimpleCollectionValueModel<String> collectionModel; PropertyChangeEvent event; public CollectionPropertyValueModelAdapterTests(String name) { @@ -40,8 +42,8 @@ public class CollectionPropertyValueModelAdapterTests extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); - this.wrappedCollectionHolder = new SimpleCollectionValueModel<String>(); - this.adapter = new LocalAdapter(this.wrappedCollectionHolder, "666"); + this.collectionModel = new SimpleCollectionValueModel<>(); + this.adapter = CollectionValueModelTools.propertyValueModel(this.collectionModel, new LocalTransformer("666")); this.event = null; } @@ -52,11 +54,12 @@ public class CollectionPropertyValueModelAdapterTests extends TestCase { } private boolean booleanValue() { - return this.adapter.getValue().booleanValue(); + Boolean value = this.adapter.getValue(); + return (value != null) && value.booleanValue(); } private Collection<String> wrappedCollection() { - return CollectionTools.hashBag(this.wrappedCollectionHolder.iterator()); + return CollectionTools.hashBag(this.collectionModel.iterator()); } public void testValue() { @@ -66,41 +69,25 @@ public class CollectionPropertyValueModelAdapterTests extends TestCase { assertFalse(this.booleanValue()); assertFalse(this.wrappedCollection().contains("666")); - this.wrappedCollectionHolder.add("111"); + this.collectionModel.add("111"); assertFalse(this.booleanValue()); - this.wrappedCollectionHolder.add("222"); + this.collectionModel.add("222"); assertFalse(this.booleanValue()); - this.wrappedCollectionHolder.add("666"); + this.collectionModel.add("666"); assertTrue(this.booleanValue()); assertTrue(this.wrappedCollection().contains("666")); - this.wrappedCollectionHolder.remove("666"); + this.collectionModel.remove("666"); assertFalse(this.booleanValue()); assertFalse(this.wrappedCollection().contains("666")); - this.wrappedCollectionHolder.add("666"); + this.collectionModel.add("666"); assertTrue(this.booleanValue()); assertTrue(this.wrappedCollection().contains("666")); - this.wrappedCollectionHolder.clear(); - assertFalse(this.booleanValue()); - assertFalse(this.wrappedCollection().contains("666")); - } - - public void testSetValue() { - this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, new PropertyChangeListener() { - public void propertyChanged(PropertyChangeEvent e) {/* OK */} - }); - assertFalse(this.booleanValue()); - assertFalse(this.wrappedCollection().contains("666")); - - this.adapter.setValue(Boolean.TRUE); - assertTrue(this.booleanValue()); - assertTrue(this.wrappedCollection().contains("666")); - - this.adapter.setValue(Boolean.FALSE); + this.collectionModel.clear(); assertFalse(this.booleanValue()); assertFalse(this.wrappedCollection().contains("666")); } @@ -113,22 +100,22 @@ public class CollectionPropertyValueModelAdapterTests extends TestCase { }); assertNull(this.event); - this.wrappedCollectionHolder.add("111"); + this.collectionModel.add("111"); assertNull(this.event); - this.wrappedCollectionHolder.add("222"); + this.collectionModel.add("222"); assertNull(this.event); - this.wrappedCollectionHolder.add("666"); + this.collectionModel.add("666"); this.verifyEvent(false, true); - this.wrappedCollectionHolder.remove("666"); + this.collectionModel.remove("666"); this.verifyEvent(true, false); - this.wrappedCollectionHolder.add("666"); + this.collectionModel.add("666"); this.verifyEvent(false, true); - this.wrappedCollectionHolder.clear(); + this.collectionModel.clear(); this.verifyEvent(true, false); } @@ -144,7 +131,7 @@ public class CollectionPropertyValueModelAdapterTests extends TestCase { public void propertyChanged(PropertyChangeEvent e) {/* OK */} }; this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener); - this.wrappedCollectionHolder.add("666"); + this.collectionModel.add("666"); assertTrue(this.booleanValue()); assertTrue(this.wrappedCollection().contains("666")); @@ -159,7 +146,7 @@ public class CollectionPropertyValueModelAdapterTests extends TestCase { public void testHasListeners() { assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); - assertFalse(((AbstractModel) this.wrappedCollectionHolder).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); + assertFalse(((AbstractModel) this.collectionModel).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); ChangeListener listener = new ChangeAdapter() { @Override @@ -167,73 +154,130 @@ public class CollectionPropertyValueModelAdapterTests extends TestCase { }; this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener); assertTrue(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); - assertTrue(((AbstractModel) this.wrappedCollectionHolder).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); + assertTrue(((AbstractModel) this.collectionModel).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); this.adapter.removePropertyChangeListener(PropertyValueModel.VALUE, listener); assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); - assertFalse(((AbstractModel) this.wrappedCollectionHolder).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); + assertFalse(((AbstractModel) this.collectionModel).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); this.adapter.addChangeListener(listener); assertTrue(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); - assertTrue(((AbstractModel) this.wrappedCollectionHolder).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); + assertTrue(((AbstractModel) this.collectionModel).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); this.adapter.removeChangeListener(listener); assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); - assertFalse(((AbstractModel) this.wrappedCollectionHolder).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); + assertFalse(((AbstractModel) this.collectionModel).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); + } + + public void testToString1() { + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, new PropertyChangeListener() { + public void propertyChanged(PropertyChangeEvent e) {/* OK */} + }); + assertTrue(this.adapter.toString().endsWith("(false)")); + this.collectionModel.add("666"); + assertTrue(this.adapter.toString().endsWith("(true)")); + } + + public void testToString3() { + CollectionPluggablePropertyValueModelAdapter.Factory<String, Boolean> f = new CollectionPluggablePropertyValueModelAdapter.Factory<>(this.collectionModel, new LocalTransformer("666")); + assertTrue(f.toString().indexOf("Factory") != -1); + } + + public void testToString4() { + PropertyChangeListener listener = new PropertyChangeListener() { + public void propertyChanged(PropertyChangeEvent e) {/* OK */} + }; + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + Object a = ObjectTools.get(this.adapter, "adapter"); + Object l = ObjectTools.get(a, "listener"); + assertTrue(l.toString().indexOf("AdapterListener") != -1); + } + + public void testCtor_NPE1A() { + Object object; + boolean exCaught = false; + try { + object = CollectionValueModelTools.propertyValueModel(null, new LocalTransformer("666")); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE1B() { + Object object; + boolean exCaught = false; + try { + object = CollectionValueModelTools.propertyValueModel(null, new LocalTransformer("666")); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE2A() { + Object object; + boolean exCaught = false; + try { + object = CollectionValueModelTools.propertyValueModel(this.collectionModel, null); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE2B() { + Object object; + boolean exCaught = false; + try { + object = CollectionValueModelTools.propertyValueModel(this.collectionModel, null); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE4() { + Object object; + boolean exCaught = false; + try { + object = PropertyValueModelTools.propertyValueModel(null); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); } // ********** member class ********** /** - * the value is true if the wrapped collection contains the specified item, - * otherwise the value is false + * Transform the collection to <code>true</code> if it contains the specified item, + * otherwise transform it to <code>false</code>. */ - static class LocalAdapter - extends CollectionPropertyValueModelAdapter<Boolean, String> - implements ModifiablePropertyValueModel<Boolean> + static class LocalTransformer + implements Transformer<Collection<String>, Boolean> { - private String item; + private final String item; - LocalAdapter(CollectionValueModel<String> collectionHolder, String item) { - super(collectionHolder); + LocalTransformer(String item) { + super(); this.item = item; } - // ********** CollectionPropertyValueModelAdapter implementation ********** - /** - * always return a Boolean - */ - @Override - public Boolean getValue() { - Boolean result = super.getValue(); - return (result == null) ? Boolean.FALSE : result; - } - @SuppressWarnings("unchecked") - public void setValue(Boolean value) { - if (this.booleanValue()) { - if ( ! this.booleanValueOf(value)) { - // the value is changing from true to false - ((SimpleCollectionValueModel<String>) this.collectionModel).remove(this.item); - } - } else { - if (this.booleanValueOf(value)) { - // the value is changing from false to true - ((SimpleCollectionValueModel<String>) this.collectionModel).add(this.item); - } - } - } - @Override - protected Boolean buildValue() { - return Boolean.valueOf(IteratorTools.contains(this.collectionModel.iterator(), this.item)); + public Boolean transform(Collection<String> collection) { + return Boolean.valueOf(collection.contains(this.item)); } - // ********** internal methods ********** - private boolean booleanValue() { - return this.booleanValueOf(this.value); - } - private boolean booleanValueOf(Object b) { - return (b == null) ? false : ((Boolean) b).booleanValue(); + @Override + public String toString() { + return ObjectTools.toString(this, this.item); } } } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CollectionValueModelToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CollectionValueModelToolsTests.java new file mode 100644 index 0000000000..e7f0f726a1 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CollectionValueModelToolsTests.java @@ -0,0 +1,438 @@ +/******************************************************************************* + * 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.model.value; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import org.eclipse.jpt.common.utility.internal.ClassTools; +import org.eclipse.jpt.common.utility.internal.closure.BooleanClosure; +import org.eclipse.jpt.common.utility.internal.model.value.CollectionValueModelTools; +import org.eclipse.jpt.common.utility.internal.model.value.SimpleCollectionValueModel; +import org.eclipse.jpt.common.utility.internal.predicate.PredicateTools; +import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent; +import org.eclipse.jpt.common.utility.model.listener.PropertyChangeAdapter; +import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; +import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; +import junit.framework.TestCase; + +@SuppressWarnings("nls") +public class CollectionValueModelToolsTests + extends TestCase +{ + public CollectionValueModelToolsTests(String name) { + super(name); + } + + public void testFirstElementPVMAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + PropertyValueModel<String> pvm = CollectionValueModelTools.firstElementPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertNull(pvm.getValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertEquals("foo", pvm.getValue()); + assertEquals("foo", listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertTrue(pvm.getValue().equals("foo") || pvm.getValue().equals("bar")); + + listener.event = null; + cvm.remove("foo"); + assertEquals("bar", pvm.getValue()); + // unpredictable + // assertEquals("bar", listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertNull(pvm.getValue()); + assertNull(listener.event.getNewValue()); + } + + public void testLastElementPVMAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + PropertyValueModel<String> pvm = CollectionValueModelTools.lastElementPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertNull(pvm.getValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertEquals("foo", pvm.getValue()); + assertEquals("foo", listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertTrue(pvm.getValue().equals("foo") || pvm.getValue().equals("bar")); + + listener.event = null; + cvm.remove("foo"); + assertEquals("bar", pvm.getValue()); + // unpredictable + // assertEquals("bar", listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertNull(pvm.getValue()); + assertNull(listener.event.getNewValue()); + } + + public void testSingleElementPVMAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + PropertyValueModel<String> pvm = CollectionValueModelTools.singleElementPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertNull(pvm.getValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertEquals("foo", pvm.getValue()); + assertEquals("foo", listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertNull(pvm.getValue()); + assertNull(listener.event.getNewValue()); + + listener.event = null; + cvm.remove("foo"); + assertEquals("bar", pvm.getValue()); + assertEquals("bar", listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertNull(pvm.getValue()); + assertNull(listener.event.getNewValue()); + } + + public void testIsNotEmptyPropertyValueModelAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + PropertyValueModel<Boolean> pvm = CollectionValueModelTools.isNotEmptyPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + } + + public void testIsNotEmptyModifiablePropertyValueModelAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + BooleanClosure.Adapter adapter = new NotEmptyBooleanClosureAdapter(cvm); + ModifiablePropertyValueModel<Boolean> pvm = CollectionValueModelTools.isNotEmptyModifiablePropertyValueModel(cvm, adapter); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + pvm.setValue(Boolean.FALSE); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + assertEquals(0, cvm.size()); + + listener.event = null; + pvm.setValue(Boolean.TRUE); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + assertEquals(2, cvm.size()); + assertTrue(cvm.contains("baz")); + assertTrue(cvm.contains("xxx")); + + listener.event = null; + cvm.remove("baz"); + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("xxx"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + } + + public static class NotEmptyBooleanClosureAdapter + implements BooleanClosure.Adapter + { + final SimpleCollectionValueModel<String> cvm; + public NotEmptyBooleanClosureAdapter(SimpleCollectionValueModel<String> cvm) { + this.cvm = cvm; + } + public void execute(boolean argument) { + if (argument) { + ArrayList<String> list = new ArrayList<>(); + list.add("baz"); + list.add("xxx"); + this.cvm.setValues(list); + } else { + this.cvm.clear(); + } + } + } + + public void testIsEmptyPropertyValueModelAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + PropertyValueModel<Boolean> pvm = CollectionValueModelTools.isEmptyPropertyValueModel(cvm); + this.verifyIsEmptyPropertyValueModelAdapter(cvm, pvm); + } + + private void verifyIsEmptyPropertyValueModelAdapter(SimpleCollectionValueModel<String> cvm, PropertyValueModel<Boolean> pvm) { + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("bar"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + } + + public void testBooleanPVM() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + PropertyValueModel<Boolean> pvm = CollectionValueModelTools.booleanPropertyValueModel(cvm, PredicateTools.collectionIsEmptyPredicate()); + this.verifyIsEmptyPropertyValueModelAdapter(cvm, pvm); + } + + public void testIsEmptyModifiablePropertyValueModelAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + BooleanClosure.Adapter adapter = new EmptyBooleanClosureAdapter(cvm); + ModifiablePropertyValueModel<Boolean> pvm = CollectionValueModelTools.isEmptyModifiablePropertyValueModel(cvm, adapter); + this.verifyIsEmptyModifiablePropertyValueModelAdapter(cvm, pvm); + } + + public void testBooleanModifiablePVM() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + BooleanClosure.Adapter adapter = new EmptyBooleanClosureAdapter(cvm); + ModifiablePropertyValueModel<Boolean> pvm = CollectionValueModelTools.booleanModifiablePropertyValueModel(cvm, PredicateTools.collectionIsEmptyPredicate(), adapter); + this.verifyIsEmptyPropertyValueModelAdapter(cvm, pvm); + } + + private void verifyIsEmptyModifiablePropertyValueModelAdapter(SimpleCollectionValueModel<String> cvm, ModifiablePropertyValueModel<Boolean> pvm) { + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + pvm.setValue(Boolean.TRUE); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + assertEquals(0, cvm.size()); + + listener.event = null; + pvm.setValue(Boolean.FALSE); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + assertEquals(2, cvm.size()); + assertTrue(cvm.contains("baz")); + assertTrue(cvm.contains("xxx")); + + listener.event = null; + cvm.remove("baz"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("xxx"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + } + + public static class EmptyBooleanClosureAdapter + implements BooleanClosure.Adapter + { + final SimpleCollectionValueModel<String> cvm; + public EmptyBooleanClosureAdapter(SimpleCollectionValueModel<String> cvm) { + this.cvm = cvm; + } + public void execute(boolean argument) { + if (argument) { + this.cvm.clear(); + } else { + ArrayList<String> list = new ArrayList<>(); + list.add("baz"); + list.add("xxx"); + this.cvm.setValues(list); + } + } + } + + public void testContainsSingleElementPropertyValueModelAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + PropertyValueModel<Boolean> pvm = CollectionValueModelTools.containsSingleElementPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + } + + public void testSizeEqualsPropertyValueModelAdapter() { + SimpleCollectionValueModel<String> cvm = new SimpleCollectionValueModel<>(); + PropertyValueModel<Boolean> pvm = CollectionValueModelTools.sizeEqualsPropertyValueModel(cvm, 2); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("bar"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("baz"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("baz"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + } + + public static class LocalPropertyChangeListener + extends PropertyChangeAdapter + { + public PropertyChangeEvent event; + public LocalPropertyChangeListener() { + super(); + } + @Override + public void propertyChanged(PropertyChangeEvent e) { + this.event = e; + } + } + + public void testConstructor() { + boolean exCaught = false; + try { + Object o = ClassTools.newInstance(CollectionValueModelTools.class); + fail("bogus: " + o); + } catch (RuntimeException ex) { + if (ex.getCause() instanceof InvocationTargetException) { + if (ex.getCause().getCause() instanceof UnsupportedOperationException) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositeBooleanPropertyValueModelTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositeAndBooleanPropertyValueModelTests.java index 29201ce800..a6de1c427a 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositeBooleanPropertyValueModelTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositeAndBooleanPropertyValueModelTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2015 Oracle. All rights reserved. + * Copyright (c) 2010, 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. @@ -10,22 +10,23 @@ package org.eclipse.jpt.common.utility.tests.internal.model.value; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; - -import junit.framework.TestCase; - -import org.eclipse.jpt.common.utility.internal.model.value.CompositeBooleanPropertyValueModel; +import org.eclipse.jpt.common.utility.internal.model.value.CollectionValueModelTools; 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.model.event.PropertyChangeEvent; import org.eclipse.jpt.common.utility.model.listener.ChangeAdapter; import org.eclipse.jpt.common.utility.model.listener.ChangeListener; import org.eclipse.jpt.common.utility.model.value.CollectionValueModel; -import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; +import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; import org.eclipse.jpt.common.utility.tests.internal.TestTools; +import junit.framework.TestCase; -public class CompositeBooleanPropertyValueModelTests extends TestCase { +public class CompositeAndBooleanPropertyValueModelTests + extends TestCase +{ private SimplePropertyValueModel<Boolean> pvm1; private ModifiablePropertyValueModel<Boolean> pvm2; private ModifiablePropertyValueModel<Boolean> pvm3; @@ -36,29 +37,29 @@ public class CompositeBooleanPropertyValueModelTests extends TestCase { PropertyChangeEvent event; - public CompositeBooleanPropertyValueModelTests(String name) { + public CompositeAndBooleanPropertyValueModelTests(String name) { super(name); } @Override protected void setUp() throws Exception { super.setUp(); - this.pvm1 = new SimplePropertyValueModel<Boolean>(Boolean.TRUE); - this.pvm2 = new SimplePropertyValueModel<Boolean>(Boolean.TRUE); - this.pvm3 = new SimplePropertyValueModel<Boolean>(Boolean.TRUE); - this.pvm4 = new SimplePropertyValueModel<Boolean>(Boolean.TRUE); - this.collection = new ArrayList<ModifiablePropertyValueModel<Boolean>>(); + this.pvm1 = new SimplePropertyValueModel<>(Boolean.TRUE); + this.pvm2 = new SimplePropertyValueModel<>(Boolean.TRUE); + this.pvm3 = new SimplePropertyValueModel<>(Boolean.TRUE); + this.pvm4 = new SimplePropertyValueModel<>(Boolean.TRUE); + this.collection = new ArrayList<>(); this.collection.add(this.pvm1); this.collection.add(this.pvm2); this.collection.add(this.pvm3); this.collection.add(this.pvm4); - this.cvm = new SimpleCollectionValueModel<ModifiablePropertyValueModel<Boolean>>(this.collection); + this.cvm = new SimpleCollectionValueModel<>(this.collection); this.compositePVM = this.buildCompositePVM(this.cvm); } private PropertyValueModel<Boolean> buildCompositePVM(CollectionValueModel<ModifiablePropertyValueModel<Boolean>> pvms) { - return CompositeBooleanPropertyValueModel.and(pvms); + return CollectionValueModelTools.and(pvms); } @Override @@ -67,7 +68,23 @@ public class CompositeBooleanPropertyValueModelTests extends TestCase { super.tearDown(); } - public void testGetValue() { + public void testGetValue1() { + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertTrue(this.compositePVM.getValue().booleanValue()); + } + + public void testGetValue2() { + this.compositePVM = CollectionValueModelTools.and(this.pvm1, this.pvm2, this.pvm3, this.pvm4); + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertTrue(this.compositePVM.getValue().booleanValue()); + } + + public void testGetValue3() { + this.compositePVM = CollectionValueModelTools.and(Arrays.asList(this.pvm1, this.pvm2, this.pvm3, this.pvm4)); assertNull(this.compositePVM.getValue()); ChangeListener listener = this.buildListener(); this.compositePVM.addChangeListener(listener); @@ -136,7 +153,7 @@ public class CompositeBooleanPropertyValueModelTests extends TestCase { private void verifyCollectionChange() { this.event = null; - ModifiablePropertyValueModel<Boolean> pvm = new SimplePropertyValueModel<Boolean>(Boolean.FALSE); + ModifiablePropertyValueModel<Boolean> pvm = new SimplePropertyValueModel<>(Boolean.FALSE); this.cvm.add(pvm); this.verifyEvent(true, false); @@ -145,15 +162,23 @@ public class CompositeBooleanPropertyValueModelTests extends TestCase { this.verifyEvent(false, true); this.event = null; + this.cvm.add(pvm); + this.verifyEvent(true, false); + + this.event = null; this.cvm.clear(); - this.verifyEvent(Boolean.TRUE, null); + this.verifyEvent(false, true); - Collection<ModifiablePropertyValueModel<Boolean>> c2 = new ArrayList<ModifiablePropertyValueModel<Boolean>>(); + this.event = null; + this.cvm.add(pvm); + this.verifyEvent(true, false); + + Collection<ModifiablePropertyValueModel<Boolean>> c2 = new ArrayList<>(); c2.add(this.pvm1); c2.add(this.pvm2); this.event = null; this.cvm.setValues(c2); - this.verifyEvent(null, Boolean.TRUE); + this.verifyEvent(false, true); } public void testLazyListening1() { @@ -182,7 +207,7 @@ public class CompositeBooleanPropertyValueModelTests extends TestCase { return new ChangeAdapter() { @Override public void propertyChanged(PropertyChangeEvent e) { - CompositeBooleanPropertyValueModelTests.this.event = e; + CompositeAndBooleanPropertyValueModelTests.this.event = e; } }; } @@ -192,6 +217,7 @@ public class CompositeBooleanPropertyValueModelTests extends TestCase { } private void verifyEvent(Boolean oldValue, Boolean newValue) { + assertNotNull(this.event); assertEquals(this.compositePVM, this.event.getSource()); assertEquals(PropertyValueModel.VALUE, this.event.getPropertyName()); assertEquals(oldValue, this.event.getOldValue()); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositeOrBooleanPropertyValueModelTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositeOrBooleanPropertyValueModelTests.java new file mode 100644 index 0000000000..a85722c639 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositeOrBooleanPropertyValueModelTests.java @@ -0,0 +1,227 @@ +/******************************************************************************* + * Copyright (c) 2010, 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.model.value; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import org.eclipse.jpt.common.utility.internal.model.value.CollectionValueModelTools; +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.model.event.PropertyChangeEvent; +import org.eclipse.jpt.common.utility.model.listener.ChangeAdapter; +import org.eclipse.jpt.common.utility.model.listener.ChangeListener; +import org.eclipse.jpt.common.utility.model.value.CollectionValueModel; +import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; +import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; +import org.eclipse.jpt.common.utility.tests.internal.TestTools; +import junit.framework.TestCase; + +public class CompositeOrBooleanPropertyValueModelTests + extends TestCase +{ + private SimplePropertyValueModel<Boolean> pvm1; + private ModifiablePropertyValueModel<Boolean> pvm2; + private ModifiablePropertyValueModel<Boolean> pvm3; + private ModifiablePropertyValueModel<Boolean> pvm4; + private Collection<ModifiablePropertyValueModel<Boolean>> collection; + private SimpleCollectionValueModel<ModifiablePropertyValueModel<Boolean>> cvm; + private PropertyValueModel<Boolean> compositePVM; + PropertyChangeEvent event; + + + public CompositeOrBooleanPropertyValueModelTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.pvm1 = new SimplePropertyValueModel<>(Boolean.FALSE); + this.pvm2 = new SimplePropertyValueModel<>(Boolean.FALSE); + this.pvm3 = new SimplePropertyValueModel<>(Boolean.FALSE); + this.pvm4 = new SimplePropertyValueModel<>(Boolean.FALSE); + this.collection = new ArrayList<>(); + this.collection.add(this.pvm1); + this.collection.add(this.pvm2); + this.collection.add(this.pvm3); + this.collection.add(this.pvm4); + this.cvm = new SimpleCollectionValueModel<>(this.collection); + + this.compositePVM = this.buildCompositePVM(this.cvm); + } + + private PropertyValueModel<Boolean> buildCompositePVM(CollectionValueModel<ModifiablePropertyValueModel<Boolean>> pvms) { + return CollectionValueModelTools.or(pvms); + } + + @Override + protected void tearDown() throws Exception { + TestTools.clear(this); + super.tearDown(); + } + + public void testGetValue1() { + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertFalse(this.compositePVM.getValue().booleanValue()); + } + + public void testGetValue2() { + this.compositePVM = CollectionValueModelTools.or(this.pvm1, this.pvm2, this.pvm3, this.pvm4); + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertFalse(this.compositePVM.getValue().booleanValue()); + } + + public void testGetValue3() { + this.compositePVM = CollectionValueModelTools.or(Arrays.asList(this.pvm1, this.pvm2, this.pvm3, this.pvm4)); + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertFalse(this.compositePVM.getValue().booleanValue()); + } + + public void testValueAndListeners1() { + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertFalse(this.compositePVM.getValue().booleanValue()); + this.compositePVM.removeChangeListener(listener); + assertNull(this.compositePVM.getValue()); + } + + public void testValueAndListeners2() { + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + assertFalse(this.compositePVM.getValue().booleanValue()); + this.compositePVM.removePropertyChangeListener(PropertyValueModel.VALUE, listener); + assertNull(this.compositePVM.getValue()); + } + + public void testPropertyChange1() { + this.compositePVM.addChangeListener(this.buildListener()); + this.verifyPropertyChange(); + } + + public void testPropertyChange2() { + this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildListener()); + this.verifyPropertyChange(); + } + + private void verifyPropertyChange() { + this.event = null; + this.pvm1.setValue(Boolean.TRUE); + this.verifyEvent(false, true); + + this.event = null; + this.pvm2.setValue(Boolean.TRUE); + assertNull(this.event); // no change + + this.event = null; + this.pvm2.setValue(Boolean.FALSE); + assertNull(this.event); // no change + + this.event = null; + this.pvm1.setValue(Boolean.FALSE); + this.verifyEvent(true, false); + + this.event = null; + this.pvm4.setValue(Boolean.TRUE); + this.verifyEvent(false, true); + } + + public void testCollectionChange1() { + this.compositePVM.addChangeListener(this.buildListener()); + this.verifyCollectionChange(); + } + + public void testCollectionChange2() { + this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildListener()); + this.verifyCollectionChange(); + } + + private void verifyCollectionChange() { + this.event = null; + ModifiablePropertyValueModel<Boolean> pvm = new SimplePropertyValueModel<>(Boolean.TRUE); + this.cvm.add(pvm); + this.verifyEvent(false, true); + + this.event = null; + this.cvm.remove(pvm); + this.verifyEvent(true, false); + + this.event = null; + this.cvm.add(pvm); + this.verifyEvent(false, true); + + this.event = null; + this.cvm.clear(); + this.verifyEvent(true, false); + + this.event = null; + this.cvm.add(pvm); + this.verifyEvent(false, true); + + Collection<ModifiablePropertyValueModel<Boolean>> c2 = new ArrayList<>(); + c2.add(this.pvm1); + c2.add(this.pvm2); + this.event = null; + this.cvm.setValues(c2); + this.verifyEvent(true, false); + } + + public void testLazyListening1() { + assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + ChangeListener listener = this.buildListener(); + + this.compositePVM.addChangeListener(listener); + assertTrue(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + + this.compositePVM.removeChangeListener(listener); + assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + } + + public void testLazyListening2() { + assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + ChangeListener listener = this.buildListener(); + + this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + assertTrue(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + + this.compositePVM.removePropertyChangeListener(PropertyValueModel.VALUE, listener); + assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + } + + private ChangeListener buildListener() { + return new ChangeAdapter() { + @Override + public void propertyChanged(PropertyChangeEvent e) { + CompositeOrBooleanPropertyValueModelTests.this.event = e; + } + }; + } + + private void verifyEvent(boolean oldValue, boolean newValue) { + this.verifyEvent(Boolean.valueOf(oldValue), Boolean.valueOf(newValue)); + } + + private void verifyEvent(Boolean oldValue, Boolean newValue) { + assertNotNull(this.event); + assertEquals(this.compositePVM, this.event.getSource()); + assertEquals(PropertyValueModel.VALUE, this.event.getPropertyName()); + assertEquals(oldValue, this.event.getOldValue()); + assertEquals(newValue, this.event.getNewValue()); + } + +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositePropertyValueModelTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositePropertyValueModelTests.java index 56acea9e90..47f632d08f 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositePropertyValueModelTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/CompositePropertyValueModelTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2015 Oracle. All rights reserved. + * Copyright (c) 2009, 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. @@ -11,27 +11,32 @@ package org.eclipse.jpt.common.utility.tests.internal.model.value; import java.util.ArrayList; import java.util.Collection; - -import junit.framework.TestCase; - -import org.eclipse.jpt.common.utility.internal.model.value.CompositePropertyValueModel; +import org.eclipse.jpt.common.utility.internal.collection.CollectionTools; +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.SimpleCollectionValueModel; import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel; +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.ChangeAdapter; import org.eclipse.jpt.common.utility.model.listener.ChangeListener; import org.eclipse.jpt.common.utility.model.value.CollectionValueModel; -import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; +import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; import org.eclipse.jpt.common.utility.tests.internal.TestTools; +import junit.framework.TestCase; -public class CompositePropertyValueModelTests extends TestCase { +@SuppressWarnings("nls") +public class CompositePropertyValueModelTests + extends TestCase +{ private SimplePropertyValueModel<Integer> pvm1; private ModifiablePropertyValueModel<Integer> pvm2; private ModifiablePropertyValueModel<Integer> pvm3; private ModifiablePropertyValueModel<Integer> pvm4; - private Collection<ModifiablePropertyValueModel<Integer>> collection; - private SimpleCollectionValueModel<ModifiablePropertyValueModel<Integer>> cvm; + private Collection<PropertyValueModel<Integer>> collection; + private SimpleCollectionValueModel<PropertyValueModel<Integer>> cvm; private PropertyValueModel<Integer> compositePVM; PropertyChangeEvent event; @@ -43,31 +48,49 @@ public class CompositePropertyValueModelTests extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); - this.pvm1 = new SimplePropertyValueModel<Integer>(Integer.valueOf(1)); - this.pvm2 = new SimplePropertyValueModel<Integer>(Integer.valueOf(2)); - this.pvm3 = new SimplePropertyValueModel<Integer>(Integer.valueOf(3)); - this.pvm4 = new SimplePropertyValueModel<Integer>(Integer.valueOf(4)); - this.collection = new ArrayList<ModifiablePropertyValueModel<Integer>>(); + this.pvm1 = new SimplePropertyValueModel<>(Integer.valueOf(1)); + this.pvm2 = new SimplePropertyValueModel<>(Integer.valueOf(2)); + this.pvm3 = new SimplePropertyValueModel<>(Integer.valueOf(3)); + this.pvm4 = new SimplePropertyValueModel<>(Integer.valueOf(4)); + this.collection = new ArrayList<>(); this.collection.add(this.pvm1); this.collection.add(this.pvm2); this.collection.add(this.pvm3); this.collection.add(this.pvm4); - this.cvm = new SimpleCollectionValueModel<ModifiablePropertyValueModel<Integer>>(this.collection); + this.cvm = new LocalSimpleCollectionValueModel<>(this.collection); this.compositePVM = this.buildCompositePVM(this.cvm); } - private <T extends PropertyValueModel<Integer>> PropertyValueModel<Integer> buildCompositePVM(CollectionValueModel<T> pvms) { - return new CompositePropertyValueModel<Integer, Integer>(pvms) { - @Override - protected Integer buildValue() { - int sum = 0; - for (PropertyValueModel<? extends Integer> each : this.collectionModel) { - sum += each.getValue().intValue(); + public static class LocalSimpleCollectionValueModel<E> + extends SimpleCollectionValueModel<E> + { + public LocalSimpleCollectionValueModel(Collection<E> collection) { + super(collection); + } + @Override + protected ChangeSupport buildChangeSupport() { + return new ChangeSupport(this, RuntimeExceptionHandler.instance()); + } + } + + private PropertyValueModel<Integer> buildCompositePVM(CollectionValueModel<PropertyValueModel<Integer>> pvms) { + return CollectionValueModelTools.compositePropertyValueModel(pvms, new LocalTransformer()); + } + + public static class LocalTransformer + extends TransformerAdapter<Collection<Integer>, Integer> + { + @Override + public Integer transform(Collection<Integer> integers) { + int sum = 0; + for (Integer integer : integers) { + if (integer != null) { + sum += integer.intValue(); } - return Integer.valueOf(sum); } - }; + return Integer.valueOf(sum); + } } @Override @@ -76,7 +99,25 @@ public class CompositePropertyValueModelTests extends TestCase { super.tearDown(); } - public void testGetValue() { + public void testGetValue1() { + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertEquals(10, this.compositePVM.getValue().intValue()); + } + + public void testGetValue2() { + ArrayList<PropertyValueModel<Integer>> list = new ArrayList<>(); + CollectionTools.addAll(list, this.cvm); + this.compositePVM = CollectionValueModelTools.compositePropertyValueModel(list, new LocalTransformer()); + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertEquals(10, this.compositePVM.getValue().intValue()); + } + + public void testGetValue3() { + this.compositePVM = CollectionValueModelTools.compositePropertyValueModel(new LocalTransformer(), this.pvm1, this.pvm2, this.pvm3, this.pvm4); assertNull(this.compositePVM.getValue()); ChangeListener listener = this.buildListener(); this.compositePVM.addChangeListener(listener); @@ -133,7 +174,7 @@ public class CompositePropertyValueModelTests extends TestCase { private void verifyCollectionChange() { this.event = null; - ModifiablePropertyValueModel<Integer> pvm = new SimplePropertyValueModel<Integer>(Integer.valueOf(77)); + ModifiablePropertyValueModel<Integer> pvm = new SimplePropertyValueModel<>(Integer.valueOf(77)); this.cvm.add(pvm); this.verifyEvent(10, 87); @@ -145,7 +186,7 @@ public class CompositePropertyValueModelTests extends TestCase { this.cvm.clear(); this.verifyEvent(10, 0); - Collection<ModifiablePropertyValueModel<Integer>> c2 = new ArrayList<ModifiablePropertyValueModel<Integer>>(); + Collection<PropertyValueModel<Integer>> c2 = new ArrayList<>(); c2.add(this.pvm1); c2.add(this.pvm2); this.event = null; @@ -175,6 +216,54 @@ public class CompositePropertyValueModelTests extends TestCase { assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); } + public void testCtor_NPE1() { + boolean exCaught = false; + try { + this.compositePVM = this.buildCompositePVM(null); + fail("bogus: " + this.compositePVM); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE2() { + boolean exCaught = false; + try { + this.compositePVM = CollectionValueModelTools.compositePropertyValueModel((CollectionValueModel<PropertyValueModel<Integer>>) this.cvm, (TransformerAdapter<Collection<Integer>, Integer>) null); + fail("bogus: " + this.compositePVM); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testNullPVM() { + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + boolean exCaught = false; + try { + this.cvm.add(null); + fail("bogus: " + this.cvm); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testDuplicatePVM() { + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + boolean exCaught = false; + try { + this.cvm.add(this.pvm1); + fail("bogus: " + this.cvm); + } catch (IllegalStateException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + private ChangeListener buildListener() { return new ChangeAdapter() { @Override @@ -185,6 +274,7 @@ public class CompositePropertyValueModelTests extends TestCase { } private void verifyEvent(int oldValue, int newValue) { + assertNotNull(this.event); assertEquals(this.compositePVM, this.event.getSource()); assertEquals(PropertyValueModel.VALUE, this.event.getPropertyName()); assertEquals(Integer.valueOf(oldValue), this.event.getOldValue()); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoubleModifiablePropertyValueModelTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoubleModifiablePropertyValueModelTests.java index 369d19e5f5..af4dc8ab43 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoubleModifiablePropertyValueModelTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoubleModifiablePropertyValueModelTests.java @@ -10,7 +10,7 @@ package org.eclipse.jpt.common.utility.tests.internal.model.value; import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel; -import org.eclipse.jpt.common.utility.internal.model.value.ValueModelTools; +import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools; import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; @@ -24,7 +24,7 @@ public class DoubleModifiablePropertyValueModelTests @Override protected PropertyValueModel<String> buildDoubleModel(ModifiablePropertyValueModel<ModifiablePropertyValueModel<String>> modelModel) { - return ValueModelTools.wrapModifiable(modelModel); + return PropertyValueModelTools.wrapModifiable(modelModel); } protected ModifiablePropertyValueModel<String> getDoubleModel() { @@ -44,7 +44,13 @@ public class DoubleModifiablePropertyValueModelTests this.stringModelModel.setValue(null); assertNull(this.doubleModel.getValue()); - this.getDoubleModel().setValue("TTT"); // NOP? + boolean exCaught = false; + try { + this.getDoubleModel().setValue("TTT"); // unsupported? + } catch (IllegalStateException ex) { + exCaught = true; + } + assertTrue(exCaught); assertEquals("bar", this.stringModel.getValue()); assertNull(this.doubleModel.getValue()); } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoublePropertyValueModelTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoublePropertyValueModelTests.java index fef14c236c..d6a5fe85e9 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoublePropertyValueModelTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoublePropertyValueModelTests.java @@ -11,7 +11,7 @@ package org.eclipse.jpt.common.utility.tests.internal.model.value; import org.eclipse.jpt.common.utility.internal.model.AbstractModel; import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel; -import org.eclipse.jpt.common.utility.internal.model.value.ValueModelTools; +import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools; import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent; import org.eclipse.jpt.common.utility.model.listener.ChangeAdapter; import org.eclipse.jpt.common.utility.model.listener.ChangeListener; @@ -55,7 +55,7 @@ public class DoublePropertyValueModelTests } protected PropertyValueModel<String> buildDoubleModel(ModifiablePropertyValueModel<ModifiablePropertyValueModel<String>> modelModel) { - return ValueModelTools.wrap(modelModel); + return PropertyValueModelTools.wrap(modelModel); } @Override diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/FilteringCollectionValueModelTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/FilteringCollectionValueModelTests.java index 92362adabc..011cf3f978 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/FilteringCollectionValueModelTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/FilteringCollectionValueModelTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2013 Oracle. All rights reserved. + * Copyright (c) 2007, 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. @@ -15,6 +15,7 @@ import java.util.Vector; import junit.framework.TestCase; import org.eclipse.jpt.common.utility.internal.collection.CollectionTools; import org.eclipse.jpt.common.utility.internal.model.AbstractModel; +import org.eclipse.jpt.common.utility.internal.model.value.CollectionValueModelTools; import org.eclipse.jpt.common.utility.internal.model.value.FilteringCollectionValueModel; import org.eclipse.jpt.common.utility.internal.model.value.SimpleCollectionValueModel; import org.eclipse.jpt.common.utility.internal.predicate.PredicateAdapter; @@ -31,13 +32,13 @@ import org.eclipse.jpt.common.utility.tests.internal.TestTools; @SuppressWarnings("nls") public class FilteringCollectionValueModelTests extends TestCase { - private SimpleCollectionValueModel<String> collectionHolder; + private SimpleCollectionValueModel<String> simpleCVM; CollectionAddEvent addEvent; CollectionRemoveEvent removeEvent; CollectionClearEvent collectionClearedEvent; CollectionChangeEvent collectionChangedEvent; - private CollectionValueModel<String> filteredCollectionHolder; + private CollectionValueModel<String> filteredCVM; CollectionAddEvent filteredAddEvent; CollectionRemoveEvent filteredRemoveEvent; CollectionClearEvent filteredCollectionClearedEvent; @@ -50,12 +51,12 @@ public class FilteringCollectionValueModelTests extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); - this.collectionHolder = new SimpleCollectionValueModel<String>(buildCollection()); - this.filteredCollectionHolder = new FilteringCollectionValueModel<String>(this.collectionHolder, this.buildFilter()); + this.simpleCVM = new SimpleCollectionValueModel<>(buildCollection()); + this.filteredCVM = CollectionValueModelTools.filter(this.simpleCVM, this.buildFilter()); } private Collection<String> buildCollection() { - Collection<String> collection = new Vector<String>(); + Collection<String> collection = new Vector<>(); collection.add("foo"); return collection; } @@ -81,72 +82,72 @@ public class FilteringCollectionValueModelTests extends TestCase { public void testIterator() { // add a listener to "activate" the wrapper - this.filteredCollectionHolder.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildFilteredListener()); + this.filteredCVM.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildFilteredListener()); - assertEquals("foo", this.collectionHolder.iterator().next()); - assertFalse(this.filteredCollectionHolder.iterator().hasNext()); + assertEquals("foo", this.simpleCVM.iterator().next()); + assertFalse(this.filteredCVM.iterator().hasNext()); - this.collectionHolder.add("bar"); - Iterator<String> collectionHolderValue = this.collectionHolder.iterator(); + this.simpleCVM.add("bar"); + Iterator<String> collectionHolderValue = this.simpleCVM.iterator(); assertEquals("foo", collectionHolderValue.next()); assertEquals("bar", collectionHolderValue.next()); - assertTrue(this.filteredCollectionHolder.iterator().hasNext()); - assertEquals("bar", this.filteredCollectionHolder.iterator().next()); + assertTrue(this.filteredCVM.iterator().hasNext()); + assertEquals("bar", this.filteredCVM.iterator().next()); - this.collectionHolder.remove("bar"); - assertEquals("foo", this.collectionHolder.iterator().next()); - assertFalse(this.filteredCollectionHolder.iterator().hasNext()); + this.simpleCVM.remove("bar"); + assertEquals("foo", this.simpleCVM.iterator().next()); + assertFalse(this.filteredCVM.iterator().hasNext()); - this.collectionHolder.remove("foo"); - assertFalse(this.collectionHolder.iterator().hasNext()); - assertFalse(this.filteredCollectionHolder.iterator().hasNext()); + this.simpleCVM.remove("foo"); + assertFalse(this.simpleCVM.iterator().hasNext()); + assertFalse(this.filteredCVM.iterator().hasNext()); - this.collectionHolder.add("foo"); - assertEquals("foo", this.collectionHolder.iterator().next()); - assertFalse(this.filteredCollectionHolder.iterator().hasNext()); + this.simpleCVM.add("foo"); + assertEquals("foo", this.simpleCVM.iterator().next()); + assertFalse(this.filteredCVM.iterator().hasNext()); } public void testSetValue() { // add a listener to "activate" the wrapper - this.filteredCollectionHolder.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildFilteredListener()); + this.filteredCVM.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildFilteredListener()); - Collection<String> newCollection = new Vector<String>(); + Collection<String> newCollection = new Vector<>(); newCollection.add("fox"); newCollection.add("baz"); - this.collectionHolder.setValues(newCollection); + this.simpleCVM.setValues(newCollection); - Iterator<String> collectionValues = this.collectionHolder.iterator(); + Iterator<String> collectionValues = this.simpleCVM.iterator(); assertEquals("fox", collectionValues.next()); assertEquals("baz", collectionValues.next()); - Iterator<String> filteredCollectionValues = this.filteredCollectionHolder.iterator(); + Iterator<String> filteredCollectionValues = this.filteredCVM.iterator(); assertEquals("baz", filteredCollectionValues.next()); assertFalse(filteredCollectionValues.hasNext()); } public void testLazyListening() { - assertTrue(((AbstractModel) this.collectionHolder).hasNoCollectionChangeListeners(CollectionValueModel.VALUES)); + assertTrue(((AbstractModel) this.simpleCVM).hasNoCollectionChangeListeners(CollectionValueModel.VALUES)); ChangeListener listener = this.buildFilteredChangeListener(); - this.filteredCollectionHolder.addChangeListener(listener); - assertTrue(((AbstractModel) this.collectionHolder).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); - this.filteredCollectionHolder.removeChangeListener(listener); - assertTrue(((AbstractModel) this.collectionHolder).hasNoCollectionChangeListeners(CollectionValueModel.VALUES)); - - this.filteredCollectionHolder.addCollectionChangeListener(CollectionValueModel.VALUES, listener); - assertTrue(((AbstractModel) this.collectionHolder).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); - this.filteredCollectionHolder.removeCollectionChangeListener(CollectionValueModel.VALUES, listener); - assertTrue(((AbstractModel) this.collectionHolder).hasNoCollectionChangeListeners(CollectionValueModel.VALUES)); + this.filteredCVM.addChangeListener(listener); + assertTrue(((AbstractModel) this.simpleCVM).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); + this.filteredCVM.removeChangeListener(listener); + assertTrue(((AbstractModel) this.simpleCVM).hasNoCollectionChangeListeners(CollectionValueModel.VALUES)); + + this.filteredCVM.addCollectionChangeListener(CollectionValueModel.VALUES, listener); + assertTrue(((AbstractModel) this.simpleCVM).hasAnyCollectionChangeListeners(CollectionValueModel.VALUES)); + this.filteredCVM.removeCollectionChangeListener(CollectionValueModel.VALUES, listener); + assertTrue(((AbstractModel) this.simpleCVM).hasNoCollectionChangeListeners(CollectionValueModel.VALUES)); } public void testCollectionChange1() { - this.collectionHolder.addChangeListener(this.buildChangeListener()); - this.filteredCollectionHolder.addChangeListener(this.buildFilteredChangeListener()); + this.simpleCVM.addChangeListener(this.buildChangeListener()); + this.filteredCVM.addChangeListener(this.buildFilteredChangeListener()); this.verifyCollectionChanges(); } public void testCollectionChange2() { - this.collectionHolder.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildListener()); - this.filteredCollectionHolder.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildFilteredListener()); + this.simpleCVM.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildListener()); + this.filteredCVM.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildFilteredListener()); this.verifyCollectionChanges(); } @@ -163,48 +164,48 @@ public class FilteringCollectionValueModelTests extends TestCase { private void verifyCollectionChanges() { clearEvents(); - this.collectionHolder.add("bar"); - Collection<String> tempCollection = new Vector<String>(); + this.simpleCVM.add("bar"); + Collection<String> tempCollection = new Vector<>(); tempCollection.add("bar"); - this.verifyEvent(this.addEvent, this.collectionHolder, tempCollection); - this.verifyEvent(this.filteredAddEvent, this.filteredCollectionHolder, tempCollection); + this.verifyEvent(this.addEvent, this.simpleCVM, tempCollection); + this.verifyEvent(this.filteredAddEvent, this.filteredCVM, tempCollection); clearEvents(); - this.collectionHolder.remove("foo"); + this.simpleCVM.remove("foo"); tempCollection.remove("bar"); tempCollection.add("foo"); - this.verifyEvent(this.removeEvent, this.collectionHolder, tempCollection); + this.verifyEvent(this.removeEvent, this.simpleCVM, tempCollection); assertNull(this.filteredRemoveEvent); clearEvents(); - this.collectionHolder.remove("bar"); + this.simpleCVM.remove("bar"); tempCollection.add("bar"); tempCollection.remove("foo"); - this.verifyEvent(this.removeEvent, this.collectionHolder, tempCollection); - this.verifyEvent(this.filteredRemoveEvent, this.filteredCollectionHolder, tempCollection); + this.verifyEvent(this.removeEvent, this.simpleCVM, tempCollection); + this.verifyEvent(this.filteredRemoveEvent, this.filteredCVM, tempCollection); clearEvents(); - this.collectionHolder.add("foo"); + this.simpleCVM.add("foo"); tempCollection.remove("bar"); tempCollection.add("foo"); - this.verifyEvent(this.addEvent, this.collectionHolder, tempCollection); + this.verifyEvent(this.addEvent, this.simpleCVM, tempCollection); assertNull(this.filteredAddEvent); clearEvents(); - Collection<String> newCollection = new Vector<String>(); + Collection<String> newCollection = new Vector<>(); newCollection.add("fox"); newCollection.add("baz"); - this.collectionHolder.setValues(newCollection); + this.simpleCVM.setValues(newCollection); - this.verifyEvent(this.collectionChangedEvent, this.collectionHolder); + this.verifyEvent(this.collectionChangedEvent, this.simpleCVM); tempCollection.remove("foo"); tempCollection.add("baz"); - this.verifyEvent(this.filteredCollectionChangedEvent, this.filteredCollectionHolder); + this.verifyEvent(this.filteredCollectionChangedEvent, this.filteredCVM); } @@ -303,8 +304,8 @@ public class FilteringCollectionValueModelTests extends TestCase { public void testRemoveFilteredItem() { // build collection with TestItems - SimpleCollectionValueModel<TestItem> tiHolder = new SimpleCollectionValueModel<TestItem>(this.buildCollection2()); - CollectionValueModel<TestItem> filteredTIHolder = new FilteringCollectionValueModel<TestItem>(tiHolder, this.buildFilter2()); + SimpleCollectionValueModel<TestItem> tiHolder = new SimpleCollectionValueModel<>(this.buildCollection2()); + CollectionValueModel<TestItem> filteredTIHolder = new FilteringCollectionValueModel<>(tiHolder, this.buildFilter2()); // add a listener to "activate" the wrapper filteredTIHolder.addCollectionChangeListener(CollectionValueModel.VALUES, this.buildFilteredListener()); @@ -325,7 +326,7 @@ public class FilteringCollectionValueModelTests extends TestCase { } private Collection<TestItem> buildCollection2() { - Collection<TestItem> collection = new Vector<TestItem>(); + Collection<TestItem> collection = new Vector<>(); collection.add(new TestItem("foo")); return collection; } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/JptCommonUtilityModelValueTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/JptCommonUtilityModelValueTests.java index ea56db400d..ab6f729e9b 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/JptCommonUtilityModelValueTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/JptCommonUtilityModelValueTests.java @@ -27,9 +27,11 @@ public class JptCommonUtilityModelValueTests { suite.addTestSuite(CollectionAspectAdapterTests.class); suite.addTestSuite(CollectionListValueModelAdapterTests.class); suite.addTestSuite(CollectionPropertyValueModelAdapterTests.class); - suite.addTestSuite(CompositeBooleanPropertyValueModelTests.class); + suite.addTestSuite(CollectionValueModelToolsTests.class); + suite.addTestSuite(CompositeAndBooleanPropertyValueModelTests.class); suite.addTestSuite(CompositeCollectionValueModelTests.class); suite.addTestSuite(CompositeListValueModelTests.class); + suite.addTestSuite(CompositeOrBooleanPropertyValueModelTests.class); suite.addTestSuite(CompositePropertyValueModelTests.class); suite.addTestSuite(DoubleModifiablePropertyValueModelTests.class); suite.addTestSuite(DoublePropertyValueModelTests.class); @@ -42,13 +44,17 @@ public class JptCommonUtilityModelValueTests { suite.addTestSuite(ItemStateListValueModelAdapterTests.class); suite.addTestSuite(ListAspectAdapterTests.class); suite.addTestSuite(ListCollectionValueModelAdapterTests.class); + suite.addTestSuite(ListCompositePropertyValueModelTests.class); suite.addTestSuite(ListCuratorTests.class); + suite.addTestSuite(ListPropertyValueModelAdapterTests.class); + suite.addTestSuite(ListValueModelToolsTests.class); suite.addTestSuite(NullCollectionValueModelTests.class); suite.addTestSuite(NullListValueModelTests.class); suite.addTestSuite(NullPropertyValueModelTests.class); suite.addTestSuite(PropertyAspectAdapterTests.class); suite.addTestSuite(PropertyCollectionValueModelAdapterTests.class); suite.addTestSuite(PropertyListValueModelAdapterTests.class); + suite.addTestSuite(PropertyValueModelToolsTests.class); suite.addTestSuite(ReadOnlyModifiablePropertyValueModelWrapperTests.class); suite.addTestSuite(SetCollectionValueModelTests.class); suite.addTestSuite(SimpleCollectionValueModelTests.class); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListCompositePropertyValueModelTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListCompositePropertyValueModelTests.java new file mode 100644 index 0000000000..4f28cd8185 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListCompositePropertyValueModelTests.java @@ -0,0 +1,304 @@ +/******************************************************************************* + * Copyright (c) 2009, 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.model.value; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import org.eclipse.jpt.common.utility.internal.collection.ListTools; +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.ListValueModelTools; +import org.eclipse.jpt.common.utility.internal.model.value.SimpleListValueModel; +import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel; +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.ChangeAdapter; +import org.eclipse.jpt.common.utility.model.listener.ChangeListener; +import org.eclipse.jpt.common.utility.model.value.ListValueModel; +import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; +import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; +import org.eclipse.jpt.common.utility.tests.internal.TestTools; +import junit.framework.TestCase; + +@SuppressWarnings("nls") +public class ListCompositePropertyValueModelTests + extends TestCase +{ + private SimplePropertyValueModel<String> pvm1; + private ModifiablePropertyValueModel<String> pvm2; + private ModifiablePropertyValueModel<String> pvm3; + private ModifiablePropertyValueModel<String> pvm4; + private List<PropertyValueModel<String>> collection; + private SimpleListValueModel<PropertyValueModel<String>> lvm; + private PropertyValueModel<String> compositePVM; + PropertyChangeEvent event; + + + public ListCompositePropertyValueModelTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.pvm1 = new SimplePropertyValueModel<>("111"); + this.pvm2 = new SimplePropertyValueModel<>("222"); + this.pvm3 = new SimplePropertyValueModel<>("333"); + this.pvm4 = new SimplePropertyValueModel<>("444"); + this.collection = new ArrayList<>(); + this.collection.add(this.pvm1); + this.collection.add(this.pvm2); + this.collection.add(this.pvm3); + this.collection.add(this.pvm4); + this.lvm = new LocalSimpleListValueModel<>(this.collection); + + this.compositePVM = this.buildCompositePVM(this.lvm); + } + + public static class LocalSimpleListValueModel<E> + extends SimpleListValueModel<E> + { + public LocalSimpleListValueModel(List<E> list) { + super(list); + } + @Override + protected ChangeSupport buildChangeSupport() { + return new ChangeSupport(this, RuntimeExceptionHandler.instance()); + } + } + + private PropertyValueModel<String> buildCompositePVM(ListValueModel<PropertyValueModel<String>> pvms) { + return ListValueModelTools.compositePropertyValueModel(pvms, new LocalTransformer()); + } + + public static class LocalTransformer + extends TransformerAdapter<List<String>, String> + { + @Override + public String transform(List<String> strings) { + StringBuilder sb = new StringBuilder(); + for (Iterator<String> stream = strings.iterator(); stream.hasNext(); ) { + String string = stream.next(); + sb.append(string); + if (stream.hasNext()) { + sb.append("-"); + } + } + return sb.toString(); + } + } + + @Override + protected void tearDown() throws Exception { + TestTools.clear(this); + super.tearDown(); + } + + public void testGetValue1() { + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertEquals("111-222-333-444", this.compositePVM.getValue()); + } + + public void testGetValue2() { + ArrayList<PropertyValueModel<String>> list = new ArrayList<>(); + ListTools.addAll(list, 0, this.lvm); + this.compositePVM = ListValueModelTools.compositePropertyValueModel(list, new LocalTransformer()); + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertEquals("111-222-333-444", this.compositePVM.getValue()); + } + + public void testGetValue3() { + this.compositePVM = ListValueModelTools.compositePropertyValueModel(new LocalTransformer(), this.pvm1, this.pvm2, this.pvm3, this.pvm4); + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertEquals("111-222-333-444", this.compositePVM.getValue()); + } + + public void testValueAndListeners1() { + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + assertEquals("111-222-333-444", this.compositePVM.getValue()); + this.compositePVM.removeChangeListener(listener); + assertNull(this.compositePVM.getValue()); + } + + public void testValueAndListeners2() { + assertNull(this.compositePVM.getValue()); + ChangeListener listener = this.buildListener(); + this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + assertEquals("111-222-333-444", this.compositePVM.getValue()); + this.compositePVM.removePropertyChangeListener(PropertyValueModel.VALUE, listener); + assertNull(this.compositePVM.getValue()); + } + + public void testPropertyChange1() { + this.compositePVM.addChangeListener(this.buildListener()); + this.verifyPropertyChange(); + } + + public void testPropertyChange2() { + this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildListener()); + this.verifyPropertyChange(); + } + + private void verifyPropertyChange() { + this.event = null; + this.pvm1.setValue("555"); + this.verifyEvent("111-222-333-444", "555-222-333-444"); + + this.event = null; + this.pvm4.setValue("000"); + this.verifyEvent("555-222-333-444", "555-222-333-000"); + } + + public void testListChange1() { + this.compositePVM.addChangeListener(this.buildListener()); + this.verifyListChange(); + } + + public void testListChange2() { + this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildListener()); + this.verifyListChange(); + } + + private void verifyListChange() { + this.event = null; + ModifiablePropertyValueModel<String> pvm7 = new SimplePropertyValueModel<>("777"); + this.lvm.add(pvm7); + this.verifyEvent("111-222-333-444", "111-222-333-444-777"); + + this.event = null; + this.lvm.remove(pvm7); + this.verifyEvent("111-222-333-444-777", "111-222-333-444"); + + this.event = null; + ModifiablePropertyValueModel<String> pvmX = new SimplePropertyValueModel<>("XXX"); + this.lvm.add(2, pvmX); + this.verifyEvent("111-222-333-444", "111-222-XXX-333-444"); + + this.event = null; + this.lvm.move(3, 2); + this.verifyEvent("111-222-XXX-333-444", "111-222-333-XXX-444"); + + this.event = null; + ModifiablePropertyValueModel<String> pvmZ = new SimplePropertyValueModel<>("ZZZ"); + this.lvm.set(3, pvmZ); + this.verifyEvent("111-222-333-XXX-444", "111-222-333-ZZZ-444"); + + this.event = null; + this.lvm.remove(3); + this.verifyEvent("111-222-333-ZZZ-444", "111-222-333-444"); + + this.event = null; + this.lvm.clear(); + this.verifyEvent("111-222-333-444", ""); + + List<PropertyValueModel<String>> c2 = new ArrayList<>(); + c2.add(this.pvm1); + c2.add(this.pvm2); + this.event = null; + this.lvm.setListValues(c2); + this.verifyEvent("", "111-222"); + } + + public void testLazyListening1() { + assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + ChangeListener listener = this.buildListener(); + + this.compositePVM.addChangeListener(listener); + assertTrue(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + + this.compositePVM.removeChangeListener(listener); + assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + } + + public void testLazyListening2() { + assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + ChangeListener listener = this.buildListener(); + + this.compositePVM.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + assertTrue(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + + this.compositePVM.removePropertyChangeListener(PropertyValueModel.VALUE, listener); + assertFalse(this.pvm1.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + } + + public void testCtor_NPE1() { + boolean exCaught = false; + try { + this.compositePVM = this.buildCompositePVM(null); + fail("bogus: " + this.compositePVM); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE2() { + boolean exCaught = false; + try { + this.compositePVM = ListValueModelTools.compositePropertyValueModel((ListValueModel<PropertyValueModel<String>>) this.lvm, (TransformerAdapter<List<String>, String>) null); + fail("bogus: " + this.compositePVM); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testNullPVM() { + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + boolean exCaught = false; + try { + this.lvm.add(null); + fail("bogus: " + this.lvm); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testDuplicatePVM() { + ChangeListener listener = this.buildListener(); + this.compositePVM.addChangeListener(listener); + boolean exCaught = false; + try { + this.lvm.add(this.pvm1); + fail("bogus: " + this.lvm); + } catch (IllegalStateException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + private ChangeListener buildListener() { + return new ChangeAdapter() { + @Override + public void propertyChanged(PropertyChangeEvent e) { + ListCompositePropertyValueModelTests.this.event = e; + } + }; + } + + private void verifyEvent(String oldValue, String newValue) { + assertNotNull(this.event); + assertEquals(this.compositePVM, this.event.getSource()); + assertEquals(PropertyValueModel.VALUE, this.event.getPropertyName()); + assertEquals(oldValue, this.event.getOldValue()); + assertEquals(newValue, this.event.getNewValue()); + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListPropertyValueModelAdapterTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListPropertyValueModelAdapterTests.java new file mode 100644 index 0000000000..84ffde9534 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListPropertyValueModelAdapterTests.java @@ -0,0 +1,316 @@ +/******************************************************************************* + * Copyright (c) 2007, 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.model.value; + +import java.util.Arrays; +import java.util.List; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.internal.model.AbstractModel; +import org.eclipse.jpt.common.utility.internal.model.value.ListPluggablePropertyValueModelAdapter; +import org.eclipse.jpt.common.utility.internal.model.value.ListValueModelTools; +import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools; +import org.eclipse.jpt.common.utility.internal.model.value.SimpleListValueModel; +import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent; +import org.eclipse.jpt.common.utility.model.listener.ChangeAdapter; +import org.eclipse.jpt.common.utility.model.listener.ChangeListener; +import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener; +import org.eclipse.jpt.common.utility.model.value.ListValueModel; +import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; +import org.eclipse.jpt.common.utility.tests.internal.TestTools; +import org.eclipse.jpt.common.utility.transformer.Transformer; +import junit.framework.TestCase; + +@SuppressWarnings("nls") +public class ListPropertyValueModelAdapterTests + extends TestCase +{ + private PropertyValueModel<Boolean> adapter; + private SimpleListValueModel<String> listModel; + PropertyChangeEvent event; + + public ListPropertyValueModelAdapterTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.listModel = new SimpleListValueModel<>(); + this.adapter = ListValueModelTools.propertyValueModel(this.listModel, new LocalTransformer(2, "666")); + this.event = null; + } + + @Override + protected void tearDown() throws Exception { + TestTools.clear(this); + super.tearDown(); + } + + private boolean booleanValue() { + Boolean value = this.adapter.getValue(); + return (value != null) && value.booleanValue(); + } + + private boolean listModelContains(int index, String value) { + return (this.listModel.size() > index) && ObjectTools.equals(this.listModel.get(index), value); + } + + public void testValue() { + assertNull(this.adapter.getValue()); + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, new PropertyChangeListener() { + public void propertyChanged(PropertyChangeEvent e) {/* OK */} + }); + assertFalse(this.booleanValue()); + assertFalse(this.listModelContains(2, "666")); + + this.listModel.add("111"); + assertFalse(this.booleanValue()); + + this.listModel.add("222"); + assertFalse(this.booleanValue()); + + this.listModel.add("666"); + assertTrue(this.booleanValue()); + assertTrue(this.listModelContains(2, "666")); + + this.listModel.remove("666"); + assertFalse(this.booleanValue()); + assertFalse(this.listModelContains(2, "666")); + + this.listModel.add("666"); + assertTrue(this.booleanValue()); + assertTrue(this.listModelContains(2, "666")); + + this.listModel.clear(); + assertFalse(this.booleanValue()); + assertFalse(this.listModelContains(2, "666")); + + this.listModel.add("111"); + this.listModel.add("222"); + this.listModel.add("666"); + assertTrue(this.booleanValue()); + assertTrue(this.listModelContains(2, "666")); + + this.listModel.set(2, "333"); + assertFalse(this.booleanValue()); + assertFalse(this.listModelContains(2, "666")); + + this.listModel.set(2, "666"); + assertTrue(this.booleanValue()); + assertTrue(this.listModelContains(2, "666")); + + this.listModel.move(0, 2); + assertFalse(this.booleanValue()); + assertTrue(this.listModelContains(0, "666")); + + this.listModel.setListValues(Arrays.asList("111", "222", "666")); + assertTrue(this.booleanValue()); + assertTrue(this.listModelContains(2, "666")); + } + + public void testEventFiring() { + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, new PropertyChangeListener() { + public void propertyChanged(PropertyChangeEvent e) { + ListPropertyValueModelAdapterTests.this.event = e; + } + }); + assertNull(this.event); + + this.listModel.add("111"); + assertNull(this.event); + + this.listModel.add("222"); + assertNull(this.event); + + this.listModel.add("666"); + this.verifyEvent(false, true); + + this.listModel.remove("666"); + this.verifyEvent(true, false); + + this.listModel.add("666"); + this.verifyEvent(false, true); + + this.listModel.clear(); + this.verifyEvent(true, false); + } + + private void verifyEvent(boolean oldValue, boolean newValue) { + assertEquals(this.adapter, this.event.getSource()); + assertEquals(Boolean.valueOf(oldValue), this.event.getOldValue()); + assertEquals(Boolean.valueOf(newValue), this.event.getNewValue()); + this.event = null; + } + + public void testStaleValue() { + PropertyChangeListener listener = new PropertyChangeListener() { + public void propertyChanged(PropertyChangeEvent e) {/* OK */} + }; + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + this.listModel.add("111"); + this.listModel.add("222"); + this.listModel.add("666"); + assertTrue(this.booleanValue()); + assertTrue(this.listModelContains(2, "666")); + + this.adapter.removePropertyChangeListener(PropertyValueModel.VALUE, listener); + assertFalse(this.booleanValue()); + assertTrue(this.listModelContains(2, "666")); + + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + assertTrue(this.booleanValue()); + assertTrue(this.listModelContains(2, "666")); + } + + public void testHasListeners() { + assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + assertFalse(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES)); + + ChangeListener listener = new ChangeAdapter() { + @Override + public void propertyChanged(PropertyChangeEvent e) {/* OK */} + }; + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + assertTrue(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + assertTrue(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES)); + + this.adapter.removePropertyChangeListener(PropertyValueModel.VALUE, listener); + assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + assertFalse(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES)); + + this.adapter.addChangeListener(listener); + assertTrue(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + assertTrue(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES)); + + this.adapter.removeChangeListener(listener); + assertFalse(((AbstractModel) this.adapter).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE)); + assertFalse(((AbstractModel) this.listModel).hasAnyListChangeListeners(ListValueModel.LIST_VALUES)); + } + + public void testToString1() { + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, new PropertyChangeListener() { + public void propertyChanged(PropertyChangeEvent e) {/* OK */} + }); + assertTrue(this.adapter.toString().endsWith("(false)")); + this.listModel.add("111"); + this.listModel.add("222"); + this.listModel.add("666"); + assertTrue(this.adapter.toString().endsWith("(true)")); + } + + public void testToString3() { + ListPluggablePropertyValueModelAdapter.Factory<String, Boolean> f = new ListPluggablePropertyValueModelAdapter.Factory<>(this.listModel, new LocalTransformer(2, "666")); + assertTrue(f.toString().indexOf("Factory") != -1); + } + + public void testToString4() { + PropertyChangeListener listener = new PropertyChangeListener() { + public void propertyChanged(PropertyChangeEvent e) {/* OK */} + }; + this.adapter.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + Object a = ObjectTools.get(this.adapter, "adapter"); + Object l = ObjectTools.get(a, "listener"); + assertTrue(l.toString().indexOf("AdapterListener") != -1); + } + + public void testCtor_NPE1A() { + Object object; + boolean exCaught = false; + try { + object = ListValueModelTools.propertyValueModel(null, new LocalTransformer(2, "666")); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE1B() { + Object object; + boolean exCaught = false; + try { + object = ListValueModelTools.propertyValueModel(null, new LocalTransformer(2, "666")); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE2A() { + Object object; + boolean exCaught = false; + try { + object = ListValueModelTools.propertyValueModel(this.listModel, null); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE2B() { + Object object; + boolean exCaught = false; + try { + object = ListValueModelTools.propertyValueModel(this.listModel, null); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testCtor_NPE4() { + Object object; + boolean exCaught = false; + try { + object = PropertyValueModelTools.propertyValueModel(null); + fail("bogus: " + object); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + + // ********** member class ********** + + /** + * Transform the list to <code>true</code> if it contains the specified item + * at the specified index, otherwise transform it to <code>false</code>. + */ + static class LocalTransformer + implements Transformer<List<String>, Boolean> + { + private final int index; + private final String item; + + LocalTransformer(int index, String item) { + super(); + this.index = index; + this.item = item; + } + + public Boolean transform(List<String> list) { + return Boolean.valueOf(this.transform_(list)); + } + + public boolean transform_(List<String> list) { + return (list.size() > this.index) && ObjectTools.equals(this.item, list.get(this.index)); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.item); + } + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListValueModelToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListValueModelToolsTests.java new file mode 100644 index 0000000000..12ce703f86 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/ListValueModelToolsTests.java @@ -0,0 +1,438 @@ +/******************************************************************************* + * 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.model.value; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import org.eclipse.jpt.common.utility.internal.ClassTools; +import org.eclipse.jpt.common.utility.internal.closure.BooleanClosure; +import org.eclipse.jpt.common.utility.internal.model.value.ListValueModelTools; +import org.eclipse.jpt.common.utility.internal.model.value.SimpleListValueModel; +import org.eclipse.jpt.common.utility.internal.predicate.PredicateTools; +import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent; +import org.eclipse.jpt.common.utility.model.listener.PropertyChangeAdapter; +import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; +import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; +import junit.framework.TestCase; + +@SuppressWarnings("nls") +public class ListValueModelToolsTests + extends TestCase +{ + public ListValueModelToolsTests(String name) { + super(name); + } + + public void testFirstElementPVMAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + PropertyValueModel<String> pvm = ListValueModelTools.firstElementPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertNull(pvm.getValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertEquals("foo", pvm.getValue()); + assertEquals("foo", listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertEquals("foo", pvm.getValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("foo"); + assertEquals("bar", pvm.getValue()); + assertEquals("bar", listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertNull(pvm.getValue()); + assertNull(listener.event.getNewValue()); + } + + public void testLastElementPVMAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + PropertyValueModel<String> pvm = ListValueModelTools.lastElementPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertNull(pvm.getValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertEquals("foo", pvm.getValue()); + assertEquals("foo", listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertEquals("bar", pvm.getValue()); + assertEquals("bar", listener.event.getNewValue()); + + listener.event = null; + cvm.remove("foo"); + assertEquals("bar", pvm.getValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("bar"); + assertNull(pvm.getValue()); + assertNull(listener.event.getNewValue()); + } + + public void testSingleElementPVMAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + PropertyValueModel<String> pvm = ListValueModelTools.singleElementPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertNull(pvm.getValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertEquals("foo", pvm.getValue()); + assertEquals("foo", listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertNull(pvm.getValue()); + assertNull(listener.event.getNewValue()); + + listener.event = null; + cvm.remove("foo"); + assertEquals("bar", pvm.getValue()); + assertEquals("bar", listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertNull(pvm.getValue()); + assertNull(listener.event.getNewValue()); + } + + public void testIsNotEmptyPropertyValueModelAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + PropertyValueModel<Boolean> pvm = ListValueModelTools.isNotEmptyPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + } + + public void testIsNotEmptyModifiablePropertyValueModelAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + BooleanClosure.Adapter adapter = new NotEmptyBooleanClosureAdapter(cvm); + ModifiablePropertyValueModel<Boolean> pvm = ListValueModelTools.isNotEmptyModifiablePropertyValueModel(cvm, adapter); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + pvm.setValue(Boolean.FALSE); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + assertEquals(0, cvm.size()); + + listener.event = null; + pvm.setValue(Boolean.TRUE); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + assertEquals(2, cvm.size()); + assertTrue(cvm.contains("baz")); + assertTrue(cvm.contains("xxx")); + + listener.event = null; + cvm.remove("baz"); + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("xxx"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + } + + public static class NotEmptyBooleanClosureAdapter + implements BooleanClosure.Adapter + { + final SimpleListValueModel<String> cvm; + public NotEmptyBooleanClosureAdapter(SimpleListValueModel<String> cvm) { + this.cvm = cvm; + } + public void execute(boolean argument) { + if (argument) { + ArrayList<String> list = new ArrayList<>(); + list.add("baz"); + list.add("xxx"); + this.cvm.setListValues(list); + } else { + this.cvm.clear(); + } + } + } + + public void testIsEmptyPropertyValueModelAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + PropertyValueModel<Boolean> pvm = ListValueModelTools.isEmptyPropertyValueModel(cvm); + this.verifyIsEmptyPropertyValueModelAdapter(cvm, pvm); + } + + private void verifyIsEmptyPropertyValueModelAdapter(SimpleListValueModel<String> cvm, PropertyValueModel<Boolean> pvm) { + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("bar"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + } + + public void testBooleanPVM() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + PropertyValueModel<Boolean> pvm = ListValueModelTools.booleanPropertyValueModel(cvm, PredicateTools.collectionIsEmptyPredicate()); + this.verifyIsEmptyPropertyValueModelAdapter(cvm, pvm); + } + + public void testIsEmptyModifiablePropertyValueModelAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + BooleanClosure.Adapter adapter = new EmptyBooleanClosureAdapter(cvm); + ModifiablePropertyValueModel<Boolean> pvm = ListValueModelTools.isEmptyModifiablePropertyValueModel(cvm, adapter); + this.verifyIsEmptyModifiablePropertyValueModelAdapter(cvm, pvm); + } + + public void testBooleanModifiablePVM() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + BooleanClosure.Adapter adapter = new EmptyBooleanClosureAdapter(cvm); + ModifiablePropertyValueModel<Boolean> pvm = ListValueModelTools.booleanModifiablePropertyValueModel(cvm, PredicateTools.collectionIsEmptyPredicate(), adapter); + this.verifyIsEmptyPropertyValueModelAdapter(cvm, pvm); + } + + private void verifyIsEmptyModifiablePropertyValueModelAdapter(SimpleListValueModel<String> cvm, ModifiablePropertyValueModel<Boolean> pvm) { + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertTrue(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + pvm.setValue(Boolean.TRUE); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + assertEquals(0, cvm.size()); + + listener.event = null; + pvm.setValue(Boolean.FALSE); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + assertEquals(2, cvm.size()); + assertTrue(cvm.contains("baz")); + assertTrue(cvm.contains("xxx")); + + listener.event = null; + cvm.remove("baz"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.remove("xxx"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + } + + public static class EmptyBooleanClosureAdapter + implements BooleanClosure.Adapter + { + final SimpleListValueModel<String> cvm; + public EmptyBooleanClosureAdapter(SimpleListValueModel<String> cvm) { + this.cvm = cvm; + } + public void execute(boolean argument) { + if (argument) { + this.cvm.clear(); + } else { + ArrayList<String> list = new ArrayList<>(); + list.add("baz"); + list.add("xxx"); + this.cvm.setListValues(list); + } + } + } + + public void testContainsSingleElementPropertyValueModelAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + PropertyValueModel<Boolean> pvm = ListValueModelTools.containsSingleElementPropertyValueModel(cvm); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("foo"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + } + + public void testSizeEqualsPropertyValueModelAdapter() { + SimpleListValueModel<String> cvm = new SimpleListValueModel<>(); + PropertyValueModel<Boolean> pvm = ListValueModelTools.sizeEqualsPropertyValueModel(cvm, 2); + LocalPropertyChangeListener listener = new LocalPropertyChangeListener(); + pvm.addPropertyChangeListener(PropertyValueModel.VALUE, listener); + + listener.event = null; + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + + listener.event = null; + cvm.add("bar"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.add("baz"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("baz"); + assertTrue(pvm.getValue().booleanValue()); + assertEquals(Boolean.TRUE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("foo"); + assertFalse(pvm.getValue().booleanValue()); + assertEquals(Boolean.FALSE, listener.event.getNewValue()); + + listener.event = null; + cvm.remove("bar"); + assertFalse(pvm.getValue().booleanValue()); + assertNull(listener.event); + } + + public static class LocalPropertyChangeListener + extends PropertyChangeAdapter + { + public PropertyChangeEvent event; + public LocalPropertyChangeListener() { + super(); + } + @Override + public void propertyChanged(PropertyChangeEvent e) { + this.event = e; + } + } + + public void testConstructor() { + boolean exCaught = false; + try { + Object o = ClassTools.newInstance(ListValueModelTools.class); + fail("bogus: " + o); //$NON-NLS-1$ + } catch (RuntimeException ex) { + if (ex.getCause() instanceof InvocationTargetException) { + if (ex.getCause().getCause() instanceof UnsupportedOperationException) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } +} 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 new file mode 100644 index 0000000000..1943731ab0 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/PropertyValueModelToolsTests.java @@ -0,0 +1,175 @@ +/******************************************************************************* + * 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.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.model.value.CollectionValueModelTools; +import org.eclipse.jpt.common.utility.internal.model.value.PluggableModifiablePropertyValueModel; +import org.eclipse.jpt.common.utility.internal.model.value.PluggableModifiablePropertyValueModel.Adapter; +import org.eclipse.jpt.common.utility.internal.model.value.PluggablePropertyValueModel; +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.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 junit.framework.TestCase; + +@SuppressWarnings("nls") +public class PropertyValueModelToolsTests + extends TestCase +{ + + public PropertyValueModelToolsTests(String name) { + super(name); + } + + public void testModifiablePropertyValueModel() { + ModifiablePropertyValueModel<String> doubleStringModel = new SimplePropertyValueModel<>("foofoo"); + PluggableModifiablePropertyValueModel.Adapter.Factory<String> factory = new HalfStringModelAdapter.Factory(doubleStringModel); + ModifiablePropertyValueModel<String> halfStringModel = PropertyValueModelTools.modifiablePropertyValueModel(factory); + HalfStringListener halfStringListener = new HalfStringListener(); + halfStringModel.addPropertyChangeListener(PropertyValueModel.VALUE, halfStringListener); + + halfStringListener.event = null; + assertEquals("foofoo", doubleStringModel.getValue()); + assertEquals("foo", halfStringModel.getValue()); + assertNull(halfStringListener.event); + + halfStringListener.event = null; + halfStringModel.setValue("bar"); + assertEquals("bar", halfStringModel.getValue()); + assertEquals("barbar", doubleStringModel.getValue()); + assertEquals("bar", halfStringListener.event.getNewValue()); + + halfStringListener.event = null; + halfStringModel.setValue("bar"); + assertEquals("bar", halfStringModel.getValue()); + assertEquals("barbar", doubleStringModel.getValue()); + assertNull(halfStringListener.event); + + halfStringListener.event = null; + doubleStringModel.setValue("xxxxxx"); + assertEquals("xxx", halfStringModel.getValue()); + assertEquals("xxxxxx", doubleStringModel.getValue()); + assertEquals("xxx", halfStringListener.event.getNewValue()); + + halfStringListener.event = null; + halfStringModel.removePropertyChangeListener(PropertyValueModel.VALUE, halfStringListener); + assertNull(halfStringModel.getValue()); + assertEquals("xxxxxx", doubleStringModel.getValue()); + assertNull(halfStringListener.event); + } + + public static class HalfStringListener + implements PropertyChangeListener + { + public PropertyChangeEvent event; + public void propertyChanged(PropertyChangeEvent e) { + this.event = e; + } + } + + public static class HalfStringModelAdapter + implements PluggableModifiablePropertyValueModel.Adapter<String> + { + private final ModifiablePropertyValueModel<String> stringModel; + private final PluggableModifiablePropertyValueModel.Adapter.Listener<String> listener; + private final PropertyChangeListener stringListener; + private volatile String value; + + public HalfStringModelAdapter(ModifiablePropertyValueModel<String> stringModel, PluggableModifiablePropertyValueModel.Adapter.Listener<String> listener) { + super(); + this.stringModel = stringModel; + this.listener = listener; + this.stringListener = new StringListener(); + this.value = null; + } + + public void engageModel() { + this.stringModel.addPropertyChangeListener(PropertyValueModel.VALUE, this.stringListener); + String v = this.stringModel.getValue(); + this.value = v.substring(v.length() / 2); + } + + public String getValue() { + return this.value; + } + + public void setValue(String value) { + this.value = value; + this.stringModel.setValue(value + value); + } + + public void disengageModel() { + this.value = null; + this.stringModel.removePropertyChangeListener(PropertyValueModel.VALUE, this.stringListener); + } + + void stringChanged(String newStringValue) { + String newValue = newStringValue.substring(newStringValue.length() / 2); + this.value = newValue; + this.listener.valueChanged(newValue); + } + + public class StringListener + implements PropertyChangeListener + { + public void propertyChanged(PropertyChangeEvent event) { + HalfStringModelAdapter.this.stringChanged((String) event.getNewValue()); + } + } + + public static class Factory + implements PluggableModifiablePropertyValueModel.Adapter.Factory<String> + { + private final ModifiablePropertyValueModel<String> stringModel; + public Factory(ModifiablePropertyValueModel<String> stringModel) { + super(); + this.stringModel = stringModel; + } + public Adapter<String> buildAdapter(PluggableModifiablePropertyValueModel.Adapter.Listener<String> listener) { + return new HalfStringModelAdapter(this.stringModel, listener); + } + } + } + + public void testPluggableModifiablePropertyValueModel_NPE() { + PluggablePropertyValueModel.Adapter.Factory<String> factory = CollectionValueModelTools.pluggablePropertyValueModelAdapterFactory(new SimpleCollectionValueModel<>(), new TransformerAdapter<>()); + Closure<String> closure = null; + boolean exCaught = false; + try { + ModifiablePropertyValueModel<String> pvm = PropertyValueModelTools.pluggableModifiablePropertyValueModel(factory, closure); + fail("bogus: " + pvm); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testConstructor() { + boolean exCaught = false; + try { + Object o = ClassTools.newInstance(PropertyValueModelTools.class); + fail("bogus: " + o); + } catch (RuntimeException ex) { + if (ex.getCause() instanceof InvocationTargetException) { + if (ex.getCause().getCause() instanceof UnsupportedOperationException) { + exCaught = true; + } + } + } + assertTrue(exCaught); + } +} |