Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Hall2008-12-02 05:46:21 +0000
committerMatthew Hall2008-12-02 05:46:21 +0000
commit5e692d8e2e83d9597b740980462ef8e9a55a6300 (patch)
tree80e6afdba9052fe1f8545ae734379c8c7ac008c1
parentfe7b61aaceb66ff0ce0a4cf7d67221450348ac9e (diff)
downloadorg.eclipse.e4.databinding-I20081202-0800b.tar.gz
org.eclipse.e4.databinding-I20081202-0800b.tar.xz
org.eclipse.e4.databinding-I20081202-0800b.zip
FIXED - bug 254524: [DataBinding] Bindings should self-dispose when target or model is disposedv20081205I20081202-0800b
https://bugs.eclipse.org/bugs/show_bug.cgi?id=254524
-rw-r--r--bundles/org.eclipse.core.databinding/src/org/eclipse/core/databinding/Binding.java25
-rw-r--r--tests/org.eclipse.jface.tests.databinding/src/org/eclipse/core/tests/databinding/BindingTest.java139
-rw-r--r--tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/databinding/BindingTestSuite.java5
3 files changed, 167 insertions, 2 deletions
diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/databinding/Binding.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/databinding/Binding.java
index a1939a4b..e7647303 100644
--- a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/databinding/Binding.java
+++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/databinding/Binding.java
@@ -9,13 +9,15 @@
* IBM Corporation - initial API and implementation
* Brad Reynolds - bug 159768
* Boris Bokowski - bug 218269
- * Matthew Hall - bug 218269
+ * Matthew Hall - bug 218269, 254524
*******************************************************************************/
package org.eclipse.core.databinding;
import java.util.Collections;
+import org.eclipse.core.databinding.observable.DisposeEvent;
+import org.eclipse.core.databinding.observable.IDisposeListener;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.Observables;
import org.eclipse.core.databinding.observable.list.IObservableList;
@@ -33,6 +35,7 @@ public abstract class Binding extends ValidationStatusProvider {
protected DataBindingContext context;
private IObservable target;
private IObservable model;
+ private IDisposeListener disposeListener;
/**
* Creates a new binding.
@@ -56,6 +59,17 @@ public abstract class Binding extends ValidationStatusProvider {
*/
public final void init(DataBindingContext context) {
this.context = context;
+ if (target.isDisposed())
+ throw new IllegalArgumentException("Target observable is disposed"); //$NON-NLS-1$
+ if (model.isDisposed())
+ throw new IllegalArgumentException("Model observable is disposed"); //$NON-NLS-1$
+ this.disposeListener = new IDisposeListener() {
+ public void handleDispose(DisposeEvent staleEvent) {
+ dispose();
+ }
+ };
+ target.addDisposeListener(disposeListener);
+ model.addDisposeListener(disposeListener);
preInit();
context.addBinding(this);
postInit();
@@ -119,6 +133,15 @@ public abstract class Binding extends ValidationStatusProvider {
context.removeBinding(this);
}
context = null;
+ if (disposeListener != null) {
+ if (target != null) {
+ target.removeDisposeListener(disposeListener);
+ }
+ if (model != null) {
+ model.removeDisposeListener(disposeListener);
+ }
+ disposeListener = null;
+ }
target = null;
model = null;
super.dispose();
diff --git a/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/core/tests/databinding/BindingTest.java b/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/core/tests/databinding/BindingTest.java
new file mode 100644
index 00000000..b175bfbe
--- /dev/null
+++ b/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/core/tests/databinding/BindingTest.java
@@ -0,0 +1,139 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Matthew Hall and others.
+ * 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:
+ * Matthew Hall - initial API and implementation (bug 254524)
+ ******************************************************************************/
+
+package org.eclipse.core.tests.databinding;
+
+import org.eclipse.core.databinding.Binding;
+import org.eclipse.core.databinding.DataBindingContext;
+import org.eclipse.core.databinding.observable.IObservable;
+import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.core.databinding.observable.value.WritableValue;
+import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
+
+public class BindingTest extends AbstractDefaultRealmTestCase {
+ private IObservable target;
+
+ private IObservable model;
+
+ private DataBindingContext dbc;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ target = WritableValue.withValueType(String.class);
+ model = WritableValue.withValueType(String.class);
+ dbc = new DataBindingContext();
+ }
+
+ public void testDisposeTargetDisposesBinding() {
+ Binding binding = createBinding();
+ assertFalse(binding.isDisposed());
+ target.dispose();
+ assertTrue(binding.isDisposed());
+ }
+
+ public void testDisposeModelDisposesBinding() {
+ Binding binding = createBinding();
+ assertFalse(binding.isDisposed());
+ model.dispose();
+ assertTrue(binding.isDisposed());
+ }
+
+ public void testPreDisposedTarget_FiresIllegalArgumentException() {
+ try {
+ target.dispose();
+ createBinding();
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+ }
+ }
+
+ public void testPreDisposedModel_FiresIllegalArgumentException() {
+ try {
+ model.dispose();
+ createBinding();
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+ }
+ }
+
+ public void testDisposeModelThenBinding() {
+ Binding binding = createBinding();
+ model.dispose();
+ binding.dispose();
+ }
+
+ public void testDisposeTargetThenBinding() {
+ Binding binding = createBinding();
+ target.dispose();
+ binding.dispose();
+ }
+
+ public void testDisposeObservablesThenBinding() {
+ Binding binding = createBinding();
+ model.dispose();
+ target.dispose();
+ binding.dispose();
+ }
+
+ public void testDisposeBindingThenModel() {
+ Binding binding = createBinding();
+ binding.dispose();
+ model.dispose();
+ }
+
+ public void testDisposeBindingThenTarget() {
+ Binding binding = createBinding();
+ binding.dispose();
+ target.dispose();
+ }
+
+ public void testDisposeBindingThenObservables() {
+ Binding binding = createBinding();
+ binding.dispose();
+ model.dispose();
+ target.dispose();
+ }
+
+ private Binding createBinding() {
+ Binding binding = new BindingStub(target, model);
+ binding.init(dbc);
+ return binding;
+ }
+
+ private static class BindingStub extends Binding {
+ BindingStub(IObservable target, IObservable model) {
+ super(target, model);
+ }
+
+ public IObservableValue getValidationStatus() {
+ return null;
+ }
+
+ protected void postInit() {
+ }
+
+ protected void preInit() {
+ }
+
+ public void updateModelToTarget() {
+ }
+
+ public void updateTargetToModel() {
+ }
+
+ public void validateModelToTarget() {
+ }
+
+ public void validateTargetToModel() {
+ }
+ }
+}
diff --git a/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/databinding/BindingTestSuite.java b/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/databinding/BindingTestSuite.java
index b320fc81..312c6ecd 100644
--- a/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/databinding/BindingTestSuite.java
+++ b/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/databinding/BindingTestSuite.java
@@ -13,7 +13,8 @@
* Ashley Cambrell - bugs 198903, 198904
* Matthew Hall - bugs 210115, 212468, 212223, 206839, 208858, 208322,
* 212518, 215531, 221351, 184830, 213145, 218269, 239015,
- * 237703, 237718, 222289, 247394, 233306, 247647, 246103
+ * 237703, 237718, 222289, 247394, 233306, 247647
+ * 254524, 246103
* Ovidio Mallo - bug 237163, bug 235195
*******************************************************************************/
package org.eclipse.jface.tests.databinding;
@@ -23,6 +24,7 @@ import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.core.tests.databinding.AggregateValidationStatusTest;
+import org.eclipse.core.tests.databinding.BindingTest;
import org.eclipse.core.tests.databinding.DatabindingContextTest;
import org.eclipse.core.tests.databinding.ListBindingTest;
import org.eclipse.core.tests.databinding.ObservablesManagerTest;
@@ -198,6 +200,7 @@ public class BindingTestSuite extends TestSuite {
// org.eclipse.core.tests.databinding
addTestSuite(AggregateValidationStatusTest.class);
+ addTestSuite(BindingTest.class);
addTestSuite(DatabindingContextTest.class);
addTestSuite(ListBindingTest.class);
addTestSuite(UpdateStrategyTest.class);

Back to the top