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