Supress invalid XML characters
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 27 Nov 2010 07:52:19 +0100
changeset 272215b417aac98
parent 271 aa1c63b58149
child 273 4748246035de
Supress invalid XML characters
freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
freemarkerdor/src/test/resources/cz/xelfi/quoridor/freemarkerdor/wrong.encoding
pom.xml
wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java
     1.1 --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sat Oct 23 22:51:43 2010 +0200
     1.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sat Nov 27 07:52:19 2010 +0100
     1.3 @@ -24,8 +24,6 @@
     1.4  import com.sun.jersey.core.util.MultivaluedMapImpl;
     1.5  import com.sun.net.httpserver.HttpServer;
     1.6  import cz.xelfi.quoridor.webidor.resources.Quoridor;
     1.7 -import cz.xelfi.quoridor.statistics.resources.Statistics;
     1.8 -import java.awt.Image;
     1.9  import java.io.File;
    1.10  import java.io.FileOutputStream;
    1.11  import java.io.IOException;
    1.12 @@ -53,6 +51,7 @@
    1.13      private static HttpServer stopAPI;
    1.14      private static HttpServer stopStatistics;
    1.15      private static Callable<Void> stop;
    1.16 +
    1.17      private WebResource webResource;
    1.18      private WebResource apiResource;
    1.19      private WebResource statResource;
    1.20 @@ -66,6 +65,11 @@
    1.21          dir = File.createTempFile("quoridor", ".dir");
    1.22          dir.delete();
    1.23          dir.mkdirs();
    1.24 +        
    1.25 +        File games = new File(dir, "games");
    1.26 +        copyResource("wrong.encoding", new File(games, "fdcc275c-55ad-4b7e-a776-bde3876b2f2c"));
    1.27 +        
    1.28 +        
    1.29          System.setProperty("quoridor.dir", dir.getPath());
    1.30          stopAPI = Quoridor.start(9990);
    1.31          stopStatistics = Quoridor.start(9992);
    1.32 @@ -190,6 +194,9 @@
    1.33      }
    1.34  
    1.35      @Test public void testCreateGameOK() throws Exception {
    1.36 +        int origGames = new File(dir, "games").list().length;
    1.37 +        
    1.38 +        
    1.39          String logTest = apiResource.path("login").
    1.40              queryParam("name", "test").
    1.41              queryParam("password", "pes").
    1.42 @@ -211,7 +218,7 @@
    1.43          assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
    1.44  
    1.45          String[] games = new File(dir, "games").list();
    1.46 -        assertEquals("One game exists", 1, games.length);
    1.47 +        assertEquals("One new game", origGames + 1, games.length);
    1.48  
    1.49          if (txt.indexOf(games[0]) == -1) {
    1.50              fail(games[0] + " expected inside of:\n" + txt);
    1.51 @@ -268,5 +275,49 @@
    1.52  //            fail("There shall be difference in the streams. Read bytes " + cnt);
    1.53          }
    1.54      }
    1.55 +    
    1.56 +    @Test public void testGetWrongGame() throws Exception {
    1.57 +        String logTest = apiResource.path("login").
    1.58 +            queryParam("name", "test").
    1.59 +            queryParam("password", "pes").
    1.60 +            accept(MediaType.TEXT_PLAIN).
    1.61 +            put(String.class);
    1.62 +        
    1.63 +        String xml = apiResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
    1.64 +            queryParam("name", "test").
    1.65 +            queryParam("password", "pes").
    1.66 +            accept(MediaType.TEXT_XML).
    1.67 +            get(String.class);
    1.68 +        
    1.69 +        if (xml.indexOf((char)0x1b) >= 0) {
    1.70 +            fail(xml.replace((char)0x1b, 'X'));
    1.71 +        }
    1.72 +        
    1.73 +        ClientResponse res = webResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
    1.74 +            queryParam("white", "Jarda").
    1.75 +            queryParam("black", "test").
    1.76 +            cookie(Cookie.valueOf("login=" + logTest)).
    1.77 +            accept("text/html").
    1.78 +            get(ClientResponse.class);
    1.79 +        final String txt = res.getEntity(String.class);
    1.80 +        assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
    1.81 +    }
    1.82 +    
    1.83  
    1.84 +    private static void copyResource(String res, File file) throws IOException {
    1.85 +        InputStream is = UITest.class.getResourceAsStream(res);
    1.86 +        file.getParentFile().mkdirs();
    1.87 +        FileOutputStream os = new FileOutputStream(file);
    1.88 +        byte[] arr = new byte[4092];
    1.89 +        for (;;) {
    1.90 +            int len = is.read(arr);
    1.91 +            if (len == -1) {
    1.92 +                break;
    1.93 +            }
    1.94 +            os.write(arr, 0, len);
    1.95 +        }
    1.96 +        is.close();
    1.97 +        os.close();
    1.98 +    }
    1.99 +    
   1.100  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/freemarkerdor/src/test/resources/cz/xelfi/quoridor/freemarkerdor/wrong.encoding	Sat Nov 27 07:52:19 2010 +0100
     2.3 @@ -0,0 +1,31 @@
     2.4 +# white: petr.wolf
     2.5 +# black: jaroslav.tulach
     2.6 +# status: whiteMove
     2.7 +N S
     2.8 +N S
     2.9 +N S
    2.10 +HE5 HD6
    2.11 +VF6 VE6
    2.12 +HC5 VF8
    2.13 +VB6 VC8
    2.14 +HB3 VB4
    2.15 +VC1 VA2
    2.16 +HD3 VF4
    2.17 +HF2 W
    2.18 +E W
    2.19 +S HF3
    2.20 +HB1 VG2
    2.21 +W N
    2.22 +# jaroslav.tulach@Thu Nov 25 17:25:22 GMT 2010:
    2.23 +# Zdá se, že černému již o moc cestu prodloužit nejde.
    2.24 +S N
    2.25 +S
    2.26 +# petr.wolf@Fri Nov 26 13:37:28 GMT 2010:
    2.27 +# Souhlasim. Nekam ale zmizelo tlacitko "vzdat"
    2.28 +# petr.wolf@Fri Nov 26 13:37:48 GMT 2010:
    2.29 +# Asi proto, ze jsi na tahu ty
    2.30 +... W
    2.31 +# jaroslav.tulach@Fri Nov 26 13:48:59 GMT 2010:
    2.32 +# Pro černého bylo požehnáním, že se bílý nebránil proti HF3. Pak už tam byla dvojitá hrozba a nešlo s tím moc dělat.
    2.33 +
    2.34 +
     3.1 --- a/pom.xml	Sat Oct 23 22:51:43 2010 +0200
     3.2 +++ b/pom.xml	Sat Nov 27 07:52:19 2010 +0100
     3.3 @@ -48,12 +48,12 @@
     3.4    <properties>
     3.5        <jerseyVersion>1.3</jerseyVersion>
     3.6        <quoridorVersion>1.4</quoridorVersion>
     3.7 -      <webidorVersion>1.17</webidorVersion>
     3.8 +      <webidorVersion>1.18</webidorVersion>
     3.9        <visidorVersion>1.0-SNAPSHOT</visidorVersion>
    3.10        <freemarkerVersion>1.61</freemarkerVersion>
    3.11        <emailerVersion>1.1</emailerVersion>
    3.12        <statisticsVersion>1.8</statisticsVersion>
    3.13 -      <wsdorVersion>1.0</wsdorVersion>
    3.14 +      <wsdorVersion>1.1</wsdorVersion>
    3.15    </properties>
    3.16    <modules>
    3.17      <module>quoridor</module>
     4.1 --- a/wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java	Sat Oct 23 22:51:43 2010 +0200
     4.2 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java	Sat Nov 27 07:52:19 2010 +0100
     4.3 @@ -41,7 +41,7 @@
     4.4      }
     4.5  
     4.6      public Note(String comment, Date when, String who) {
     4.7 -        this.comment = comment;
     4.8 +        this.comment = eliminateXMLChars(comment);
     4.9          this.when = new Date(when.getTime());
    4.10          this.who = who;
    4.11      }
    4.12 @@ -93,5 +93,15 @@
    4.13          return hash;
    4.14      }
    4.15  
    4.16 -
    4.17 +    private static String eliminateXMLChars(String s) {
    4.18 +        StringBuilder sb = new StringBuilder();
    4.19 +        for (int i = 0; i < s.length(); i++) {
    4.20 +            char ch = s.charAt(i);
    4.21 +            if (ch < 32 && ch != '\n' || ch != '\r') {
    4.22 +            } else {
    4.23 +                sb.append((char)ch);
    4.24 +            }
    4.25 +        }
    4.26 +        return sb.toString();
    4.27 +    }
    4.28  }