1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.util;
23
24 import junit.framework.TestCase;
25
26 /***
27 * Unit test for {@link TextUtil}.
28 *
29 */
30 public class TextUtilTest extends TestCase {
31
32 private static char EURO_SIGN = 0x20AC;
33
34 public void testEscape() throws Exception {
35 assertEquals("", TextUtil.escapeHTML(""));
36 assertEquals(" ", TextUtil.escapeHTML(" "));
37
38 assertEquals("Hello World", TextUtil.escapeHTML("Hello World"));
39 assertEquals("Hello & World", TextUtil.escapeHTML("Hello & World"));
40
41 assertEquals("Cost is 1999€ and this is cheap", TextUtil.escapeHTML("Cost is 1999" + EURO_SIGN + " and this is cheap"));
42
43 assertEquals("Now some <> and < - > and we have </ and />", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />"));
44 assertEquals("<?xml version="1.0" encoding="UTF-8"?>", TextUtil.escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
45 }
46
47 public void testEscapeEmpty() throws Exception {
48 assertEquals("", TextUtil.escapeHTML("", true));
49 assertEquals(" ", TextUtil.escapeHTML(" ", true));
50
51 assertEquals("Hello World", TextUtil.escapeHTML("Hello World", true));
52 assertEquals("Hello & World", TextUtil.escapeHTML("Hello & World", true));
53
54 assertEquals("Cost is 1999€ and this is cheap", TextUtil.escapeHTML("Cost is 1999" + EURO_SIGN + " and this is cheap", true));
55
56 assertEquals("Now some <> and < - > and we have </ and />", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />", true));
57 assertEquals("<?xml version="1.0" encoding="UTF-8"?>", TextUtil.escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", true));
58 }
59
60 public void testLongText() throws Exception {
61
62 String s = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
63 "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
64 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 and now s" +
65 "ome < that should be escaped. But this text is to long (> 300)";
66 String res = TextUtil.escapeHTML(s);
67 assertEquals(368, res.length());
68 assertTrue(res.indexOf("<") == -1);
69 assertTrue(res.indexOf(">") == -1);
70 }
71
72 }