View Javadoc

1   /*
2    * $Id: TextUtilTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.views.util;
23  
24  import junit.framework.TestCase;
25  
26  /***
27   * Unit test for {@link TextUtil}.
28   *
29   */
30  public class TextUtilTest extends TestCase {
31  
32      private static char EURO_SIGN = 0x20AC;
33  
34      public void testEscape() throws Exception {
35          assertEquals("", TextUtil.escapeHTML(""));
36          assertEquals("   ", TextUtil.escapeHTML("   "));
37  
38          assertEquals("Hello World", TextUtil.escapeHTML("Hello World"));
39          assertEquals("Hello & World", TextUtil.escapeHTML("Hello & World"));
40  
41          assertEquals("Cost is 1999€ and this is cheap", TextUtil.escapeHTML("Cost is 1999" + EURO_SIGN + " and this is cheap"));
42  
43          assertEquals("Now some &lt;&gt; and &lt; - &gt; and we have &lt;/ and /&gt;", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />"));
44          assertEquals("&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;", TextUtil.escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
45      }
46  
47      public void testEscapeEmpty() throws Exception {
48          assertEquals("", TextUtil.escapeHTML("", true));
49          assertEquals("   ", TextUtil.escapeHTML("   ", true));
50  
51          assertEquals("Hello World", TextUtil.escapeHTML("Hello World", true));
52          assertEquals("Hello &amp; World", TextUtil.escapeHTML("Hello & World", true));
53  
54          assertEquals("Cost is 1999&euro; and this is cheap", TextUtil.escapeHTML("Cost is 1999" + EURO_SIGN + " and this is cheap", true));
55  
56          assertEquals("Now some &lt;&gt; and &lt; - &gt; and we have &lt;/ and /&gt;", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />", true));
57          assertEquals("&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;", TextUtil.escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", true));
58      }
59  
60      public void testLongText() throws Exception {
61          // TextUtil behaves special internally for long texts 
62          String s = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
63                     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
64                     "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 and now s" +
65                     "ome < that should be escaped. But this text is to long (> 300)";
66          String res = TextUtil.escapeHTML(s);
67          assertEquals(368, res.length());
68          assertTrue(res.indexOf("<") == -1);
69          assertTrue(res.indexOf(">") == -1);
70      }
71  
72  }