Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpfullbright2009-04-03 20:55:49 +0000
committerpfullbright2009-04-03 20:55:49 +0000
commit5d31976c7f9380d31b32f62b778d4abb86a298aa (patch)
treebc59c54eceac3a78f7016cb874b4f0b33c242a3e
parent095113364f932e95180e138ea3a70f820de9cc4d (diff)
downloadwebtools.dali-5d31976c7f9380d31b32f62b778d4abb86a298aa.tar.gz
webtools.dali-5d31976c7f9380d31b32f62b778d4abb86a298aa.tar.xz
webtools.dali-5d31976c7f9380d31b32f62b778d4abb86a298aa.zip
added ReadOnlyWritablePropertyValueModelWrapper - irony is fun!
-rw-r--r--jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/ReadOnlyWritablePropertyValueModelWrapper.java44
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/JptUtilityModelValueTests.java1
-rw-r--r--jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/ReadOnlyWritablePropertyValueModelWrapperTests.java143
3 files changed, 188 insertions, 0 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/ReadOnlyWritablePropertyValueModelWrapper.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/ReadOnlyWritablePropertyValueModelWrapper.java
new file mode 100644
index 0000000000..c70c263479
--- /dev/null
+++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/ReadOnlyWritablePropertyValueModelWrapper.java
@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Oracle.
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jpt.utility.internal.model.value;
+
+import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
+import org.eclipse.jpt.utility.model.value.PropertyValueModel;
+import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;
+
+/**
+ * A simple implementation of {@link WritablePropertyValueModel} that actually
+ * isn't ... writable. It can however be used in places that require a
+ * {@link WritablePropertyValueModel} where the developer is sure that no attempt
+ * will be made to write to it.
+ */
+public class ReadOnlyWritablePropertyValueModelWrapper<T>
+ extends PropertyValueModelWrapper<T>
+ implements WritablePropertyValueModel<T>
+{
+ public ReadOnlyWritablePropertyValueModelWrapper(PropertyValueModel<? extends T> valueHolder) {
+ super(valueHolder);
+ }
+
+
+ public T getValue() {
+ return this.valueHolder.getValue();
+ }
+
+ public void setValue(T value) {
+ throw new UnsupportedOperationException("setValue");
+ };
+
+ @Override
+ protected void valueChanged(PropertyChangeEvent event) {
+ firePropertyChanged(event.cloneWithSource(this));
+ }
+}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/JptUtilityModelValueTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/JptUtilityModelValueTests.java
index 99585e69b1..7e1c715df3 100644
--- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/JptUtilityModelValueTests.java
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/JptUtilityModelValueTests.java
@@ -46,6 +46,7 @@ public class JptUtilityModelValueTests {
suite.addTestSuite(PropertyAspectAdapterTests.class);
suite.addTestSuite(PropertyCollectionValueModelAdapterTests.class);
suite.addTestSuite(PropertyListValueModelAdapterTests.class);
+ suite.addTestSuite(ReadOnlyWritablePropertyValueModelWrapperTests.class);
suite.addTestSuite(StaticCollectionValueModelTests.class);
suite.addTestSuite(StaticListValueModelTests.class);
suite.addTestSuite(SimpleCollectionValueModelTests.class);
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/ReadOnlyWritablePropertyValueModelWrapperTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/ReadOnlyWritablePropertyValueModelWrapperTests.java
new file mode 100644
index 0000000000..30ddbc3113
--- /dev/null
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/ReadOnlyWritablePropertyValueModelWrapperTests.java
@@ -0,0 +1,143 @@
+package org.eclipse.jpt.utility.tests.internal.model.value;
+
+import junit.framework.TestCase;
+import org.eclipse.jpt.utility.internal.model.AbstractModel;
+import org.eclipse.jpt.utility.internal.model.value.ReadOnlyWritablePropertyValueModelWrapper;
+import org.eclipse.jpt.utility.internal.model.value.SimplePropertyValueModel;
+import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
+import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
+import org.eclipse.jpt.utility.model.value.PropertyValueModel;
+import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;
+import org.eclipse.jpt.utility.tests.internal.TestTools;
+
+public class ReadOnlyWritablePropertyValueModelWrapperTests
+ extends TestCase
+{
+ private WritablePropertyValueModel<String> objectHolder;
+
+ private PropertyChangeEvent event;
+
+ private WritablePropertyValueModel<String> wrapperObjectHolder;
+
+ private PropertyChangeEvent wrapperEvent;
+
+
+ public ReadOnlyWritablePropertyValueModelWrapperTests(String name) {
+ super(name);
+ }
+
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.objectHolder = new SimplePropertyValueModel<String>("foo");
+ this.wrapperObjectHolder = new ReadOnlyWritablePropertyValueModelWrapper<String>(this.objectHolder);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ TestTools.clear(this);
+ super.tearDown();
+ }
+
+
+ public void testValue() {
+ assertEquals("foo", this.objectHolder.getValue());
+ assertEquals("foo", this.wrapperObjectHolder.getValue());
+
+ this.objectHolder.setValue("bar");
+ assertEquals("bar", this.objectHolder.getValue());
+ assertEquals("bar", this.wrapperObjectHolder.getValue());
+
+ this.objectHolder.setValue(null);
+ assertNull(this.objectHolder.getValue());
+ assertNull(this.wrapperObjectHolder.getValue());
+
+ this.objectHolder.setValue("foo");
+ assertEquals("foo", this.objectHolder.getValue());
+ assertEquals("foo", this.wrapperObjectHolder.getValue());
+ }
+
+ public void testSetValue() {
+ boolean exceptionOccurred = false;
+ try {
+ this.wrapperObjectHolder.setValue("bar");
+ }
+ catch (UnsupportedOperationException uoe) {
+ exceptionOccurred = true;
+ }
+
+ assertTrue(exceptionOccurred);
+ assertEquals("foo", this.objectHolder.getValue());
+ assertEquals("foo", this.wrapperObjectHolder.getValue());
+ }
+
+ public void testLazyListening() {
+ assertTrue(((AbstractModel) this.objectHolder).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
+ PropertyChangeListener listener = buildWrapperListener();
+ this.wrapperObjectHolder.addPropertyChangeListener(listener);
+ assertTrue(((AbstractModel) this.objectHolder).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
+ this.wrapperObjectHolder.removePropertyChangeListener(listener);
+ assertTrue(((AbstractModel) this.objectHolder).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
+
+ this.wrapperObjectHolder.addPropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertTrue(((AbstractModel) this.objectHolder).hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
+ this.wrapperObjectHolder.removePropertyChangeListener(PropertyValueModel.VALUE, listener);
+ assertTrue(((AbstractModel) this.objectHolder).hasNoPropertyChangeListeners(PropertyValueModel.VALUE));
+ }
+
+ public void testPropertyChange1() {
+ this.objectHolder.addPropertyChangeListener(this.buildListener());
+ this.wrapperObjectHolder.addPropertyChangeListener(this.buildWrapperListener());
+ this.verifyPropertyChanges();
+ }
+
+ public void testPropertyChange2() {
+ this.objectHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildListener());
+ this.wrapperObjectHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.buildWrapperListener());
+ this.verifyPropertyChanges();
+ }
+
+ private void verifyPropertyChanges() {
+ this.event = null;
+ this.wrapperEvent = null;
+ this.objectHolder.setValue("bar");
+ verifyEvent(this.event, this.objectHolder, "foo", "bar");
+ verifyEvent(this.wrapperEvent, this.wrapperObjectHolder, "foo", "bar");
+
+ this.event = null;
+ this.wrapperEvent = null;
+ this.objectHolder.setValue(null);
+ verifyEvent(this.event, this.objectHolder, "bar", null);
+ verifyEvent(this.wrapperEvent, this.wrapperObjectHolder, "bar", null);
+
+ this.event = null;
+ this.wrapperEvent = null;
+ this.objectHolder.setValue("foo");
+ verifyEvent(this.event, this.objectHolder, null, "foo");
+ verifyEvent(this.wrapperEvent, this.wrapperObjectHolder, null, "foo");
+ }
+
+ private PropertyChangeListener buildListener() {
+ return new PropertyChangeListener() {
+ public void propertyChanged(PropertyChangeEvent e) {
+ ReadOnlyWritablePropertyValueModelWrapperTests.this.event = e;
+ }
+ };
+ }
+
+ private PropertyChangeListener buildWrapperListener() {
+ return new PropertyChangeListener() {
+ public void propertyChanged(PropertyChangeEvent e) {
+ ReadOnlyWritablePropertyValueModelWrapperTests.this.wrapperEvent = e;
+ }
+ };
+ }
+
+ private void verifyEvent(PropertyChangeEvent e, Object source, Object oldValue, Object newValue) {
+ assertEquals(source, e.getSource());
+ assertEquals(PropertyValueModel.VALUE, e.getPropertyName());
+ assertEquals(oldValue, e.getOldValue());
+ assertEquals(newValue, e.getNewValue());
+ }
+}

Back to the top