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
011: * Brad Reynolds - bug 164653
012: *******************************************************************************/package org.eclipse.core.tests.databinding.observable.value;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import org.eclipse.core.databinding.observable.value.ComputedValue;
019: import org.eclipse.core.databinding.observable.value.WritableValue;
020: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
021:
022: /**
023: * @since 1.0
024: *
025: */
026: public class ComputedValueTest extends AbstractDefaultRealmTestCase {
027: public void testValueType() throws Exception {
028: ComputedValue cv = new ComputedValue(Integer.TYPE) {
029: protected Object calculate() {
030: return new Integer(42);
031: }
032: };
033: assertEquals("value type should be the type that was set",
034: Integer.TYPE, cv.getValueType());
035:
036: cv = new ComputedValue() {
037: protected Object calculate() {
038: // TODO Auto-generated method stub
039: return null;
040: }
041: };
042:
043: assertNull(cv.getValueType());
044: }
045:
046: public void test_getValue() throws Exception {
047: ComputedValue cv = new ComputedValue() {
048: protected Object calculate() {
049: return new Integer(42);
050: }
051: };
052: assertEquals("Calculated value should be 42", new Integer(42),
053: cv.getValue());
054: }
055:
056: public void testDependencyValueChange() throws Exception {
057: final WritableValue value = new WritableValue(new Integer(42),
058: Integer.TYPE);
059:
060: ComputedValue cv = new ComputedValue() {
061: protected Object calculate() {
062: return value.getValue();
063: }
064: };
065:
066: assertEquals(
067: "calculated value should have been that of the writable value",
068: value.getValue(), cv.getValue());
069:
070: value.setValue(new Integer(44));
071:
072: assertEquals(
073: "calculated value should have been that of the writable value",
074: value.getValue(), cv.getValue());
075: }
076:
077: private static class WritableValueExt extends WritableValue {
078: public WritableValueExt(Object valueType, Object initialValue) {
079: super (initialValue, valueType);
080: }
081:
082: public boolean hasListeners() {
083: return super .hasListeners();
084: }
085: }
086:
087: public void testHookAndUnhookDependantObservables()
088: throws Exception {
089: final List values = new ArrayList();
090:
091: ComputedValue cv = new ComputedValue() {
092: protected Object calculate() {
093: int sum = 0;
094: for (Iterator it = values.iterator(); it.hasNext();) {
095: WritableValue value = (WritableValue) it.next();
096: sum += ((Integer) value.getValue()).intValue();
097:
098: }
099:
100: return new Integer(sum);
101: }
102: };
103:
104: WritableValueExt value1 = new WritableValueExt(Integer.TYPE,
105: new Integer(1));
106: WritableValueExt value2 = new WritableValueExt(Integer.TYPE,
107: new Integer(1));
108: values.add(value1);
109: values.add(value2);
110:
111: assertFalse(value1.hasListeners());
112: assertFalse(value2.hasListeners());
113: cv.getValue();
114: assertTrue(value1.hasListeners());
115: assertTrue(value2.hasListeners());
116:
117: //force the computed value to be stale
118: value2.setValue(new Integer(2));
119: //remove value2 from the values that are used to compute the value
120: values.remove(value2);
121:
122: //force the value to be computed
123: cv.getValue();
124: assertEquals(new Integer(1), cv.getValue());
125: assertTrue(value1.hasListeners());
126: assertFalse(
127: "because value2 is not a part of the calculation the listeners should have been removed",
128: value2.hasListeners());
129: }
130:
131: public void testSetValueUnsupportedOperationException()
132: throws Exception {
133: ComputedValue cv = new ComputedValue() {
134: protected Object calculate() {
135: return null;
136: }
137: };
138:
139: try {
140: cv.setValue(new Object());
141: fail("exception should have been thrown");
142: } catch (UnsupportedOperationException e) {
143: }
144: }
145: }
|