samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 25 Aug 2009 11:32:10 +0200
changeset 337 d5b6a877e5a8
parent 296 20476950e410
child 340 9c1a298e51a9
permissions -rw-r--r--
In fact builder pattern is combining mutable objects during creation with immutable result
     1 package org.apidesign.aserverinfo.test;
     2 
     3 import java.net.MalformedURLException;
     4 import java.net.URL;
     5 import org.apidesign.aserverinfo.builder.ServerConnector;
     6 import org.apidesign.aserverinfo.builder.ServerInfo;
     7 import org.apidesign.aserverinfo.spi.NameProvider;
     8 import org.apidesign.aserverinfo.spi.ResetHandler;
     9 import org.apidesign.aserverinfo.spi.URLProvider;
    10 import org.junit.After;
    11 import org.junit.Before;
    12 import org.junit.Test;
    13 import static org.junit.Assert.*;
    14 import org.openide.util.Exceptions;
    15 
    16 public class BuilderFactoryTest {
    17 
    18     public BuilderFactoryTest() {
    19     }
    20 
    21     @Before
    22     public void setUp() {
    23     }
    24 
    25     @After
    26     public void tearDown() {
    27     }
    28 
    29     @Test
    30     public void showUseOfBuilder() throws Exception {
    31         Prov p = new Prov();
    32         NameProvider np = p;
    33         URLProvider up = p;
    34         ResetHandler res = p;
    35         ServerConnector connection;
    36         
    37         // BEGIN: ServerConnector.builder.creation
    38         connection = ServerInfo.empty()
    39                 .nameProvider(np).urlProvider(up).reset(res).connect();
    40         // END: ServerConnector.builder.creation
    41         
    42         assertEquals("API Design Server", connection.getName());
    43         assertEquals("http://www.apidesign.org", connection.getURL().toExternalForm());
    44         connection.reset();
    45         assertEquals("Once reset", 1, p.resets);
    46         
    47     }
    48     
    49     @Test
    50     public void showVerboseUseOfBuilder() throws Exception {
    51         Prov prov = new Prov();
    52         ServerConnector connection;
    53         
    54         // BEGIN: ServerConnector.builder.creation.verbose
    55         ServerInfo empty = ServerInfo.empty();
    56         ServerInfo name = empty.nameProvider(prov);
    57         ServerInfo urlAndName = name.urlProvider(prov);
    58         ServerInfo all = urlAndName.reset(prov);
    59         connection = all.connect();
    60         // END: ServerConnector.builder.creation.verbose
    61         
    62         assertEquals("API Design Server", connection.getName());
    63         assertEquals("http://www.apidesign.org", connection.getURL().toExternalForm());
    64         connection.reset();
    65         assertEquals("Once reset", 1, prov.resets);
    66         
    67     }
    68     
    69     
    70     private static class Prov implements NameProvider, URLProvider, ResetHandler {
    71         int resets;
    72 
    73         public String getName() {
    74             return "API Design Server";
    75         }
    76 
    77         public URL getURL() {
    78             try {
    79                 return new URL("http://www.apidesign.org");
    80             } catch (MalformedURLException ex) {
    81                 Exceptions.printStackTrace(ex);
    82                 return null;
    83             }
    84         }
    85 
    86         public void reset() {
    87             resets++;
    88         }
    89 
    90     }
    91         
    92 }