1   package org.apache.maven.doxia.util;
2   
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  import org.codehaus.plexus.PlexusTestCase;
23  
24  /**
25   * Test case for <code>HtmlTools</code>.
26   *
27   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
28   * @version $Id: HtmlToolsTest.java 747735 2009-02-25 10:43:09Z ltheussl $
29   */
30  public class HtmlToolsTest
31      extends PlexusTestCase
32  {
33      /**
34       * Verify the expected results.
35       */
36      public void testEscapeHTML()
37      {
38          assertEquals( HtmlTools.escapeHTML( "" ), "" );
39          assertEquals( HtmlTools.escapeHTML( "<" ), "&lt;" );
40          assertEquals( HtmlTools.escapeHTML( ">" ), "&gt;" );
41          assertEquals( HtmlTools.escapeHTML( "&" ), "&amp;" );
42          assertEquals( HtmlTools.escapeHTML( "\"" ), "&quot;" );
43          assertEquals( HtmlTools.escapeHTML( "&amp;" ), "&amp;amp;" );
44  
45          // xml mode
46          assertEquals( HtmlTools.escapeHTML( "\u00e4", true ), "\u00e4" );
47          assertEquals( HtmlTools.escapeHTML( "\u00e4", false ), "&#228;" );
48      }
49  
50      /**
51       * Verify the expected results.
52       */
53      public void testEncodeId()
54      {
55          assertEquals( HtmlTools.encodeId( null ), null );
56          assertEquals( HtmlTools.encodeId( "" ), "" );
57          assertEquals( HtmlTools.encodeId( " " ), "" );
58          assertEquals( HtmlTools.encodeId( " _ " ), "a_" );
59          assertEquals( HtmlTools.encodeId( "1" ), "a1" );
60          assertEquals( HtmlTools.encodeId( "1anchor" ), "a1anchor" );
61          assertEquals( HtmlTools.encodeId( "_anchor" ), "a_anchor" );
62          assertEquals( HtmlTools.encodeId( "a b-c123 " ), "a_b-c123" );
63          assertEquals( HtmlTools.encodeId( "   anchor" ), "anchor" );
64          assertEquals( HtmlTools.encodeId( "myAnchor" ), "myAnchor" );
65      }
66  
67      /**
68       * Verify the expected results.
69       */
70      public void testEncodeURL()
71      {
72          assertNull( HtmlTools.encodeURL( null ) );
73          assertEquals( HtmlTools.encodeURL( "" ), "" );
74          assertEquals( HtmlTools.encodeURL(
75              "http://www.example.com/?This is a simple test." ),
76              "http://www.example.com/?This%20is%20a%20simple%20test." );
77  
78          // TODO: the & is not encoded?
79          //assertEquals( HtmlTools.encodeURL(
80          //    "http://www.example.com/?This is a simple & short test." ),
81          //    "http://www.example.com/?This%20is%20a%20simple%20%26%20short%20test." );
82      }
83  
84      /**
85       * Verify the expected results.
86       */
87      public void testIsId()
88      {
89          assertFalse( HtmlTools.isId( null ) );
90          assertFalse( HtmlTools.isId( "" ) );
91          assertFalse( HtmlTools.isId( " " ) );
92          assertFalse( HtmlTools.isId( " _ " ) );
93          assertFalse( HtmlTools.isId( "1" ) );
94          assertFalse( HtmlTools.isId( "1anchor" ) );
95          assertFalse( HtmlTools.isId( "_anchor" ) );
96          assertFalse( HtmlTools.isId( "a b-c123 " ) );
97          assertFalse( HtmlTools.isId( "   anchor" ) );
98          assertTrue( HtmlTools.isId( "myAnchor" ) );
99          assertTrue( HtmlTools.isId( "a_" ) );
100         assertTrue( HtmlTools.isId( "a-" ) );
101         assertTrue( HtmlTools.isId( "a:" ) );
102         assertTrue( HtmlTools.isId( "a." ) );
103     }
104 }