Verifying that empty games can be read
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 16:04:27 +0200
changeset 5245fb5f885591
parent 51 8358ef0d29d7
child 53 ccc325a936cc
Verifying that empty games can be read
webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Sun Aug 30 15:19:24 2009 +0200
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Sun Aug 30 16:04:27 2009 +0200
     1.3 @@ -106,8 +106,10 @@
     1.4          @Override
     1.5          public List<Move> unmarshal(String[] arr) throws Exception {
     1.6              List<Move> res = new ArrayList<Move>();
     1.7 -            for (String v : arr) {
     1.8 -                res.add(Move.valueOf(v));
     1.9 +            if (arr != null) {
    1.10 +                for (String v : arr) {
    1.11 +                    res.add(Move.valueOf(v));
    1.12 +                }
    1.13              }
    1.14              return res;
    1.15          }
    1.16 @@ -115,8 +117,10 @@
    1.17          @Override
    1.18          public String[] marshal(List<Move> arr) throws Exception {
    1.19              List<String> res = new ArrayList<String>();
    1.20 -            for (Move m : arr) {
    1.21 -                res.add(m.toString()); 
    1.22 +            if (arr != null) {
    1.23 +                for (Move m : arr) {
    1.24 +                    res.add(m.toString());
    1.25 +                }
    1.26              }
    1.27              return res.toArray(new String[0]);
    1.28          }
    1.29 @@ -126,12 +130,12 @@
    1.30  
    1.31          @Override
    1.32          public Board unmarshal(String v) throws Exception {
    1.33 -            return Board.valueOf(v);
    1.34 +            return v == null ? null : Board.valueOf(v);
    1.35          }
    1.36  
    1.37          @Override
    1.38          public String marshal(Board v) throws Exception {
    1.39 -            return v.toString();
    1.40 +            return v == null ? null : v.toString();
    1.41          }
    1.42  
    1.43      }
     2.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sun Aug 30 15:19:24 2009 +0200
     2.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sun Aug 30 16:04:27 2009 +0200
     2.3 @@ -33,6 +33,7 @@
     2.4  import com.sun.net.httpserver.HttpServer;
     2.5  import java.io.File;
     2.6  import java.io.IOException;
     2.7 +import java.net.ServerSocket;
     2.8  import javax.ws.rs.Path;
     2.9  
    2.10  /**
    2.11 @@ -77,6 +78,11 @@
    2.12      }
    2.13  
    2.14      public static HttpServer start(int port) throws IOException {
    2.15 +        if (port == -1) {
    2.16 +            ServerSocket ss = new ServerSocket(0);
    2.17 +            port =ss.getLocalPort();
    2.18 +            ss.close();
    2.19 +        }
    2.20          final String baseUri = "http://localhost:" + port + "/";
    2.21  
    2.22          File home = new File(System.getProperty("user.home"));
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Sun Aug 30 16:04:27 2009 +0200
     3.3 @@ -0,0 +1,84 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * The contents of this file are subject to the terms of either the GNU
     3.8 + * General Public License Version 2 only ("GPL") or the Common
     3.9 + * Development and Distribution License("CDDL") (collectively, the
    3.10 + * "License"). You may not use this file except in compliance with the
    3.11 + * License. You can obtain a copy of the License at
    3.12 + * http://www.netbeans.org/cddl-gplv2.html
    3.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.14 + * specific language governing permissions and limitations under the
    3.15 + * License.  When distributing the software, include this License Header
    3.16 + * Notice in each file and include the License file at
    3.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    3.18 + * particular file as subject to the "Classpath" exception as provided
    3.19 + * by Sun in the GPL Version 2 section of the License file that
    3.20 + * accompanied this code. If applicable, add the following below the
    3.21 + * License Header, with the fields enclosed by brackets [] replaced by
    3.22 + * your own identifying information:
    3.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.24 + *
    3.25 + * Contributor(s):
    3.26 + *
    3.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    3.28 + */
    3.29 +
    3.30 +package cz.xelfi.quoridor.webidor;
    3.31 +
    3.32 +import cz.xelfi.quoridor.webidor.resources.Games;
    3.33 +import java.io.File;
    3.34 +import java.io.FileOutputStream;
    3.35 +import java.io.IOException;
    3.36 +import org.junit.After;
    3.37 +import org.junit.Before;
    3.38 +import org.junit.Test;
    3.39 +import static org.junit.Assert.*;
    3.40 +
    3.41 +/**
    3.42 + *
    3.43 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.44 + */
    3.45 +public class GamesTest extends Object {
    3.46 +    private File dir;
    3.47 +
    3.48 +    @Before
    3.49 +    public void setUp() throws Exception {
    3.50 +        dir = File.createTempFile("quoridor", ".dir");
    3.51 +        dir.delete();
    3.52 +        System.setProperty("quoridor.dir", dir.getPath());
    3.53 +    }
    3.54 +
    3.55 +    @After
    3.56 +    public void tearDown() throws Exception {
    3.57 +        deleteRec(dir);
    3.58 +    }
    3.59 +
    3.60 +    static void deleteRec(File dir) throws IOException {
    3.61 +        if (dir == null) {
    3.62 +            return;
    3.63 +        }
    3.64 +        File[] arr = dir.listFiles();
    3.65 +        if (arr != null) {
    3.66 +            for (File f : arr) {
    3.67 +                deleteRec(f);
    3.68 +            }
    3.69 +        }
    3.70 +        dir.delete();
    3.71 +    }
    3.72 +
    3.73 +    @Test public void testCreateAGame() throws Exception {
    3.74 +        File f = new File(dir, "x");
    3.75 +        f.getParentFile().mkdirs();
    3.76 +        FileOutputStream os = new FileOutputStream(f);
    3.77 +        os.write("# white: W\n# black: B\n# status: IN_PROGRESS\n\n\n".getBytes("UTF-8"));
    3.78 +        os.close();
    3.79 +
    3.80 +        Games games = new Games(dir);
    3.81 +        Game g = games.getBoardInfo("x");
    3.82 +        assertNotNull("Game found", g);
    3.83 +        assertNotNull("Board found", g.getBoard());
    3.84 +        assertEquals("List of moves is empty", 0, g.getMoves().size());
    3.85 +    }
    3.86 +
    3.87 +}