Test to verify that we are able to read the main page
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 14:56:32 +0200
changeset 501cce50d16bb5
parent 49 75074e02f345
child 51 8358ef0d29d7
Test to verify that we are able to read the main page
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
     1.1 --- a/freemarkerdor/pom.xml	Sun Aug 30 14:44:49 2009 +0200
     1.2 +++ b/freemarkerdor/pom.xml	Sun Aug 30 14:56:32 2009 +0200
     1.3 @@ -14,12 +14,6 @@
     1.4    <url>http://maven.apache.org</url>
     1.5    <dependencies>
     1.6      <dependency>
     1.7 -      <groupId>junit</groupId>
     1.8 -      <artifactId>junit</artifactId>
     1.9 -      <version>3.8.1</version>
    1.10 -      <scope>test</scope>
    1.11 -    </dependency>
    1.12 -    <dependency>
    1.13        <groupId>${project.groupId}</groupId>
    1.14        <artifactId>webidor</artifactId>
    1.15        <version>1.0</version>
    1.16 @@ -39,6 +33,19 @@
    1.17        <artifactId>jersey-client</artifactId>
    1.18        <version>1.1.0-ea</version>
    1.19      </dependency>
    1.20 +    <dependency>
    1.21 +      <groupId>com.sun.jersey.test.framework</groupId>
    1.22 +      <artifactId>jersey-test-framework</artifactId>
    1.23 +      <version>1.1.0-ea</version>
    1.24 +      <scope>test</scope>
    1.25 +      <type>jar</type>
    1.26 +    </dependency>
    1.27 +    <dependency>
    1.28 +      <groupId>junit</groupId>
    1.29 +      <artifactId>junit</artifactId>
    1.30 +      <version>4.5</version>
    1.31 +      <scope>test</scope>
    1.32 +    </dependency>
    1.33    </dependencies>
    1.34    <build>
    1.35      <plugins>
    1.36 @@ -57,3 +64,4 @@
    1.37  
    1.38  
    1.39  
    1.40 +
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Aug 30 14:44:49 2009 +0200
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Aug 30 14:56:32 2009 +0200
     2.3 @@ -44,6 +44,7 @@
     2.4  import java.util.List;
     2.5  import java.util.Map;
     2.6  import java.util.Properties;
     2.7 +import java.util.concurrent.Callable;
     2.8  import javax.ws.rs.DefaultValue;
     2.9  import javax.ws.rs.FormParam;
    2.10  import javax.ws.rs.GET;
    2.11 @@ -201,17 +202,30 @@
    2.12      //
    2.13  
    2.14      public static void main(String[] args) throws Exception {
    2.15 -        HttpServer api = Quoridor.start(9998);
    2.16 +        Callable<Void> r = startServers();
    2.17 +
    2.18 +        System.in.read();
    2.19 +        r.call();
    2.20 +        System.exit(0);
    2.21 +    }
    2.22 +
    2.23 +    static Callable<Void> startServers() throws Exception {
    2.24 +        final HttpServer api = Quoridor.start(9998);
    2.25          Client client = new Client();
    2.26          base = client.resource(new URI("http://localhost:9998/api/"));
    2.27  
    2.28 -        HttpServer s = start(9997);
    2.29 +        final HttpServer s = start(9997);
    2.30          System.out.println(
    2.31              "Quoridor started at port 9997\n" + "Hit enter to stop it..."
    2.32          );
    2.33 -        System.in.read();
    2.34 -        s.stop(0);
    2.35 -        System.exit(0);
    2.36 +
    2.37 +        return new Callable<Void>() {
    2.38 +            public Void call() throws Exception {
    2.39 +                s.stop(0);
    2.40 +                api.stop(0);
    2.41 +                return null;
    2.42 +            }
    2.43 +        };
    2.44      }
    2.45  
    2.46      static HttpServer start(int port) throws IOException {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sun Aug 30 14:56:32 2009 +0200
     3.3 @@ -0,0 +1,102 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * The contents of this file are subject to the terms of either the GNU
     3.8 + * General Public License Version 2 only ("GPL") or the Common
     3.9 + * Development and Distribution License("CDDL") (collectively, the
    3.10 + * "License"). You may not use this file except in compliance with the
    3.11 + * License. You can obtain a copy of the License at
    3.12 + * http://www.netbeans.org/cddl-gplv2.html
    3.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.14 + * specific language governing permissions and limitations under the
    3.15 + * License.  When distributing the software, include this License Header
    3.16 + * Notice in each file and include the License file at
    3.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    3.18 + * particular file as subject to the "Classpath" exception as provided
    3.19 + * by Sun in the GPL Version 2 section of the License file that
    3.20 + * accompanied this code. If applicable, add the following below the
    3.21 + * License Header, with the fields enclosed by brackets [] replaced by
    3.22 + * your own identifying information:
    3.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.24 + *
    3.25 + * Contributor(s):
    3.26 + *
    3.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    3.28 + */
    3.29 +
    3.30 +package cz.xelfi.quoridor.freemarkerdor;
    3.31 +
    3.32 +import com.sun.jersey.api.client.Client;
    3.33 +import com.sun.jersey.api.client.WebResource;
    3.34 +import com.sun.jersey.core.header.MediaTypes;
    3.35 +import java.io.File;
    3.36 +import java.io.IOException;
    3.37 +import java.net.URI;
    3.38 +import java.util.concurrent.Callable;
    3.39 +import org.junit.After;
    3.40 +import org.junit.Before;
    3.41 +import org.junit.Test;
    3.42 +import static org.junit.Assert.*;
    3.43 +
    3.44 +/**
    3.45 + *
    3.46 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.47 + */
    3.48 +public class UITest extends Object {
    3.49 +    private File dir;
    3.50 +    private Callable<Void> stop;
    3.51 +    private WebResource webResource;
    3.52 +
    3.53 +    public UITest() throws Exception {
    3.54 +    }
    3.55 +
    3.56 +    @Before
    3.57 +    public void setUp() throws Exception {
    3.58 +        dir = File.createTempFile("quoridor", ".dir");
    3.59 +        dir.delete();
    3.60 +        System.setProperty("quoridor.dir", dir.getPath());
    3.61 +        stop = UI.startServers();
    3.62 +
    3.63 +        Client client = new Client();
    3.64 +        webResource = client.resource(new URI("http://localhost:9997/"));
    3.65 +    }
    3.66 +
    3.67 +    @After
    3.68 +    public void tearDown() throws Exception {
    3.69 +        deleteRec(dir);
    3.70 +        if (stop != null) {
    3.71 +            stop.call();
    3.72 +        }
    3.73 +    }
    3.74 +
    3.75 +    static void deleteRec(File dir) throws IOException {
    3.76 +        if (dir == null) {
    3.77 +            return;
    3.78 +        }
    3.79 +        File[] arr = dir.listFiles();
    3.80 +        if (arr != null) {
    3.81 +            for (File f : arr) {
    3.82 +                deleteRec(f);
    3.83 +            }
    3.84 +        }
    3.85 +        dir.delete();
    3.86 +    }
    3.87 +
    3.88 +    /**
    3.89 +     * Test if a WADL document is available at the relative path
    3.90 +     * "application.wadl".
    3.91 +     */
    3.92 +    @Test public void testApplicationWadl() {
    3.93 +        String serviceWadl = webResource.path("application.wadl").
    3.94 +                accept(MediaTypes.WADL).get(String.class);
    3.95 +        assertTrue(serviceWadl.length() > 0);
    3.96 +    }
    3.97 +
    3.98 +    @Test public void testGetIndexPage() throws Exception {
    3.99 +        String res = webResource.accept("text/html").get(String.class);
   3.100 +        if (res.indexOf("Quoridor") == -1) {
   3.101 +            fail("Wrong index.html:\n" + res);
   3.102 +        }
   3.103 +    }
   3.104 +
   3.105 +}