webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 11 Aug 2009 14:26:49 +0200
changeset 41 c94f68ddef59
parent 39 6b889f2717e9
child 46 71e4cf307c93
permissions -rw-r--r--
Simple, freemarker based textual UI
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor;
    28 
    29 import com.sun.jersey.api.client.GenericType;
    30 import com.sun.jersey.api.client.UniformInterfaceException;
    31 import com.sun.jersey.core.header.MediaTypes;
    32 import com.sun.jersey.test.framework.JerseyTest;
    33 import cz.xelfi.quoridor.Board;
    34 import cz.xelfi.quoridor.Move;
    35 import cz.xelfi.quoridor.webidor.resources.Games;
    36 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    37 import java.io.File;
    38 import java.io.FileReader;
    39 import java.io.IOException;
    40 import java.util.List;
    41 import org.junit.Test;
    42 import static org.junit.Assert.*;
    43 
    44 /**
    45  *
    46  * @author Jaroslav Tulach <jtulach@netbeans.org>
    47  */
    48 public class QuoridorTest extends JerseyTest {
    49     private File dir;
    50 
    51     public QuoridorTest() throws Exception {
    52         super("cz.xelfi.quoridor.webidor.resources");
    53     }
    54 
    55     @Override
    56     public void setUp() throws Exception {
    57         dir = File.createTempFile("quoridor", ".dir");
    58         dir.delete();
    59         System.setProperty("quoridor.dir", dir.getPath());
    60         super.setUp();
    61     }
    62 
    63     @Override
    64     public void tearDown() throws Exception {
    65         super.tearDown();
    66         deleteRec(dir);
    67     }
    68 
    69     static void deleteRec(File dir) throws IOException {
    70         if (dir == null) {
    71             return;
    72         }
    73         File[] arr = dir.listFiles();
    74         if (arr != null) {
    75             for (File f : arr) {
    76                 deleteRec(f);
    77             }
    78         }
    79         dir.delete();
    80     }
    81 
    82     /**
    83      * Test if a WADL document is available at the relative path
    84      * "application.wadl".
    85      */
    86     @Test public void testApplicationWadl() {
    87         String serviceWadl = webResource.path("application.wadl").
    88                 accept(MediaTypes.WADL).get(String.class);
    89         assertTrue(serviceWadl.length() > 0);
    90     }
    91 
    92     @Test public void testCreateAGame() throws Exception {
    93         webResource = webResource.path("api");
    94         Game s = webResource.path("games").queryParam("white", "Jarda")
    95                 .queryParam("black", "Jirka").post(Game.class);
    96 
    97         String msg = webResource.path("games").get(String.class);
    98         //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
    99 
   100         GenericType<List<Game>> gType = new GenericType<List<Game>>() {};
   101 
   102         List<Game> games = webResource.path("games").accept("application/json").get(gType);
   103         assertEquals("One game", 1, games.size());
   104         assertEquals("Same white", "Jarda", games.get(0).getWhite());
   105         assertEquals("Same black", "Jirka", games.get(0).getBlack());
   106 
   107         Game s1 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
   108         try {
   109             Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
   110             fail("Not Jarda's turn, previous call shall fail");
   111         } catch (UniformInterfaceException ex) {
   112             // OK
   113         }
   114         try {
   115             Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(Game.class);
   116             fail("Invalid move");
   117         } catch (UniformInterfaceException ex) {
   118             // OK
   119         }
   120         Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(Game.class);
   121         assertNotNull("Successful move", s2);
   122 
   123         File game = new File(new File(dir, "games"), s2.getId());
   124         assertTrue("File for game exists", game.exists());
   125 
   126         char[] arr = new char[4096];
   127         FileReader gameContent = new FileReader(game);
   128         int len = gameContent.read(arr);
   129         String content = new String(arr, 0, len);
   130 
   131         if (!content.contains("# white: Jarda")) {
   132             fail(content);
   133         }
   134         if (!content.contains("# black: Jirka")) {
   135             fail(content);
   136         }
   137         if (!content.contains("N S")) {
   138             fail(content);
   139         }
   140 
   141         Games read = new Games(new File(dir, "games"));
   142         List<Game> readGames = read.getGames();
   143         assertEquals("One game read", 1, readGames.size());
   144         Board board = read.getGames().get(0).getBoard();
   145         assertEquals(1, board.getPlayers().get(0).getRow());
   146         assertEquals(7, board.getPlayers().get(1).getRow());
   147         assertEquals(Move.NORTH, read.getGames().get(0).getMoves().get(0));
   148         assertEquals(Move.SOUTH, read.getGames().get(0).getMoves().get(1));
   149     }
   150 
   151 }