Tests for magical bag - e.g. overuse of lookup
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 15 Nov 2008 08:25:52 +0100
changeset 295d6fa728fb2ba
parent 294 0753acb192c7
child 296 20476950e410
Tests for magical bag - e.g. overuse of lookup
samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java
samples/aserverinfo/test/org/apidesign/aserverinfo/test/MagicalBagFactoryTest.java
     1.1 --- a/samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java	Sat Nov 15 08:19:20 2008 +0100
     1.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java	Sat Nov 15 08:25:52 2008 +0100
     1.3 @@ -27,7 +27,7 @@
     1.4      }
     1.5  
     1.6      @Test
     1.7 -    public void showUseOfCumulativeFactory() throws Exception {
     1.8 +    public void showUseOfBuilder() throws Exception {
     1.9          Prov p = new Prov();
    1.10          NameProvider np = p;
    1.11          URLProvider up = p;
    1.12 @@ -47,7 +47,7 @@
    1.13      }
    1.14      
    1.15      @Test
    1.16 -    public void showVerboseUseOfCumulativeFactory() throws Exception {
    1.17 +    public void showVerboseUseOfBuilder() throws Exception {
    1.18          Prov prov = new Prov();
    1.19          ServerConnector info;
    1.20          
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/MagicalBagFactoryTest.java	Sat Nov 15 08:25:52 2008 +0100
     2.3 @@ -0,0 +1,79 @@
     2.4 +package org.apidesign.aserverinfo.test;
     2.5 +
     2.6 +import java.net.MalformedURLException;
     2.7 +import java.net.URL;
     2.8 +import org.apidesign.aserverinfo.magicalbagfactory.ServerConnector;
     2.9 +import org.apidesign.aserverinfo.spi.NameProvider;
    2.10 +import org.apidesign.aserverinfo.spi.ResetHandler;
    2.11 +import org.apidesign.aserverinfo.spi.URLProvider;
    2.12 +import org.junit.After;
    2.13 +import org.junit.Before;
    2.14 +import org.junit.Test;
    2.15 +import static org.junit.Assert.*;
    2.16 +import org.openide.util.Exceptions;
    2.17 +import org.openide.util.Lookup;
    2.18 +import org.openide.util.lookup.AbstractLookup;
    2.19 +import org.openide.util.lookup.InstanceContent;
    2.20 +
    2.21 +public class MagicalBagFactoryTest {
    2.22 +
    2.23 +    public MagicalBagFactoryTest() {
    2.24 +    }
    2.25 +
    2.26 +    @Before
    2.27 +    public void setUp() {
    2.28 +    }
    2.29 +
    2.30 +    @After
    2.31 +    public void tearDown() {
    2.32 +    }
    2.33 +
    2.34 +    @Test
    2.35 +    public void showUseOfMagicalBagFactory() throws Exception {
    2.36 +        Prov provider = new Prov();
    2.37 +        ServerConnector inf;
    2.38 +
    2.39 +        // BEGIN: ServerConnector.magicalbag.creation
    2.40 +        InstanceContent magicalContent = new InstanceContent();
    2.41 +        Lookup magicalBag = new AbstractLookup(magicalContent);
    2.42 +        magicalContent.add(provider);
    2.43 +        
    2.44 +        inf = ServerConnector.create(magicalBag);
    2.45 +        // END: ServerConnector.magicalbag.creation
    2.46 +        
    2.47 +        assertEquals("API Design Server", inf.getName());
    2.48 +        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    2.49 +        inf.reset();
    2.50 +        assertEquals("Once reset", 1, provider.resets);
    2.51 +
    2.52 +        provider.resets = 0;
    2.53 +        // BEGIN: ServerConnector.magicalbag.change
    2.54 +        magicalContent.remove(provider);
    2.55 +        inf.reset();
    2.56 +        assertEquals("No reset called now", 0, provider.resets);
    2.57 +        // END: ServerConnector.magicalbag.change
    2.58 +    }
    2.59 +    
    2.60 +    private static class Prov implements NameProvider, URLProvider, ResetHandler {
    2.61 +        int resets;
    2.62 +
    2.63 +        public String getName() {
    2.64 +            return "API Design Server";
    2.65 +        }
    2.66 +
    2.67 +        public URL getURL() {
    2.68 +            try {
    2.69 +                return new URL("http://www.apidesign.org");
    2.70 +            } catch (MalformedURLException ex) {
    2.71 +                Exceptions.printStackTrace(ex);
    2.72 +                return null;
    2.73 +            }
    2.74 +        }
    2.75 +
    2.76 +        public void reset() {
    2.77 +            resets++;
    2.78 +        }
    2.79 +
    2.80 +    }
    2.81 +        
    2.82 +}
    2.83 \ No newline at end of file