01: /*******************************************************************************
02: * Copyright (c) 2007 Brad Reynolds and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Brad Reynolds - initial API and implementation
10: ******************************************************************************/package org.eclipse.core.tests.internal.databinding.internal.beans;
11:
12: import java.beans.PropertyDescriptor;
13: import java.util.Arrays;
14: import java.util.HashSet;
15:
16: import junit.framework.TestCase;
17:
18: import org.eclipse.core.databinding.observable.set.ISetChangeListener;
19: import org.eclipse.core.databinding.observable.set.SetChangeEvent;
20: import org.eclipse.core.internal.databinding.internal.beans.JavaBeanObservableSet;
21: import org.eclipse.jface.databinding.swt.SWTObservables;
22: import org.eclipse.swt.widgets.Display;
23:
24: /**
25: * @since 3.3
26: */
27: public class JavaBeanObservableSetTest extends TestCase {
28: private JavaBeanObservableSet observableSet;
29: private Bean bean;
30: private PropertyDescriptor propertyDescriptor;
31: private String propertyName;
32: private SetChangeListener listener;
33:
34: /*
35: * (non-Javadoc)
36: *
37: * @see junit.framework.TestCase#setUp()
38: */
39: protected void setUp() throws Exception {
40: bean = new Bean();
41: propertyName = "set";
42: propertyDescriptor = new PropertyDescriptor(propertyName,
43: Bean.class);
44:
45: observableSet = new JavaBeanObservableSet(SWTObservables
46: .getRealm(Display.getDefault()), bean,
47: propertyDescriptor, Bean.class);
48: listener = new SetChangeListener();
49: }
50:
51: public void testGetObserved() throws Exception {
52: assertEquals(bean, observableSet.getObserved());
53: }
54:
55: public void testGetPropertyDescriptor() throws Exception {
56: assertEquals(propertyDescriptor, observableSet
57: .getPropertyDescriptor());
58: }
59:
60: public void testGetElementType() throws Exception {
61: assertEquals(Bean.class, observableSet.getElementType());
62: }
63:
64: public void testRegistersListenerAfterFirstListenerIsAdded()
65: throws Exception {
66: assertFalse(bean.changeSupport.hasListeners(propertyName));
67: observableSet.addSetChangeListener(new SetChangeListener());
68: assertTrue(bean.changeSupport.hasListeners(propertyName));
69: }
70:
71: public void testRemovesListenerAfterLastListenerIsRemoved()
72: throws Exception {
73: observableSet.addSetChangeListener(listener);
74:
75: assertTrue(bean.changeSupport.hasListeners(propertyName));
76: observableSet.removeSetChangeListener(listener);
77: assertFalse(bean.changeSupport.hasListeners(propertyName));
78: }
79:
80: public void testFiresChangeEvents() throws Exception {
81: observableSet.addSetChangeListener(listener);
82: assertEquals(0, listener.count);
83: bean.setSet(new HashSet(Arrays.asList(new String[] { "1" })));
84: assertEquals(1, listener.count);
85: }
86:
87: static class SetChangeListener implements ISetChangeListener {
88: int count;
89:
90: public void handleSetChange(SetChangeEvent event) {
91: count++;
92: }
93: }
94: }
|