samples/aserverinfo/test/org/apidesign/aserverinfo/test/CummulativeFactoryTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 294 0753acb192c7
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.util.logging.Logger;
     4 import java.util.logging.Level;
     5 import java.net.MalformedURLException;
     6 import java.net.URL;
     7 import org.apidesign.aserverinfo.cummulativefactory.ServerConnector;
     8 import org.apidesign.aserverinfo.spi.NameProvider;
     9 import org.apidesign.aserverinfo.spi.ResetHandler;
    10 import org.apidesign.aserverinfo.spi.URLProvider;
    11 import org.junit.After;
    12 import org.junit.Before;
    13 import org.junit.Test;
    14 import static org.junit.Assert.*;
    15 
    16 public class CummulativeFactoryTest {
    17 
    18     public CummulativeFactoryTest() {
    19     }
    20 
    21     @Before
    22     public void setUp() {
    23     }
    24 
    25     @After
    26     public void tearDown() {
    27     }
    28 
    29     @Test
    30     public void showUseOfCumulativeFactory() throws Exception {
    31         Prov p = new Prov();
    32         NameProvider np = p;
    33         URLProvider up = p;
    34         ResetHandler res = p;
    35         ServerConnector inf;
    36         
    37         // BEGIN: ServerConnector.cumulative.creation
    38         inf = ServerConnector.empty().nameProvider(np).urlProvider(up).reset(res);
    39         // END: ServerConnector.cumulative.creation
    40         
    41         assertEquals("API Design Server", inf.getName());
    42         assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    43         inf.reset();
    44         assertEquals("Once reset", 1, p.resets);
    45         
    46     }
    47     
    48     @Test
    49     public void showVerboseUseOfCumulativeFactory() throws Exception {
    50         Prov prov = new Prov();
    51         ServerConnector info;
    52         
    53         // BEGIN: ServerConnector.cumulative.creation.verbose
    54         ServerConnector empty = ServerConnector.empty();
    55         ServerConnector name = empty.nameProvider(prov);
    56         ServerConnector urlAndName = name.urlProvider(prov);
    57         info = urlAndName.reset(prov);
    58         // END: ServerConnector.cumulative.creation.verbose
    59         
    60         assertEquals("API Design Server", info.getName());
    61         assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
    62         info.reset();
    63         assertEquals("Once reset", 1, prov.resets);
    64         
    65     }
    66     
    67     
    68     private static class Prov implements NameProvider, URLProvider, ResetHandler {
    69         int resets;
    70 
    71         public String getName() {
    72             return "API Design Server";
    73         }
    74 
    75         public URL getURL() {
    76             try {
    77                 return new URL("http://www.apidesign.org");
    78             } catch (MalformedURLException ex) {
    79                 Logger.getLogger(CummulativeFactoryTest.class.getName()).log(Level.SEVERE, null, ex);
    80                 return null;
    81             }
    82         }
    83 
    84         public void reset() {
    85             resets++;
    86         }
    87 
    88     }
    89         
    90 }