samples/componentinjection/test/org/apidesign/component/InjectionViaServiceLoaderTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:08 +0200
changeset 153 b5cbb797ec0a
parent 132 3bc4c54f4bcc
child 154 0fd5e9c500b9
permissions -rw-r--r--
up to line 2000
jtulach@83
     1
package org.apidesign.component;
jtulach@83
     2
jtulach@83
     3
import java.util.Iterator;
jtulach@83
     4
import org.junit.BeforeClass;
jtulach@83
     5
import org.junit.Test;
jtulach@83
     6
import org.netbeans.junit.MockServices;
jtulach@83
     7
import static org.junit.Assert.*;
jtulach@83
     8
jtulach@83
     9
// BEGIN: lookup.mockservices.serviceloader
jtulach@83
    10
public class InjectionViaServiceLoaderTest {
jtulach@83
    11
    public InjectionViaServiceLoaderTest() {
jtulach@83
    12
    }
jtulach@83
    13
jtulach@83
    14
    @BeforeClass
jtulach@83
    15
    public static void setUpClass() throws Exception {
jtulach@83
    16
        // this is a support for "dynamic" registration of any class
jtulach@153
    17
        // into the global pool of Lookup.getDefault() and java.util.ServiceLoader
jtulach@83
    18
        // either in @BeforeClass or @Before register 
jtulach@83
    19
        // classes of the mock instances that you want your code 
jtulach@83
    20
        // to find
jtulach@83
    21
        MockServices.setServices(ImplOne.class, ImplTwo.class);
jtulach@83
    22
    }
jtulach@83
    23
jtulach@83
    24
    @Test
jtulach@83
    25
    public void singleSlot() {
jtulach@83
    26
        InjectionSlot result = InjectionViaServiceLoader.singleSlot();
jtulach@83
    27
        assertNotNull("Some result found", result);
jtulach@153
    28
        assertEquals("The first is ImplOne", ImplOne.class, result.getClass());
jtulach@83
    29
    }
jtulach@83
    30
jtulach@83
    31
    @Test
jtulach@83
    32
    public void multiSlot() {
jtulach@153
    33
        Iterator<? extends InjectionSlot> it = InjectionViaServiceLoader.multiSlot().iterator();
jtulach@83
    34
        assertTrue("Has at least one", it.hasNext());
jtulach@153
    35
        assertEquals("The first is ImplOne", ImplOne.class, it.next().getClass());
jtulach@83
    36
        assertTrue("Has two", it.hasNext());
jtulach@153
    37
        assertEquals("The second is ImplTwo", ImplTwo.class, it.next().getClass());
jtulach@83
    38
        assertFalse("No other instance registered", it.hasNext());
jtulach@83
    39
    }
jtulach@83
    40
jtulach@83
    41
    public static final class ImplOne extends InjectionSlot {
jtulach@83
    42
    }
jtulach@83
    43
    public static final class ImplTwo extends InjectionSlot {
jtulach@83
    44
    }
jtulach@83
    45
}
jtulach@84
    46
// END: lookup.mockservices.serviceloader