# HG changeset patch # User Jaroslav Tulach # Date 1213430023 -7200 # Node ID d098b8e4de15892ea3ff6d01cd5802aeb57bdbbd # Parent fda142e38ae4845305c5a9bab83cdcfc2b1edc2f MockServices diff -r fda142e38ae4 -r d098b8e4de15 samples/componentinjection/nbproject/project.properties --- a/samples/componentinjection/nbproject/project.properties Sat Jun 14 09:53:43 2008 +0200 +++ b/samples/componentinjection/nbproject/project.properties Sat Jun 14 09:53:43 2008 +0200 @@ -17,6 +17,8 @@ dist.javadoc.dir=${dist.dir}/javadoc excludes= file.reference.junit-4.4.jar=../libs/dist/junit-4.4.jar +file.reference.org-netbeans-insane.jar=../libs/dist/org-netbeans-insane.jar +file.reference.org-netbeans-modules-nbjunit.jar=../libs/dist/org-netbeans-modules-nbjunit.jar file.reference.org-openide-util.jar=../libs/dist/org-openide-util.jar includes=** jar.compress=false @@ -32,7 +34,9 @@ ${build.classes.dir}:\ ${libs.junit.classpath}:\ ${libs.junit_4.classpath}:\ - ${file.reference.junit-4.4.jar} + ${file.reference.junit-4.4.jar}:\ + ${file.reference.org-netbeans-insane.jar}:\ + ${file.reference.org-netbeans-modules-nbjunit.jar} javadoc.additionalparam= javadoc.author=false javadoc.encoding=${source.encoding} diff -r fda142e38ae4 -r d098b8e4de15 samples/componentinjection/src/org/apidesign/component/InjectionSlot.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/componentinjection/src/org/apidesign/component/InjectionSlot.java Sat Jun 14 09:53:43 2008 +0200 @@ -0,0 +1,18 @@ +package org.apidesign.component; + +import java.util.Collection; +import org.openide.util.Lookup; + +// BEGIN: lookup.define.slot +public class InjectionSlot { + public static InjectionSlot singleSlot() { + // get one implementation + return Lookup.getDefault().lookup(InjectionSlot.class); + } + + public static Collection multiSlot() { + // get all registered implementations + return Lookup.getDefault().lookupAll(InjectionSlot.class); + } +} +// END: lookup.define.slot diff -r fda142e38ae4 -r d098b8e4de15 samples/componentinjection/test/org/apidesign/component/InjectionSlotTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/componentinjection/test/org/apidesign/component/InjectionSlotTest.java Sat Jun 14 09:53:43 2008 +0200 @@ -0,0 +1,46 @@ +package org.apidesign.component; + +import java.util.Iterator; +import org.junit.BeforeClass; +import org.junit.Test; +import org.netbeans.junit.MockServices; +import static org.junit.Assert.*; + +// BEGIN: lookup.mockservices +public class InjectionSlotTest { + public InjectionSlotTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + // this is a support for "dynamic" registration of any class + // into the global pool of Lookup.getDefault() and java.util.ServiceLoader + // either in @BeforeClass or @Before register + // classes of the mock instances that you want your code + // to find + MockServices.setServices(ImplOne.class, ImplTwo.class); + } + + @Test + public void singleSlot() { + InjectionSlot result = InjectionSlot.singleSlot(); + assertNotNull("Some result found", result); + assertEquals("The first is ImplOne", ImplOne.class, result.getClass()); + } + + @Test + public void multiSlot() { + Iterator it = InjectionSlot.multiSlot().iterator(); + assertTrue("Has at least one", it.hasNext()); + assertEquals("The first is ImplOne", ImplOne.class, it.next().getClass()); + assertTrue("Has two", it.hasNext()); + assertEquals("The second is ImplTwo", ImplTwo.class, it.next().getClass()); + assertFalse("No other instance registered", it.hasNext()); + } + + public static final class ImplOne extends InjectionSlot { + } + public static final class ImplTwo extends InjectionSlot { + } +} +// BEGIN: lookup.mockservices diff -r fda142e38ae4 -r d098b8e4de15 samples/libs/build.xml --- a/samples/libs/build.xml Sat Jun 14 09:53:43 2008 +0200 +++ b/samples/libs/build.xml Sat Jun 14 09:53:43 2008 +0200 @@ -14,6 +14,16 @@ + + + + + + + + + +