webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 08 Nov 2009 10:21:46 +0100
changeset 149 a441b02a638a
parent 117 c1057591a344
child 162 c1bfbe98152b
permissions -rw-r--r--
Changing the port value for each test
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;
jtulach@117
    34
import java.util.ResourceBundle;
jaroslav@52
    35
import org.junit.After;
jaroslav@52
    36
import org.junit.Before;
jaroslav@52
    37
import org.junit.Test;
jaroslav@52
    38
import static org.junit.Assert.*;
jaroslav@52
    39
jaroslav@52
    40
/**
jaroslav@52
    41
 *
jaroslav@52
    42
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@52
    43
 */
jaroslav@52
    44
public class GamesTest extends Object {
jaroslav@149
    45
    static {
jaroslav@149
    46
        System.setProperty("JERSEY_HTTP_PORT", "33436");
jaroslav@149
    47
    }
jaroslav@52
    48
    private File dir;
jaroslav@52
    49
jaroslav@52
    50
    @Before
jaroslav@52
    51
    public void setUp() throws Exception {
jaroslav@52
    52
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@52
    53
        dir.delete();
jaroslav@52
    54
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@52
    55
    }
jaroslav@52
    56
jaroslav@52
    57
    @After
jaroslav@52
    58
    public void tearDown() throws Exception {
jaroslav@52
    59
        deleteRec(dir);
jaroslav@52
    60
    }
jaroslav@52
    61
jaroslav@52
    62
    static void deleteRec(File dir) throws IOException {
jaroslav@52
    63
        if (dir == null) {
jaroslav@52
    64
            return;
jaroslav@52
    65
        }
jaroslav@52
    66
        File[] arr = dir.listFiles();
jaroslav@52
    67
        if (arr != null) {
jaroslav@52
    68
            for (File f : arr) {
jaroslav@52
    69
                deleteRec(f);
jaroslav@52
    70
            }
jaroslav@52
    71
        }
jaroslav@52
    72
        dir.delete();
jaroslav@52
    73
    }
jaroslav@52
    74
jaroslav@52
    75
    @Test public void testCreateAGame() throws Exception {
jaroslav@52
    76
        File f = new File(dir, "x");
jaroslav@52
    77
        f.getParentFile().mkdirs();
jaroslav@52
    78
        FileOutputStream os = new FileOutputStream(f);
jtulach@79
    79
        os.write("# white: W\n# black: B\n# status: IN_PROGRESS\nN S\n\n".getBytes("UTF-8"));
jaroslav@52
    80
        os.close();
jaroslav@52
    81
jtulach@79
    82
        Thread.sleep(1000);
jtulach@79
    83
        
jtulach@79
    84
        long middle = f.lastModified();
jtulach@79
    85
jtulach@79
    86
        Thread.sleep(1000);
jtulach@79
    87
jtulach@82
    88
        Games games = new Games(dir, new Quoridor());
jaroslav@100
    89
        Game g = games.getBoardInfo("x", -1);
jaroslav@52
    90
        assertNotNull("Game found", g);
jaroslav@52
    91
        assertNotNull("Board found", g.getBoard());
jtulach@79
    92
        assertEquals("List of moves has two", 2, g.getMoves().size());
jtulach@79
    93
jtulach@79
    94
        assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
jaroslav@52
    95
    }
jaroslav@52
    96
jaroslav@114
    97
    @Test public void testLoadGameWithComments() throws Exception {
jaroslav@114
    98
        File f = new File(dir, "x");
jaroslav@114
    99
        f.getParentFile().mkdirs();
jaroslav@114
   100
        FileOutputStream os = new FileOutputStream(f);
jaroslav@114
   101
        os.write("# white: W\n# black: B\n# status: IN_PROGRESS\nN #good move\n... S # ok move\n\n".getBytes("UTF-8"));
jaroslav@114
   102
        os.close();
jaroslav@114
   103
jaroslav@114
   104
        Thread.sleep(1000);
jaroslav@114
   105
jaroslav@114
   106
        long middle = f.lastModified();
jaroslav@114
   107
jaroslav@114
   108
        Thread.sleep(1000);
jaroslav@114
   109
jaroslav@114
   110
        Games games = new Games(dir, new Quoridor());
jaroslav@114
   111
        Game g = games.getBoardInfo("x", -1);
jaroslav@114
   112
        assertNotNull("Game found", g);
jaroslav@114
   113
        assertNotNull("Board found", g.getBoard());
jaroslav@114
   114
        assertEquals("List of moves has two", 2, g.getMoves().size());
jaroslav@114
   115
jaroslav@114
   116
        assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
jaroslav@114
   117
    }
jaroslav@114
   118
jtulach@117
   119
    @Test public void testLoadGameWithInternationalComments() throws Exception {
jtulach@117
   120
        File f = new File(dir, "x");
jtulach@117
   121
        f.getParentFile().mkdirs();
jtulach@117
   122
        FileOutputStream os = new FileOutputStream(f);
jtulach@117
   123
        ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
jtulach@117
   124
        String comment = b.getString("COMMENT");
jtulach@117
   125
        os.write(("# white: W\n# black: B\n# status: IN_PROGRESS\nN\n#" +
jtulach@117
   126
                comment + "\n... S # ok move\n\n").getBytes("UTF-8"));
jtulach@117
   127
        os.close();
jtulach@117
   128
jtulach@117
   129
        Thread.sleep(1000);
jtulach@117
   130
jtulach@117
   131
        long middle = f.lastModified();
jtulach@117
   132
jtulach@117
   133
        Thread.sleep(1000);
jtulach@117
   134
jtulach@117
   135
        Games games = new Games(dir, new Quoridor());
jtulach@117
   136
        Game g = games.getBoardInfo("x", -1);
jtulach@117
   137
        assertNotNull("Game found", g);
jtulach@117
   138
        assertNotNull("Board found", g.getBoard());
jtulach@117
   139
        assertEquals("List of moves has two", 2, g.getMoves().size());
jtulach@117
   140
        String commentRead = g.getMoves().get(0).getComments().get(0).getComment();
jtulach@117
   141
        assertEquals(comment, commentRead);
jtulach@117
   142
jtulach@117
   143
        assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
jtulach@117
   144
    }
jtulach@117
   145
jaroslav@52
   146
}