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