LookupListener demo
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:45 +0200
changeset 8433aa60b18830
parent 83 ab11b9173089
child 85 935c169667a2
LookupListener demo
samples/componentinjection/test/org/apidesign/component/InjectionSlotChangesTest.java
samples/componentinjection/test/org/apidesign/component/InjectionViaServiceLoaderTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/componentinjection/test/org/apidesign/component/InjectionSlotChangesTest.java	Sat Jun 14 09:53:45 2008 +0200
     1.3 @@ -0,0 +1,60 @@
     1.4 +package org.apidesign.component;
     1.5 +
     1.6 +import java.util.Iterator;
     1.7 +import org.junit.BeforeClass;
     1.8 +import org.junit.Test;
     1.9 +import org.netbeans.junit.MockServices;
    1.10 +import org.openide.util.Lookup;
    1.11 +import static org.junit.Assert.*;
    1.12 +import org.openide.util.Lookup.Result;
    1.13 +import org.openide.util.LookupEvent;
    1.14 +import org.openide.util.LookupListener;
    1.15 +
    1.16 +public class InjectionSlotChangesTest {
    1.17 +    public InjectionSlotChangesTest() {
    1.18 +    }
    1.19 +
    1.20 +    @BeforeClass
    1.21 +    public static void setUpClass() throws Exception {
    1.22 +        // this is a support for "dynamic" registration of any class
    1.23 +        // into the global pool of Lookup.getDefault() and java.util.ServiceLoader
    1.24 +        // either in @BeforeClass or @Before register 
    1.25 +        // classes of the mock instances that you want your code 
    1.26 +        // to find
    1.27 +        MockServices.setServices(ImplOne.class, ImplTwo.class);
    1.28 +    }
    1.29 +
    1.30 +    @Test
    1.31 +    public void multiSlot() {
    1.32 +        Iterator<? extends InjectionSlot> it = InjectionSlot.multiSlot().iterator();
    1.33 +        assertTrue("Has at least one", it.hasNext());
    1.34 +        assertEquals("The first is ImplOne", ImplOne.class, it.next().getClass());
    1.35 +        assertTrue("Has two", it.hasNext());
    1.36 +        assertEquals("The second is ImplTwo", ImplTwo.class, it.next().getClass());
    1.37 +        assertFalse("No other instance registered", it.hasNext());
    1.38 +
    1.39 +        // BEGIN: lookup.listener
    1.40 +        class Listener implements LookupListener {
    1.41 +            int cnt;
    1.42 +            public void resultChanged(LookupEvent arg0) {
    1.43 +                cnt++;
    1.44 +            }
    1.45 +        }
    1.46 +        Listener listener = new Listener();
    1.47 +        Lookup.Result<InjectionSlot> res = Lookup.getDefault().lookupResult(InjectionSlot.class);
    1.48 +        res.addLookupListener(listener);
    1.49 +        assertEquals("Two services now", 2, res.allInstances().size());
    1.50 +        
    1.51 +        MockServices.setServices(ImplTwo.class);
    1.52 +        
    1.53 +        assertEquals("One service only", 1, res.allInstances().size());
    1.54 +        assertEquals("One change in listener", 1, listener.cnt);
    1.55 +        assertEquals("The second is ImplTwo", ImplTwo.class, res.allInstances().iterator().next().getClass());
    1.56 +        // END: lookup.listener
    1.57 +    }
    1.58 +
    1.59 +    public static final class ImplOne extends InjectionSlot {
    1.60 +    }
    1.61 +    public static final class ImplTwo extends InjectionSlot {
    1.62 +    }
    1.63 +}
     2.1 --- a/samples/componentinjection/test/org/apidesign/component/InjectionViaServiceLoaderTest.java	Sat Jun 14 09:53:44 2008 +0200
     2.2 +++ b/samples/componentinjection/test/org/apidesign/component/InjectionViaServiceLoaderTest.java	Sat Jun 14 09:53:45 2008 +0200
     2.3 @@ -43,4 +43,4 @@
     2.4      public static final class ImplTwo extends InjectionSlot {
     2.5      }
     2.6  }
     2.7 -// BEGIN: lookup.mockservices.serviceloader
     2.8 +// END: lookup.mockservices.serviceloader