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