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