Specifying UTF-8 when reading and writing files
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 01 Oct 2009 06:06:55 +0200
changeset 117c1057591a344
parent 116 5263ee1916e5
child 118 bbe71fd2f13b
Specifying UTF-8 when reading and writing files
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
webidor/src/test/resources/cz/xelfi/quoridor/webidor/TestBundle.properties
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Mon Sep 28 14:53:48 2009 +0200
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Thu Oct 01 06:06:55 2009 +0200
     1.3 @@ -32,10 +32,16 @@
     1.4  import java.io.BufferedReader;
     1.5  import java.io.BufferedWriter;
     1.6  import java.io.File;
     1.7 +import java.io.FileInputStream;
     1.8 +import java.io.FileOutputStream;
     1.9  import java.io.FileReader;
    1.10  import java.io.FileWriter;
    1.11  import java.io.IOException;
    1.12 +import java.io.InputStream;
    1.13 +import java.io.InputStreamReader;
    1.14 +import java.io.OutputStreamWriter;
    1.15  import java.io.PrintWriter;
    1.16 +import java.io.Writer;
    1.17  import java.util.ArrayList;
    1.18  import java.util.Collections;
    1.19  import java.util.Date;
    1.20 @@ -212,7 +218,8 @@
    1.21      private static final Pattern saidWho = Pattern.compile("# *([^ ]*) *@(.*):$");
    1.22  
    1.23      private Game readGame(File f) throws IOException {
    1.24 -        BufferedReader r = new BufferedReader(new FileReader(f));
    1.25 +        InputStream is = new FileInputStream(f);
    1.26 +        BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    1.27          String white = null;
    1.28          String black = null;
    1.29          Game g = null;
    1.30 @@ -308,7 +315,9 @@
    1.31      }
    1.32  
    1.33      final void storeGame(Game g, File f) throws IOException {
    1.34 -        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
    1.35 +        FileOutputStream os = new FileOutputStream(f);
    1.36 +        Writer w = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
    1.37 +        PrintWriter pw = new PrintWriter(w);
    1.38          pw.println("# white: " + g.getId().getWhite());
    1.39          pw.println("# black: " + g.getId().getBlack());
    1.40          pw.println("# status: " + g.getId().getStatus());
    1.41 @@ -351,5 +360,6 @@
    1.42          pw.println();
    1.43          pw.flush();
    1.44          pw.close();
    1.45 +        w.close();
    1.46      }
    1.47  }
     2.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Mon Sep 28 14:53:48 2009 +0200
     2.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Thu Oct 01 06:06:55 2009 +0200
     2.3 @@ -31,6 +31,7 @@
     2.4  import java.io.File;
     2.5  import java.io.FileOutputStream;
     2.6  import java.io.IOException;
     2.7 +import java.util.ResourceBundle;
     2.8  import org.junit.After;
     2.9  import org.junit.Before;
    2.10  import org.junit.Test;
    2.11 @@ -112,4 +113,31 @@
    2.12          assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
    2.13      }
    2.14  
    2.15 +    @Test public void testLoadGameWithInternationalComments() throws Exception {
    2.16 +        File f = new File(dir, "x");
    2.17 +        f.getParentFile().mkdirs();
    2.18 +        FileOutputStream os = new FileOutputStream(f);
    2.19 +        ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
    2.20 +        String comment = b.getString("COMMENT");
    2.21 +        os.write(("# white: W\n# black: B\n# status: IN_PROGRESS\nN\n#" +
    2.22 +                comment + "\n... S # ok move\n\n").getBytes("UTF-8"));
    2.23 +        os.close();
    2.24 +
    2.25 +        Thread.sleep(1000);
    2.26 +
    2.27 +        long middle = f.lastModified();
    2.28 +
    2.29 +        Thread.sleep(1000);
    2.30 +
    2.31 +        Games games = new Games(dir, new Quoridor());
    2.32 +        Game g = games.getBoardInfo("x", -1);
    2.33 +        assertNotNull("Game found", g);
    2.34 +        assertNotNull("Board found", g.getBoard());
    2.35 +        assertEquals("List of moves has two", 2, g.getMoves().size());
    2.36 +        String commentRead = g.getMoves().get(0).getComments().get(0).getComment();
    2.37 +        assertEquals(comment, commentRead);
    2.38 +
    2.39 +        assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
    2.40 +    }
    2.41 +
    2.42  }
     3.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java	Mon Sep 28 14:53:48 2009 +0200
     3.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java	Thu Oct 01 06:06:55 2009 +0200
     3.3 @@ -39,6 +39,7 @@
     3.4  import java.io.FileReader;
     3.5  import java.io.IOException;
     3.6  import java.util.List;
     3.7 +import java.util.ResourceBundle;
     3.8  import javax.ws.rs.core.MediaType;
     3.9  import org.junit.Test;
    3.10  import static org.junit.Assert.*;
    3.11 @@ -109,6 +110,9 @@
    3.12          }
    3.13          Thread.sleep(100);
    3.14  
    3.15 +        ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
    3.16 +        String comment = b.getString("COMMENT");
    3.17 +
    3.18          GameId s1 = webResource.path("games/" + s.getId()).
    3.19              queryParam("loginID", logJarda).
    3.20              queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    3.21 @@ -119,7 +123,7 @@
    3.22  
    3.23          GameId comment2 = webResource.path("games/" + s.getId()).
    3.24              queryParam("loginID", logJirka).
    3.25 -            queryParam("player", "Jirka").queryParam("comment", "I love it too!").put(GameId.class);
    3.26 +            queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
    3.27          GameId s2 = webResource.path("games/" + s.getId()).
    3.28              queryParam("loginID", logJirka).
    3.29              queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    3.30 @@ -144,7 +148,7 @@
    3.31          if (!content.contains("I like")) {
    3.32              fail(content);
    3.33          }
    3.34 -        if (!content.contains("I love")) {
    3.35 +        if (!content.contains(comment)) {
    3.36              fail(content);
    3.37          }
    3.38  
    3.39 @@ -166,7 +170,7 @@
    3.40          if (!cmnts.get(0).getComment().contains("I like")) {
    3.41              fail();
    3.42          }
    3.43 -        if (!cmnts.get(1).getComment().contains("I love")) {
    3.44 +        if (!cmnts.get(1).getComment().contains(comment)) {
    3.45              fail();
    3.46          }
    3.47  
    3.48 @@ -189,7 +193,7 @@
    3.49          if (!cmnts.get(0).getComment().contains("I like")) {
    3.50              fail();
    3.51          }
    3.52 -        if (!cmnts.get(1).getComment().contains("I love")) {
    3.53 +        if (!cmnts.get(1).getComment().contains(comment)) {
    3.54              fail();
    3.55          }
    3.56      }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/webidor/src/test/resources/cz/xelfi/quoridor/webidor/TestBundle.properties	Thu Oct 01 06:06:55 2009 +0200
     4.3 @@ -0,0 +1,3 @@
     4.4 +
     4.5 +
     4.6 +COMMENT=\u017Dlu\u0165ou\u010Dk\u00FD k\u016F\u0148 \u010Dm\u00E1ral po \u0161p\u00EDn\u011B