samples/componentinjection/test/org/apidesign/component/InjectionSlotChangesTest.java
1 package org.apidesign.component;
3 import java.util.Iterator;
4 import org.junit.BeforeClass;
6 import org.netbeans.junit.MockServices;
7 import org.openide.util.Lookup;
8 import static org.junit.Assert.*;
9 import org.openide.util.Lookup.Result;
10 import org.openide.util.LookupEvent;
11 import org.openide.util.LookupListener;
13 public class InjectionSlotChangesTest {
14 public InjectionSlotChangesTest() {
18 public static void setUpClass() throws Exception {
19 // this is a support for "dynamic" registration of any class
20 // into the global pool of Lookup.getDefault() and java.util.ServiceLoader
21 // either in @BeforeClass or @Before register
22 // classes of the mock instances that you want your code
24 MockServices.setServices(ImplOne.class, ImplTwo.class);
28 public void multiSlot() {
29 Iterator<? extends InjectionSlot> it = InjectionSlot.multiSlot().iterator();
30 assertTrue("Has at least one", it.hasNext());
31 assertEquals("The first is ImplOne", ImplOne.class, it.next().getClass());
32 assertTrue("Has two", it.hasNext());
33 assertEquals("The second is ImplTwo", ImplTwo.class, it.next().getClass());
34 assertFalse("No other instance registered", it.hasNext());
36 // BEGIN: lookup.listener
37 class Listener implements LookupListener {
39 public void resultChanged(LookupEvent arg0) {
43 Listener listener = new Listener();
44 Lookup.Result<InjectionSlot> res =
45 Lookup.getDefault().lookupResult(InjectionSlot.class);
46 res.addLookupListener(listener);
47 assertEquals("Two services now", 2, res.allInstances().size());
49 MockServices.setServices(ImplTwo.class);
51 assertEquals("One service only", 1, res.allInstances().size());
52 assertEquals("One change in listener", 1, listener.cnt);
54 "The second is ImplTwo",
56 res.allInstances().iterator().next().getClass()
58 // END: lookup.listener
61 public static final class ImplOne extends InjectionSlot {
63 public static final class ImplTwo extends InjectionSlot {