001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brad Reynolds - bug 159539
011: * Brad Reynolds - bug 140644
012: * Brad Reynolds - bug 159940
013: * Brad Reynolds - bug 116920, 159768
014: *******************************************************************************/package org.eclipse.core.tests.databinding;
015:
016: import java.util.ArrayList;
017:
018: import org.eclipse.core.databinding.AggregateValidationStatus;
019: import org.eclipse.core.databinding.Binding;
020: import org.eclipse.core.databinding.DataBindingContext;
021: import org.eclipse.core.databinding.UpdateListStrategy;
022: import org.eclipse.core.databinding.UpdateValueStrategy;
023: import org.eclipse.core.databinding.observable.list.IObservableList;
024: import org.eclipse.core.databinding.observable.list.WritableList;
025: import org.eclipse.core.databinding.observable.map.IObservableMap;
026: import org.eclipse.core.databinding.observable.value.IObservableValue;
027: import org.eclipse.core.databinding.observable.value.WritableValue;
028: import org.eclipse.core.databinding.validation.IValidator;
029: import org.eclipse.core.databinding.validation.ValidationStatus;
030: import org.eclipse.core.runtime.IStatus;
031: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
032: import org.eclipse.jface.tests.databinding.EventTrackers.ChangeEventTracker;
033: import org.eclipse.jface.tests.databinding.EventTrackers.ValueChangeEventTracker;
034:
035: public class DatabindingContextTest extends
036: AbstractDefaultRealmTestCase {
037: private DataBindingContext dbc;
038:
039: /*
040: * (non-Javadoc)
041: *
042: * @see org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase#setUp()
043: */
044: protected void setUp() throws Exception {
045: super .setUp();
046:
047: dbc = new DataBindingContext();
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase#tearDown()
054: */
055: protected void tearDown() throws Exception {
056: if (dbc != null) {
057: dbc.dispose();
058: }
059: super .tearDown();
060: }
061:
062: public void testDisposeBindings() throws Exception {
063: Binding binding = new BindingStub();
064: binding.init(dbc);
065:
066: assertFalse(binding.isDisposed());
067: dbc.dispose();
068: assertTrue("binding should be diposed when dbc is disposed",
069: binding.isDisposed());
070: }
071:
072: public void testBindValue() throws Exception {
073: IObservableValue target = WritableValue
074: .withValueType(String.class);
075: IObservableValue model = WritableValue
076: .withValueType(String.class);
077:
078: Binding binding = dbc.bindValue(target, model, null, null);
079: assertTrue("binding is of the incorrect type", binding
080: .getClass().getName().endsWith("ValueBinding"));
081: }
082:
083: public void testBindList() throws Exception {
084: IObservableList target = WritableList
085: .withElementType(Object.class);
086: IObservableList model = WritableList
087: .withElementType(Object.class);
088:
089: Binding binding = dbc.bindList(target, model, null, null);
090: assertTrue("binding is of the incorrect type", binding
091: .getClass().getName().endsWith("ListBinding"));
092: }
093:
094: /**
095: * Asserts that IStatus is populated and change events are fired when a
096: * Binding that is associated with a context is in error.
097: *
098: * @throws Exception
099: */
100: public void testValidationError() throws Exception {
101: WritableValue targetObservable = WritableValue
102: .withValueType(String.class);
103: WritableValue modelObservable = WritableValue
104: .withValueType(String.class);
105:
106: final String errorMessage = "error";
107: ValueChangeEventTracker errorCounter = new ValueChangeEventTracker();
108: ChangeEventTracker errorsCounter = new ChangeEventTracker();
109:
110: IObservableValue error = new AggregateValidationStatus(dbc
111: .getBindings(), AggregateValidationStatus.MAX_SEVERITY);
112: error.addValueChangeListener(errorCounter);
113: assertTrue(((IStatus) error.getValue()).isOK());
114:
115: IObservableMap errors = dbc.getValidationStatusMap();
116: errors.addChangeListener(errorsCounter);
117: assertEquals(0, errors.size());
118:
119: IValidator validator = new IValidator() {
120: public IStatus validate(Object value) {
121: return ValidationStatus.error(errorMessage);
122: }
123: };
124:
125: dbc.bindValue(targetObservable, modelObservable,
126: new UpdateValueStrategy()
127: .setAfterGetValidator(validator), null);
128:
129: targetObservable.setValue("");
130: assertFalse(((IStatus) error.getValue()).isOK());
131: assertEquals(errorMessage, ((IStatus) error.getValue())
132: .getMessage());
133: assertEquals(1, errors.size());
134: assertEquals(1, errorsCounter.count);
135: assertEquals(1, errorCounter.count);
136: error.dispose();
137: }
138:
139: /**
140: * Asserts that then
141: * {@link DataBindingContext#bindValue(IObservableValue, IObservableValue, org.eclipse.jface.databinding.DefaultBindSpec, BindSpec)}
142: * if invoked the created binding is added to the internal list of bindings.
143: *
144: * @throws Exception
145: */
146: public void testBindValueAddBinding() throws Exception {
147: WritableValue targetValue = WritableValue
148: .withValueType(String.class);
149: WritableValue modelValue = WritableValue
150: .withValueType(String.class);
151:
152: assertNotNull(dbc.getBindings());
153: assertEquals(0, dbc.getBindings().size());
154:
155: Binding binding = dbc.bindValue(targetValue, modelValue, null,
156: null);
157: assertNotNull(binding);
158: assertNotNull(dbc.getBindings());
159: assertEquals(1, dbc.getBindings().size());
160: assertEquals(binding, dbc.getBindings().get(0));
161: }
162:
163: /**
164: * Asserts that when
165: * {@link DataBindingContext#bindList(IObservableList, IObservableList, org.eclipse.jface.databinding.DefaultBindSpec, UpdateListStrategy)}
166: * is invoked the created binding is added to the intenal list of bindings.
167: *
168: * @throws Exception
169: */
170: public void testBindListAddBinding() throws Exception {
171: WritableList targetList = new WritableList(new ArrayList(),
172: Object.class);
173: WritableList modelList = new WritableList(new ArrayList(),
174: Object.class);
175:
176: assertNotNull(dbc.getBindings());
177: assertEquals(0, dbc.getBindings().size());
178:
179: Binding binding = dbc.bindList(targetList, modelList, null,
180: null);
181: assertNotNull(binding);
182: assertNotNull(dbc.getBindings());
183: assertEquals(1, dbc.getBindings().size());
184: assertEquals(binding, dbc.getBindings().get(0));
185: }
186:
187: public void testGetBindingsImmutability() throws Exception {
188: BindingStub binding = new BindingStub();
189: binding.init(dbc);
190:
191: try {
192: dbc.getBindings().remove(0);
193: fail("exception should have been thrown");
194: } catch (UnsupportedOperationException e) {
195: }
196: }
197:
198: public void testRemoveBinding() throws Exception {
199: BindingStub binding = new BindingStub();
200: binding.init(dbc);
201:
202: assertTrue("context should contain the binding", dbc
203: .getBindings().contains(binding));
204: binding.dispose();
205: assertFalse("binding should have been removed", dbc
206: .getBindings().contains(binding));
207: }
208:
209: /**
210: * Asserts that when a ValueBinding is created validation is ran to ensure
211: * that the validation status of the Binding reflects the validity of the
212: * value in the target.
213: *
214: * @throws Exception
215: */
216: public void testValidateTargetAfterValueBindingCreation()
217: throws Exception {
218: WritableValue target = new WritableValue("", String.class);
219: WritableValue model = new WritableValue("2", String.class);
220: class Validator implements IValidator {
221: public IStatus validate(Object value) {
222: return ValidationStatus.error("error");
223: }
224: }
225:
226: Binding binding = dbc.bindValue(target, model,
227: new UpdateValueStrategy()
228: .setAfterConvertValidator(new Validator()),
229: null);
230:
231: assertEquals(IStatus.ERROR, ((IStatus) binding
232: .getValidationStatus().getValue()).getSeverity());
233: }
234:
235: protected void assertNoErrorsFound() {
236: IStatus status = AggregateValidationStatus
237: .getStatusMaxSeverity(dbc.getBindings());
238: assertTrue("No errors should be found, but found " + status,
239: status.isOK());
240: }
241:
242: protected void assertErrorsFound() {
243: IStatus status = AggregateValidationStatus
244: .getStatusMaxSeverity(dbc.getBindings());
245: assertFalse("Errors should be found, but found none.", status
246: .isOK());
247: }
248:
249: private static class BindingStub extends Binding {
250: DataBindingContext context;
251:
252: public BindingStub() {
253: super (new WritableValue(), new WritableValue());
254: }
255:
256: public IObservableValue getPartialValidationStatus() {
257: return null;
258: }
259:
260: public IObservableValue getValidationStatus() {
261: return null;
262: }
263:
264: public void updateTargetToModel() {
265: }
266:
267: public void updateModelToTarget() {
268: }
269:
270: public void updateTargetFromModel(int phase) {
271: }
272:
273: public void updateModelFromTarget(int phase) {
274: }
275:
276: protected void postInit() {
277: }
278:
279: protected void preInit() {
280: }
281:
282: public void validateModelToTarget() {
283: }
284:
285: public void validateTargetToModel() {
286: }
287: }
288: }
|