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.databinding.observable.map;
011:
012: import java.util.HashMap;
013: import java.util.Map;
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.map.ObservableMap;
021: import org.eclipse.jface.tests.databinding.RealmTester;
022: import org.eclipse.jface.tests.databinding.RealmTester.CurrentRealm;
023:
024: /**
025: * @since 3.2
026: *
027: */
028: public class ObservableMapTest extends TestCase {
029: ObservableMapStub map;
030:
031: protected void setUp() throws Exception {
032: RealmTester.setDefault(new CurrentRealm(true));
033: map = new ObservableMapStub(new HashMap());
034: }
035:
036: protected void tearDown() throws Exception {
037: RealmTester.setDefault(null);
038: }
039:
040: public void testDisposeMapChangeListeners() throws Exception {
041: class MapChangeListener implements IMapChangeListener {
042: int count;
043:
044: public void handleMapChange(MapChangeEvent event) {
045: count++;
046: }
047: }
048:
049: MapChangeListener listener = new MapChangeListener();
050: map.addMapChangeListener(listener);
051:
052: assertEquals(0, listener.count);
053: map.fireMapChange(null);
054: assertEquals(1, listener.count);
055:
056: map.dispose();
057: try {
058: map.fireMapChange(null);
059: } catch (Exception e) {
060: // do nothing
061: }
062:
063: assertEquals("listener should not have been notified", 1,
064: listener.count);
065: }
066:
067: public void testIsStaleRealmChecks() throws Exception {
068: RealmTester.exerciseCurrent(new Runnable() {
069: public void run() {
070: map.isStale();
071: }
072: });
073: }
074:
075: public void testSetStaleRealmChecks() throws Exception {
076: RealmTester.exerciseCurrent(new Runnable() {
077: public void run() {
078: map.setStale(true);
079: }
080: });
081: }
082:
083: public void testFireMapChangeRealmChecks() throws Exception {
084: RealmTester.exerciseCurrent(new Runnable() {
085: public void run() {
086: map.fireMapChange(null);
087: }
088: });
089: }
090:
091: static class ObservableMapStub extends ObservableMap {
092: /**
093: * @param wrappedMap
094: */
095: public ObservableMapStub(Map wrappedMap) {
096: super (wrappedMap);
097: }
098:
099: protected void fireMapChange(MapDiff diff) {
100: super.fireMapChange(diff);
101: }
102: }
103: }
|