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
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor;
    28 
    29 import cz.xelfi.quoridor.webidor.resources.Games;
    30 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    31 import java.io.File;
    32 import java.io.FileOutputStream;
    33 import java.io.IOException;
    34 import java.util.ResourceBundle;
    35 import org.junit.After;
    36 import org.junit.Before;
    37 import org.junit.Test;
    38 import static org.junit.Assert.*;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 public class GamesTest extends Object {
    45     private File dir;
    46 
    47     @Before
    48     public void setUp() throws Exception {
    49         dir = File.createTempFile("quoridor", ".dir");
    50         dir.delete();
    51         System.setProperty("quoridor.dir", dir.getPath());
    52     }
    53 
    54     @After
    55     public void tearDown() throws Exception {
    56         deleteRec(dir);
    57     }
    58 
    59     static void deleteRec(File dir) throws IOException {
    60         if (dir == null) {
    61             return;
    62         }
    63         File[] arr = dir.listFiles();
    64         if (arr != null) {
    65             for (File f : arr) {
    66                 deleteRec(f);
    67             }
    68         }
    69         dir.delete();
    70     }
    71 
    72     @Test public void testCreateAGame() throws Exception {
    73         File f = new File(dir, "x");
    74         f.getParentFile().mkdirs();
    75         FileOutputStream os = new FileOutputStream(f);
    76         os.write("# white: W\n# black: B\n# status: IN_PROGRESS\nN S\n\n".getBytes("UTF-8"));
    77         os.close();
    78 
    79         Thread.sleep(1000);
    80         
    81         long middle = f.lastModified();
    82 
    83         Thread.sleep(1000);
    84 
    85         Games games = new Games(dir, new Quoridor());
    86         Game g = games.getBoardInfo("x", -1);
    87         assertNotNull("Game found", g);
    88         assertNotNull("Board found", g.getBoard());
    89         assertEquals("List of moves has two", 2, g.getMoves().size());
    90 
    91         assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
    92     }
    93 
    94     @Test public void testLoadGameWithComments() throws Exception {
    95         File f = new File(dir, "x");
    96         f.getParentFile().mkdirs();
    97         FileOutputStream os = new FileOutputStream(f);
    98         os.write("# white: W\n# black: B\n# status: IN_PROGRESS\nN #good move\n... S # ok move\n\n".getBytes("UTF-8"));
    99         os.close();
   100 
   101         Thread.sleep(1000);
   102 
   103         long middle = f.lastModified();
   104 
   105         Thread.sleep(1000);
   106 
   107         Games games = new Games(dir, new Quoridor());
   108         Game g = games.getBoardInfo("x", -1);
   109         assertNotNull("Game found", g);
   110         assertNotNull("Board found", g.getBoard());
   111         assertEquals("List of moves has two", 2, g.getMoves().size());
   112 
   113         assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
   114     }
   115 
   116     @Test public void testLoadGameWithInternationalComments() throws Exception {
   117         File f = new File(dir, "x");
   118         f.getParentFile().mkdirs();
   119         FileOutputStream os = new FileOutputStream(f);
   120         ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
   121         String comment = b.getString("COMMENT");
   122         os.write(("# white: W\n# black: B\n# status: IN_PROGRESS\nN\n#" +
   123                 comment + "\n... S # ok move\n\n").getBytes("UTF-8"));
   124         os.close();
   125 
   126         Thread.sleep(1000);
   127 
   128         long middle = f.lastModified();
   129 
   130         Thread.sleep(1000);
   131 
   132         Games games = new Games(dir, new Quoridor());
   133         Game g = games.getBoardInfo("x", -1);
   134         assertNotNull("Game found", g);
   135         assertNotNull("Board found", g.getBoard());
   136         assertEquals("List of moves has two", 2, g.getMoves().size());
   137         String commentRead = g.getMoves().get(0).getComments().get(0).getComment();
   138         assertEquals(comment, commentRead);
   139 
   140         assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
   141     }
   142 
   143 }