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: ******************************************************************************/package org.eclipse.core.tests.databinding.observable.map;
011:
012: import java.util.Collections;
013: import java.util.Set;
014:
015: import junit.framework.TestCase;
016:
017: import org.eclipse.core.databinding.observable.Realm;
018: import org.eclipse.core.databinding.observable.map.MapChangeEvent;
019: import org.eclipse.core.databinding.observable.map.WritableMap;
020: import org.eclipse.jface.tests.databinding.RealmTester;
021: import org.eclipse.jface.tests.databinding.EventTrackers.MapChangeEventTracker;
022: import org.eclipse.jface.tests.databinding.RealmTester.CurrentRealm;
023:
024: /**
025: * @since 3.2
026: *
027: */
028: public class WritableMapTest extends TestCase {
029: protected void setUp() throws Exception {
030: RealmTester.setDefault(new CurrentRealm(true));
031: }
032:
033: protected void tearDown() throws Exception {
034: RealmTester.setDefault(null);
035: }
036:
037: public void testPutRealmChecks() throws Exception {
038: RealmTester.exerciseCurrent(new Runnable() {
039: public void run() {
040: WritableMap map = new WritableMap();
041: map.put("", "");
042: }
043: });
044: }
045:
046: public void testRemoveRealmChecks() throws Exception {
047: RealmTester.exerciseCurrent(new Runnable() {
048: public void run() {
049: WritableMap map = new WritableMap();
050: CurrentRealm realm = (CurrentRealm) Realm.getDefault();
051: boolean current = realm.isCurrent();
052: realm.setCurrent(true);
053: map.put("", "");
054: realm.setCurrent(current);
055:
056: map.remove("");
057: }
058: });
059: }
060:
061: public void testClearRealmChecks() throws Exception {
062: RealmTester.exerciseCurrent(new Runnable() {
063: public void run() {
064: WritableMap map = new WritableMap();
065: map.clear();
066: }
067: });
068: }
069:
070: public void testPutAllRealmChecks() throws Exception {
071: RealmTester.exerciseCurrent(new Runnable() {
072: public void run() {
073: WritableMap map = new WritableMap();
074: map.putAll(Collections.EMPTY_MAP);
075: }
076: });
077: }
078:
079: public void testPutWithExistingKeyMapChangeEvent() throws Exception {
080: WritableMap map = new WritableMap();
081: String key = "key";
082: String value = "value";
083: map.put(key, value);
084:
085: MapChangeEventTracker listener = new MapChangeEventTracker();
086: map.addMapChangeListener(listener);
087:
088: assertEquals(0, listener.count);
089:
090: String newValue = "new value";
091: map.put(key, newValue);
092:
093: assertEquals(1, listener.count);
094: MapChangeEvent event = listener.event;
095:
096: Set changedKeys = event.diff.getChangedKeys();
097: assertEquals(1, changedKeys.size());
098: assertTrue(changedKeys.contains(key));
099: assertEquals(value, event.diff.getOldValue(key));
100: assertEquals(newValue, event.diff.getNewValue(key));
101: }
102: }
|