samples/componentinjection/test/org/apidesign/component/InjectionSlotChangesTest.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 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 = 
    45             Lookup.getDefault().lookupResult(InjectionSlot.class);
    46         res.addLookupListener(listener);
    47         assertEquals("Two services now", 2, res.allInstances().size());
    48         
    49         MockServices.setServices(ImplTwo.class);
    50         
    51         assertEquals("One service only", 1, res.allInstances().size());
    52         assertEquals("One change in listener", 1, listener.cnt);
    53         assertEquals(
    54             "The second is ImplTwo", 
    55             ImplTwo.class, 
    56             res.allInstances().iterator().next().getClass()
    57         );
    58         // END: lookup.listener
    59     }
    60 
    61     public static final class ImplOne extends InjectionSlot {
    62     }
    63     public static final class ImplTwo extends InjectionSlot {
    64     }
    65 }