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
     1 package org.apidesign.component;
     2 
     3 import java.util.Iterator;
     4 import org.junit.BeforeClass;
     5 import org.junit.Test;
     6 import org.netbeans.junit.MockServices;
     7 import static org.junit.Assert.*;
     8 
     9 // BEGIN: lookup.mockservices.serviceloader
    10 public class InjectionViaServiceLoaderTest {
    11     public InjectionViaServiceLoaderTest() {
    12     }
    13 
    14     @BeforeClass
    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 java.util.ServiceLoader
    18         // either in @BeforeClass or @Before register 
    19         // classes of the mock instances that you want your code 
    20         // to find
    21         MockServices.setServices(ImplOne.class, ImplTwo.class);
    22     }
    23 
    24     @Test
    25     public void singleSlot() {
    26         InjectionSlot result = InjectionViaServiceLoader.singleSlot();
    27         assertNotNull("Some result found", result);
    28         assertEquals("The first is ImplOne", ImplOne.class, result.getClass());
    29     }
    30 
    31     @Test
    32     public void multiSlot() {
    33         Iterator<? extends InjectionSlot> it = InjectionViaServiceLoader.multiSlot().iterator();
    34         assertTrue("Has at least one", it.hasNext());
    35         assertEquals("The first is ImplOne", ImplOne.class, it.next().getClass());
    36         assertTrue("Has two", it.hasNext());
    37         assertEquals("The second is ImplTwo", ImplTwo.class, it.next().getClass());
    38         assertFalse("No other instance registered", it.hasNext());
    39     }
    40 
    41     public static final class ImplOne extends InjectionSlot {
    42     }
    43     public static final class ImplTwo extends InjectionSlot {
    44     }
    45 }
    46 // END: lookup.mockservices.serviceloader