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; 023import java.util.Locale; 024 025import org.codehaus.plexus.PlexusTestCase; 026 027/** 028 * Test case for <code>HtmlTools</code>. 029 * 030 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 031 * @version $Id$ 032 */ 033public class HtmlToolsTest 034 extends PlexusTestCase 035{ 036 /** 037 * Verify the expected results. 038 */ 039 public void testEscapeHTML() 040 { 041 assertEquals( HtmlTools.escapeHTML( null ), "" ); 042 assertEquals( HtmlTools.escapeHTML( "" ), "" ); 043 assertEquals( HtmlTools.escapeHTML( "\u0009" ), "\u0009" ); 044 assertEquals( HtmlTools.escapeHTML( "\u0001" ), "\u0001" ); 045 046 // Predefined entities 047 assertEquals( HtmlTools.escapeHTML( "<" ), "<" ); 048 assertEquals( HtmlTools.escapeHTML( ">" ), ">" ); 049 assertEquals( HtmlTools.escapeHTML( "&" ), "&" ); 050 assertEquals( HtmlTools.escapeHTML( "\"" ), """ ); 051 assertEquals( HtmlTools.escapeHTML( "\'" ), "'" ); 052 assertEquals( HtmlTools.escapeHTML( "\'", false ), "\'" ); 053 054 // xml mode 055 assertEquals( HtmlTools.escapeHTML( "&" ), "&amp;" ); 056 assertEquals( HtmlTools.escapeHTML( "\u00e4", true ), "\u00e4" ); 057 assertEquals( HtmlTools.escapeHTML( "\u00e4", false ), "ä" ); 058 assertEquals( HtmlTools.escapeHTML( "\u0159", false ), "ř" ); 059 assertEquals( HtmlTools.escapeHTML( "\uD835\uDFED", false ), "𝟭" ); 060 } 061 062 /** 063 * Verify the expected results. 064 */ 065 public void testUnescapeHTML() 066 { 067 assertNull( HtmlTools.unescapeHTML( null ) ); 068 assertEquals( "", HtmlTools.unescapeHTML( "" ) ); 069 assertEquals( "\u0009", HtmlTools.unescapeHTML( "\u0009" ) ); 070 assertEquals( "\u0001", HtmlTools.unescapeHTML( "\u0001" ) ); 071 assertEquals( "<", HtmlTools.unescapeHTML( "<" ) ); 072 assertEquals( ">", HtmlTools.unescapeHTML( ">" ) ); 073 assertEquals( "&", HtmlTools.unescapeHTML( "&" ) ); 074 assertEquals( "\"", HtmlTools.unescapeHTML( """ ) ); 075 assertEquals( "'", HtmlTools.unescapeHTML( "'" ) ); 076 assertEquals( "\'", HtmlTools.unescapeHTML( "'", true ) ); 077 assertEquals( "&", HtmlTools.unescapeHTML( "&amp;" ) ); 078 assertEquals( "<Français>", HtmlTools.unescapeHTML( "&lt;Fran&ccedil;ais&gt;" ) ); 079 assertEquals( "\u0159", HtmlTools.unescapeHTML( "ř" ) ); 080 assertEquals( "\uD808\uDF45", HtmlTools.unescapeHTML( "𒍅" ) ); 081 assertEquals( "\uD835\uDFED", HtmlTools.unescapeHTML( "𝟭" ) ); 082 assertEquals( "\uD808\uDF45\uD835\uDFED", HtmlTools.unescapeHTML( "𒍅𝟭" ) ); 083 assertEquals( "𝟭 𝟭", HtmlTools.unescapeHTML( "𝟭 𝟭" ) ); 084 assertEquals( "𝟭 \uD835\uDFED", HtmlTools.unescapeHTML( "𝟭 𝟭" ) ); 085 assertEquals( "&#xQWER;", HtmlTools.unescapeHTML( "&#xQWER;" ) ); 086 assertEquals( "\u00E5", HtmlTools.unescapeHTML( "å" ) ); 087 assertEquals( "<>&\"\u00E5\u0159\uD835\uDFED", 088 HtmlTools.unescapeHTML( "<>&"åř𝟭" ) ); 089 } 090 091 /** 092 * Verify the expected results. 093 */ 094 public void testEncodeId() 095 { 096 assertEquals( HtmlTools.encodeId( null ), null ); 097 assertEquals( HtmlTools.encodeId( "" ), "a" ); 098 assertEquals( HtmlTools.encodeId( " " ), "a" ); 099 assertEquals( HtmlTools.encodeId( " _ " ), "a_" ); 100 assertEquals( HtmlTools.encodeId( "1" ), "a1" ); 101 assertEquals( HtmlTools.encodeId( "1anchor" ), "a1anchor" ); 102 assertEquals( HtmlTools.encodeId( "_anchor" ), "a_anchor" ); 103 assertEquals( HtmlTools.encodeId( "a b-c123 " ), "a_b-c123" ); 104 assertEquals( HtmlTools.encodeId( " anchor" ), "anchor" ); 105 assertEquals( HtmlTools.encodeId( "myAnchor" ), "myAnchor" ); 106 assertEquals( HtmlTools.encodeId( "H\u00E5kon" ), "Hkon" ); 107 assertEquals( HtmlTools.encodeId( "Theu\u00DFl" ), "Theul" ); 108 } 109 110 /** 111 * Verify the expected results. 112 * 113 * @throws Exception should not happen. 114 */ 115 public void testEncodeURL() 116 throws Exception 117 { 118 assertNull( HtmlTools.encodeURL( null ) ); 119 assertEquals( HtmlTools.encodeURL( "" ), "" ); 120 assertEquals( HtmlTools.encodeURL( "http://www.example.com/?This is a simple test." ), 121 "http://www.example.com/?This%20is%20a%20simple%20test." ); 122 123 assertEquals( HtmlTools.encodeURL( "http://www.example.com/?This is a simple & short test." ), 124 "http://www.example.com/?This%20is%20a%20simple%20&%20short%20test." ); 125 126 String url = "\uD808\uDF45"; 127 assertEquals( HtmlTools.encodeURL( url ), URLEncoder.encode( url, "UTF-8" ).toLowerCase( Locale.ENGLISH ) ); 128 } 129 130 /** 131 * Verify the expected results. 132 */ 133 public void testIsId() 134 { 135 assertFalse( HtmlTools.isId( null ) ); 136 assertFalse( HtmlTools.isId( "" ) ); 137 assertFalse( HtmlTools.isId( " " ) ); 138 assertFalse( HtmlTools.isId( " _ " ) ); 139 assertFalse( HtmlTools.isId( "1" ) ); 140 assertFalse( HtmlTools.isId( "1anchor" ) ); 141 assertFalse( HtmlTools.isId( "_anchor" ) ); 142 assertFalse( HtmlTools.isId( "a b-c123 " ) ); 143 assertFalse( HtmlTools.isId( " anchor" ) ); 144 assertTrue( HtmlTools.isId( "myAnchor" ) ); 145 assertTrue( HtmlTools.isId( "a_" ) ); 146 assertTrue( HtmlTools.isId( "a-" ) ); 147 assertTrue( HtmlTools.isId( "a:" ) ); 148 assertTrue( HtmlTools.isId( "a." ) ); 149 assertFalse( HtmlTools.isId( "Theu\u00DFl" ) ); 150 } 151 152 /** 153 * Verify the expected results. 154 */ 155 public void testGetHtmlTag() 156 { 157 assertNull( HtmlTools.getHtmlTag( null ) ); 158 assertNull( HtmlTools.getHtmlTag( "" ) ); 159 assertNull( HtmlTools.getHtmlTag( "weirdHtmlTag" ) ); 160 assertNotNull( HtmlTools.getHtmlTag( "strong" ) ); 161 } 162}