001: /*******************************************************************************
002: * Copyright (c) 2006 Brad Reynolds 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: * Brad Reynolds - initial API and implementation
010: * Brad Reynolds - bug 171616
011: * Katarzyna Marszalek - test case for bug 198519
012: ******************************************************************************/package org.eclipse.core.tests.internal.databinding.internal.beans;
013:
014: import java.beans.IntrospectionException;
015: import java.beans.PropertyDescriptor;
016:
017: import junit.framework.Test;
018:
019: import org.eclipse.core.databinding.beans.BeansObservables;
020: import org.eclipse.core.databinding.observable.ChangeEvent;
021: import org.eclipse.core.databinding.observable.IChangeListener;
022: import org.eclipse.core.databinding.observable.IObservable;
023: import org.eclipse.core.databinding.observable.Realm;
024: import org.eclipse.core.databinding.observable.value.ComputedValue;
025: import org.eclipse.core.databinding.observable.value.IObservableValue;
026: import org.eclipse.core.internal.databinding.internal.beans.JavaBeanObservableValue;
027: import org.eclipse.jface.conformance.databinding.AbstractObservableValueContractDelegate;
028: import org.eclipse.jface.conformance.databinding.ObservableValueContractTest;
029: import org.eclipse.jface.conformance.databinding.SuiteBuilder;
030: import org.eclipse.jface.examples.databinding.model.SimplePerson;
031: import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
032:
033: /**
034: * @since 3.2
035: */
036: public class JavaBeanObservableValueTest extends
037: AbstractDefaultRealmTestCase {
038: private Bean bean;
039: private JavaBeanObservableValue observableValue;
040: private PropertyDescriptor propertyDescriptor;
041: private String propertyName;
042:
043: /* (non-Javadoc)
044: * @see junit.framework.TestCase#setUp()
045: */
046: protected void setUp() throws Exception {
047: super .setUp();
048:
049: bean = new Bean();
050: propertyName = "value";
051: propertyDescriptor = new PropertyDescriptor(propertyName,
052: Bean.class);
053: observableValue = new JavaBeanObservableValue(Realm
054: .getDefault(), bean, propertyDescriptor);
055: }
056:
057: public void testSetsValueInBean() throws Exception {
058: String value = "value";
059: assertNull(observableValue.getValue());
060: observableValue.setValue(value);
061: assertEquals("value", value, observableValue.getValue());
062: }
063:
064: public void testGetObserved() throws Exception {
065: assertEquals(bean, observableValue.getObserved());
066: }
067:
068: public void testGetPropertyDescriptor() throws Exception {
069: assertEquals(propertyDescriptor, observableValue
070: .getPropertyDescriptor());
071: }
072:
073: public void testSetValueThrowsExceptionThrownByBean()
074: throws Exception {
075: ThrowsSetException temp = new ThrowsSetException();
076: JavaBeanObservableValue observable = new JavaBeanObservableValue(
077: Realm.getDefault(), temp, new PropertyDescriptor(
078: "value", ThrowsSetException.class));
079:
080: try {
081: observable.setValue("");
082: fail("exception should have been thrown");
083: } catch (RuntimeException e) {
084: assertEquals(temp.thrownException, e.getCause());
085: }
086: }
087:
088: public void testGetValueThrowsExceptionThrownByBean()
089: throws Exception {
090: ThrowsGetException temp = new ThrowsGetException();
091: JavaBeanObservableValue observable = new JavaBeanObservableValue(
092: Realm.getDefault(), temp, new PropertyDescriptor(
093: "value", ThrowsGetException.class));
094:
095: try {
096: observable.getValue();
097: fail("exception should have been thrown");
098: } catch (RuntimeException e) {
099: assertEquals(temp.thrownException, e.getCause());
100: }
101: }
102:
103: public void testBug198519() {
104: final SimplePerson person = new SimplePerson();
105: final ComputedValue cv = new ComputedValue() {
106: final IObservableValue name = BeansObservables
107: .observeValue(person, "name"); //$NON-NLS-1$
108:
109: protected Object calculate() {
110: return Boolean.valueOf(name.getValue() != null);
111: }
112: };
113: cv.addChangeListener(new IChangeListener() {
114: public void handleChange(ChangeEvent event) {
115: cv.getValue();
116: }
117: });
118: person.setName("foo");
119: }
120:
121: public static Test suite() {
122: Object[] params = new Object[] { new Delegate() };
123:
124: return new SuiteBuilder().addTests(
125: JavaBeanObservableValueTest.class)
126: .addParameterizedTests(
127: ObservableValueContractTest.class, params)
128: .build();
129: }
130:
131: /* package */static class Delegate extends
132: AbstractObservableValueContractDelegate {
133: private Bean bean;
134:
135: public void setUp() {
136: super .setUp();
137:
138: bean = new Bean("");
139: }
140:
141: public IObservableValue createObservableValue(Realm realm) {
142: try {
143: PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
144: "value", Bean.class);
145: return new JavaBeanObservableValue(realm, bean,
146: propertyDescriptor);
147: } catch (IntrospectionException e) {
148: throw new RuntimeException(e);
149: }
150: }
151:
152: public void change(IObservable observable) {
153: IObservableValue observableValue = (IObservableValue) observable;
154: observableValue.setValue(createValue(observableValue));
155: }
156:
157: public Object getValueType(IObservableValue observable) {
158: return String.class;
159: }
160:
161: public Object createValue(IObservableValue observable) {
162: return observable.getValue() + "a";
163: }
164: }
165:
166: /**
167: * Throws an exception when the value is set.
168: *
169: * @since 3.2
170: */
171: /* package */class ThrowsSetException {
172: private String value;
173:
174: /* package */NullPointerException thrownException;
175:
176: public void setValue(String value) {
177: throw thrownException = new NullPointerException();
178: }
179:
180: public String getValue() {
181: return value;
182: }
183: }
184:
185: /* package */class ThrowsGetException {
186: public String value;
187:
188: /* package */NullPointerException thrownException;
189:
190: public String getValue() {
191: throw thrownException = new NullPointerException();
192: }
193:
194: public void setValue(String value) {
195: this.value = value;
196: }
197: }
198: }
|