webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 13 Sep 2009 16:48:54 +0200
changeset 82 9ac7acee7d9f
parent 79 89bca098e14e
child 100 8b899ed24f9f
permissions -rw-r--r--
Providing REST like authentication
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;
jtulach@82
    30
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jaroslav@52
    31
import java.io.File;
jaroslav@52
    32
import java.io.FileOutputStream;
jaroslav@52
    33
import java.io.IOException;
jaroslav@52
    34
import org.junit.After;
jaroslav@52
    35
import org.junit.Before;
jaroslav@52
    36
import org.junit.Test;
jaroslav@52
    37
import static org.junit.Assert.*;
jaroslav@52
    38
jaroslav@52
    39
/**
jaroslav@52
    40
 *
jaroslav@52
    41
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@52
    42
 */
jaroslav@52
    43
public class GamesTest extends Object {
jaroslav@52
    44
    private File dir;
jaroslav@52
    45
jaroslav@52
    46
    @Before
jaroslav@52
    47
    public void setUp() throws Exception {
jaroslav@52
    48
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@52
    49
        dir.delete();
jaroslav@52
    50
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@52
    51
    }
jaroslav@52
    52
jaroslav@52
    53
    @After
jaroslav@52
    54
    public void tearDown() throws Exception {
jaroslav@52
    55
        deleteRec(dir);
jaroslav@52
    56
    }
jaroslav@52
    57
jaroslav@52
    58
    static void deleteRec(File dir) throws IOException {
jaroslav@52
    59
        if (dir == null) {
jaroslav@52
    60
            return;
jaroslav@52
    61
        }
jaroslav@52
    62
        File[] arr = dir.listFiles();
jaroslav@52
    63
        if (arr != null) {
jaroslav@52
    64
            for (File f : arr) {
jaroslav@52
    65
                deleteRec(f);
jaroslav@52
    66
            }
jaroslav@52
    67
        }
jaroslav@52
    68
        dir.delete();
jaroslav@52
    69
    }
jaroslav@52
    70
jaroslav@52
    71
    @Test public void testCreateAGame() throws Exception {
jaroslav@52
    72
        File f = new File(dir, "x");
jaroslav@52
    73
        f.getParentFile().mkdirs();
jaroslav@52
    74
        FileOutputStream os = new FileOutputStream(f);
jtulach@79
    75
        os.write("# white: W\n# black: B\n# status: IN_PROGRESS\nN S\n\n".getBytes("UTF-8"));
jaroslav@52
    76
        os.close();
jaroslav@52
    77
jtulach@79
    78
        Thread.sleep(1000);
jtulach@79
    79
        
jtulach@79
    80
        long middle = f.lastModified();
jtulach@79
    81
jtulach@79
    82
        Thread.sleep(1000);
jtulach@79
    83
jtulach@82
    84
        Games games = new Games(dir, new Quoridor());
jaroslav@52
    85
        Game g = games.getBoardInfo("x");
jaroslav@52
    86
        assertNotNull("Game found", g);
jaroslav@52
    87
        assertNotNull("Board found", g.getBoard());
jtulach@79
    88
        assertEquals("List of moves has two", 2, g.getMoves().size());
jtulach@79
    89
jtulach@79
    90
        assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
jaroslav@52
    91
    }
jaroslav@52
    92
jaroslav@52
    93
}