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
     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 
    18         // java.util.ServiceLoader
    19         // either in @BeforeClass or @Before register 
    20         // classes of the mock instances that you want your code 
    21         // to find
    22         MockServices.setServices(ImplOne.class, ImplTwo.class);
    23     }
    24 
    25     @Test
    26     public void singleSlot() {
    27         InjectionSlot result = InjectionViaServiceLoader.singleSlot();
    28         assertNotNull("Some result found", result);
    29         assertEquals(
    30             "The first is ImplOne", ImplOne.class, result.getClass()
    31         );
    32     }
    33 
    34     @Test
    35     public void multiSlot() {
    36         Iterator<? extends InjectionSlot> it = 
    37             InjectionViaServiceLoader.multiSlot().iterator();
    38         assertTrue("Has at least one", it.hasNext());
    39         assertEquals(
    40             "The first is ImplOne", ImplOne.class, it.next().getClass()
    41         );
    42         assertTrue("Has two", it.hasNext());
    43         assertEquals(
    44             "The second is ImplTwo", ImplTwo.class, it.next().getClass()
    45         );
    46         assertFalse("No other instance registered", it.hasNext());
    47     }
    48 
    49     public static final class ImplOne extends InjectionSlot {
    50     }
    51     public static final class ImplTwo extends InjectionSlot {
    52     }
    53 }
    54 // END: lookup.mockservices.serviceloader