samples/componentinjection/test/org/apidesign/component/InjectionViaServiceLoaderTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 84 33aa60b18830
child 153 b5cbb797ec0a
permissions -rw-r--r--
Truncating all examples to 80 characters per line
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@132
    17
        // into the global pool of Lookup.getDefault() and 
jtulach@132
    18
        // java.util.ServiceLoader
jtulach@83
    19
        // either in @BeforeClass or @Before register 
jtulach@83
    20
        // classes of the mock instances that you want your code 
jtulach@83
    21
        // to find
jtulach@83
    22
        MockServices.setServices(ImplOne.class, ImplTwo.class);
jtulach@83
    23
    }
jtulach@83
    24
jtulach@83
    25
    @Test
jtulach@83
    26
    public void singleSlot() {
jtulach@83
    27
        InjectionSlot result = InjectionViaServiceLoader.singleSlot();
jtulach@83
    28
        assertNotNull("Some result found", result);
jtulach@132
    29
        assertEquals(
jtulach@132
    30
            "The first is ImplOne", ImplOne.class, result.getClass()
jtulach@132
    31
        );
jtulach@83
    32
    }
jtulach@83
    33
jtulach@83
    34
    @Test
jtulach@83
    35
    public void multiSlot() {
jtulach@132
    36
        Iterator<? extends InjectionSlot> it = 
jtulach@132
    37
            InjectionViaServiceLoader.multiSlot().iterator();
jtulach@83
    38
        assertTrue("Has at least one", it.hasNext());
jtulach@132
    39
        assertEquals(
jtulach@132
    40
            "The first is ImplOne", ImplOne.class, it.next().getClass()
jtulach@132
    41
        );
jtulach@83
    42
        assertTrue("Has two", it.hasNext());
jtulach@132
    43
        assertEquals(
jtulach@132
    44
            "The second is ImplTwo", ImplTwo.class, it.next().getClass()
jtulach@132
    45
        );
jtulach@83
    46
        assertFalse("No other instance registered", it.hasNext());
jtulach@83
    47
    }
jtulach@83
    48
jtulach@83
    49
    public static final class ImplOne extends InjectionSlot {
jtulach@83
    50
    }
jtulach@83
    51
    public static final class ImplTwo extends InjectionSlot {
jtulach@83
    52
    }
jtulach@83
    53
}
jtulach@84
    54
// END: lookup.mockservices.serviceloader