View Javadoc

1   /*
2    * $Id: TextUtilTest.java 418521 2006-07-01 23:36:50Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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 &lt;&gt; and &lt; - &gt; and we have &lt;/ and /&gt;", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />"));
40          assertEquals("&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;", 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 &amp; World", TextUtil.escapeHTML("Hello & World", true));
49  
50          assertEquals("Cost is 1999&euro; and this is cheap", TextUtil.escapeHTML("Cost is 1999" + EURO_SIGN + " and this is cheap", true));
51  
52          assertEquals("Now some &lt;&gt; and &lt; - &gt; and we have &lt;/ and /&gt;", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />", true));
53          assertEquals("&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;", TextUtil.escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", true));
54      }
55  
56      public void testLongText() throws Exception {
57          // TextUtil behaves special internally for long texts 
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  }