1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 package org.apache.maven.html2xdoc;
63
64 import junit.framework.Test;
65 import junit.framework.TestCase;
66 import junit.framework.TestSuite;
67 import junit.textui.TestRunner;
68
69 import org.cyberneko.html.parsers.SAXParser;
70 import org.dom4j.Document;
71 import org.dom4j.io.OutputFormat;
72 import org.dom4j.io.SAXReader;
73 import org.dom4j.io.XMLWriter;
74
75 import java.io.IOException;
76 import java.io.StringWriter;
77
78 import java.net.URL;
79
80 /**
81 * A test harness for the HTML to XDOC converter
82 *
83 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
84 */
85 public class TestHtml2Xdoc extends TestCase
86 {
87 protected boolean verbose = false;
88
89 public TestHtml2Xdoc( String testName )
90 {
91 super( testName );
92 }
93
94 public static void main( String[] args )
95 {
96 TestRunner.run( suite() );
97 }
98
99 public static Test suite()
100 {
101 return new TestSuite( TestHtml2Xdoc.class );
102 }
103
104
105
106 public void testOne() throws Exception
107 {
108 assertConversion( "missingParaBug.html", "missingParaBug.xml" );
109 assertConversion( "linkInHeading.html", "linkInHeading.xml" );
110 assertConversion( "codeinpara.html", "codeinpara.xml" );
111 assertConversion( "input1.html", "output1.xml" );
112 assertConversion( "h1h2.html", "h1h2.xml" );
113 assertConversion( "h2h3.html", "h1h2.xml" );
114 assertConversion( "h2h4.html", "h1h2.xml" );
115 assertConversion( "h3h4.html", "h1h2.xml" );
116 assertConversion( "link.html", "link.xml" );
117 assertConversion( "comment.html", "comment.xml" );
118 }
119
120
121
122 protected void assertConversion( String input, String output )
123 throws Exception
124 {
125 Html2XdocBean converter = createConverter();
126 Document inputDoc = parseHtml( input );
127
128 Document expectedDoc = parse( output );
129
130 Document actualDoc = converter.convert( inputDoc );
131
132 if ( verbose )
133 {
134 System.out.println( "Comparing: " + input + " to: " + output );
135 System.out.println( "Parsed: " + inputDoc.asXML() );
136 System.out.println( "Generated: " + actualDoc.asXML() );
137 System.out.println();
138 System.out.println();
139 }
140
141 assertEqual( "Output for: " + input + " does not match: " + output,
142 expectedDoc, actualDoc );
143 }
144
145 /**
146 * Asserts that the given two documents are equal
147 *
148 * @param string
149 * @param expectedDoc
150 * @param actualDoc
151 */
152 protected void assertEqual( String message, Document expectedDoc,
153 Document actualDoc ) throws IOException
154 {
155 String expectedText = getPrettyPrintText( expectedDoc );
156 String actualText = getPrettyPrintText( actualDoc );
157
158 if ( !expectedText.equals( actualText ) )
159 {
160 System.out.println( "Expected: " + expectedText );
161 System.out.println( "Actual: " + actualText );
162 }
163
164 assertEquals( message, expectedText, actualText );
165 }
166
167 /**
168 * @param expectedDoc
169 * @return Object
170 */
171 protected String getPrettyPrintText( Document doc )
172 throws IOException
173 {
174 OutputFormat format = OutputFormat.createPrettyPrint();
175 format.setPadText( false );
176 StringWriter buffer = new StringWriter();
177 XMLWriter writer = new XMLWriter( buffer, format );
178
179 writer.write( doc );
180 writer.close();
181
182 return buffer.toString();
183 }
184
185 /**
186 * Parses the given String URI on the classpath and returns the docuemnt
187 *
188 * @param input
189 * @return Document
190 */
191 protected Document parse( String input )
192 throws Exception
193 {
194 URL url = getClassURL( input );
195 SAXReader saxReader = new SAXReader();
196 saxReader.setMergeAdjacentText( true );
197 saxReader.setStripWhitespaceText( true );
198
199 return saxReader.read( url );
200 }
201
202 /**
203 * Parses the given HTML using a String URI on the classpath
204 * and returns the docuemnt
205 *
206 * @param input
207 * @return Document
208 */
209 protected Document parseHtml( String input )
210 throws Exception
211 {
212 URL url = getClassURL( input );
213 SAXParser htmlParser = new SAXParser();
214
215 htmlParser.setProperty( "http://cyberneko.org/html/properties/names/elems",
216 "lower" );
217 htmlParser.setProperty( "http://cyberneko.org/html/properties/names/attrs",
218 "lower" );
219
220 SAXReader saxReader = new SAXReader( htmlParser );
221
222 return saxReader.read( url );
223 }
224
225 protected URL getClassURL( String input )
226 throws Exception
227 {
228 URL url = getClass().getResource( input );
229
230 assertTrue( "Could not find resource on classpath for: " + input,
231 url != null );
232
233 return url;
234 }
235
236 protected Html2XdocBean createConverter()
237 {
238 return new Html2XdocBean();
239 }
240 }