# HG changeset patch # User Jaroslav Tulach # Date 1254370015 -7200 # Node ID c1057591a344ed7ca141ecddbc39d20ab7898df1 # Parent 5263ee1916e50f8af1acb06046445c5d7295ae6b Specifying UTF-8 when reading and writing files diff -r 5263ee1916e5 -r c1057591a344 webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Mon Sep 28 14:53:48 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Thu Oct 01 06:06:55 2009 +0200 @@ -32,10 +32,16 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.io.Writer; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -212,7 +218,8 @@ private static final Pattern saidWho = Pattern.compile("# *([^ ]*) *@(.*):$"); private Game readGame(File f) throws IOException { - BufferedReader r = new BufferedReader(new FileReader(f)); + InputStream is = new FileInputStream(f); + BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8")); String white = null; String black = null; Game g = null; @@ -308,7 +315,9 @@ } final void storeGame(Game g, File f) throws IOException { - PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f))); + FileOutputStream os = new FileOutputStream(f); + Writer w = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); + PrintWriter pw = new PrintWriter(w); pw.println("# white: " + g.getId().getWhite()); pw.println("# black: " + g.getId().getBlack()); pw.println("# status: " + g.getId().getStatus()); @@ -351,5 +360,6 @@ pw.println(); pw.flush(); pw.close(); + w.close(); } } diff -r 5263ee1916e5 -r c1057591a344 webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java Mon Sep 28 14:53:48 2009 +0200 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java Thu Oct 01 06:06:55 2009 +0200 @@ -31,6 +31,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.ResourceBundle; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -112,4 +113,31 @@ assertEquals("Last move is last touch of the file", middle, g.getId().getModified()); } + @Test public void testLoadGameWithInternationalComments() throws Exception { + File f = new File(dir, "x"); + f.getParentFile().mkdirs(); + FileOutputStream os = new FileOutputStream(f); + ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle"); + String comment = b.getString("COMMENT"); + os.write(("# white: W\n# black: B\n# status: IN_PROGRESS\nN\n#" + + comment + "\n... S # ok move\n\n").getBytes("UTF-8")); + os.close(); + + Thread.sleep(1000); + + long middle = f.lastModified(); + + Thread.sleep(1000); + + Games games = new Games(dir, new Quoridor()); + Game g = games.getBoardInfo("x", -1); + assertNotNull("Game found", g); + assertNotNull("Board found", g.getBoard()); + assertEquals("List of moves has two", 2, g.getMoves().size()); + String commentRead = g.getMoves().get(0).getComments().get(0).getComment(); + assertEquals(comment, commentRead); + + assertEquals("Last move is last touch of the file", middle, g.getId().getModified()); + } + } diff -r 5263ee1916e5 -r c1057591a344 webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java Mon Sep 28 14:53:48 2009 +0200 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java Thu Oct 01 06:06:55 2009 +0200 @@ -39,6 +39,7 @@ import java.io.FileReader; import java.io.IOException; import java.util.List; +import java.util.ResourceBundle; import javax.ws.rs.core.MediaType; import org.junit.Test; import static org.junit.Assert.*; @@ -109,6 +110,9 @@ } Thread.sleep(100); + ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle"); + String comment = b.getString("COMMENT"); + GameId s1 = webResource.path("games/" + s.getId()). queryParam("loginID", logJarda). queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class); @@ -119,7 +123,7 @@ GameId comment2 = webResource.path("games/" + s.getId()). queryParam("loginID", logJirka). - queryParam("player", "Jirka").queryParam("comment", "I love it too!").put(GameId.class); + queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class); GameId s2 = webResource.path("games/" + s.getId()). queryParam("loginID", logJirka). queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class); @@ -144,7 +148,7 @@ if (!content.contains("I like")) { fail(content); } - if (!content.contains("I love")) { + if (!content.contains(comment)) { fail(content); } @@ -166,7 +170,7 @@ if (!cmnts.get(0).getComment().contains("I like")) { fail(); } - if (!cmnts.get(1).getComment().contains("I love")) { + if (!cmnts.get(1).getComment().contains(comment)) { fail(); } @@ -189,7 +193,7 @@ if (!cmnts.get(0).getComment().contains("I like")) { fail(); } - if (!cmnts.get(1).getComment().contains("I love")) { + if (!cmnts.get(1).getComment().contains(comment)) { fail(); } } diff -r 5263ee1916e5 -r c1057591a344 webidor/src/test/resources/cz/xelfi/quoridor/webidor/TestBundle.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/test/resources/cz/xelfi/quoridor/webidor/TestBundle.properties Thu Oct 01 06:06:55 2009 +0200 @@ -0,0 +1,3 @@ + + +COMMENT=\u017Dlu\u0165ou\u010Dk\u00FD k\u016F\u0148 \u010Dm\u00E1ral po \u0161p\u00EDn\u011B