# HG changeset patch # User Jaroslav Tulach # Date 1251641067 -7200 # Node ID 45fb5f885591f83d4beff8914ec8a3f72ec0b489 # Parent 8358ef0d29d7416d18efe3cfb59a98262500de70 Verifying that empty games can be read diff -r 8358ef0d29d7 -r 45fb5f885591 webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Sun Aug 30 15:19:24 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Sun Aug 30 16:04:27 2009 +0200 @@ -106,8 +106,10 @@ @Override public List unmarshal(String[] arr) throws Exception { List res = new ArrayList(); - for (String v : arr) { - res.add(Move.valueOf(v)); + if (arr != null) { + for (String v : arr) { + res.add(Move.valueOf(v)); + } } return res; } @@ -115,8 +117,10 @@ @Override public String[] marshal(List arr) throws Exception { List res = new ArrayList(); - for (Move m : arr) { - res.add(m.toString()); + if (arr != null) { + for (Move m : arr) { + res.add(m.toString()); + } } return res.toArray(new String[0]); } @@ -126,12 +130,12 @@ @Override public Board unmarshal(String v) throws Exception { - return Board.valueOf(v); + return v == null ? null : Board.valueOf(v); } @Override public String marshal(Board v) throws Exception { - return v.toString(); + return v == null ? null : v.toString(); } } diff -r 8358ef0d29d7 -r 45fb5f885591 webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Sun Aug 30 15:19:24 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Sun Aug 30 16:04:27 2009 +0200 @@ -33,6 +33,7 @@ import com.sun.net.httpserver.HttpServer; import java.io.File; import java.io.IOException; +import java.net.ServerSocket; import javax.ws.rs.Path; /** @@ -77,6 +78,11 @@ } public static HttpServer start(int port) throws IOException { + if (port == -1) { + ServerSocket ss = new ServerSocket(0); + port =ss.getLocalPort(); + ss.close(); + } final String baseUri = "http://localhost:" + port + "/"; File home = new File(System.getProperty("user.home")); diff -r 8358ef0d29d7 -r 45fb5f885591 webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java Sun Aug 30 16:04:27 2009 +0200 @@ -0,0 +1,84 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import cz.xelfi.quoridor.webidor.resources.Games; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Jaroslav Tulach + */ +public class GamesTest extends Object { + private File dir; + + @Before + public void setUp() throws Exception { + dir = File.createTempFile("quoridor", ".dir"); + dir.delete(); + System.setProperty("quoridor.dir", dir.getPath()); + } + + @After + public void tearDown() throws Exception { + deleteRec(dir); + } + + static void deleteRec(File dir) throws IOException { + if (dir == null) { + return; + } + File[] arr = dir.listFiles(); + if (arr != null) { + for (File f : arr) { + deleteRec(f); + } + } + dir.delete(); + } + + @Test public void testCreateAGame() throws Exception { + File f = new File(dir, "x"); + f.getParentFile().mkdirs(); + FileOutputStream os = new FileOutputStream(f); + os.write("# white: W\n# black: B\n# status: IN_PROGRESS\n\n\n".getBytes("UTF-8")); + os.close(); + + Games games = new Games(dir); + Game g = games.getBoardInfo("x"); + assertNotNull("Game found", g); + assertNotNull("Board found", g.getBoard()); + assertEquals("List of moves is empty", 0, g.getMoves().size()); + } + +}