# HG changeset patch # User Jaroslav Tulach # Date 1226733952 -3600 # Node ID d6fa728fb2ba57ff9154b358574131263caadbb1 # Parent 0753acb192c7f9ac8ffffff871f5ccb81481e031 Tests for magical bag - e.g. overuse of lookup diff -r 0753acb192c7 -r d6fa728fb2ba samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java --- a/samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java Sat Nov 15 08:19:20 2008 +0100 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java Sat Nov 15 08:25:52 2008 +0100 @@ -27,7 +27,7 @@ } @Test - public void showUseOfCumulativeFactory() throws Exception { + public void showUseOfBuilder() throws Exception { Prov p = new Prov(); NameProvider np = p; URLProvider up = p; @@ -47,7 +47,7 @@ } @Test - public void showVerboseUseOfCumulativeFactory() throws Exception { + public void showVerboseUseOfBuilder() throws Exception { Prov prov = new Prov(); ServerConnector info; diff -r 0753acb192c7 -r d6fa728fb2ba samples/aserverinfo/test/org/apidesign/aserverinfo/test/MagicalBagFactoryTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/MagicalBagFactoryTest.java Sat Nov 15 08:25:52 2008 +0100 @@ -0,0 +1,79 @@ +package org.apidesign.aserverinfo.test; + +import java.net.MalformedURLException; +import java.net.URL; +import org.apidesign.aserverinfo.magicalbagfactory.ServerConnector; +import org.apidesign.aserverinfo.spi.NameProvider; +import org.apidesign.aserverinfo.spi.ResetHandler; +import org.apidesign.aserverinfo.spi.URLProvider; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; +import org.openide.util.Exceptions; +import org.openide.util.Lookup; +import org.openide.util.lookup.AbstractLookup; +import org.openide.util.lookup.InstanceContent; + +public class MagicalBagFactoryTest { + + public MagicalBagFactoryTest() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @Test + public void showUseOfMagicalBagFactory() throws Exception { + Prov provider = new Prov(); + ServerConnector inf; + + // BEGIN: ServerConnector.magicalbag.creation + InstanceContent magicalContent = new InstanceContent(); + Lookup magicalBag = new AbstractLookup(magicalContent); + magicalContent.add(provider); + + inf = ServerConnector.create(magicalBag); + // END: ServerConnector.magicalbag.creation + + assertEquals("API Design Server", inf.getName()); + assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm()); + inf.reset(); + assertEquals("Once reset", 1, provider.resets); + + provider.resets = 0; + // BEGIN: ServerConnector.magicalbag.change + magicalContent.remove(provider); + inf.reset(); + assertEquals("No reset called now", 0, provider.resets); + // END: ServerConnector.magicalbag.change + } + + private static class Prov implements NameProvider, URLProvider, ResetHandler { + int resets; + + public String getName() { + return "API Design Server"; + } + + public URL getURL() { + try { + return new URL("http://www.apidesign.org"); + } catch (MalformedURLException ex) { + Exceptions.printStackTrace(ex); + return null; + } + } + + public void reset() { + resets++; + } + + } + +} \ No newline at end of file