webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 10 Sep 2009 23:19:40 +0200
changeset 75 6802034b7a6f
parent 57 fa12b02023a0
child 77 d574ac6e44cc
permissions -rw-r--r--
Support for giving up the game
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.test.framework.JerseyTest;
jaroslav@57
    30
import java.io.File;
jaroslav@57
    31
import java.io.IOException;
jaroslav@57
    32
import javax.ws.rs.core.MediaType;
jaroslav@57
    33
import org.junit.Test;
jaroslav@57
    34
import static org.junit.Assert.*;
jaroslav@57
    35
jaroslav@57
    36
/**
jaroslav@57
    37
 *
jaroslav@57
    38
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@57
    39
 */
jaroslav@57
    40
public class FinishedGameTest extends JerseyTest {
jaroslav@57
    41
    private File dir;
jaroslav@57
    42
jaroslav@57
    43
    public FinishedGameTest() throws Exception {
jaroslav@57
    44
        super("cz.xelfi.quoridor.webidor.resources");
jaroslav@57
    45
    }
jaroslav@57
    46
jaroslav@57
    47
    @Override
jaroslav@57
    48
    public void setUp() throws Exception {
jaroslav@57
    49
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@57
    50
        dir.delete();
jaroslav@57
    51
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@57
    52
        super.setUp();
jaroslav@57
    53
    }
jaroslav@57
    54
jaroslav@57
    55
    @Override
jaroslav@57
    56
    public void tearDown() throws Exception {
jaroslav@57
    57
        super.tearDown();
jaroslav@57
    58
        deleteRec(dir);
jaroslav@57
    59
    }
jaroslav@57
    60
jaroslav@57
    61
    static void deleteRec(File dir) throws IOException {
jaroslav@57
    62
        if (dir == null) {
jaroslav@57
    63
            return;
jaroslav@57
    64
        }
jaroslav@57
    65
        File[] arr = dir.listFiles();
jaroslav@57
    66
        if (arr != null) {
jaroslav@57
    67
            for (File f : arr) {
jaroslav@57
    68
                deleteRec(f);
jaroslav@57
    69
            }
jaroslav@57
    70
        }
jaroslav@57
    71
        dir.delete();
jaroslav@57
    72
    }
jaroslav@57
    73
jaroslav@57
    74
    @Test public void testCreateAGame() throws Exception {
jaroslav@57
    75
        webResource = webResource.path("api");
jaroslav@57
    76
        GameId s = webResource.path("games").queryParam("white", "Jarda")
jaroslav@57
    77
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@57
    78
jaroslav@57
    79
        for (int i = 0; i < 3; i++) {
jaroslav@57
    80
            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@57
    81
            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@57
    82
        }
jaroslav@57
    83
jaroslav@57
    84
        webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@57
    85
        webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
jaroslav@57
    86
jaroslav@57
    87
        for (int i = 0; i < 3; i++) {
jaroslav@57
    88
            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@57
    89
            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@57
    90
        }
jaroslav@57
    91
jaroslav@57
    92
        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jaroslav@57
    93
        assertEquals("BlackWins", GameResult.BLACK_WON, end.getId().getResult());
jaroslav@57
    94
    }
jaroslav@57
    95
jtulach@75
    96
    @Test public void testResignAGame() throws Exception {
jtulach@75
    97
        webResource = webResource.path("api");
jtulach@75
    98
        GameId s = webResource.path("games").queryParam("white", "Jarda")
jtulach@75
    99
                .queryParam("black", "Jirka").post(GameId.class);
jtulach@75
   100
jtulach@75
   101
        webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "RESIGN").put(GameId.class);
jtulach@75
   102
        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jtulach@75
   103
        assertEquals("BlackWins", GameResult.BLACK_WON, end.getId().getResult());
jtulach@75
   104
    }
jtulach@75
   105
jaroslav@57
   106
}