MockServices
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:43 +0200
changeset 82d098b8e4de15
parent 81 fda142e38ae4
child 83 ab11b9173089
MockServices
samples/componentinjection/nbproject/project.properties
samples/componentinjection/src/org/apidesign/component/InjectionSlot.java
samples/componentinjection/test/org/apidesign/component/InjectionSlotTest.java
samples/libs/build.xml
     1.1 --- a/samples/componentinjection/nbproject/project.properties	Sat Jun 14 09:53:43 2008 +0200
     1.2 +++ b/samples/componentinjection/nbproject/project.properties	Sat Jun 14 09:53:43 2008 +0200
     1.3 @@ -17,6 +17,8 @@
     1.4  dist.javadoc.dir=${dist.dir}/javadoc
     1.5  excludes=
     1.6  file.reference.junit-4.4.jar=../libs/dist/junit-4.4.jar
     1.7 +file.reference.org-netbeans-insane.jar=../libs/dist/org-netbeans-insane.jar
     1.8 +file.reference.org-netbeans-modules-nbjunit.jar=../libs/dist/org-netbeans-modules-nbjunit.jar
     1.9  file.reference.org-openide-util.jar=../libs/dist/org-openide-util.jar
    1.10  includes=**
    1.11  jar.compress=false
    1.12 @@ -32,7 +34,9 @@
    1.13      ${build.classes.dir}:\
    1.14      ${libs.junit.classpath}:\
    1.15      ${libs.junit_4.classpath}:\
    1.16 -    ${file.reference.junit-4.4.jar}
    1.17 +    ${file.reference.junit-4.4.jar}:\
    1.18 +    ${file.reference.org-netbeans-insane.jar}:\
    1.19 +    ${file.reference.org-netbeans-modules-nbjunit.jar}
    1.20  javadoc.additionalparam=
    1.21  javadoc.author=false
    1.22  javadoc.encoding=${source.encoding}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/componentinjection/src/org/apidesign/component/InjectionSlot.java	Sat Jun 14 09:53:43 2008 +0200
     2.3 @@ -0,0 +1,18 @@
     2.4 +package org.apidesign.component;
     2.5 +
     2.6 +import java.util.Collection;
     2.7 +import org.openide.util.Lookup;
     2.8 +
     2.9 +// BEGIN: lookup.define.slot
    2.10 +public class InjectionSlot {
    2.11 +    public static InjectionSlot singleSlot() {
    2.12 +        // get one implementation
    2.13 +        return Lookup.getDefault().lookup(InjectionSlot.class);
    2.14 +    }
    2.15 +    
    2.16 +    public static Collection<? extends InjectionSlot> multiSlot() {
    2.17 +        // get all registered implementations
    2.18 +        return Lookup.getDefault().lookupAll(InjectionSlot.class);
    2.19 +    }
    2.20 +}
    2.21 +// END: lookup.define.slot
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/componentinjection/test/org/apidesign/component/InjectionSlotTest.java	Sat Jun 14 09:53:43 2008 +0200
     3.3 @@ -0,0 +1,46 @@
     3.4 +package org.apidesign.component;
     3.5 +
     3.6 +import java.util.Iterator;
     3.7 +import org.junit.BeforeClass;
     3.8 +import org.junit.Test;
     3.9 +import org.netbeans.junit.MockServices;
    3.10 +import static org.junit.Assert.*;
    3.11 +
    3.12 +// BEGIN: lookup.mockservices
    3.13 +public class InjectionSlotTest {
    3.14 +    public InjectionSlotTest() {
    3.15 +    }
    3.16 +
    3.17 +    @BeforeClass
    3.18 +    public static void setUpClass() throws Exception {
    3.19 +        // this is a support for "dynamic" registration of any class
    3.20 +        // into the global pool of Lookup.getDefault() and java.util.ServiceLoader
    3.21 +        // either in @BeforeClass or @Before register 
    3.22 +        // classes of the mock instances that you want your code 
    3.23 +        // to find
    3.24 +        MockServices.setServices(ImplOne.class, ImplTwo.class);
    3.25 +    }
    3.26 +
    3.27 +    @Test
    3.28 +    public void singleSlot() {
    3.29 +        InjectionSlot result = InjectionSlot.singleSlot();
    3.30 +        assertNotNull("Some result found", result);
    3.31 +        assertEquals("The first is ImplOne", ImplOne.class, result.getClass());
    3.32 +    }
    3.33 +
    3.34 +    @Test
    3.35 +    public void multiSlot() {
    3.36 +        Iterator<? extends InjectionSlot> it = InjectionSlot.multiSlot().iterator();
    3.37 +        assertTrue("Has at least one", it.hasNext());
    3.38 +        assertEquals("The first is ImplOne", ImplOne.class, it.next().getClass());
    3.39 +        assertTrue("Has two", it.hasNext());
    3.40 +        assertEquals("The second is ImplTwo", ImplTwo.class, it.next().getClass());
    3.41 +        assertFalse("No other instance registered", it.hasNext());
    3.42 +    }
    3.43 +
    3.44 +    public static final class ImplOne extends InjectionSlot {
    3.45 +    }
    3.46 +    public static final class ImplTwo extends InjectionSlot {
    3.47 +    }
    3.48 +}
    3.49 +// BEGIN: lookup.mockservices
     4.1 --- a/samples/libs/build.xml	Sat Jun 14 09:53:43 2008 +0200
     4.2 +++ b/samples/libs/build.xml	Sat Jun 14 09:53:43 2008 +0200
     4.3 @@ -14,6 +14,16 @@
     4.4              <param name="library.include" value="**/org-openide-util.jar"/>
     4.5              <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/platform8/org-openide-util.nbm"/>
     4.6          </antcall>
     4.7 +        <antcall target="-library-from-zip">
     4.8 +            <param name="library" value="org-netbeans-modules-nbjunit"/>
     4.9 +            <param name="library.include" value="**/org-netbeans-modules-nbjunit.jar"/>
    4.10 +            <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/testtools/org-netbeans-modules-nbjunit.nbm"/>
    4.11 +        </antcall>
    4.12 +        <antcall target="-library-from-zip">
    4.13 +            <param name="library" value="org-netbeans-insane"/>
    4.14 +            <param name="library.include" value="**/org-netbeans-insane.jar"/>
    4.15 +            <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/ide9/org-netbeans-insane.nbm"/>
    4.16 +        </antcall>
    4.17      </target>
    4.18      
    4.19      <!-- support methods -->