001: /*******************************************************************************
002: * Copyright (c) 2006, 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 116920, 159768
011: *******************************************************************************/package org.eclipse.core.tests.databinding;
012:
013: import java.util.ArrayList;
014: import java.util.Arrays;
015: import java.util.List;
016:
017: import org.eclipse.core.databinding.Binding;
018: import org.eclipse.core.databinding.DataBindingContext;
019: import org.eclipse.core.databinding.UpdateListStrategy;
020: import org.eclipse.core.databinding.observable.list.IObservableList;
021: import org.eclipse.core.databinding.observable.list.WritableList;
022: import org.eclipse.core.databinding.validation.ValidationStatus;
023: import org.eclipse.core.internal.databinding.BindingStatus;
024: import org.eclipse.core.runtime.IStatus;
025: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
026:
027: /**
028: * @since 1.1
029: */
030: public class ListBindingTest extends AbstractDefaultRealmTestCase {
031: private IObservableList target;
032: private IObservableList model;
033: private DataBindingContext dbc;
034:
035: /*
036: * (non-Javadoc)
037: *
038: * @see junit.framework.TestCase#setUp()
039: */
040: protected void setUp() throws Exception {
041: super .setUp();
042:
043: target = new WritableList(new ArrayList(), String.class);
044: model = new WritableList(new ArrayList(), String.class);
045: dbc = new DataBindingContext();
046: }
047:
048: public void testUpdateModelFromTarget() throws Exception {
049: Binding binding = dbc.bindList(target, model,
050: new UpdateListStrategy(
051: UpdateListStrategy.POLICY_ON_REQUEST),
052: new UpdateListStrategy(
053: UpdateListStrategy.POLICY_ON_REQUEST));
054:
055: target.add("1");
056: List targetCopy = new ArrayList(target.size());
057: targetCopy.addAll(target);
058:
059: model.add("2");
060:
061: assertFalse("target should not equal model", target
062: .equals(model));
063: binding.updateTargetToModel();
064: assertEquals("target should not have changed", targetCopy,
065: target);
066: assertEquals("target != model", target, model);
067: }
068:
069: public void testUpdateTargetFromModel() throws Exception {
070: Binding binding = dbc.bindList(target, model,
071: new UpdateListStrategy(
072: UpdateListStrategy.POLICY_ON_REQUEST),
073: new UpdateListStrategy(
074: UpdateListStrategy.POLICY_ON_REQUEST));
075:
076: target.add("1");
077: model.add("2");
078:
079: List modelCopy = new ArrayList(model.size());
080: modelCopy.addAll(model);
081:
082: assertFalse("model should not equal target", model
083: .equals(target));
084: binding.updateModelToTarget();
085:
086: assertEquals("model should not have changed", modelCopy, model);
087: assertEquals("model != target", model, target);
088: }
089:
090: public void testGetTarget() throws Exception {
091: Binding binding = dbc.bindList(target, model, null, null);
092: assertEquals(target, binding.getTarget());
093: }
094:
095: public void testGetModel() throws Exception {
096: Binding binding = dbc.bindList(target, model, null, null);
097: assertEquals(model, binding.getModel());
098: }
099:
100: public void testStatusIsInstanceOfBindingStatus() throws Exception {
101: Binding binding = dbc.bindList(target, model, null, null);
102: assertTrue(binding.getValidationStatus().getValue() instanceof BindingStatus);
103: }
104:
105: public void testAddValidationStatusContainsMultipleStatuses()
106: throws Exception {
107: UpdateListStrategy strategy = new UpdateListStrategy() {
108: protected IStatus doAdd(IObservableList observableList,
109: Object element, int index) {
110: super .doAdd(observableList, element, index);
111:
112: switch (index) {
113: case 0:
114: return ValidationStatus.error("");
115: case 1:
116: return ValidationStatus.info("");
117: }
118:
119: return null;
120: }
121: };
122:
123: Binding binding = dbc.bindList(target, model, strategy, null);
124: target.addAll(Arrays.asList(new String[] { "1", "2" }));
125:
126: IStatus status = (IStatus) binding.getValidationStatus()
127: .getValue();
128: assertEquals("maximum status", IStatus.ERROR, status
129: .getSeverity());
130: assertTrue("multi status", status.isMultiStatus());
131:
132: IStatus[] children = status.getChildren();
133: assertEquals("multi status children", 2, children.length);
134: assertEquals("first status severity", IStatus.ERROR,
135: children[0].getSeverity());
136: assertEquals("second status severity", IStatus.INFO,
137: children[1].getSeverity());
138: }
139:
140: public void testRemoveValidationStatusContainsMultipleStatuses()
141: throws Exception {
142: List items = Arrays.asList(new String[] { "1", "2" });
143: model.addAll(items);
144:
145: UpdateListStrategy strategy = new UpdateListStrategy() {
146: int count;
147:
148: /* (non-Javadoc)
149: * @see org.eclipse.core.databinding.UpdateListStrategy#doRemove(org.eclipse.core.databinding.observable.list.IObservableList, int)
150: */
151: protected IStatus doRemove(IObservableList observableList,
152: int index) {
153: super .doRemove(observableList, index);
154:
155: switch (count++) {
156: case 0:
157: return ValidationStatus.error("");
158: case 1:
159: return ValidationStatus.info("");
160: }
161:
162: return null;
163: }
164: };
165:
166: Binding binding = dbc.bindList(target, model, strategy, null);
167: target.removeAll(items);
168:
169: IStatus status = (IStatus) binding.getValidationStatus()
170: .getValue();
171: assertEquals("maximum status", IStatus.ERROR, status
172: .getSeverity());
173: assertTrue("multi status", status.isMultiStatus());
174:
175: IStatus[] children = status.getChildren();
176: assertEquals("multi status children", 2, children.length);
177: assertEquals("first status severity", IStatus.ERROR,
178: children[0].getSeverity());
179: assertEquals("second status severity", IStatus.INFO,
180: children[1].getSeverity());
181: }
182:
183: public void testAddOKValidationStatus() throws Exception {
184: Binding binding = dbc.bindList(target, model, null, null);
185: target.add("1");
186:
187: IStatus status = (IStatus) binding.getValidationStatus()
188: .getValue();
189: assertTrue(status.isOK());
190: assertEquals(0, status.getChildren().length);
191: }
192:
193: public void testRemoveOKValidationStatus() throws Exception {
194: model.add("1");
195: Binding binding = dbc.bindList(target, model, null, null);
196:
197: target.remove("1");
198: IStatus status = (IStatus) binding.getValidationStatus()
199: .getValue();
200: assertTrue(status.isOK());
201: assertEquals(0, status.getChildren().length);
202: }
203: }
|