01: /*
02: * $Id: TextUtilTest.java 471756 2006-11-06 15:01:43Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
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
39: .escapeHTML("Hello & World"));
40:
41: assertEquals("Cost is 1999€ and this is cheap", TextUtil
42: .escapeHTML("Cost is 1999" + EURO_SIGN
43: + " and this is cheap"));
44:
45: assertEquals(
46: "Now some <> and < - > and we have </ and />",
47: TextUtil
48: .escapeHTML("Now some <> and < - > and we have </ and />"));
49: assertEquals(
50: "<?xml version="1.0" encoding="UTF-8"?>",
51: TextUtil
52: .escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
53: }
54:
55: public void testEscapeEmpty() throws Exception {
56: assertEquals("", TextUtil.escapeHTML("", true));
57: assertEquals(" ", TextUtil.escapeHTML(" ", true));
58:
59: assertEquals("Hello World", TextUtil.escapeHTML("Hello World",
60: true));
61: assertEquals("Hello & World", TextUtil.escapeHTML(
62: "Hello & World", true));
63:
64: assertEquals("Cost is 1999€ and this is cheap", TextUtil
65: .escapeHTML("Cost is 1999" + EURO_SIGN
66: + " and this is cheap", true));
67:
68: assertEquals(
69: "Now some <> and < - > and we have </ and />",
70: TextUtil.escapeHTML(
71: "Now some <> and < - > and we have </ and />",
72: true));
73: assertEquals(
74: "<?xml version="1.0" encoding="UTF-8"?>",
75: TextUtil.escapeHTML(
76: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
77: true));
78: }
79:
80: public void testLongText() throws Exception {
81: // TextUtil behaves special internally for long texts
82: String s = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
83: + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
84: + "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 and now s"
85: + "ome < that should be escaped. But this text is to long (> 300)";
86: String res = TextUtil.escapeHTML(s);
87: assertEquals(368, res.length());
88: assertTrue(res.indexOf("<") == -1);
89: assertTrue(res.indexOf(">") == -1);
90: }
91:
92: }
|