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: *******************************************************************************/package org.eclipse.core.tests.internal.databinding.internal.beans;
011:
012: import java.beans.PropertyDescriptor;
013: import java.util.HashSet;
014:
015: import junit.framework.TestCase;
016:
017: import org.eclipse.core.databinding.observable.map.IMapChangeListener;
018: import org.eclipse.core.databinding.observable.map.MapChangeEvent;
019: import org.eclipse.core.databinding.observable.map.MapDiff;
020: import org.eclipse.core.databinding.observable.set.WritableSet;
021: import org.eclipse.core.internal.databinding.internal.beans.JavaBeanObservableMap;
022: import org.eclipse.core.tests.databinding.observable.ThreadRealm;
023:
024: /**
025: * @since 3.2
026: *
027: */
028: public class JavaBeanObservableMapTest extends TestCase {
029: private Bean model1;
030:
031: private Bean model2;
032:
033: private WritableSet set;
034:
035: private PropertyDescriptor propertyDescriptor;
036:
037: private JavaBeanObservableMap map;
038:
039: protected void setUp() throws Exception {
040: ThreadRealm realm = new ThreadRealm();
041: realm.init(Thread.currentThread());
042: model1 = new Bean("1");
043: model2 = new Bean("2");
044:
045: set = new WritableSet(realm, new HashSet(), Bean.class);
046: set.add(model1);
047: set.add(model2);
048:
049: propertyDescriptor = new PropertyDescriptor("value", Bean.class);
050: map = new JavaBeanObservableMap(set, propertyDescriptor);
051: }
052:
053: public void testGetValue() throws Exception {
054: assertEquals(
055: "The 'value' from the map should be the value of the property of the model.",
056: model1.getValue(), map.get(model1));
057: }
058:
059: public void testSetValueNotifications() throws Exception {
060: String oldValue = model1.getValue();
061: String newValue = model1.getValue() + model1.getValue();
062: MapChangeListener listener = new MapChangeListener();
063:
064: map.addMapChangeListener(listener);
065: assertEquals(0, listener.count);
066: model1.setValue(newValue);
067: assertEquals(1, listener.count);
068: assertTrue(listener.diff.getChangedKeys().contains(model1));
069: assertEquals(newValue, listener.diff.getNewValue(model1));
070: assertEquals(oldValue, listener.diff.getOldValue(model1));
071: assertFalse(listener.diff.getAddedKeys().contains(model1));
072: assertFalse(listener.diff.getRemovedKeys().contains(model1));
073: }
074:
075: public void testPutValue() throws Exception {
076: String oldValue = model1.getValue();
077: String newValue = model1.getValue() + model1.getValue();
078: MapChangeListener listener = new MapChangeListener();
079: map.addMapChangeListener(listener);
080:
081: assertEquals(0, listener.count);
082: map.put(model1, newValue);
083: assertEquals(1, listener.count);
084: assertEquals(newValue, model1.getValue());
085: assertEquals(oldValue, listener.diff.getOldValue(model1));
086: assertEquals(newValue, listener.diff.getNewValue(model1));
087: assertFalse(listener.diff.getAddedKeys().contains(model1));
088: assertTrue(listener.diff.getChangedKeys().contains(model1));
089: assertFalse(listener.diff.getRemovedKeys().contains(model1));
090: }
091:
092: public void testAddKey() throws Exception {
093: MapChangeListener listener = new MapChangeListener();
094: map.addMapChangeListener(listener);
095:
096: Bean model3 = new Bean("3");
097:
098: assertEquals(0, listener.count);
099: set.add(model3);
100: assertEquals(1, listener.count);
101: assertTrue(listener.diff.getAddedKeys().contains(model3));
102: assertEquals(model3.getValue(), map.get(model3));
103:
104: String newValue = model3.getValue() + model3.getValue();
105: model3.setValue(newValue);
106: assertEquals(2, listener.count);
107: assertEquals(3, map.size());
108: }
109:
110: public void testRemoveKey() throws Exception {
111: MapChangeListener listener = new MapChangeListener();
112: map.addMapChangeListener(listener);
113:
114: assertEquals(0, listener.count);
115: set.remove(model1);
116: assertEquals(1, listener.count);
117: assertFalse(listener.diff.getAddedKeys().contains(model1));
118: assertFalse(listener.diff.getChangedKeys().contains(model1));
119: assertTrue(listener.diff.getRemovedKeys().contains(model1));
120: assertEquals(1, map.size());
121: }
122:
123: public void testGetObserved() throws Exception {
124: assertEquals(set, map.getObserved());
125: }
126:
127: public void testGetPropertyDescriptor() throws Exception {
128: assertEquals(propertyDescriptor, map.getPropertyDescriptor());
129: }
130:
131: private static class MapChangeListener implements
132: IMapChangeListener {
133: int count;
134:
135: MapDiff diff;
136:
137: public void handleMapChange(MapChangeEvent event) {
138: count++;
139: this.diff = event.diff;
140: }
141: }
142: }
|