Standardizing mock listener classes.
1.1 --- a/openide.util/test/unit/src/org/openide/util/ChangeSupportTest.java Mon Apr 16 20:46:47 2007 +0000
1.2 +++ b/openide.util/test/unit/src/org/openide/util/ChangeSupportTest.java Tue Apr 17 23:22:59 2007 +0000
1.3 @@ -20,10 +20,12 @@
1.4 package org.openide.util;
1.5
1.6 import java.util.HashSet;
1.7 +import java.util.List;
1.8 import java.util.Set;
1.9 import javax.swing.event.ChangeEvent;
1.10 import javax.swing.event.ChangeListener;
1.11 import org.netbeans.junit.NbTestCase;
1.12 +import org.openide.util.test.MockChangeListener;
1.13
1.14 /**
1.15 *
1.16 @@ -37,26 +39,8 @@
1.17
1.18 public void testChangeSupport() {
1.19 final int[] changeCount = { 0 };
1.20 - class Listener implements ChangeListener {
1.21 - private int notified;
1.22 - private ChangeEvent lastEvent;
1.23 - public void stateChanged(ChangeEvent event) {
1.24 - lastEvent = event;
1.25 - notified++;
1.26 - }
1.27 - public int getNotifiedAndReset() {
1.28 - try {
1.29 - return notified;
1.30 - } finally {
1.31 - notified = 0;
1.32 - }
1.33 - }
1.34 - public ChangeEvent getLastEvent() {
1.35 - return lastEvent;
1.36 - }
1.37 - }
1.38 ChangeSupport support = new ChangeSupport(this);
1.39 - Listener listener1 = new Listener(), listener2 = new Listener();
1.40 + MockChangeListener listener1 = new MockChangeListener(), listener2 = new MockChangeListener();
1.41
1.42 support.addChangeListener(null);
1.43 assertFalse(support.hasListeners());
1.44 @@ -73,34 +57,35 @@
1.45 assertTrue(listeners.contains(listener2));
1.46
1.47 support.fireChange();
1.48 - assertEquals(1, listener1.getNotifiedAndReset());
1.49 - assertEquals(1, listener2.getNotifiedAndReset());
1.50 - assertSame(this, listener1.getLastEvent().getSource());
1.51 + List<ChangeEvent> events = listener1.allEvents();
1.52 + assertEquals(1, events.size());
1.53 + listener2.assertEventCount(1);
1.54 + assertSame(this, events.iterator().next().getSource());
1.55
1.56 support.removeChangeListener(new ChangeListener() {
1.57 public void stateChanged(ChangeEvent e) {
1.58 }
1.59 });
1.60 support.fireChange();
1.61 - assertEquals(1, listener1.getNotifiedAndReset());
1.62 - assertEquals(1, listener2.getNotifiedAndReset());
1.63 + listener1.assertEventCount(1);
1.64 + listener2.assertEventCount(1);
1.65
1.66 support.removeChangeListener(listener1);
1.67 support.fireChange();
1.68 - assertEquals(0, listener1.getNotifiedAndReset());
1.69 - assertEquals(1, listener2.getNotifiedAndReset());
1.70 + listener1.assertEventCount(0);
1.71 + listener2.assertEventCount(1);
1.72
1.73 support.addChangeListener(listener2);
1.74 support.fireChange();
1.75 - assertEquals(2, listener2.getNotifiedAndReset());
1.76 + listener2.assertEventCount(2);
1.77
1.78 support.removeChangeListener(listener2);
1.79 support.fireChange();
1.80 - assertEquals(1, listener2.getNotifiedAndReset());
1.81 + listener2.assertEventCount(1);
1.82
1.83 support.removeChangeListener(listener2);
1.84 support.fireChange();
1.85 - assertEquals(0, listener2.getNotifiedAndReset());
1.86 + listener2.assertEventCount(0);
1.87 assertFalse(support.hasListeners());
1.88 }
1.89 }
2.1 --- a/openide.util/test/unit/src/org/openide/util/SharedClassObjectTest.java Mon Apr 16 20:46:47 2007 +0000
2.2 +++ b/openide.util/test/unit/src/org/openide/util/SharedClassObjectTest.java Tue Apr 17 23:22:59 2007 +0000
2.3 @@ -19,8 +19,6 @@
2.4
2.5 package org.openide.util;
2.6
2.7 -import java.beans.PropertyChangeEvent;
2.8 -import java.beans.PropertyChangeListener;
2.9 import java.io.ByteArrayInputStream;
2.10 import java.io.ByteArrayOutputStream;
2.11 import java.io.ObjectInputStream;
2.12 @@ -32,9 +30,9 @@
2.13 import java.lang.reflect.Method;
2.14 import java.net.URL;
2.15 import java.net.URLClassLoader;
2.16 -import junit.textui.TestRunner;
2.17 import org.netbeans.junit.NbTestCase;
2.18 import org.openide.util.SharedClassObject;
2.19 +import org.openide.util.test.MockPropertyChangeListener;
2.20
2.21 /** Test SharedClassObject singletons: esp. initialization semantics.
2.22 * @author Jesse Glick
2.23 @@ -46,7 +44,7 @@
2.24 }
2.25
2.26 public void testSimpleSCO() throws Exception {
2.27 - Class c = makeClazz("SimpleSCO");
2.28 + Class<? extends SharedClassObject> c = makeClazz("SimpleSCO");
2.29 assertTrue(c != SimpleSCO.class);
2.30 assertNull("No instance created yet", SharedClassObject.findObject(c, false));
2.31 SharedClassObject o = SharedClassObject.findObject(c, true);
2.32 @@ -58,7 +56,7 @@
2.33 assertEquals("has been initialized", 1, o.getClass().getField("initcount").getInt(o));
2.34 assertNull(o.getProperty("bar"));
2.35 assertEquals("has been initialized just once", 1, o.getClass().getField("initcount").getInt(null));
2.36 - Class c2 = makeClazz("SimpleSCO");
2.37 + Class<? extends SharedClassObject> c2 = makeClazz("SimpleSCO");
2.38 assertTrue("Call to makeClazz created a fresh class", c != c2);
2.39 SharedClassObject o2 = SharedClassObject.findObject(c2, true);
2.40 o2.getProperty("baz");
2.41 @@ -66,32 +64,32 @@
2.42 }
2.43
2.44 public void testClearSharedData() throws Exception {
2.45 - Class c = makeClazz("DontClearSharedDataSCO");
2.46 + Class<? extends SharedClassObject> c = makeClazz("DontClearSharedDataSCO");
2.47 SharedClassObject o = SharedClassObject.findObject(c, true);
2.48 - o.putProperty("inited", Boolean.TRUE);
2.49 - assertEquals("DCSD has been initialized", Boolean.TRUE, o.getProperty("inited"));
2.50 - Reference r = new WeakReference(o);
2.51 + o.putProperty("inited", true);
2.52 + assertEquals("DCSD has been initialized", true, o.getProperty("inited"));
2.53 + Reference<?> r = new WeakReference<Object>(o);
2.54 o = null;
2.55 assertGC("collected SCO instance", r);
2.56 assertNull("findObject(Class,false) gives nothing after running GC + finalization #1", SharedClassObject.findObject(c));
2.57 o = SharedClassObject.findObject(c, true);
2.58 - assertEquals("has still been initialized", Boolean.TRUE, o.getProperty("inited"));
2.59 + assertEquals("has still been initialized", true, o.getProperty("inited"));
2.60 c = makeClazz("ClearSharedDataSCO");
2.61 o = SharedClassObject.findObject(c, true);
2.62 - o.putProperty("inited", Boolean.TRUE);
2.63 - assertEquals("CSD has been initialized", Boolean.TRUE, o.getProperty("inited"));
2.64 - r = new WeakReference(o);
2.65 + o.putProperty("inited", true);
2.66 + assertEquals("CSD has been initialized", true, o.getProperty("inited"));
2.67 + r = new WeakReference<Object>(o);
2.68 o = null;
2.69 assertGC("collected SCO instance", r);
2.70 assertNull("findObject(Class,false) gives nothing after running GC + finalization #2", SharedClassObject.findObject(c));
2.71 o = SharedClassObject.findObject(c, true);
2.72 assertEquals("is no longer initialized", null, o.getProperty("inited"));
2.73 - o.putProperty("inited", Boolean.TRUE);
2.74 - assertEquals("has now been initialized again", Boolean.TRUE, o.getProperty("inited"));
2.75 + o.putProperty("inited", true);
2.76 + assertEquals("has now been initialized again", true, o.getProperty("inited"));
2.77 }
2.78
2.79 public void testIllegalState() throws Exception {
2.80 - Class c = makeClazz("InitErrorSCO");
2.81 + Class<? extends SharedClassObject> c = makeClazz("InitErrorSCO");
2.82 SharedClassObject o = SharedClassObject.findObject(c, true);
2.83 assertNotNull(o);
2.84 try {
2.85 @@ -109,43 +107,35 @@
2.86 }
2.87
2.88 public void testPropertyChanges() throws Exception {
2.89 - Class c = makeClazz("PropFirerSCO");
2.90 - Method putprop = c.getMethod("putprop", new Class[] {Object.class, Boolean.TYPE});
2.91 - Method getprop = c.getMethod("getprop", new Class[] {});
2.92 + Class<? extends SharedClassObject> c = makeClazz("PropFirerSCO");
2.93 + Method putprop = c.getMethod("putprop", Object.class, Boolean.TYPE);
2.94 + Method getprop = c.getMethod("getprop");
2.95 Field count = c.getField("addCount");
2.96 SharedClassObject o = SharedClassObject.findObject(c, true);
2.97 - assertNull(getprop.invoke(o, null));
2.98 + assertNull(getprop.invoke(o));
2.99 assertEquals(0, count.getInt(o));
2.100 - class Listener implements PropertyChangeListener {
2.101 - public int count = 0;
2.102 - public void propertyChange(PropertyChangeEvent ev) {
2.103 - if ("key".equals(ev.getPropertyName())) {
2.104 - count++;
2.105 - }
2.106 - }
2.107 - }
2.108 - Listener l = new Listener();
2.109 + MockPropertyChangeListener l = new MockPropertyChangeListener("key");
2.110 o.addPropertyChangeListener(l);
2.111 assertEquals(1, count.getInt(o));
2.112 - Listener l2 = new Listener();
2.113 + MockPropertyChangeListener l2 = new MockPropertyChangeListener("key");
2.114 o.addPropertyChangeListener(l2);
2.115 assertEquals(1, count.getInt(o));
2.116 o.removePropertyChangeListener(l2);
2.117 assertEquals(1, count.getInt(o));
2.118 - putprop.invoke(o, new Object[] {"something", Boolean.FALSE});
2.119 - assertEquals(0, l.count);
2.120 - assertEquals("something", getprop.invoke(o, null));
2.121 - putprop.invoke(o, new Object[] {"somethingelse", Boolean.TRUE});
2.122 - assertEquals(1, l.count);
2.123 - assertEquals("somethingelse", getprop.invoke(o, null));
2.124 + putprop.invoke(o, "something", false);
2.125 + l.assertEventCount(0);
2.126 + assertEquals("something", getprop.invoke(o));
2.127 + putprop.invoke(o, "somethingelse", true);
2.128 + l.assertEventCount(1);
2.129 + assertEquals("somethingelse", getprop.invoke(o));
2.130 // Check that setting the same val does not fire an additional change (cf. #37769):
2.131 - putprop.invoke(o, new Object[] {"somethingelse", Boolean.TRUE});
2.132 - assertEquals(1, l.count);
2.133 - assertEquals("somethingelse", getprop.invoke(o, null));
2.134 + putprop.invoke(o, "somethingelse", true);
2.135 + l.assertEventCount(0);
2.136 + assertEquals("somethingelse", getprop.invoke(o));
2.137 // Check equals() as well as ==:
2.138 - putprop.invoke(o, new Object[] {new String("somethingelse"), Boolean.TRUE});
2.139 - assertEquals(1, l.count);
2.140 - assertEquals("somethingelse", getprop.invoke(o, null));
2.141 + putprop.invoke(o, new String("somethingelse"), true);
2.142 + l.assertEventCount(0);
2.143 + assertEquals("somethingelse", getprop.invoke(o));
2.144 o.removePropertyChangeListener(l);
2.145 assertEquals(0, count.getInt(o));
2.146 o.addPropertyChangeListener(l);
2.147 @@ -155,7 +145,7 @@
2.148 }
2.149
2.150 public void testRecursiveInit() throws Exception {
2.151 - Class c = makeClazz("RecursiveInitSCO");
2.152 + Class<? extends SharedClassObject> c = makeClazz("RecursiveInitSCO");
2.153 SharedClassObject o = SharedClassObject.findObject(c, true);
2.154 assertEquals(0, c.getField("count").getInt(null));
2.155 o.getProperty("foo");
2.156 @@ -182,8 +172,8 @@
2.157 /** Create a fresh Class object from one of this test's inner classes.
2.158 * Produces a new classloader so the class is always fresh.
2.159 */
2.160 - private Class makeClazz(String name) throws Exception {
2.161 - return Class.forName("org.openide.util.SharedClassObjectTest$" + name, false, new MaskingURLClassLoader());
2.162 + private Class<? extends SharedClassObject> makeClazz(String name) throws Exception {
2.163 + return Class.forName("org.openide.util.SharedClassObjectTest$" + name, false, new MaskingURLClassLoader()).asSubclass(SharedClassObject.class);
2.164 }
2.165 private static final class MaskingURLClassLoader extends URLClassLoader {
2.166 public MaskingURLClassLoader() {
2.167 @@ -272,7 +262,7 @@
2.168 }
2.169
2.170 public static class RecursiveInitSCO extends SharedClassObject {
2.171 - public static final RecursiveInitSCO INSTANCE = (RecursiveInitSCO)SharedClassObject.findObject(RecursiveInitSCO.class, true);
2.172 + public static final RecursiveInitSCO INSTANCE = SharedClassObject.findObject(RecursiveInitSCO.class, true);
2.173 public static int count = 0;
2.174 protected void initialize() {
2.175 super.initialize();
3.1 --- a/openide.util/test/unit/src/org/openide/util/actions/ActionsInfraHid.java Mon Apr 16 20:46:47 2007 +0000
3.2 +++ b/openide.util/test/unit/src/org/openide/util/actions/ActionsInfraHid.java Tue Apr 17 23:22:59 2007 +0000
3.3 @@ -19,8 +19,6 @@
3.4
3.5 package org.openide.util.actions;
3.6
3.7 -import java.beans.PropertyChangeEvent;
3.8 -import java.beans.PropertyChangeListener;
3.9 import java.util.ArrayList;
3.10 import java.util.Collections;
3.11 import java.util.HashSet;
3.12 @@ -79,38 +77,6 @@
3.13 }
3.14 }
3.15
3.16 - /** Prop listener that will tell you if it gets a change.
3.17 - */
3.18 - public static final class WaitPCL implements PropertyChangeListener {
3.19 - /** whether a change has been received, and if so count */
3.20 - public int gotit = 0;
3.21 - /** optional property name to filter by (if null, accept any) */
3.22 - private final String prop;
3.23 - public WaitPCL(String p) {
3.24 - prop = p;
3.25 - }
3.26 - public synchronized void propertyChange(PropertyChangeEvent evt) {
3.27 - if (prop == null || prop.equals(evt.getPropertyName())) {
3.28 - gotit++;
3.29 - notifyAll();
3.30 - }
3.31 - }
3.32 - public boolean changed() {
3.33 - return changed(1500);
3.34 - }
3.35 - public synchronized boolean changed(int timeout) {
3.36 - if (gotit > 0) {
3.37 - return true;
3.38 - }
3.39 - try {
3.40 - wait(timeout);
3.41 - } catch (InterruptedException ie) {
3.42 - ie.printStackTrace();
3.43 - }
3.44 - return gotit > 0;
3.45 - }
3.46 - }
3.47 -
3.48 // Stolen from RequestProcessorTest.
3.49 public static void doGC() {
3.50 doGC(10);
4.1 --- a/openide.util/test/unit/src/org/openide/util/actions/BooleanStateActionTest.java Mon Apr 16 20:46:47 2007 +0000
4.2 +++ b/openide.util/test/unit/src/org/openide/util/actions/BooleanStateActionTest.java Tue Apr 17 23:22:59 2007 +0000
4.3 @@ -21,6 +21,7 @@
4.4
4.5 import org.netbeans.junit.NbTestCase;
4.6 import org.openide.util.HelpCtx;
4.7 +import org.openide.util.test.MockPropertyChangeListener;
4.8
4.9 /** Test that boolean actions are in fact toggled.
4.10 * @author Jesse Glick
4.11 @@ -38,16 +39,16 @@
4.12 BooleanStateAction a2 = (BooleanStateAction)SystemAction.get(SimpleBooleanAction2.class);
4.13 assertTrue(a1.getBooleanState());
4.14 assertFalse(a2.getBooleanState());
4.15 - ActionsInfraHid.WaitPCL l = new ActionsInfraHid.WaitPCL(BooleanStateAction.PROP_BOOLEAN_STATE);
4.16 + MockPropertyChangeListener l = new MockPropertyChangeListener();
4.17 a1.addPropertyChangeListener(l);
4.18 a1.actionPerformed(null);
4.19 - assertTrue(l.changed());
4.20 + l.expectEvent(BooleanStateAction.PROP_BOOLEAN_STATE, 1500);
4.21 assertFalse(a1.getBooleanState());
4.22 a1.removePropertyChangeListener(l);
4.23 - l.gotit = 0;
4.24 + l.reset();//l.gotit = 0;
4.25 a2.addPropertyChangeListener(l);
4.26 a2.actionPerformed(null);
4.27 - assertTrue(l.changed());
4.28 + l.expectEvent(BooleanStateAction.PROP_BOOLEAN_STATE, 1500);
4.29 assertTrue(a2.getBooleanState());
4.30 a2.removePropertyChangeListener(l);
4.31 }
5.1 --- a/openide.util/test/unit/src/org/openide/util/actions/CallbackSystemActionTest.java Mon Apr 16 20:46:47 2007 +0000
5.2 +++ b/openide.util/test/unit/src/org/openide/util/actions/CallbackSystemActionTest.java Tue Apr 17 23:22:59 2007 +0000
5.3 @@ -41,6 +41,7 @@
5.4 import org.openide.util.actions.CallbackSystemAction;
5.5 import org.openide.util.actions.SystemAction;
5.6 import org.openide.util.lookup.Lookups;
5.7 +import org.openide.util.test.MockPropertyChangeListener;
5.8
5.9 /** Test CallbackSystemAction: changing performer, focus tracking.
5.10 * @author Jesse Glick
5.11 @@ -447,13 +448,13 @@
5.12 Lookup context = Lookups.singleton(map);
5.13 clone = system.createContextAwareInstance(context);
5.14
5.15 - CntListener listener = new CntListener();
5.16 + MockPropertyChangeListener listener = new MockPropertyChangeListener();
5.17 clone.addPropertyChangeListener(listener);
5.18
5.19 assertTrue("Not enabled now", !clone.isEnabled());
5.20 action.setEnabled(true);
5.21 assertTrue("Clone is enabled because the action in ActionMap is", clone.isEnabled());
5.22 - listener.assertCnt("One change expected", 1);
5.23 + listener.assertEventCount(1);
5.24
5.25 system.setActionPerformer(action);
5.26 clone.actionPerformed(new ActionEvent(this, 0, ""));
5.27 @@ -463,7 +464,7 @@
5.28
5.29 action.setEnabled(false);
5.30 assertTrue("Clone is disabled because the action in ActionMap is", !clone.isEnabled());
5.31 - listener.assertCnt("Another change expected", 1);
5.32 + listener.assertEventCount(1);
5.33
5.34 clone.actionPerformed(new ActionEvent(this, 0, ""));
5.35 assertEquals("MyAction.actionPerformed invoked again", 2, action.actionPerformed);
5.36 @@ -471,18 +472,4 @@
5.37
5.38 }
5.39
5.40 - private static final class CntListener extends Object
5.41 - implements PropertyChangeListener {
5.42 - private int cnt;
5.43 -
5.44 - public void propertyChange(PropertyChangeEvent evt) {
5.45 - cnt++;
5.46 - }
5.47 -
5.48 - public void assertCnt(String msg, int count) {
5.49 - assertEquals(msg, count, this.cnt);
5.50 - this.cnt = 0;
5.51 - }
5.52 - } // end of CntListener
5.53 -
5.54 }
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/openide.util/test/unit/src/org/openide/util/test/MockChangeListener.java Tue Apr 17 23:22:59 2007 +0000
6.3 @@ -0,0 +1,163 @@
6.4 +/*
6.5 + * The contents of this file are subject to the terms of the Common Development
6.6 + * and Distribution License (the License). You may not use this file except in
6.7 + * compliance with the License.
6.8 + *
6.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
6.10 + * or http://www.netbeans.org/cddl.txt.
6.11 + *
6.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
6.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
6.14 + * If applicable, add the following below the CDDL Header, with the fields
6.15 + * enclosed by brackets [] replaced by your own identifying information:
6.16 + * "Portions Copyrighted [year] [name of copyright owner]"
6.17 + *
6.18 + * The Original Software is NetBeans. The Initial Developer of the Original
6.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
6.20 + * Microsystems, Inc. All Rights Reserved.
6.21 + */
6.22 +
6.23 +package org.openide.util.test;
6.24 +
6.25 +import java.util.ArrayList;
6.26 +import java.util.List;
6.27 +import javax.swing.event.ChangeEvent;
6.28 +import javax.swing.event.ChangeListener;
6.29 +import static junit.framework.Assert.*;
6.30 +import junit.framework.AssertionFailedError;
6.31 +
6.32 +/**
6.33 + * A scriptable change listener.
6.34 + */
6.35 +public class MockChangeListener implements ChangeListener {
6.36 +
6.37 + private final List<ChangeEvent> events = new ArrayList<ChangeEvent>();
6.38 + private String msg;
6.39 +
6.40 + /**
6.41 + * Makes a fresh listener.
6.42 + */
6.43 + public MockChangeListener() {}
6.44 +
6.45 + public synchronized void stateChanged(ChangeEvent ev) {
6.46 + events.add(ev);
6.47 + }
6.48 +
6.49 + /**
6.50 + * Specifies a failure message to use for the next assertion.
6.51 + * @return this object, for convenient chaining
6.52 + */
6.53 + public MockChangeListener msg(String msg) {
6.54 + this.msg = msg;
6.55 + return this;
6.56 + }
6.57 +
6.58 + private String compose(String msg) {
6.59 + return this.msg == null ? msg : msg + ": " + this.msg;
6.60 + }
6.61 +
6.62 + /**
6.63 + * Asserts that at least one change event has been fired.
6.64 + * After this call, the count is reset, so each call checks events since the last call.
6.65 + */
6.66 + public synchronized void assertEvent() throws AssertionFailedError {
6.67 + try {
6.68 + assertFalse(msg, events.isEmpty());
6.69 + } finally {
6.70 + reset();
6.71 + }
6.72 + }
6.73 +
6.74 + /**
6.75 + * Asserts that no change events have been fired.
6.76 + */
6.77 + public synchronized void assertNoEvents() throws AssertionFailedError {
6.78 + try {
6.79 + assertTrue(msg, events.isEmpty());
6.80 + } finally {
6.81 + reset();
6.82 + }
6.83 + }
6.84 +
6.85 + /**
6.86 + * Asserts that a certain number of change events has been fired.
6.87 + * After this call, the count is reset, so each call checks events since the last call.
6.88 + */
6.89 + public synchronized void assertEventCount(int expectedCount) throws AssertionFailedError {
6.90 + try {
6.91 + assertEquals(msg, expectedCount, events.size());
6.92 + } finally {
6.93 + reset();
6.94 + }
6.95 + }
6.96 +
6.97 + /**
6.98 + * Expects a single event to be fired.
6.99 + * After this call, the list of received events is reset, so each call checks events since the last call.
6.100 + * @param timeout a timeout in milliseconds (zero means no timeout)
6.101 + */
6.102 + public synchronized void expectEvent(long timeout) throws AssertionFailedError {
6.103 + try {
6.104 + if (events.isEmpty()) {
6.105 + try {
6.106 + wait(timeout);
6.107 + } catch (InterruptedException x) {
6.108 + fail(compose("Timed out waiting for event"));
6.109 + }
6.110 + }
6.111 + assertFalse(compose("Did not get event"), events.isEmpty());
6.112 + assertFalse(compose("Got too many events"), events.size() > 1);
6.113 + } finally {
6.114 + reset();
6.115 + }
6.116 + }
6.117 +
6.118 + /**
6.119 + * Expects no further events to be fired.
6.120 + * After this call, the list of received events is reset, so each call checks events since the last call.
6.121 + * @param timeout a timeout in milliseconds (will fail if an event is received within this time)
6.122 + */
6.123 + public synchronized void expectNoEvents(long timeout) throws AssertionFailedError {
6.124 + if (timeout == 0) {
6.125 + throw new IllegalArgumentException("Cannot use zero timeout");
6.126 + }
6.127 + try {
6.128 + if (!events.isEmpty()) {
6.129 + fail(compose("Already had an event"));
6.130 + }
6.131 + try {
6.132 + wait(timeout);
6.133 + } catch (InterruptedException x) {
6.134 + fail(compose("Interrupted"));
6.135 + }
6.136 + if (!events.isEmpty()) {
6.137 + fail(compose("Got an event"));
6.138 + }
6.139 + } finally {
6.140 + reset();
6.141 + }
6.142 + }
6.143 +
6.144 + /**
6.145 + * Gets a list of all received events, for special processing.
6.146 + * (For example, checking of source, ...)
6.147 + * After this call, the list of received events is reset, so each call checks events since the last call.
6.148 + */
6.149 + public synchronized List<ChangeEvent> allEvents() {
6.150 + try {
6.151 + return new ArrayList<ChangeEvent>(events);
6.152 + } finally {
6.153 + reset();
6.154 + }
6.155 + }
6.156 +
6.157 + /**
6.158 + * Simply resets the list of events without checking anything.
6.159 + * Also resets any failure message.
6.160 + */
6.161 + public synchronized void reset() {
6.162 + msg = null;
6.163 + events.clear();
6.164 + }
6.165 +
6.166 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/openide.util/test/unit/src/org/openide/util/test/MockChangeListenerTest.java Tue Apr 17 23:22:59 2007 +0000
7.3 @@ -0,0 +1,96 @@
7.4 +/*
7.5 + * The contents of this file are subject to the terms of the Common Development
7.6 + * and Distribution License (the License). You may not use this file except in
7.7 + * compliance with the License.
7.8 + *
7.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7.10 + * or http://www.netbeans.org/cddl.txt.
7.11 + *
7.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
7.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
7.14 + * If applicable, add the following below the CDDL Header, with the fields
7.15 + * enclosed by brackets [] replaced by your own identifying information:
7.16 + * "Portions Copyrighted [year] [name of copyright owner]"
7.17 + *
7.18 + * The Original Software is NetBeans. The Initial Developer of the Original
7.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
7.20 + * Microsystems, Inc. All Rights Reserved.
7.21 + */
7.22 +
7.23 +package org.openide.util.test;
7.24 +
7.25 +import junit.framework.AssertionFailedError;
7.26 +import junit.framework.TestCase;
7.27 +import org.openide.util.ChangeSupport;
7.28 +
7.29 +public class MockChangeListenerTest extends TestCase {
7.30 +
7.31 + public MockChangeListenerTest(String n) {
7.32 + super(n);
7.33 + }
7.34 +
7.35 + Object source;
7.36 + ChangeSupport cs;
7.37 + MockChangeListener l;
7.38 +
7.39 + @Override
7.40 + protected void setUp() throws Exception {
7.41 + super.setUp();
7.42 + source = new Object();
7.43 + cs = new ChangeSupport(source);
7.44 + l = new MockChangeListener();
7.45 + cs.addChangeListener(l);
7.46 + }
7.47 +
7.48 + // XXX test expect
7.49 +
7.50 + public void testBasicUsage() throws Exception {
7.51 + l.assertNoEvents();
7.52 + l.assertEventCount(0);
7.53 + try {
7.54 + l.assertEvent();
7.55 + assert false;
7.56 + } catch (AssertionFailedError e) {}
7.57 + try {
7.58 + l.assertEventCount(1);
7.59 + assert false;
7.60 + } catch (AssertionFailedError e) {}
7.61 + cs.fireChange();
7.62 + l.assertEvent();
7.63 + l.assertNoEvents();
7.64 + l.assertEventCount(0);
7.65 + cs.fireChange();
7.66 + cs.fireChange();
7.67 + l.assertEventCount(2);
7.68 + cs.fireChange();
7.69 + l.assertEvent();
7.70 + l.assertNoEvents();
7.71 + l.assertNoEvents();
7.72 + cs.fireChange();
7.73 + l.reset();
7.74 + l.assertNoEvents();
7.75 + cs.fireChange();
7.76 + cs.fireChange();
7.77 + assertEquals(2, l.allEvents().size());
7.78 + }
7.79 +
7.80 + public void testMessages() throws Exception {
7.81 + try {
7.82 + l.assertEvent();
7.83 + assert false;
7.84 + } catch (AssertionFailedError e) {}
7.85 + try {
7.86 + l.msg("stuff").assertEvent();
7.87 + assert false;
7.88 + } catch (AssertionFailedError e) {
7.89 + assertTrue(e.getMessage().contains("stuff"));
7.90 + }
7.91 + try {
7.92 + l.assertEvent();
7.93 + assert false;
7.94 + } catch (AssertionFailedError e) {
7.95 + assertFalse(String.valueOf(e.getMessage()).contains("stuff"));
7.96 + }
7.97 + }
7.98 +
7.99 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/openide.util/test/unit/src/org/openide/util/test/MockPropertyChangeListener.java Tue Apr 17 23:22:59 2007 +0000
8.3 @@ -0,0 +1,202 @@
8.4 +/*
8.5 + * The contents of this file are subject to the terms of the Common Development
8.6 + * and Distribution License (the License). You may not use this file except in
8.7 + * compliance with the License.
8.8 + *
8.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
8.10 + * or http://www.netbeans.org/cddl.txt.
8.11 + *
8.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
8.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
8.14 + * If applicable, add the following below the CDDL Header, with the fields
8.15 + * enclosed by brackets [] replaced by your own identifying information:
8.16 + * "Portions Copyrighted [year] [name of copyright owner]"
8.17 + *
8.18 + * The Original Software is NetBeans. The Initial Developer of the Original
8.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
8.20 + * Microsystems, Inc. All Rights Reserved.
8.21 + */
8.22 +
8.23 +package org.openide.util.test;
8.24 +
8.25 +import java.beans.PropertyChangeEvent;
8.26 +import java.beans.PropertyChangeListener;
8.27 +import java.util.ArrayList;
8.28 +import java.util.Arrays;
8.29 +import java.util.HashMap;
8.30 +import java.util.HashSet;
8.31 +import java.util.List;
8.32 +import java.util.Map;
8.33 +import java.util.Set;
8.34 +import java.util.TreeSet;
8.35 +import static junit.framework.Assert.*;
8.36 +import junit.framework.AssertionFailedError;
8.37 +
8.38 +/**
8.39 + * A scriptable property change listener.
8.40 + */
8.41 +public class MockPropertyChangeListener implements PropertyChangeListener {
8.42 +
8.43 + private final List<PropertyChangeEvent> events = new ArrayList<PropertyChangeEvent>();
8.44 + private final Set<String> whitelist;
8.45 + private final Set<String> blacklist = new HashSet<String>();
8.46 + private final Set<String> ignored = new HashSet<String>();
8.47 + private String msg;
8.48 +
8.49 + /**
8.50 + * Makes a fresh listener.
8.51 + * @param whiteListedPropertyNames an optional list of property names; if any others are received, an assertion will be thrown
8.52 + */
8.53 + public MockPropertyChangeListener(String... whitelistedPropertyNames) {
8.54 + whitelist = whitelistedPropertyNames.length > 0 ? new HashSet<String>(Arrays.asList(whitelistedPropertyNames)) : null;
8.55 + }
8.56 +
8.57 + /**
8.58 + * Marks certain property names as being definitely not expected.
8.59 + * If any are received henceforth, an assertion will be thrown.
8.60 + */
8.61 + public synchronized void blacklist(String... blacklistedPropertyNames) {
8.62 + assertNull("Meaningless to blacklist some properties while there is an active whitelist", whitelist);
8.63 + blacklist.addAll(Arrays.asList(blacklistedPropertyNames));
8.64 + }
8.65 +
8.66 + /**
8.67 + * Marks certain property names as being ignored.
8.68 + * If any are received henceforth, no action will be taken.
8.69 + */
8.70 + public synchronized void ignore(String... ignoredPropertyNames) {
8.71 + ignored.addAll(Arrays.asList(ignoredPropertyNames));
8.72 + }
8.73 +
8.74 + public synchronized void propertyChange(PropertyChangeEvent ev) {
8.75 + String propname = ev.getPropertyName();
8.76 + if (ignored.contains(propname)) {
8.77 + return;
8.78 + }
8.79 + assertTrue("Property name " + propname + " not expected", whitelist == null || whitelist.contains(propname));
8.80 + assertFalse("Property name " + propname + " not expected", blacklist.contains(propname));
8.81 + if (propname == null) {
8.82 + assertNull("null prop name -> null old value", ev.getOldValue());
8.83 + assertNull("null prop name -> null new value", ev.getNewValue());
8.84 + }
8.85 + events.add(ev);
8.86 + notifyAll();
8.87 + }
8.88 +
8.89 + /**
8.90 + * Specifies a failure message to use for the next assertion.
8.91 + * @return this object, for convenient chaining
8.92 + */
8.93 + public MockPropertyChangeListener msg(String msg) {
8.94 + this.msg = msg;
8.95 + return this;
8.96 + }
8.97 +
8.98 + private String compose(String msg) {
8.99 + return this.msg == null ? msg : msg + ": " + this.msg;
8.100 + }
8.101 +
8.102 + /**
8.103 + * Asserts that the set of property change event names fired matches an expected list.
8.104 + * (Order and multiplicity of events is not considered.)
8.105 + * After this call, the list of received events is reset, so each call checks events since the last call.
8.106 + */
8.107 + public synchronized void assertEvents(String... expectedPropertyNames) throws AssertionFailedError {
8.108 + try {
8.109 + Set<String> actualEvents = new TreeSet<String>();
8.110 + for (PropertyChangeEvent ev : events) {
8.111 + actualEvents.add(ev.getPropertyName());
8.112 + }
8.113 + assertEquals(msg, new TreeSet<String>(Arrays.asList(expectedPropertyNames)).toString(), actualEvents.toString());
8.114 + } finally {
8.115 + reset();
8.116 + }
8.117 + }
8.118 +
8.119 + /**
8.120 + * Asserts that a certain number of events have been received.
8.121 + * (Property name is not considered.)
8.122 + * After this call, the list of received events is reset, so each call checks events since the last call.
8.123 + */
8.124 + public synchronized void assertEventCount(int expectedCount) throws AssertionFailedError {
8.125 + try {
8.126 + assertEquals(msg, expectedCount, events.size());
8.127 + } finally {
8.128 + reset();
8.129 + }
8.130 + }
8.131 +
8.132 + /**
8.133 + * Asserts that some events were received with particular old and new values.
8.134 + * After this call, the list of received events is reset, so each call checks events since the last call.
8.135 + * @param expectedOldValues mapping from expected property names to old values (may be left null to skip this check)
8.136 + * @param expectedNewValues mapping from expected property names to new values
8.137 + */
8.138 + public synchronized void assertEventsAndValues(Map<String,?> expectedOldValues, Map<String,?> expectedNewValues) throws AssertionFailedError {
8.139 + try {
8.140 + if (expectedOldValues != null) {
8.141 + Map<String,Object> actualOldValues = new HashMap<String,Object>();
8.142 + for (PropertyChangeEvent ev : events) {
8.143 + actualOldValues.put(ev.getPropertyName(), ev.getOldValue());
8.144 + }
8.145 + assertEquals(msg, expectedOldValues, actualOldValues);
8.146 + }
8.147 + Map<String,Object> actualNewValues = new HashMap<String,Object>();
8.148 + for (PropertyChangeEvent ev : events) {
8.149 + actualNewValues.put(ev.getPropertyName(), ev.getNewValue());
8.150 + }
8.151 + assertEquals(msg, expectedNewValues, actualNewValues);
8.152 + } finally {
8.153 + reset();
8.154 + }
8.155 + }
8.156 +
8.157 + /**
8.158 + * Expects a single event to be fired.
8.159 + * After this call, the list of received events is reset, so each call checks events since the last call.
8.160 + * @param propertyName optional property name which is expected (or null for any)
8.161 + * @param timeout a timeout in milliseconds (zero means no timeout)
8.162 + */
8.163 + public synchronized void expectEvent(String expectedPropertyName, long timeout) throws AssertionFailedError {
8.164 + try {
8.165 + if (events.isEmpty()) {
8.166 + try {
8.167 + wait(timeout);
8.168 + } catch (InterruptedException x) {
8.169 + fail(compose("Timed out waiting for event"));
8.170 + }
8.171 + }
8.172 + assertFalse(compose("Did not get event"), events.isEmpty());
8.173 + assertFalse(compose("Got too many events"), events.size() > 1);
8.174 + PropertyChangeEvent received = events.iterator().next();
8.175 + if (expectedPropertyName != null) {
8.176 + assertEquals(msg, expectedPropertyName, received.getPropertyName());
8.177 + }
8.178 + } finally {
8.179 + reset();
8.180 + }
8.181 + }
8.182 +
8.183 + /**
8.184 + * Gets a list of all received events, for special processing.
8.185 + * (For example, checking of source, ...)
8.186 + * After this call, the list of received events is reset, so each call checks events since the last call.
8.187 + */
8.188 + public synchronized List<PropertyChangeEvent> allEvents() {
8.189 + try {
8.190 + return new ArrayList<PropertyChangeEvent>(events);
8.191 + } finally {
8.192 + reset();
8.193 + }
8.194 + }
8.195 +
8.196 + /**
8.197 + * Simply resets the list of events without checking anything.
8.198 + * Also resets any failure message.
8.199 + */
8.200 + public synchronized void reset() {
8.201 + msg = null;
8.202 + events.clear();
8.203 + }
8.204 +
8.205 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/openide.util/test/unit/src/org/openide/util/test/MockPropertyChangeListenerTest.java Tue Apr 17 23:22:59 2007 +0000
9.3 @@ -0,0 +1,144 @@
9.4 +/*
9.5 + * The contents of this file are subject to the terms of the Common Development
9.6 + * and Distribution License (the License). You may not use this file except in
9.7 + * compliance with the License.
9.8 + *
9.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9.10 + * or http://www.netbeans.org/cddl.txt.
9.11 + *
9.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
9.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
9.14 + * If applicable, add the following below the CDDL Header, with the fields
9.15 + * enclosed by brackets [] replaced by your own identifying information:
9.16 + * "Portions Copyrighted [year] [name of copyright owner]"
9.17 + *
9.18 + * The Original Software is NetBeans. The Initial Developer of the Original
9.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
9.20 + * Microsystems, Inc. All Rights Reserved.
9.21 + */
9.22 +
9.23 +package org.openide.util.test;
9.24 +
9.25 +import java.beans.PropertyChangeSupport;
9.26 +import java.util.Collections;
9.27 +import junit.framework.AssertionFailedError;
9.28 +import junit.framework.TestCase;
9.29 +
9.30 +public class MockPropertyChangeListenerTest extends TestCase {
9.31 +
9.32 + public MockPropertyChangeListenerTest(String n) {
9.33 + super(n);
9.34 + }
9.35 +
9.36 + Object source;
9.37 + PropertyChangeSupport pcs;
9.38 + MockPropertyChangeListener l;
9.39 +
9.40 + @Override
9.41 + protected void setUp() throws Exception {
9.42 + super.setUp();
9.43 + source = new Object();
9.44 + pcs = new PropertyChangeSupport(source);
9.45 + l = new MockPropertyChangeListener();
9.46 + pcs.addPropertyChangeListener(l);
9.47 + }
9.48 +
9.49 + // XXX test expect
9.50 +
9.51 + public void testBasicUsage() throws Exception {
9.52 + l.assertEvents();
9.53 + try {
9.54 + l.assertEvents("whatever");
9.55 + assert false;
9.56 + } catch (AssertionFailedError e) {}
9.57 + pcs.firePropertyChange("foo", null, null);
9.58 + l.assertEvents("foo");
9.59 + try {
9.60 + l.assertEvents("foo");
9.61 + assert false;
9.62 + } catch (AssertionFailedError e) {}
9.63 + l.assertEventCount(0);
9.64 + pcs.firePropertyChange("bar", null, null);
9.65 + pcs.firePropertyChange("baz", null, null);
9.66 + l.assertEventCount(2);
9.67 + try {
9.68 + l.assertEventCount(2);
9.69 + assert false;
9.70 + } catch (AssertionFailedError e) {}
9.71 + assertEquals(0, l.allEvents().size());
9.72 + pcs.firePropertyChange("bar", null, null);
9.73 + pcs.firePropertyChange("baz", null, null);
9.74 + assertEquals(2, l.allEvents().size());
9.75 + assertEquals(0, l.allEvents().size());
9.76 + pcs.firePropertyChange("foo", "old", "new");
9.77 + l.assertEventsAndValues(null, Collections.singletonMap("foo", "new"));
9.78 + pcs.firePropertyChange("foo", "old2", "new2");
9.79 + l.assertEventsAndValues(Collections.singletonMap("foo", "old2"), Collections.singletonMap("foo", "new2"));
9.80 + try {
9.81 + l.assertEventsAndValues(null, Collections.singletonMap("foo", "new2"));
9.82 + assert false;
9.83 + } catch (AssertionFailedError e) {}
9.84 + pcs.firePropertyChange("foo", null, null);
9.85 + l.reset();
9.86 + l.assertEvents();
9.87 + pcs.firePropertyChange("x", null, null);
9.88 + try {
9.89 + l.assertEvents();
9.90 + assert false;
9.91 + } catch (AssertionFailedError e) {}
9.92 + l.assertEvents();
9.93 + }
9.94 +
9.95 + public void testMessages() throws Exception {
9.96 + pcs.firePropertyChange("foo", null, null);
9.97 + try {
9.98 + l.assertEvents();
9.99 + assert false;
9.100 + } catch (AssertionFailedError e) {}
9.101 + pcs.firePropertyChange("foo", null, null);
9.102 + try {
9.103 + l.msg("stuff").assertEvents();
9.104 + assert false;
9.105 + } catch (AssertionFailedError e) {
9.106 + assertTrue(e.getMessage().contains("stuff"));
9.107 + }
9.108 + pcs.firePropertyChange("foo", null, null);
9.109 + try {
9.110 + l.assertEvents();
9.111 + assert false;
9.112 + } catch (AssertionFailedError e) {
9.113 + assertFalse(e.getMessage().contains("stuff"));
9.114 + }
9.115 + }
9.116 +
9.117 + public void testPropertyNameFiltering() throws Exception {
9.118 + l.ignore("irrelevant");
9.119 + l.blacklist("bad", "worse");
9.120 + pcs.firePropertyChange("relevant", null, null);
9.121 + l.assertEvents("relevant");
9.122 + pcs.firePropertyChange("irrelevant", null, null);
9.123 + l.assertEvents();
9.124 + try {
9.125 + pcs.firePropertyChange("bad", null, null);
9.126 + assert false;
9.127 + } catch (AssertionFailedError e) {}
9.128 + try {
9.129 + pcs.firePropertyChange("worse", null, null);
9.130 + assert false;
9.131 + } catch (AssertionFailedError e) {}
9.132 + pcs.removePropertyChangeListener(l);
9.133 + l = new MockPropertyChangeListener("expected1", "expected2");
9.134 + pcs.addPropertyChangeListener(l);
9.135 + l.ignore("irrelevant");
9.136 + pcs.firePropertyChange("expected1", null, null);
9.137 + pcs.firePropertyChange("expected2", null, null);
9.138 + l.assertEvents("expected1", "expected2");
9.139 + pcs.firePropertyChange("irrelevant", null, null);
9.140 + l.assertEvents();
9.141 + try {
9.142 + pcs.firePropertyChange("other", null, null);
9.143 + assert false;
9.144 + } catch (AssertionFailedError e) {}
9.145 + }
9.146 +
9.147 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/openide.util/test/unit/src/org/openide/util/test/package-info.java Tue Apr 17 23:22:59 2007 +0000
10.3 @@ -0,0 +1,23 @@
10.4 +/*
10.5 + * The contents of this file are subject to the terms of the Common Development
10.6 + * and Distribution License (the License). You may not use this file except in
10.7 + * compliance with the License.
10.8 + *
10.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
10.10 + * or http://www.netbeans.org/cddl.txt.
10.11 + *
10.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
10.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
10.14 + * If applicable, add the following below the CDDL Header, with the fields
10.15 + * enclosed by brackets [] replaced by your own identifying information:
10.16 + * "Portions Copyrighted [year] [name of copyright owner]"
10.17 + *
10.18 + * The Original Software is NetBeans. The Initial Developer of the Original
10.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
10.20 + * Microsystems, Inc. All Rights Reserved.
10.21 + */
10.22 +
10.23 +/**
10.24 + * General utility classes useful for writing unit tests in various modules.
10.25 + */
10.26 +package org.openide.util.test;