samples/aserverinfo/test/org/apidesign/aserverinfo/test/ServerConnectorTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 15 Nov 2008 06:53:43 +0100
changeset 292 60bb8519cb2d
parent 291 samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java@9cbb8364c4ae
permissions -rw-r--r--
Renaming the AServerInfo class to ServerConnector and spliting it into three separate classes.
     1 package org.apidesign.aserverinfo.test;
     2 
     3 import java.net.MalformedURLException;
     4 import java.net.URL;
     5 import org.apidesign.aserverinfo.cummulativefactory.ServerConnector;
     6 import org.apidesign.aserverinfo.spi.NameProvider;
     7 import org.apidesign.aserverinfo.spi.ResetHandler;
     8 import org.apidesign.aserverinfo.spi.URLProvider;
     9 import org.junit.After;
    10 import org.junit.Before;
    11 import org.junit.Test;
    12 import static org.junit.Assert.*;
    13 import org.openide.util.Exceptions;
    14 
    15 public class ServerConnectorTest {
    16 
    17     public ServerConnectorTest() {
    18     }
    19 
    20     @Before
    21     public void setUp() {
    22     }
    23 
    24     @After
    25     public void tearDown() {
    26     }
    27 
    28     @Test
    29     public void showUseOfCumulativeFactory() throws Exception {
    30         Prov p = new Prov();
    31         NameProvider np = p;
    32         URLProvider up = p;
    33         ResetHandler res = p;
    34         ServerConnector inf;
    35         
    36         // BEGIN: ServerConnector.cumulative.creation
    37         inf = ServerConnector.empty().nameProvider(np).urlProvider(up).reset(res);
    38         // END: ServerConnector.cumulative.creation
    39         
    40         assertEquals("API Design Server", inf.getName());
    41         assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    42         inf.reset();
    43         assertEquals("Once reset", 1, p.resets);
    44         
    45     }
    46     
    47     @Test
    48     public void showVerboseUseOfCumulativeFactory() throws Exception {
    49         Prov prov = new Prov();
    50         ServerConnector info;
    51         
    52         // BEGIN: ServerConnector.cumulative.creation.verbose
    53         ServerConnector empty = ServerConnector.empty();
    54         ServerConnector name = empty.nameProvider(prov);
    55         ServerConnector urlAndName = name.urlProvider(prov);
    56         info = urlAndName.reset(prov);
    57         // END: ServerConnector.cumulative.creation.verbose
    58         
    59         assertEquals("API Design Server", info.getName());
    60         assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
    61         info.reset();
    62         assertEquals("Once reset", 1, prov.resets);
    63         
    64     }
    65     
    66     
    67     private static class Prov implements NameProvider, URLProvider, ResetHandler {
    68         int resets;
    69 
    70         public String getName() {
    71             return "API Design Server";
    72         }
    73 
    74         public URL getURL() {
    75             try {
    76                 return new URL("http://www.apidesign.org");
    77             } catch (MalformedURLException ex) {
    78                 Exceptions.printStackTrace(ex);
    79                 return null;
    80             }
    81         }
    82 
    83         public void reset() {
    84             resets++;
    85         }
    86 
    87     }
    88         
    89 }