# HG changeset patch # User Jaroslav Tulach # Date 1251636992 -7200 # Node ID 1cce50d16bb5a5fb5609cc42f6f645021eac6512 # Parent 75074e02f345ac585c671a3ba833e8510d107aa3 Test to verify that we are able to read the main page diff -r 75074e02f345 -r 1cce50d16bb5 freemarkerdor/pom.xml --- a/freemarkerdor/pom.xml Sun Aug 30 14:44:49 2009 +0200 +++ b/freemarkerdor/pom.xml Sun Aug 30 14:56:32 2009 +0200 @@ -14,12 +14,6 @@ http://maven.apache.org - junit - junit - 3.8.1 - test - - ${project.groupId} webidor 1.0 @@ -39,6 +33,19 @@ jersey-client 1.1.0-ea + + com.sun.jersey.test.framework + jersey-test-framework + 1.1.0-ea + test + jar + + + junit + junit + 4.5 + test + @@ -57,3 +64,4 @@ + diff -r 75074e02f345 -r 1cce50d16bb5 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sun Aug 30 14:44:49 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sun Aug 30 14:56:32 2009 +0200 @@ -44,6 +44,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.concurrent.Callable; import javax.ws.rs.DefaultValue; import javax.ws.rs.FormParam; import javax.ws.rs.GET; @@ -201,17 +202,30 @@ // public static void main(String[] args) throws Exception { - HttpServer api = Quoridor.start(9998); + Callable r = startServers(); + + System.in.read(); + r.call(); + System.exit(0); + } + + static Callable startServers() throws Exception { + final HttpServer api = Quoridor.start(9998); Client client = new Client(); base = client.resource(new URI("http://localhost:9998/api/")); - HttpServer s = start(9997); + final HttpServer s = start(9997); System.out.println( "Quoridor started at port 9997\n" + "Hit enter to stop it..." ); - System.in.read(); - s.stop(0); - System.exit(0); + + return new Callable() { + public Void call() throws Exception { + s.stop(0); + api.stop(0); + return null; + } + }; } static HttpServer start(int port) throws IOException { diff -r 75074e02f345 -r 1cce50d16bb5 freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Sun Aug 30 14:56:32 2009 +0200 @@ -0,0 +1,102 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.freemarkerdor; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.core.header.MediaTypes; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.util.concurrent.Callable; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Jaroslav Tulach + */ +public class UITest extends Object { + private File dir; + private Callable stop; + private WebResource webResource; + + public UITest() throws Exception { + } + + @Before + public void setUp() throws Exception { + dir = File.createTempFile("quoridor", ".dir"); + dir.delete(); + System.setProperty("quoridor.dir", dir.getPath()); + stop = UI.startServers(); + + Client client = new Client(); + webResource = client.resource(new URI("http://localhost:9997/")); + } + + @After + public void tearDown() throws Exception { + deleteRec(dir); + if (stop != null) { + stop.call(); + } + } + + static void deleteRec(File dir) throws IOException { + if (dir == null) { + return; + } + File[] arr = dir.listFiles(); + if (arr != null) { + for (File f : arr) { + deleteRec(f); + } + } + dir.delete(); + } + + /** + * Test if a WADL document is available at the relative path + * "application.wadl". + */ + @Test public void testApplicationWadl() { + String serviceWadl = webResource.path("application.wadl"). + accept(MediaTypes.WADL).get(String.class); + assertTrue(serviceWadl.length() > 0); + } + + @Test public void testGetIndexPage() throws Exception { + String res = webResource.accept("text/html").get(String.class); + if (res.indexOf("Quoridor") == -1) { + fail("Wrong index.html:\n" + res); + } + } + +}