samples/aserverinfo/test/org/apidesign/aserverinfo/test/FactoriesTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 15 Nov 2008 08:30:06 +0100
changeset 298 5d5cb122088c
parent 297 adae776dcb9f
child 340 9c1a298e51a9
permissions -rw-r--r--
two code snippets were named the same
jtulach@297
     1
package org.apidesign.aserverinfo.test;
jtulach@297
     2
jtulach@297
     3
import java.net.MalformedURLException;
jtulach@297
     4
import java.net.URL;
jtulach@297
     5
import org.apidesign.aserverinfo.factories.ServerConnector;
jtulach@297
     6
import org.apidesign.aserverinfo.spi.NameProvider;
jtulach@297
     7
import org.apidesign.aserverinfo.spi.ResetHandler;
jtulach@297
     8
import org.apidesign.aserverinfo.spi.ShutdownHandler;
jtulach@297
     9
import org.apidesign.aserverinfo.spi.URLProvider;
jtulach@297
    10
import org.junit.After;
jtulach@297
    11
import org.junit.Before;
jtulach@297
    12
import org.junit.Test;
jtulach@297
    13
import static org.junit.Assert.*;
jtulach@297
    14
import org.openide.util.Exceptions;
jtulach@297
    15
jtulach@297
    16
public class FactoriesTest {
jtulach@297
    17
jtulach@297
    18
    public FactoriesTest() {
jtulach@297
    19
    }
jtulach@297
    20
jtulach@297
    21
    @Before
jtulach@297
    22
    public void setUp() {
jtulach@297
    23
    }
jtulach@297
    24
jtulach@297
    25
    @After
jtulach@297
    26
    public void tearDown() {
jtulach@297
    27
    }
jtulach@297
    28
jtulach@297
    29
    @Test
jtulach@297
    30
    public void showUseOfFactoryVersion10() throws Exception {
jtulach@297
    31
        Prov p = new Prov();
jtulach@297
    32
        NameProvider np = p;
jtulach@297
    33
        URLProvider up = p;
jtulach@297
    34
        ResetHandler res = p;
jtulach@297
    35
        ServerConnector inf;
jtulach@297
    36
        
jtulach@297
    37
        // BEGIN: ServerConnector.factory.creation
jtulach@297
    38
        inf = ServerConnector.create(np, up, res);
jtulach@297
    39
        // END: ServerConnector.factory.creation
jtulach@297
    40
        
jtulach@297
    41
        assertEquals("API Design Server", inf.getName());
jtulach@297
    42
        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
jtulach@297
    43
        inf.reset();
jtulach@297
    44
        assertEquals("Once reset", 1, p.resets);
jtulach@297
    45
        
jtulach@297
    46
    }
jtulach@297
    47
    @Test
jtulach@297
    48
    public void showUseOfFactoryVersion20() throws Exception {
jtulach@297
    49
        Prov p = new Prov();
jtulach@297
    50
        NameProvider np = p;
jtulach@297
    51
        URLProvider up = p;
jtulach@297
    52
        ResetHandler res = p;
jtulach@297
    53
        ShutdownHandler shutdown = new ShutdownHandler() {
jtulach@297
    54
            public void shutdown() {
jtulach@297
    55
                // OK
jtulach@297
    56
            }
jtulach@297
    57
        };
jtulach@297
    58
        ServerConnector inf;
jtulach@297
    59
jtulach@298
    60
        // BEGIN: ServerConnector.factory.creation2
jtulach@297
    61
        inf = ServerConnector.create(np, up, res, shutdown);
jtulach@298
    62
        // END: ServerConnector.factory.creation2
jtulach@297
    63
jtulach@297
    64
        assertEquals("API Design Server", inf.getName());
jtulach@297
    65
        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
jtulach@297
    66
        inf.reset();
jtulach@297
    67
        assertEquals("Once reset", 1, p.resets);
jtulach@297
    68
jtulach@297
    69
    }
jtulach@297
    70
    
jtulach@297
    71
    private static class Prov implements NameProvider, URLProvider, ResetHandler {
jtulach@297
    72
        int resets;
jtulach@297
    73
jtulach@297
    74
        public String getName() {
jtulach@297
    75
            return "API Design Server";
jtulach@297
    76
        }
jtulach@297
    77
jtulach@297
    78
        public URL getURL() {
jtulach@297
    79
            try {
jtulach@297
    80
                return new URL("http://www.apidesign.org");
jtulach@297
    81
            } catch (MalformedURLException ex) {
jtulach@297
    82
                Exceptions.printStackTrace(ex);
jtulach@297
    83
                return null;
jtulach@297
    84
            }
jtulach@297
    85
        }
jtulach@297
    86
jtulach@297
    87
        public void reset() {
jtulach@297
    88
            resets++;
jtulach@297
    89
        }
jtulach@297
    90
jtulach@297
    91
    }
jtulach@297
    92
        
jtulach@297
    93
}