samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java
changeset 294 0753acb192c7
child 295 d6fa728fb2ba
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java	Sat Nov 15 08:19:20 2008 +0100
     1.3 @@ -0,0 +1,92 @@
     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.builder.ServerConnector;
     1.9 +import org.apidesign.aserverinfo.builder.ServerInfo;
    1.10 +import org.apidesign.aserverinfo.spi.NameProvider;
    1.11 +import org.apidesign.aserverinfo.spi.ResetHandler;
    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 BuilderFactoryTest {
    1.20 +
    1.21 +    public BuilderFactoryTest() {
    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 showUseOfCumulativeFactory() 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.cumulative.creation
    1.41 +        inf = ServerInfo.empty()
    1.42 +                .nameProvider(np).urlProvider(up).reset(res).connect();
    1.43 +        // END: ServerConnector.cumulative.creation
    1.44 +        
    1.45 +        assertEquals("API Design Server", inf.getName());
    1.46 +        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    1.47 +        inf.reset();
    1.48 +        assertEquals("Once reset", 1, p.resets);
    1.49 +        
    1.50 +    }
    1.51 +    
    1.52 +    @Test
    1.53 +    public void showVerboseUseOfCumulativeFactory() throws Exception {
    1.54 +        Prov prov = new Prov();
    1.55 +        ServerConnector info;
    1.56 +        
    1.57 +        // BEGIN: ServerConnector.builder.creation.verbose
    1.58 +        ServerInfo empty = ServerInfo.empty();
    1.59 +        ServerInfo name = empty.nameProvider(prov);
    1.60 +        ServerInfo urlAndName = name.urlProvider(prov);
    1.61 +        ServerInfo all = urlAndName.reset(prov);
    1.62 +        info = all.connect();
    1.63 +        // END: ServerConnector.builder.creation.verbose
    1.64 +        
    1.65 +        assertEquals("API Design Server", info.getName());
    1.66 +        assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
    1.67 +        info.reset();
    1.68 +        assertEquals("Once reset", 1, prov.resets);
    1.69 +        
    1.70 +    }
    1.71 +    
    1.72 +    
    1.73 +    private static class Prov implements NameProvider, URLProvider, ResetHandler {
    1.74 +        int resets;
    1.75 +
    1.76 +        public String getName() {
    1.77 +            return "API Design Server";
    1.78 +        }
    1.79 +
    1.80 +        public URL getURL() {
    1.81 +            try {
    1.82 +                return new URL("http://www.apidesign.org");
    1.83 +            } catch (MalformedURLException ex) {
    1.84 +                Exceptions.printStackTrace(ex);
    1.85 +                return null;
    1.86 +            }
    1.87 +        }
    1.88 +
    1.89 +        public void reset() {
    1.90 +            resets++;
    1.91 +        }
    1.92 +
    1.93 +    }
    1.94 +        
    1.95 +}
    1.96 \ No newline at end of file