webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
changeset 57 fa12b02023a0
child 75 6802034b7a6f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Fri Sep 04 21:14:33 2009 +0200
     1.3 @@ -0,0 +1,106 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.webidor;
    1.31 +
    1.32 +import com.sun.jersey.api.client.GenericType;
    1.33 +import com.sun.jersey.api.client.UniformInterfaceException;
    1.34 +import com.sun.jersey.core.header.MediaTypes;
    1.35 +import com.sun.jersey.test.framework.JerseyTest;
    1.36 +import cz.xelfi.quoridor.Board;
    1.37 +import cz.xelfi.quoridor.Move;
    1.38 +import cz.xelfi.quoridor.webidor.resources.Games;
    1.39 +import java.io.File;
    1.40 +import java.io.FileReader;
    1.41 +import java.io.IOException;
    1.42 +import java.util.List;
    1.43 +import java.util.Map;
    1.44 +import javax.ws.rs.core.MediaType;
    1.45 +import org.junit.Test;
    1.46 +import org.w3c.dom.Document;
    1.47 +import static org.junit.Assert.*;
    1.48 +
    1.49 +/**
    1.50 + *
    1.51 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.52 + */
    1.53 +public class FinishedGameTest extends JerseyTest {
    1.54 +    private File dir;
    1.55 +
    1.56 +    public FinishedGameTest() throws Exception {
    1.57 +        super("cz.xelfi.quoridor.webidor.resources");
    1.58 +    }
    1.59 +
    1.60 +    @Override
    1.61 +    public void setUp() throws Exception {
    1.62 +        dir = File.createTempFile("quoridor", ".dir");
    1.63 +        dir.delete();
    1.64 +        System.setProperty("quoridor.dir", dir.getPath());
    1.65 +        super.setUp();
    1.66 +    }
    1.67 +
    1.68 +    @Override
    1.69 +    public void tearDown() throws Exception {
    1.70 +        super.tearDown();
    1.71 +        deleteRec(dir);
    1.72 +    }
    1.73 +
    1.74 +    static void deleteRec(File dir) throws IOException {
    1.75 +        if (dir == null) {
    1.76 +            return;
    1.77 +        }
    1.78 +        File[] arr = dir.listFiles();
    1.79 +        if (arr != null) {
    1.80 +            for (File f : arr) {
    1.81 +                deleteRec(f);
    1.82 +            }
    1.83 +        }
    1.84 +        dir.delete();
    1.85 +    }
    1.86 +
    1.87 +    @Test public void testCreateAGame() throws Exception {
    1.88 +        webResource = webResource.path("api");
    1.89 +        GameId s = webResource.path("games").queryParam("white", "Jarda")
    1.90 +                .queryParam("black", "Jirka").post(GameId.class);
    1.91 +
    1.92 +        for (int i = 0; i < 3; i++) {
    1.93 +            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    1.94 +            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    1.95 +        }
    1.96 +
    1.97 +        webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    1.98 +        webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
    1.99 +
   1.100 +        for (int i = 0; i < 3; i++) {
   1.101 +            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   1.102 +            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   1.103 +        }
   1.104 +
   1.105 +        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   1.106 +        assertEquals("BlackWins", GameResult.BLACK_WON, end.getId().getResult());
   1.107 +    }
   1.108 +
   1.109 +}