jtulach@84: package org.apidesign.component; jtulach@84: jtulach@84: import java.util.Iterator; jtulach@84: import org.junit.BeforeClass; jtulach@84: import org.junit.Test; jtulach@84: import org.netbeans.junit.MockServices; jtulach@84: import org.openide.util.Lookup; jtulach@84: import static org.junit.Assert.*; jtulach@84: import org.openide.util.Lookup.Result; jtulach@84: import org.openide.util.LookupEvent; jtulach@84: import org.openide.util.LookupListener; jtulach@84: jtulach@84: public class InjectionSlotChangesTest { jtulach@84: public InjectionSlotChangesTest() { jtulach@84: } jtulach@84: jtulach@84: @BeforeClass jtulach@84: public static void setUpClass() throws Exception { jtulach@84: // this is a support for "dynamic" registration of any class jtulach@84: // into the global pool of Lookup.getDefault() and java.util.ServiceLoader jtulach@84: // either in @BeforeClass or @Before register jtulach@84: // classes of the mock instances that you want your code jtulach@84: // to find jtulach@84: MockServices.setServices(ImplOne.class, ImplTwo.class); jtulach@84: } jtulach@84: jtulach@84: @Test jtulach@84: public void multiSlot() { jtulach@84: Iterator it = InjectionSlot.multiSlot().iterator(); jtulach@84: assertTrue("Has at least one", it.hasNext()); jtulach@84: assertEquals("The first is ImplOne", ImplOne.class, it.next().getClass()); jtulach@84: assertTrue("Has two", it.hasNext()); jtulach@84: assertEquals("The second is ImplTwo", ImplTwo.class, it.next().getClass()); jtulach@84: assertFalse("No other instance registered", it.hasNext()); jtulach@84: jtulach@84: // BEGIN: lookup.listener jtulach@84: class Listener implements LookupListener { jtulach@84: int cnt; jtulach@84: public void resultChanged(LookupEvent arg0) { jtulach@84: cnt++; jtulach@84: } jtulach@84: } jtulach@84: Listener listener = new Listener(); jtulach@132: Lookup.Result res = jtulach@132: Lookup.getDefault().lookupResult(InjectionSlot.class); jtulach@84: res.addLookupListener(listener); jtulach@84: assertEquals("Two services now", 2, res.allInstances().size()); jtulach@84: jtulach@84: MockServices.setServices(ImplTwo.class); jtulach@84: jtulach@84: assertEquals("One service only", 1, res.allInstances().size()); jtulach@84: assertEquals("One change in listener", 1, listener.cnt); jtulach@132: assertEquals( jtulach@132: "The second is ImplTwo", jtulach@132: ImplTwo.class, jtulach@132: res.allInstances().iterator().next().getClass() jtulach@132: ); jtulach@84: // END: lookup.listener jtulach@84: } jtulach@84: jtulach@84: public static final class ImplOne extends InjectionSlot { jtulach@84: } jtulach@84: public static final class ImplTwo extends InjectionSlot { jtulach@84: } jtulach@84: }