webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Sep 2009 21:14:33 +0200
changeset 57 fa12b02023a0
child 75 6802034b7a6f
permissions -rw-r--r--
Separating games by their current state
jaroslav@57
     1
/*
jaroslav@57
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@57
     3
 *
jaroslav@57
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@57
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@57
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@57
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@57
     8
 * License. You can obtain a copy of the License at
jaroslav@57
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@57
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@57
    11
 * specific language governing permissions and limitations under the
jaroslav@57
    12
 * License.  When distributing the software, include this License Header
jaroslav@57
    13
 * Notice in each file and include the License file at
jaroslav@57
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@57
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@57
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@57
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@57
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@57
    19
 * your own identifying information:
jaroslav@57
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@57
    21
 *
jaroslav@57
    22
 * Contributor(s):
jaroslav@57
    23
 *
jaroslav@57
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@57
    25
 */
jaroslav@57
    26
jaroslav@57
    27
package cz.xelfi.quoridor.webidor;
jaroslav@57
    28
jaroslav@57
    29
import com.sun.jersey.api.client.GenericType;
jaroslav@57
    30
import com.sun.jersey.api.client.UniformInterfaceException;
jaroslav@57
    31
import com.sun.jersey.core.header.MediaTypes;
jaroslav@57
    32
import com.sun.jersey.test.framework.JerseyTest;
jaroslav@57
    33
import cz.xelfi.quoridor.Board;
jaroslav@57
    34
import cz.xelfi.quoridor.Move;
jaroslav@57
    35
import cz.xelfi.quoridor.webidor.resources.Games;
jaroslav@57
    36
import java.io.File;
jaroslav@57
    37
import java.io.FileReader;
jaroslav@57
    38
import java.io.IOException;
jaroslav@57
    39
import java.util.List;
jaroslav@57
    40
import java.util.Map;
jaroslav@57
    41
import javax.ws.rs.core.MediaType;
jaroslav@57
    42
import org.junit.Test;
jaroslav@57
    43
import org.w3c.dom.Document;
jaroslav@57
    44
import static org.junit.Assert.*;
jaroslav@57
    45
jaroslav@57
    46
/**
jaroslav@57
    47
 *
jaroslav@57
    48
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@57
    49
 */
jaroslav@57
    50
public class FinishedGameTest extends JerseyTest {
jaroslav@57
    51
    private File dir;
jaroslav@57
    52
jaroslav@57
    53
    public FinishedGameTest() throws Exception {
jaroslav@57
    54
        super("cz.xelfi.quoridor.webidor.resources");
jaroslav@57
    55
    }
jaroslav@57
    56
jaroslav@57
    57
    @Override
jaroslav@57
    58
    public void setUp() throws Exception {
jaroslav@57
    59
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@57
    60
        dir.delete();
jaroslav@57
    61
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@57
    62
        super.setUp();
jaroslav@57
    63
    }
jaroslav@57
    64
jaroslav@57
    65
    @Override
jaroslav@57
    66
    public void tearDown() throws Exception {
jaroslav@57
    67
        super.tearDown();
jaroslav@57
    68
        deleteRec(dir);
jaroslav@57
    69
    }
jaroslav@57
    70
jaroslav@57
    71
    static void deleteRec(File dir) throws IOException {
jaroslav@57
    72
        if (dir == null) {
jaroslav@57
    73
            return;
jaroslav@57
    74
        }
jaroslav@57
    75
        File[] arr = dir.listFiles();
jaroslav@57
    76
        if (arr != null) {
jaroslav@57
    77
            for (File f : arr) {
jaroslav@57
    78
                deleteRec(f);
jaroslav@57
    79
            }
jaroslav@57
    80
        }
jaroslav@57
    81
        dir.delete();
jaroslav@57
    82
    }
jaroslav@57
    83
jaroslav@57
    84
    @Test public void testCreateAGame() throws Exception {
jaroslav@57
    85
        webResource = webResource.path("api");
jaroslav@57
    86
        GameId s = webResource.path("games").queryParam("white", "Jarda")
jaroslav@57
    87
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@57
    88
jaroslav@57
    89
        for (int i = 0; i < 3; i++) {
jaroslav@57
    90
            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@57
    91
            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@57
    92
        }
jaroslav@57
    93
jaroslav@57
    94
        webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@57
    95
        webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
jaroslav@57
    96
jaroslav@57
    97
        for (int i = 0; i < 3; i++) {
jaroslav@57
    98
            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@57
    99
            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@57
   100
        }
jaroslav@57
   101
jaroslav@57
   102
        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jaroslav@57
   103
        assertEquals("BlackWins", GameResult.BLACK_WON, end.getId().getResult());
jaroslav@57
   104
    }
jaroslav@57
   105
jaroslav@57
   106
}