samples/componentinjection/test/org/apidesign/component/InjectionViaServiceLoaderTest.java
1 package org.apidesign.component;
3 import java.util.Iterator;
4 import org.junit.BeforeClass;
6 import org.netbeans.junit.MockServices;
7 import static org.junit.Assert.*;
9 // BEGIN: lookup.mockservices.serviceloader
10 public class InjectionViaServiceLoaderTest {
11 public InjectionViaServiceLoaderTest() {
15 public static void setUpClass() throws Exception {
16 // this is a support for "dynamic" registration of any class
17 // into the global pool of Lookup.getDefault() and
18 // java.util.ServiceLoader
19 // either in @BeforeClass or @Before register
20 // classes of the mock instances that you want your code
22 MockServices.setServices(ImplOne.class, ImplTwo.class);
26 public void singleSlot() {
27 InjectionSlot result = InjectionViaServiceLoader.singleSlot();
28 assertNotNull("Some result found", result);
30 "The first is ImplOne", ImplOne.class, result.getClass()
35 public void multiSlot() {
36 Iterator<? extends InjectionSlot> it =
37 InjectionViaServiceLoader.multiSlot().iterator();
38 assertTrue("Has at least one", it.hasNext());
40 "The first is ImplOne", ImplOne.class, it.next().getClass()
42 assertTrue("Has two", it.hasNext());
44 "The second is ImplTwo", ImplTwo.class, it.next().getClass()
46 assertFalse("No other instance registered", it.hasNext());
49 public static final class ImplOne extends InjectionSlot {
51 public static final class ImplTwo extends InjectionSlot {
54 // END: lookup.mockservices.serviceloader