Simplifying some more tests with MockServices.
1.1 --- a/openide.util/test/unit/src/org/openide/util/UtilitiesTest.java Sun May 07 09:51:19 2006 +0000
1.2 +++ b/openide.util/test/unit/src/org/openide/util/UtilitiesTest.java Fri May 12 23:08:01 2006 +0000
1.3 @@ -29,20 +29,16 @@
1.4 import javax.swing.JPopupMenu;
1.5 import javax.swing.KeyStroke;
1.6 import junit.framework.TestCase;
1.7 +import org.netbeans.junit.MockServices;
1.8 import org.netbeans.modules.openide.util.AWTBridge;
1.9 import org.openide.util.actions.Presenter;
1.10 import org.openide.util.lookup.Lookups;
1.11 -import org.openide.util.lookup.ProxyLookup;
1.12
1.13 /**
1.14 * @author Jiri Rechtacek et al.
1.15 */
1.16 public class UtilitiesTest extends TestCase {
1.17
1.18 - static {
1.19 - System.setProperty(Lookup.class.getName(), Lkp.class.getName());
1.20 - }
1.21 -
1.22 public UtilitiesTest (String testName) {
1.23 super (testName);
1.24 }
1.25 @@ -133,6 +129,7 @@
1.26 }
1.27
1.28 public void testActionsToPopupWithLookup() throws Exception {
1.29 + MockServices.setServices(AwtBridgeImpl.class);
1.30 final List<String> commands = new ArrayList<String>();
1.31 class BasicAction extends AbstractAction {
1.32 public BasicAction(String name) {
1.33 @@ -412,16 +409,7 @@
1.34 }
1.35 */
1.36
1.37 - public static final class Lkp extends ProxyLookup {
1.38 - public Lkp() {
1.39 - super(new Lookup[] {
1.40 - Lookups.fixed(new Object[] {new AwtBridgeImpl()}),
1.41 - Lookups.metaInfServices(Lkp.class.getClassLoader()),
1.42 - });
1.43 - }
1.44 - }
1.45 -
1.46 - private static final class AwtBridgeImpl extends AWTBridge {
1.47 + public static final class AwtBridgeImpl extends AWTBridge {
1.48 public JPopupMenu createEmptyPopup() {
1.49 return new JPopupMenu();
1.50 }
2.1 --- a/openide.util/test/unit/src/org/openide/util/WeakListenersTest.java Sun May 07 09:51:19 2006 +0000
2.2 +++ b/openide.util/test/unit/src/org/openide/util/WeakListenersTest.java Fri May 12 23:08:01 2006 +0000
2.3 @@ -22,15 +22,12 @@
2.4 import java.util.logging.Level;
2.5 import javax.swing.event.ChangeEvent;
2.6 import javax.swing.event.ChangeListener;
2.7 +import org.netbeans.junit.MockServices;
2.8 import org.netbeans.junit.NbTestCase;
2.9 import org.openide.ErrorManager;
2.10 -import org.openide.util.Utilities;
2.11 -import org.openide.util.WeakListeners;
2.12
2.13 public class WeakListenersTest extends NbTestCase {
2.14 - static {
2.15 - System.setProperty("org.openide.util.Lookup", "org.openide.util.WeakListenersTest$Lkp");
2.16 - }
2.17 +
2.18 private static Thread activeQueueThread;
2.19
2.20 private ErrorManager log;
2.21 @@ -48,10 +45,11 @@
2.22 }
2.23
2.24 protected void setUp () throws Exception {
2.25 + MockServices.setServices(ErrManager.class);
2.26 log = ErrorManager.getDefault().getInstance("TEST-" + getName());
2.27
2.28 if (activeQueueThread == null) {
2.29 - class WR extends WeakReference implements Runnable {
2.30 + class WR extends WeakReference<Object> implements Runnable {
2.31 public WR (Object o) {
2.32 super (o, Utilities.activeReferenceQueue ());
2.33 }
2.34 @@ -182,7 +180,7 @@
2.35 log.log("setText changed to ahoj");
2.36 assertEquals ("Listener called once", 1, l.cnt);
2.37
2.38 - WeakReference ref = new WeakReference (l);
2.39 + Reference<?> ref = new WeakReference<Object>(l);
2.40 log.log("Clearing listener");
2.41 l = null;
2.42
2.43 @@ -225,7 +223,7 @@
2.44 assertEquals ("Unregister called just once", 1, button.cnt);
2.45
2.46 // and because it is not here, it can be GCed
2.47 - WeakReference weakRef = new WeakReference (weakL);
2.48 + Reference<?> weakRef = new WeakReference<Object>(weakL);
2.49 weakL = null;
2.50 log.log("Doing assertGC at the end");
2.51 assertGC ("Weak listener can go away as well", weakRef);
2.52 @@ -238,7 +236,7 @@
2.53
2.54 b.addPropertyChangeListener (WeakListeners.propertyChange (l, b));
2.55
2.56 - WeakReference ref = new WeakReference (b);
2.57 + Reference<?> ref = new WeakReference<Object>(b);
2.58 b = null;
2.59
2.60 assertGC ("Source can be GC", ref);
2.61 @@ -257,7 +255,7 @@
2.62 c.addNamingListener("", javax.naming.event.EventContext.OBJECT_SCOPE, weakL);
2.63 assertEquals ("Weak listener is there", weakL, c.listener);
2.64
2.65 - WeakReference ref = new WeakReference (l);
2.66 + Reference<?> ref = new WeakReference<Object>(l);
2.67 l = null;
2.68
2.69 synchronized (c) {
2.70 @@ -326,7 +324,7 @@
2.71 PropChBean bean = new PropChBean();
2.72 Listener listener = new Listener();
2.73 PCL weakL = (PCL) WeakListeners.create(PCL.class, listener, bean);
2.74 - WeakReference ref = new WeakReference(listener);
2.75 + Reference<?> ref = new WeakReference<Object>(listener);
2.76
2.77 bean.addPCL(weakL);
2.78
2.79 @@ -337,7 +335,7 @@
2.80 listener = null;
2.81 assertGC("Listener wasn't GCed", ref);
2.82
2.83 - ref = new WeakReference(weakL);
2.84 + ref = new WeakReference<Object>(weakL);
2.85 weakL = null;
2.86 assertGC("WeakListener wasn't GCed", ref);
2.87
2.88 @@ -351,13 +349,13 @@
2.89 ChangeListener l = new ChangeListener() {public void stateChanged(ChangeEvent e) {}};
2.90 Singleton.addChangeListener(WeakListeners.change(l, Singleton.class));
2.91 assertEquals(1, Singleton.listeners.size());
2.92 - Reference r = new WeakReference(l);
2.93 + Reference<?> r = new WeakReference<Object>(l);
2.94 l = null;
2.95 assertGC("could collect listener", r);
2.96 assertEquals("called remove method", 0, Singleton.listeners.size());
2.97 }
2.98 public static class Singleton {
2.99 - public static List listeners = new ArrayList();
2.100 + public static List<ChangeListener> listeners = new ArrayList<ChangeListener>();
2.101 public static void addChangeListener(ChangeListener l) {
2.102 listeners.add(l);
2.103 }
2.104 @@ -435,41 +433,6 @@
2.105 private static interface PCL extends PropertyChangeListener {
2.106 } // End of PrivatePropL class
2.107
2.108 -
2.109 -
2.110 -
2.111 -
2.112 -
2.113 -
2.114 -
2.115 -
2.116 - //
2.117 - // Our fake lookup
2.118 - //
2.119 - public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
2.120 - private ErrManager err = new ErrManager ();
2.121 - private org.openide.util.lookup.InstanceContent ic;
2.122 -
2.123 - public Lkp () {
2.124 - this (new org.openide.util.lookup.InstanceContent ());
2.125 - }
2.126 -
2.127 - private Lkp (org.openide.util.lookup.InstanceContent ic) {
2.128 - super (ic);
2.129 - ic.add (err);
2.130 - this.ic = ic;
2.131 - }
2.132 -
2.133 - public static void turn (boolean on) {
2.134 - Lkp lkp = (Lkp)org.openide.util.Lookup.getDefault ();
2.135 - if (on) {
2.136 - lkp.ic.add (lkp.err);
2.137 - } else {
2.138 - lkp.ic.remove (lkp.err);
2.139 - }
2.140 - }
2.141 - }
2.142 -
2.143 //
2.144 // Manager to delegate to
2.145 //