Showing use of "plain" factories
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 15 Nov 2008 08:29:32 +0100
changeset 297adae776dcb9f
parent 296 20476950e410
child 298 5d5cb122088c
Showing use of "plain" factories
samples/aserverinfo/test/org/apidesign/aserverinfo/test/FactoriesTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/FactoriesTest.java	Sat Nov 15 08:29:32 2008 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +package org.apidesign.aserverinfo.test;
     1.5 +
     1.6 +import java.net.MalformedURLException;
     1.7 +import java.net.URL;
     1.8 +import org.apidesign.aserverinfo.factories.ServerConnector;
     1.9 +import org.apidesign.aserverinfo.spi.NameProvider;
    1.10 +import org.apidesign.aserverinfo.spi.ResetHandler;
    1.11 +import org.apidesign.aserverinfo.spi.ShutdownHandler;
    1.12 +import org.apidesign.aserverinfo.spi.URLProvider;
    1.13 +import org.junit.After;
    1.14 +import org.junit.Before;
    1.15 +import org.junit.Test;
    1.16 +import static org.junit.Assert.*;
    1.17 +import org.openide.util.Exceptions;
    1.18 +
    1.19 +public class FactoriesTest {
    1.20 +
    1.21 +    public FactoriesTest() {
    1.22 +    }
    1.23 +
    1.24 +    @Before
    1.25 +    public void setUp() {
    1.26 +    }
    1.27 +
    1.28 +    @After
    1.29 +    public void tearDown() {
    1.30 +    }
    1.31 +
    1.32 +    @Test
    1.33 +    public void showUseOfFactoryVersion10() throws Exception {
    1.34 +        Prov p = new Prov();
    1.35 +        NameProvider np = p;
    1.36 +        URLProvider up = p;
    1.37 +        ResetHandler res = p;
    1.38 +        ServerConnector inf;
    1.39 +        
    1.40 +        // BEGIN: ServerConnector.factory.creation
    1.41 +        inf = ServerConnector.create(np, up, res);
    1.42 +        // END: ServerConnector.factory.creation
    1.43 +        
    1.44 +        assertEquals("API Design Server", inf.getName());
    1.45 +        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    1.46 +        inf.reset();
    1.47 +        assertEquals("Once reset", 1, p.resets);
    1.48 +        
    1.49 +    }
    1.50 +    @Test
    1.51 +    public void showUseOfFactoryVersion20() throws Exception {
    1.52 +        Prov p = new Prov();
    1.53 +        NameProvider np = p;
    1.54 +        URLProvider up = p;
    1.55 +        ResetHandler res = p;
    1.56 +        ShutdownHandler shutdown = new ShutdownHandler() {
    1.57 +            public void shutdown() {
    1.58 +                // OK
    1.59 +            }
    1.60 +        };
    1.61 +        ServerConnector inf;
    1.62 +
    1.63 +        // BEGIN: ServerConnector.factory.creation
    1.64 +        inf = ServerConnector.create(np, up, res, shutdown);
    1.65 +        // END: ServerConnector.factory.creation
    1.66 +
    1.67 +        assertEquals("API Design Server", inf.getName());
    1.68 +        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    1.69 +        inf.reset();
    1.70 +        assertEquals("Once reset", 1, p.resets);
    1.71 +
    1.72 +    }
    1.73 +    
    1.74 +    private static class Prov implements NameProvider, URLProvider, ResetHandler {
    1.75 +        int resets;
    1.76 +
    1.77 +        public String getName() {
    1.78 +            return "API Design Server";
    1.79 +        }
    1.80 +
    1.81 +        public URL getURL() {
    1.82 +            try {
    1.83 +                return new URL("http://www.apidesign.org");
    1.84 +            } catch (MalformedURLException ex) {
    1.85 +                Exceptions.printStackTrace(ex);
    1.86 +                return null;
    1.87 +            }
    1.88 +        }
    1.89 +
    1.90 +        public void reset() {
    1.91 +            resets++;
    1.92 +        }
    1.93 +
    1.94 +    }
    1.95 +        
    1.96 +}
    1.97 \ No newline at end of file