webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 16:04:27 +0200
changeset 52 45fb5f885591
child 79 89bca098e14e
permissions -rw-r--r--
Verifying that empty games can be read
jaroslav@52
     1
/*
jaroslav@52
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@52
     3
 *
jaroslav@52
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@52
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@52
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@52
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@52
     8
 * License. You can obtain a copy of the License at
jaroslav@52
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@52
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@52
    11
 * specific language governing permissions and limitations under the
jaroslav@52
    12
 * License.  When distributing the software, include this License Header
jaroslav@52
    13
 * Notice in each file and include the License file at
jaroslav@52
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@52
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@52
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@52
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@52
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@52
    19
 * your own identifying information:
jaroslav@52
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@52
    21
 *
jaroslav@52
    22
 * Contributor(s):
jaroslav@52
    23
 *
jaroslav@52
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@52
    25
 */
jaroslav@52
    26
jaroslav@52
    27
package cz.xelfi.quoridor.webidor;
jaroslav@52
    28
jaroslav@52
    29
import cz.xelfi.quoridor.webidor.resources.Games;
jaroslav@52
    30
import java.io.File;
jaroslav@52
    31
import java.io.FileOutputStream;
jaroslav@52
    32
import java.io.IOException;
jaroslav@52
    33
import org.junit.After;
jaroslav@52
    34
import org.junit.Before;
jaroslav@52
    35
import org.junit.Test;
jaroslav@52
    36
import static org.junit.Assert.*;
jaroslav@52
    37
jaroslav@52
    38
/**
jaroslav@52
    39
 *
jaroslav@52
    40
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@52
    41
 */
jaroslav@52
    42
public class GamesTest extends Object {
jaroslav@52
    43
    private File dir;
jaroslav@52
    44
jaroslav@52
    45
    @Before
jaroslav@52
    46
    public void setUp() throws Exception {
jaroslav@52
    47
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@52
    48
        dir.delete();
jaroslav@52
    49
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@52
    50
    }
jaroslav@52
    51
jaroslav@52
    52
    @After
jaroslav@52
    53
    public void tearDown() throws Exception {
jaroslav@52
    54
        deleteRec(dir);
jaroslav@52
    55
    }
jaroslav@52
    56
jaroslav@52
    57
    static void deleteRec(File dir) throws IOException {
jaroslav@52
    58
        if (dir == null) {
jaroslav@52
    59
            return;
jaroslav@52
    60
        }
jaroslav@52
    61
        File[] arr = dir.listFiles();
jaroslav@52
    62
        if (arr != null) {
jaroslav@52
    63
            for (File f : arr) {
jaroslav@52
    64
                deleteRec(f);
jaroslav@52
    65
            }
jaroslav@52
    66
        }
jaroslav@52
    67
        dir.delete();
jaroslav@52
    68
    }
jaroslav@52
    69
jaroslav@52
    70
    @Test public void testCreateAGame() throws Exception {
jaroslav@52
    71
        File f = new File(dir, "x");
jaroslav@52
    72
        f.getParentFile().mkdirs();
jaroslav@52
    73
        FileOutputStream os = new FileOutputStream(f);
jaroslav@52
    74
        os.write("# white: W\n# black: B\n# status: IN_PROGRESS\n\n\n".getBytes("UTF-8"));
jaroslav@52
    75
        os.close();
jaroslav@52
    76
jaroslav@52
    77
        Games games = new Games(dir);
jaroslav@52
    78
        Game g = games.getBoardInfo("x");
jaroslav@52
    79
        assertNotNull("Game found", g);
jaroslav@52
    80
        assertNotNull("Board found", g.getBoard());
jaroslav@52
    81
        assertEquals("List of moves is empty", 0, g.getMoves().size());
jaroslav@52
    82
    }
jaroslav@52
    83
jaroslav@52
    84
}