samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 337 d5b6a877e5a8
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
     1 package org.apidesign.aserverinfo.test;
     2 
     3 import java.net.MalformedURLException;
     4 import java.net.URL;
     5 import java.util.logging.Level;
     6 import java.util.logging.Logger;
     7 import org.apidesign.aserverinfo.builder.ServerConnector;
     8 import org.apidesign.aserverinfo.builder.ServerInfo;
     9 import org.apidesign.aserverinfo.spi.NameProvider;
    10 import org.apidesign.aserverinfo.spi.ResetHandler;
    11 import org.apidesign.aserverinfo.spi.URLProvider;
    12 import org.junit.After;
    13 import org.junit.Before;
    14 import org.junit.Test;
    15 import static org.junit.Assert.*;
    16 
    17 public class BuilderFactoryTest {
    18 
    19     public BuilderFactoryTest() {
    20     }
    21 
    22     @Before
    23     public void setUp() {
    24     }
    25 
    26     @After
    27     public void tearDown() {
    28     }
    29 
    30     @Test
    31     public void showUseOfBuilder() throws Exception {
    32         Prov p = new Prov();
    33         NameProvider np = p;
    34         URLProvider up = p;
    35         ResetHandler res = p;
    36         ServerConnector connection;
    37         
    38         // BEGIN: ServerConnector.builder.creation
    39         connection = ServerInfo.empty()
    40                 .nameProvider(np).urlProvider(up).reset(res).connect();
    41         // END: ServerConnector.builder.creation
    42         
    43         assertEquals("API Design Server", connection.getName());
    44         assertEquals("http://www.apidesign.org", connection.getURL().toExternalForm());
    45         connection.reset();
    46         assertEquals("Once reset", 1, p.resets);
    47         
    48     }
    49     
    50     @Test
    51     public void showVerboseUseOfBuilder() throws Exception {
    52         Prov prov = new Prov();
    53         ServerConnector connection;
    54         
    55         // BEGIN: ServerConnector.builder.creation.verbose
    56         ServerInfo empty = ServerInfo.empty();
    57         ServerInfo name = empty.nameProvider(prov);
    58         ServerInfo urlAndName = name.urlProvider(prov);
    59         ServerInfo all = urlAndName.reset(prov);
    60         connection = all.connect();
    61         // END: ServerConnector.builder.creation.verbose
    62         
    63         assertEquals("API Design Server", connection.getName());
    64         assertEquals("http://www.apidesign.org", connection.getURL().toExternalForm());
    65         connection.reset();
    66         assertEquals("Once reset", 1, prov.resets);
    67         
    68     }
    69     
    70     
    71     private static class Prov implements NameProvider, URLProvider, ResetHandler {
    72         int resets;
    73 
    74         public String getName() {
    75             return "API Design Server";
    76         }
    77 
    78         public URL getURL() {
    79             try {
    80                 return new URL("http://www.apidesign.org");
    81             } catch (MalformedURLException ex) {
    82                 Logger.getLogger(BuilderFactoryTest.class.getName()).log(Level.SEVERE, null, ex);
    83                 return null;
    84             }
    85         }
    86 
    87         public void reset() {
    88             resets++;
    89         }
    90 
    91     }
    92         
    93 }