webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 29 Jul 2009 08:19:35 +0200
changeset 37 782d925cb5a1
parent 35 2e85dd878f04
child 39 6b889f2717e9
permissions -rw-r--r--
Games are being stored in a file on disk
     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 java.io.File;
    34 import java.io.FileReader;
    35 import java.io.IOException;
    36 import java.util.Arrays;
    37 import java.util.List;
    38 import org.junit.Test;
    39 import static org.junit.Assert.*;
    40 
    41 /**
    42  *
    43  * @author Jaroslav Tulach <jtulach@netbeans.org>
    44  */
    45 public class QuoridorTest extends JerseyTest {
    46     private File dir;
    47 
    48     public QuoridorTest() throws Exception {
    49         super("cz.xelfi.quoridor.webidor.resources");
    50     }
    51 
    52     @Override
    53     public void setUp() throws Exception {
    54         dir = File.createTempFile("quoridor", ".dir");
    55         dir.delete();
    56         System.setProperty("quoridor.dir", dir.getPath());
    57         super.setUp();
    58     }
    59 
    60     @Override
    61     public void tearDown() throws Exception {
    62         super.tearDown();
    63         deleteRec(dir);
    64     }
    65 
    66     static void deleteRec(File dir) throws IOException {
    67         if (dir == null) {
    68             return;
    69         }
    70         File[] arr = dir.listFiles();
    71         if (arr != null) {
    72             for (File f : arr) {
    73                 deleteRec(f);
    74             }
    75         }
    76         dir.delete();
    77     }
    78 
    79     /**
    80      * Test if a WADL document is available at the relative path
    81      * "application.wadl".
    82      */
    83     @Test public void testApplicationWadl() {
    84         String serviceWadl = webResource.path("application.wadl").
    85                 accept(MediaTypes.WADL).get(String.class);
    86         assertTrue(serviceWadl.length() > 0);
    87     }
    88 
    89     @Test public void testCreateAGame() throws Exception {
    90         Game s = webResource.path("games").queryParam("white", "Jarda")
    91                 .queryParam("black", "Jirka").post(Game.class);
    92 
    93         String msg = webResource.path("games").get(String.class);
    94         //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
    95 
    96         GenericType<List<Game>> gType = new GenericType<List<Game>>() {};
    97 
    98         List<Game> games = webResource.path("games").accept("application/json").get(gType);
    99         assertEquals("One game", 1, games.size());
   100         assertEquals("Same white", "Jarda", games.get(0).getWhite());
   101         assertEquals("Same black", "Jirka", games.get(0).getBlack());
   102 
   103         Game s1 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
   104         try {
   105             Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
   106             fail("Not Jarda's turn, previous call shall fail");
   107         } catch (UniformInterfaceException ex) {
   108             // OK
   109         }
   110         try {
   111             Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(Game.class);
   112             fail("Invalid move");
   113         } catch (UniformInterfaceException ex) {
   114             // OK
   115         }
   116         Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(Game.class);
   117         assertNotNull("Successful move", s2);
   118 
   119         File game = new File(new File(dir, "games"), s2.getId());
   120         assertTrue("File for game exists", game.exists());
   121 
   122         char[] arr = new char[4096];
   123         FileReader gameContent = new FileReader(game);
   124         int len = gameContent.read(arr);
   125         String content = new String(arr, 0, len);
   126 
   127         if (!content.contains("# white: Jarda")) {
   128             fail(content);
   129         }
   130         if (!content.contains("# black: Jirka")) {
   131             fail(content);
   132         }
   133         if (!content.contains("N S")) {
   134             fail(content);
   135         }
   136     }
   137 
   138 }