More tests to check behaviour of "UI", querying for text/xml in create new game page
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 08 Sep 2009 15:29:15 +0200
changeset 73b3165f3a9ad7
parent 72 5f081edc8502
child 74 b2af4da1cbbe
More tests to check behaviour of "UI", querying for text/xml in create new game 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	Tue Sep 08 07:54:12 2009 +0200
     1.2 +++ b/freemarkerdor/pom.xml	Tue Sep 08 15:29:15 2009 +0200
     1.3 @@ -100,17 +100,6 @@
     1.4      </plugin>
     1.5    </plugins>
     1.6    </build>
     1.7 -    <version>1.0</version>
     1.8 +    <version>1.1</version>
     1.9  </project>
    1.10  
    1.11 -
    1.12 -
    1.13 -
    1.14 -
    1.15 -
    1.16 -
    1.17 -
    1.18 -
    1.19 -
    1.20 -
    1.21 -
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Tue Sep 08 07:54:12 2009 +0200
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Tue Sep 08 15:29:15 2009 +0200
     2.3 @@ -90,7 +90,7 @@
     2.4      public Response login(
     2.5          @FormParam("name") String name, @FormParam("password") String password
     2.6      ) throws Exception {
     2.7 -        File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd");
     2.8 +        File f = new File(new File(System.getProperty("quoridor.dir")), "passwd");
     2.9          Properties p = new Properties();
    2.10          try {
    2.11              p.load(new FileInputStream(f));
    2.12 @@ -165,22 +165,23 @@
    2.13      @GET
    2.14      @Path("games/create")
    2.15      @Produces(MediaType.TEXT_HTML)
    2.16 -    public Viewable create(
    2.17 +    public Response create(
    2.18          @QueryParam("white") String white,
    2.19          @QueryParam("black") String black
    2.20      ) {
    2.21          Viewable v = checkLogin();
    2.22          if (v != null) {
    2.23 -            return v;
    2.24 +            return Response.status(Response.Status.FORBIDDEN).entity(v).build();
    2.25          }
    2.26  
    2.27          if (user.equals(white) || user.equals(black)) {
    2.28              Object obj =
    2.29                  base.path("games").queryParam("white", white).
    2.30 -                queryParam("black", black).post(Document.class);
    2.31 -            return welcomeImpl();
    2.32 +                queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class);
    2.33 +            return Response.ok(welcomeImpl()).build();
    2.34          } else {
    2.35 -            return welcomeImpl("message", "You (" + user + ") must be white or black!");
    2.36 +            return Response.status(Response.Status.NOT_FOUND).
    2.37 +                entity(welcomeImpl("message", "You (" + user + ") must be white or black!")).build();
    2.38          }
    2.39      }
    2.40  
    2.41 @@ -215,10 +216,12 @@
    2.42      }
    2.43  
    2.44      static Callable<Void> startServers(int port) throws Exception {
    2.45 -        File home = new File(System.getProperty("user.home"));
    2.46 -        File quoridor = new File(home, ".quoridor");
    2.47  
    2.48 -        System.setProperty("quoridor.dir", quoridor.getPath());
    2.49 +        if (System.getProperty("quoridor.dir") == null) {
    2.50 +            File home = new File(System.getProperty("user.home"));
    2.51 +            File quoridor = new File(home, ".quoridor");
    2.52 +            System.setProperty("quoridor.dir", quoridor.getPath());
    2.53 +        }
    2.54  
    2.55          ResourceConfig rc = new PackagesResourceConfig(
    2.56              "cz.xelfi.quoridor.webidor",
     3.1 --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Tue Sep 08 07:54:12 2009 +0200
     3.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Tue Sep 08 15:29:15 2009 +0200
     3.3 @@ -27,15 +27,19 @@
     3.4  package cz.xelfi.quoridor.freemarkerdor;
     3.5  
     3.6  import com.sun.jersey.api.client.Client;
     3.7 +import com.sun.jersey.api.client.ClientResponse;
     3.8  import com.sun.jersey.api.client.WebResource;
     3.9  import com.sun.jersey.core.header.MediaTypes;
    3.10 +import com.sun.jersey.core.util.MultivaluedMapImpl;
    3.11  import java.io.File;
    3.12 +import java.io.FileOutputStream;
    3.13  import java.io.IOException;
    3.14  import java.net.URI;
    3.15  import java.util.Locale;
    3.16  import java.util.concurrent.Callable;
    3.17  import javax.ws.rs.core.Cookie;
    3.18 -import org.junit.After;
    3.19 +import javax.ws.rs.core.MultivaluedMap;
    3.20 +import org.junit.AfterClass;
    3.21  import org.junit.Before;
    3.22  import org.junit.BeforeClass;
    3.23  import org.junit.Test;
    3.24 @@ -46,31 +50,37 @@
    3.25   * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.26   */
    3.27  public class UITest extends Object {
    3.28 -    private File dir;
    3.29 -    private Callable<Void> stop;
    3.30 +    private static File dir;
    3.31 +    private static Callable<Void> stop;
    3.32      private WebResource webResource;
    3.33  
    3.34      public UITest() throws Exception {
    3.35      }
    3.36  
    3.37      @BeforeClass
    3.38 -    public static void localeEnglish() {
    3.39 +    public static void localeEnglish() throws Exception {
    3.40          Locale.setDefault(Locale.ENGLISH);
    3.41 +        dir = File.createTempFile("quoridor", ".dir");
    3.42 +        dir.delete();
    3.43 +        dir.mkdirs();
    3.44 +        System.setProperty("quoridor.dir", dir.getPath());
    3.45 +        stop = UI.startServers(9991);
    3.46 +
    3.47 +        File passwd = new File(dir, "passwd");
    3.48 +        FileOutputStream os = new FileOutputStream(passwd);
    3.49 +        os.write("test=pes\n".getBytes("UTF-8"));
    3.50 +        os.close();
    3.51      }
    3.52  
    3.53      @Before
    3.54      public void setUp() throws Exception {
    3.55 -        dir = File.createTempFile("quoridor", ".dir");
    3.56 -        dir.delete();
    3.57 -        System.setProperty("quoridor.dir", dir.getPath());
    3.58 -        stop = UI.startServers(9991);
    3.59  
    3.60          Client client = new Client();
    3.61          webResource = client.resource(new URI("http://localhost:9991/"));
    3.62      }
    3.63  
    3.64 -    @After
    3.65 -    public void tearDown() throws Exception {
    3.66 +    @AfterClass
    3.67 +    public static void cleanUpAll() throws Exception {
    3.68          deleteRec(dir);
    3.69          if (stop != null) {
    3.70              stop.call();
    3.71 @@ -120,4 +130,52 @@
    3.72          }
    3.73      }
    3.74  
    3.75 +    @Test public void testLogin() throws Exception {
    3.76 +        MultivaluedMap formData = new MultivaluedMapImpl();
    3.77 +        formData.add("name", "test");
    3.78 +        formData.add("password", "pes");
    3.79 +        ClientResponse response = webResource.path("login").
    3.80 +            accept("text/html").type("application/x-www-form-urlencoded").
    3.81 +            post(ClientResponse.class, formData);
    3.82 +        final String res = response.getEntity(String.class);
    3.83 +        assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
    3.84 +        if (res.indexOf("You are logged as test") == -1) {
    3.85 +            fail("res: " + res);
    3.86 +        }
    3.87 +    }
    3.88 +
    3.89 +
    3.90 +    @Test public void testCreateGameWrongUsers() throws Exception {
    3.91 +        ClientResponse res = webResource.path("games/create").
    3.92 +            queryParam("white", "unknown1").
    3.93 +            queryParam("black", "unknown2").
    3.94 +            cookie(Cookie.valueOf("login=test")).
    3.95 +            accept("text/html").
    3.96 +            get(ClientResponse.class);
    3.97 +        final String txt = res.getEntity(String.class);
    3.98 +        assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
    3.99 +        if (txt.indexOf("You (test) must be") == -1) {
   3.100 +            fail(txt);
   3.101 +        }
   3.102 +    }
   3.103 +
   3.104 +    @Test public void testCreateGameOK() throws Exception {
   3.105 +        ClientResponse res = webResource.path("games/create").
   3.106 +            queryParam("white", "unknown1").
   3.107 +            queryParam("black", "test").
   3.108 +            cookie(Cookie.valueOf("login=test")).
   3.109 +            accept("text/html").
   3.110 +            get(ClientResponse.class);
   3.111 +
   3.112 +        final String txt = res.getEntity(String.class);
   3.113 +        assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
   3.114 +
   3.115 +        String[] games = new File(dir, "games").list();
   3.116 +        assertEquals("One game exists", 1, games.length);
   3.117 +
   3.118 +        if (txt.indexOf(games[0]) == -1) {
   3.119 +            fail(games[0] + " expected inside of:\n" + txt);
   3.120 +        }
   3.121 +    }
   3.122 +
   3.123  }