samples/componentinjection/test/org/apidesign/component/InjectionSlotTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 85 935c169667a2
child 153 b5cbb797ec0a
permissions -rw-r--r--
Truncating all examples to 80 characters per line
jtulach@82
     1
package org.apidesign.component;
jtulach@82
     2
jtulach@82
     3
import java.util.Iterator;
jtulach@82
     4
import org.junit.BeforeClass;
jtulach@82
     5
import org.junit.Test;
jtulach@82
     6
import org.netbeans.junit.MockServices;
jtulach@82
     7
import static org.junit.Assert.*;
jtulach@82
     8
jtulach@82
     9
// BEGIN: lookup.mockservices
jtulach@82
    10
public class InjectionSlotTest {
jtulach@82
    11
    public InjectionSlotTest() {
jtulach@82
    12
    }
jtulach@82
    13
jtulach@82
    14
    @BeforeClass
jtulach@82
    15
    public static void setUpClass() throws Exception {
jtulach@82
    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@82
    19
        // either in @BeforeClass or @Before register 
jtulach@82
    20
        // classes of the mock instances that you want your code 
jtulach@82
    21
        // to find
jtulach@82
    22
        MockServices.setServices(ImplOne.class, ImplTwo.class);
jtulach@82
    23
    }
jtulach@82
    24
jtulach@82
    25
    @Test
jtulach@82
    26
    public void singleSlot() {
jtulach@82
    27
        InjectionSlot result = InjectionSlot.singleSlot();
jtulach@82
    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@82
    32
    }
jtulach@82
    33
jtulach@82
    34
    @Test
jtulach@82
    35
    public void multiSlot() {
jtulach@132
    36
        Iterator<? extends InjectionSlot> it = 
jtulach@132
    37
            InjectionSlot.multiSlot().iterator();
jtulach@82
    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@82
    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@82
    46
        assertFalse("No other instance registered", it.hasNext());
jtulach@82
    47
    }
jtulach@82
    48
jtulach@82
    49
    public static final class ImplOne extends InjectionSlot {
jtulach@82
    50
    }
jtulach@82
    51
    public static final class ImplTwo extends InjectionSlot {
jtulach@82
    52
    }
jtulach@82
    53
}
jtulach@85
    54
// END: lookup.mockservices