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