01: /*
02: * Copyright (c) 2002-2006 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.views.util;
06:
07: import junit.framework.TestCase;
08:
09: /**
10: * Unit test for {@link TextUtil}.
11: *
12: * @author Claus Ibsen
13: */
14: public class TextUtilTest extends TestCase {
15:
16: private static char EURO_SIGN = 0x20AC;
17:
18: public void testEscape() throws Exception {
19: assertEquals("", TextUtil.escapeHTML(""));
20: assertEquals(" ", TextUtil.escapeHTML(" "));
21:
22: assertEquals("Hello World", TextUtil.escapeHTML("Hello World"));
23: assertEquals("Hello & World", TextUtil
24: .escapeHTML("Hello & World"));
25:
26: assertEquals("Cost is 1999€ and this is cheap", TextUtil
27: .escapeHTML("Cost is 1999" + EURO_SIGN
28: + " and this is cheap"));
29:
30: assertEquals(
31: "Now some <> and < - > and we have </ and />",
32: TextUtil
33: .escapeHTML("Now some <> and < - > and we have </ and />"));
34: assertEquals(
35: "<?xml version="1.0" encoding="UTF-8"?>",
36: TextUtil
37: .escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
38: }
39:
40: public void testEscapeEmpty() throws Exception {
41: assertEquals("", TextUtil.escapeHTML("", true));
42: assertEquals(" ", TextUtil.escapeHTML(" ", true));
43:
44: assertEquals("Hello World", TextUtil.escapeHTML("Hello World",
45: true));
46: assertEquals("Hello & World", TextUtil.escapeHTML(
47: "Hello & World", true));
48:
49: assertEquals("Cost is 1999€ and this is cheap", TextUtil
50: .escapeHTML("Cost is 1999" + EURO_SIGN
51: + " and this is cheap", true));
52:
53: assertEquals(
54: "Now some <> and < - > and we have </ and />",
55: TextUtil.escapeHTML(
56: "Now some <> and < - > and we have </ and />",
57: true));
58: assertEquals(
59: "<?xml version="1.0" encoding="UTF-8"?>",
60: TextUtil.escapeHTML(
61: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
62: true));
63: }
64:
65: public void testLongText() throws Exception {
66: // TextUtil behaves special internally for long texts
67: String s = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
68: + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
69: + "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 and now s"
70: + "ome < that should be escaped. But this text is to long (> 300)";
71: String res = TextUtil.escapeHTML(s);
72: assertEquals(368, res.length());
73: assertTrue(res.indexOf("<") == -1);
74: assertTrue(res.indexOf(">") == -1);
75: }
76:
77: }
|