001package org.apache.maven.doxia.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.net.URLEncoder; 023 024import org.codehaus.plexus.PlexusTestCase; 025 026/** 027 * Test case for <code>HtmlTools</code>. 028 * 029 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 030 * @version $Id$ 031 */ 032public class HtmlToolsTest 033 extends PlexusTestCase 034{ 035 /** 036 * Verify the expected results. 037 */ 038 public void testEscapeHTML() 039 { 040 assertEquals( HtmlTools.escapeHTML( null ), "" ); 041 assertEquals( HtmlTools.escapeHTML( "" ), "" ); 042 assertEquals( HtmlTools.escapeHTML( "\u0009" ), "\u0009" ); 043 assertEquals( HtmlTools.escapeHTML( "\u0001" ), "\u0001" ); 044 045 // Predefined entities 046 assertEquals( HtmlTools.escapeHTML( "<" ), "<" ); 047 assertEquals( HtmlTools.escapeHTML( ">" ), ">" ); 048 assertEquals( HtmlTools.escapeHTML( "&" ), "&" ); 049 assertEquals( HtmlTools.escapeHTML( "\"" ), """ ); 050 assertEquals( HtmlTools.escapeHTML( "\'" ), "'" ); 051 assertEquals( HtmlTools.escapeHTML( "\'", false ), "\'" ); 052 053 // xml mode 054 assertEquals( HtmlTools.escapeHTML( "&" ), "&amp;" ); 055 assertEquals( HtmlTools.escapeHTML( "\u00e4", true ), "\u00e4" ); 056 assertEquals( HtmlTools.escapeHTML( "\u00e4", false ), "ä" ); 057 assertEquals( HtmlTools.escapeHTML( "\u0159", false ), "ř" ); 058 assertEquals( HtmlTools.escapeHTML( "\uD835\uDFED", false ), "𝟭" ); 059 } 060 061 /** 062 * Verify the expected results. 063 */ 064 public void testUnescapeHTML() 065 { 066 assertNull( HtmlTools.unescapeHTML( null ) ); 067 assertEquals( "", HtmlTools.unescapeHTML( "" ) ); 068 assertEquals( "\u0009", HtmlTools.unescapeHTML( "\u0009" ) ); 069 assertEquals( "\u0001", HtmlTools.unescapeHTML( "\u0001" ) ); 070 assertEquals( "<", HtmlTools.unescapeHTML( "<" ) ); 071 assertEquals( ">", HtmlTools.unescapeHTML( ">" ) ); 072 assertEquals( "&", HtmlTools.unescapeHTML( "&" ) ); 073 assertEquals( "\"", HtmlTools.unescapeHTML( """ ) ); 074 assertEquals( "'", HtmlTools.unescapeHTML( "'" ) ); 075 assertEquals( "\'", HtmlTools.unescapeHTML( "'", true ) ); 076 assertEquals( "&", HtmlTools.unescapeHTML( "&amp;" ) ); 077 assertEquals( "<Français>", HtmlTools.unescapeHTML( "&lt;Fran&ccedil;ais&gt;" ) ); 078 assertEquals( "\u0159", HtmlTools.unescapeHTML( "ř" ) ); 079 assertEquals( "\uD808\uDF45", HtmlTools.unescapeHTML( "𒍅" ) ); 080 assertEquals( "\uD835\uDFED", HtmlTools.unescapeHTML( "𝟭" ) ); 081 assertEquals( "\uD808\uDF45\uD835\uDFED", HtmlTools.unescapeHTML( "𒍅𝟭" ) ); 082 assertEquals( "𝟭 𝟭", HtmlTools.unescapeHTML( "𝟭 𝟭" ) ); 083 assertEquals( "𝟭 \uD835\uDFED", HtmlTools.unescapeHTML( "𝟭 𝟭" ) ); 084 assertEquals( "&#xQWER;", HtmlTools.unescapeHTML( "&#xQWER;" ) ); 085 assertEquals( "\u00E5", HtmlTools.unescapeHTML( "å" ) ); 086 assertEquals( "<>&\"\u00E5\u0159\uD835\uDFED", 087 HtmlTools.unescapeHTML( "<>&"åř𝟭" ) ); 088 } 089 090 /** 091 * Verify the expected results. 092 */ 093 public void testEncodeId() 094 { 095 assertEquals( HtmlTools.encodeId( null ), null ); 096 assertEquals( HtmlTools.encodeId( "" ), "a" ); 097 assertEquals( HtmlTools.encodeId( " " ), "a" ); 098 assertEquals( HtmlTools.encodeId( " _ " ), "a_" ); 099 assertEquals( HtmlTools.encodeId( "1" ), "a1" ); 100 assertEquals( HtmlTools.encodeId( "1anchor" ), "a1anchor" ); 101 assertEquals( HtmlTools.encodeId( "_anchor" ), "a_anchor" ); 102 assertEquals( HtmlTools.encodeId( "a b-c123 " ), "a_b-c123" ); 103 assertEquals( HtmlTools.encodeId( " anchor" ), "anchor" ); 104 assertEquals( HtmlTools.encodeId( "myAnchor" ), "myAnchor" ); 105 assertEquals( HtmlTools.encodeId( "H\u00E5kon" ), "H.C3.A5kon" ); 106 assertEquals( HtmlTools.encodeId( "Theu\u00DFl" ), "Theu.C3.9Fl" ); 107 } 108 109 /** 110 * Verify the expected results. 111 * 112 * @throws Exception should not happen. 113 */ 114 public void testEncodeURL() 115 throws Exception 116 { 117 assertNull( HtmlTools.encodeURL( null ) ); 118 assertEquals( HtmlTools.encodeURL( "" ), "" ); 119 assertEquals( HtmlTools.encodeURL( "http://www.example.com/?This is a simple test." ), 120 "http://www.example.com/?This%20is%20a%20simple%20test." ); 121 122 assertEquals( HtmlTools.encodeURL( "http://www.example.com/?This is a simple & short test." ), 123 "http://www.example.com/?This%20is%20a%20simple%20&%20short%20test." ); 124 125 String url = "\uD808\uDF45"; 126 assertEquals( HtmlTools.encodeURL( url ), URLEncoder.encode( url, "UTF-8" ) ); 127 } 128 129 /** 130 * Verify the expected results. 131 */ 132 public void testIsId() 133 { 134 assertFalse( HtmlTools.isId( null ) ); 135 assertFalse( HtmlTools.isId( "" ) ); 136 assertFalse( HtmlTools.isId( " " ) ); 137 assertFalse( HtmlTools.isId( " _ " ) ); 138 assertFalse( HtmlTools.isId( "1" ) ); 139 assertFalse( HtmlTools.isId( "1anchor" ) ); 140 assertFalse( HtmlTools.isId( "_anchor" ) ); 141 assertFalse( HtmlTools.isId( "a b-c123 " ) ); 142 assertFalse( HtmlTools.isId( " anchor" ) ); 143 assertTrue( HtmlTools.isId( "myAnchor" ) ); 144 assertTrue( HtmlTools.isId( "a_" ) ); 145 assertTrue( HtmlTools.isId( "a-" ) ); 146 assertTrue( HtmlTools.isId( "a:" ) ); 147 assertTrue( HtmlTools.isId( "a." ) ); 148 assertFalse( HtmlTools.isId( "Theu\u00DFl" ) ); 149 } 150 151 /** 152 * Verify the expected results. 153 */ 154 public void testGetHtmlTag() 155 { 156 assertNull( HtmlTools.getHtmlTag( null ) ); 157 assertNull( HtmlTools.getHtmlTag( "" ) ); 158 assertNull( HtmlTools.getHtmlTag( "weirdHtmlTag" ) ); 159 assertNotNull( HtmlTools.getHtmlTag( "strong" ) ); 160 } 161}