samples/componentinjection/test/org/apidesign/component/InjectionSlotChangesTest.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 org.openide.util.Lookup;
     8 import static org.junit.Assert.*;
     9 import org.openide.util.Lookup.Result;
    10 import org.openide.util.LookupEvent;
    11 import org.openide.util.LookupListener;
    12 
    13 public class InjectionSlotChangesTest {
    14     public InjectionSlotChangesTest() {
    15     }
    16 
    17     @BeforeClass
    18     public static void setUpClass() throws Exception {
    19         // this is a support for "dynamic" registration of any class
    20         // into the global pool of Lookup.getDefault() and java.util.ServiceLoader
    21         // either in @BeforeClass or @Before register 
    22         // classes of the mock instances that you want your code 
    23         // to find
    24         MockServices.setServices(ImplOne.class, ImplTwo.class);
    25     }
    26 
    27     @Test
    28     public void multiSlot() {
    29         Iterator<? extends InjectionSlot> it = InjectionSlot.multiSlot().iterator();
    30         assertTrue("Has at least one", it.hasNext());
    31         assertEquals("The first is ImplOne", ImplOne.class, it.next().getClass());
    32         assertTrue("Has two", it.hasNext());
    33         assertEquals("The second is ImplTwo", ImplTwo.class, it.next().getClass());
    34         assertFalse("No other instance registered", it.hasNext());
    35 
    36         // BEGIN: lookup.listener
    37         class Listener implements LookupListener {
    38             int cnt;
    39             public void resultChanged(LookupEvent arg0) {
    40                 cnt++;
    41             }
    42         }
    43         Listener listener = new Listener();
    44         Lookup.Result<InjectionSlot> res = Lookup.getDefault().lookupResult(InjectionSlot.class);
    45         res.addLookupListener(listener);
    46         assertEquals("Two services now", 2, res.allInstances().size());
    47         
    48         MockServices.setServices(ImplTwo.class);
    49         
    50         assertEquals("One service only", 1, res.allInstances().size());
    51         assertEquals("One change in listener", 1, listener.cnt);
    52         assertEquals("The second is ImplTwo", ImplTwo.class, res.allInstances().iterator().next().getClass());
    53         // END: lookup.listener
    54     }
    55 
    56     public static final class ImplOne extends InjectionSlot {
    57     }
    58     public static final class ImplTwo extends InjectionSlot {
    59     }
    60 }