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