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 164268, 171616
011: *******************************************************************************/package org.eclipse.core.tests.databinding.beans;
012:
013: import java.util.Arrays;
014:
015: import org.eclipse.core.databinding.beans.BeansObservables;
016: import org.eclipse.core.databinding.beans.IBeanObservable;
017: import org.eclipse.core.databinding.observable.Realm;
018: import org.eclipse.core.databinding.observable.list.IObservableList;
019: import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
020: import org.eclipse.core.databinding.observable.set.IObservableSet;
021: import org.eclipse.core.databinding.observable.value.IObservableValue;
022: import org.eclipse.core.databinding.observable.value.WritableValue;
023: import org.eclipse.core.internal.databinding.internal.beans.BeanObservableListDecorator;
024: import org.eclipse.core.internal.databinding.internal.beans.BeanObservableSetDecorator;
025: import org.eclipse.core.internal.databinding.internal.beans.BeanObservableValueDecorator;
026: import org.eclipse.core.tests.internal.databinding.internal.beans.Bean;
027: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
028:
029: /**
030: * @since 3.2
031: */
032: public class BeansObservablesTest extends AbstractDefaultRealmTestCase {
033: Bean[] elements = null;
034: Bean model = null;
035: Class elementType = null;
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039:
040: elements = new Bean[] { new Bean("1"), new Bean("2"),
041: new Bean("3") };
042: model = new Bean(Arrays.asList(elements));
043: elementType = Bean.class;
044: }
045:
046: public void testObserveListArrayInferredElementType()
047: throws Exception {
048: IObservableList list = BeansObservables.observeList(Realm
049: .getDefault(), model, "list", null);
050: assertEquals("element type", Object.class, list
051: .getElementType());
052: }
053:
054: public void testObserveListNonInferredElementType()
055: throws Exception {
056: elementType = Object.class;
057: IObservableList list = BeansObservables.observeList(Realm
058: .getDefault(), model, "list", null);
059: assertEquals("element type", elementType, list.getElementType());
060: }
061:
062: public void testListFactory() throws Exception {
063: IObservableFactory factory = BeansObservables.listFactory(Realm
064: .getDefault(), "list", elementType);
065: IObservableList list = (IObservableList) factory
066: .createObservable(model);
067:
068: assertTrue("elements of the list", Arrays.equals(elements, list
069: .toArray(new Bean[list.size()])));
070: assertEquals("element type", elementType, list.getElementType());
071: }
072:
073: public void testObserveDetailListElementType() throws Exception {
074: WritableValue parent = WritableValue.withValueType(Bean.class);
075: parent.setValue(model);
076: IObservableList list = BeansObservables.observeDetailList(Realm
077: .getDefault(), parent, "list", elementType);
078:
079: assertEquals("element type", elementType, list.getElementType());
080: assertTrue("elements of list", Arrays.equals(elements, list
081: .toArray(new Bean[list.size()])));
082: }
083:
084: public void testObserveDetailValueIBeanObservable()
085: throws Exception {
086: WritableValue parent = WritableValue.withValueType(Bean.class);
087: parent.setValue(new Bean());
088:
089: IObservableValue detailValue = BeansObservables
090: .observeDetailValue(Realm.getDefault(), parent,
091: "value", String.class);
092: assertTrue(detailValue instanceof IBeanObservable);
093:
094: BeanObservableValueDecorator beanObservable = (BeanObservableValueDecorator) detailValue;
095: assertEquals("property descriptor", Bean.class.getMethod(
096: "getValue", null), beanObservable
097: .getPropertyDescriptor().getReadMethod());
098: assertEquals("observed", parent.getValue(), beanObservable
099: .getObserved());
100: assertTrue("delegate", beanObservable.getDelegate().getClass()
101: .getName().endsWith("DetailObservableValue"));
102: }
103:
104: public void testObserveDetailValueNullOuterElementType()
105: throws Exception {
106: WritableValue parent = new WritableValue(new Bean(), null);
107:
108: IObservableValue detailValue = BeansObservables
109: .observeDetailValue(Realm.getDefault(), parent,
110: "value", String.class);
111:
112: assertNull("property descriptor",
113: ((IBeanObservable) detailValue).getPropertyDescriptor());
114: }
115:
116: public void testObservableDetailListIBeanObservable()
117: throws Exception {
118: WritableValue parent = WritableValue.withValueType(Bean.class);
119: parent.setValue(new Bean());
120:
121: IObservableList detailList = BeansObservables
122: .observeDetailList(Realm.getDefault(), parent, "list",
123: Bean.class);
124: assertTrue("detail is not an IBeanObservable",
125: detailList instanceof IBeanObservable);
126:
127: BeanObservableListDecorator beanObservable = (BeanObservableListDecorator) detailList;
128: assertEquals("property descriptor", Bean.class.getMethod(
129: "getList", null), beanObservable
130: .getPropertyDescriptor().getReadMethod());
131: assertEquals("observed", parent, beanObservable.getObserved());
132:
133: // DetailObservableList is package level we can do a straight instanceof
134: // check
135: assertTrue("delegate is the observed", beanObservable
136: .getDelegate().equals(detailList));
137: }
138:
139: public void testObservableDetailListNullOuterElementType()
140: throws Exception {
141: WritableValue parent = new WritableValue(new Bean(), null);
142:
143: IObservableList detailList = BeansObservables
144: .observeDetailList(Realm.getDefault(), parent, "list",
145: Bean.class);
146:
147: assertNull("property descriptor",
148: ((IBeanObservable) detailList).getPropertyDescriptor());
149: }
150:
151: public void testObservableDetailSetIBeanObservable()
152: throws Exception {
153: WritableValue parent = WritableValue.withValueType(Bean.class);
154: parent.setValue(new Bean());
155:
156: IObservableSet detailSet = BeansObservables.observeDetailSet(
157: Realm.getDefault(), parent, "set", Bean.class);
158: assertTrue("detail is not an IBeanObservable",
159: detailSet instanceof IBeanObservable);
160:
161: BeanObservableSetDecorator beanObservable = (BeanObservableSetDecorator) detailSet;
162: assertEquals("property descriptor", Bean.class.getMethod(
163: "getSet", null), beanObservable.getPropertyDescriptor()
164: .getReadMethod());
165: assertEquals("observed", parent, beanObservable.getObserved());
166:
167: // DetailObservableSet is package level we can't do a straight
168: // instanceof check
169: assertTrue("delegate is the observed", beanObservable
170: .getDelegate().equals(detailSet));
171: }
172:
173: public void testObservableDetailSetNullOuterElementType()
174: throws Exception {
175: WritableValue parent = new WritableValue(new Bean(), null);
176:
177: IObservableSet detailSet = BeansObservables.observeDetailSet(
178: Realm.getDefault(), parent, "set", Bean.class);
179:
180: assertNull("property descriptor", ((IBeanObservable) detailSet)
181: .getPropertyDescriptor());
182: }
183:
184: public void testObserveSetElementType() throws Exception {
185: Bean bean = new Bean();
186: IObservableSet observableSet = BeansObservables.observeSet(
187: Realm.getDefault(), bean, "set", Bean.class);
188: assertEquals(Bean.class, observableSet.getElementType());
189: }
190:
191: public void testObserveSetNonInferredElementType() throws Exception {
192: Bean bean = new Bean();
193: IObservableSet observableSet = BeansObservables.observeSet(
194: Realm.getDefault(), bean, "set");
195: assertEquals(Object.class, observableSet.getElementType());
196: }
197: }
|