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