webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 27 Nov 2010 09:10:22 +0100
changeset 274 b5dd175ac6d3
parent 264 d60370059c3c
permissions -rw-r--r--
Create games subdirectory
jaroslav@52
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@52
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@52
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jaroslav@52
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jaroslav@52
    17
 */
jaroslav@52
    18
jaroslav@52
    19
package cz.xelfi.quoridor.webidor;
jaroslav@52
    20
jaroslav@52
    21
import cz.xelfi.quoridor.webidor.resources.Games;
jtulach@82
    22
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jaroslav@52
    23
import java.io.File;
jaroslav@52
    24
import java.io.FileOutputStream;
jaroslav@52
    25
import java.io.IOException;
jtulach@117
    26
import java.util.ResourceBundle;
jaroslav@52
    27
import org.junit.After;
jaroslav@52
    28
import org.junit.Before;
jaroslav@52
    29
import org.junit.Test;
jaroslav@52
    30
import static org.junit.Assert.*;
jaroslav@52
    31
jaroslav@52
    32
/**
jaroslav@52
    33
 *
jaroslav@52
    34
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@52
    35
 */
jaroslav@52
    36
public class GamesTest extends Object {
jaroslav@149
    37
    static {
jaroslav@149
    38
        System.setProperty("JERSEY_HTTP_PORT", "33436");
jaroslav@149
    39
    }
jaroslav@52
    40
    private File dir;
jaroslav@52
    41
jaroslav@52
    42
    @Before
jaroslav@52
    43
    public void setUp() throws Exception {
jaroslav@52
    44
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@274
    45
        deleteRec(dir);
jaroslav@274
    46
        dir.mkdirs();
jaroslav@52
    47
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@52
    48
    }
jaroslav@52
    49
jaroslav@52
    50
    @After
jaroslav@52
    51
    public void tearDown() throws Exception {
jaroslav@52
    52
        deleteRec(dir);
jaroslav@52
    53
    }
jaroslav@52
    54
jaroslav@52
    55
    static void deleteRec(File dir) throws IOException {
jaroslav@52
    56
        if (dir == null) {
jaroslav@52
    57
            return;
jaroslav@52
    58
        }
jaroslav@52
    59
        File[] arr = dir.listFiles();
jaroslav@52
    60
        if (arr != null) {
jaroslav@52
    61
            for (File f : arr) {
jaroslav@52
    62
                deleteRec(f);
jaroslav@52
    63
            }
jaroslav@52
    64
        }
jaroslav@52
    65
        dir.delete();
jaroslav@52
    66
    }
jaroslav@52
    67
jaroslav@52
    68
    @Test public void testCreateAGame() throws Exception {
jaroslav@274
    69
        File f = new File(new File(dir, "games"), "x");
jaroslav@52
    70
        f.getParentFile().mkdirs();
jaroslav@52
    71
        FileOutputStream os = new FileOutputStream(f);
jtulach@79
    72
        os.write("# white: W\n# black: B\n# status: IN_PROGRESS\nN S\n\n".getBytes("UTF-8"));
jaroslav@52
    73
        os.close();
jaroslav@52
    74
jtulach@79
    75
        Thread.sleep(1000);
jtulach@79
    76
        
jtulach@79
    77
        long middle = f.lastModified();
jtulach@79
    78
jtulach@79
    79
        Thread.sleep(1000);
jtulach@79
    80
jaroslav@274
    81
        Games games = new Games(f.getParentFile(), new Quoridor());
jaroslav@162
    82
        Game g = games.getBoardInfo("", "x", -1);
jaroslav@52
    83
        assertNotNull("Game found", g);
jaroslav@52
    84
        assertNotNull("Board found", g.getBoard());
jtulach@79
    85
        assertEquals("List of moves has two", 2, g.getMoves().size());
jtulach@79
    86
jtulach@79
    87
        assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
jaroslav@52
    88
    }
jaroslav@52
    89
jaroslav@114
    90
    @Test public void testLoadGameWithComments() throws Exception {
jaroslav@274
    91
        File f = new File(new File(dir, "games"), "x");
jaroslav@114
    92
        f.getParentFile().mkdirs();
jaroslav@114
    93
        FileOutputStream os = new FileOutputStream(f);
jaroslav@114
    94
        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
    95
        os.close();
jaroslav@114
    96
jaroslav@114
    97
        Thread.sleep(1000);
jaroslav@114
    98
jaroslav@114
    99
        long middle = f.lastModified();
jaroslav@114
   100
jaroslav@114
   101
        Thread.sleep(1000);
jaroslav@114
   102
jaroslav@274
   103
        Games games = new Games(f.getParentFile(), new Quoridor());
jaroslav@162
   104
        Game g = games.getBoardInfo("", "x", -1);
jaroslav@114
   105
        assertNotNull("Game found", g);
jaroslav@114
   106
        assertNotNull("Board found", g.getBoard());
jaroslav@114
   107
        assertEquals("List of moves has two", 2, g.getMoves().size());
jaroslav@114
   108
jaroslav@114
   109
        assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
jaroslav@114
   110
    }
jaroslav@114
   111
jtulach@117
   112
    @Test public void testLoadGameWithInternationalComments() throws Exception {
jaroslav@274
   113
        File f = new File(new File(dir, "games"), "x");
jtulach@117
   114
        f.getParentFile().mkdirs();
jtulach@117
   115
        FileOutputStream os = new FileOutputStream(f);
jtulach@117
   116
        ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
jtulach@117
   117
        String comment = b.getString("COMMENT");
jtulach@117
   118
        os.write(("# white: W\n# black: B\n# status: IN_PROGRESS\nN\n#" +
jtulach@117
   119
                comment + "\n... S # ok move\n\n").getBytes("UTF-8"));
jtulach@117
   120
        os.close();
jtulach@117
   121
jtulach@117
   122
        Thread.sleep(1000);
jtulach@117
   123
jtulach@117
   124
        long middle = f.lastModified();
jtulach@117
   125
jtulach@117
   126
        Thread.sleep(1000);
jtulach@117
   127
jaroslav@274
   128
        Games games = new Games(f.getParentFile(), new Quoridor());
jaroslav@162
   129
        Game g = games.getBoardInfo("", "x", -1);
jtulach@117
   130
        assertNotNull("Game found", g);
jtulach@117
   131
        assertNotNull("Board found", g.getBoard());
jtulach@117
   132
        assertEquals("List of moves has two", 2, g.getMoves().size());
jtulach@117
   133
        String commentRead = g.getMoves().get(0).getComments().get(0).getComment();
jtulach@117
   134
        assertEquals(comment, commentRead);
jtulach@117
   135
jtulach@117
   136
        assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
jtulach@117
   137
    }
jtulach@117
   138
jaroslav@52
   139
}