View Javadoc

1   /*
2    * $Id: TextUtilTest.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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.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 &lt;&gt; and &lt; - &gt; and we have &lt;/ and /&gt;", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />"));
43          assertEquals("&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;", 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 &amp; World", TextUtil.escapeHTML("Hello & World", true));
52  
53          assertEquals("Cost is 1999&euro; and this is cheap", TextUtil.escapeHTML("Cost is 1999" + EURO_SIGN + " and this is cheap", true));
54  
55          assertEquals("Now some &lt;&gt; and &lt; - &gt; and we have &lt;/ and /&gt;", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />", true));
56          assertEquals("&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;", TextUtil.escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", true));
57      }
58  
59      public void testLongText() throws Exception {
60          // TextUtil behaves special internally for long texts 
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  }