1   /*
2    * /home/cvs/jakarta-turbine-maven/src/plugins-build/jellydoc/src/main/org/apache/maven/jellydoc/TagXMLDoclet.java,v 1.1 2003/02/07 12:10:44 jstrachan Exp
3    * 1.1
4    * 2003/02/07 12:10:44
5    *
6    * ====================================================================
7    *
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 2002 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution, if
26   *    any, must include the following acknowlegement:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowlegement may appear in the software itself,
30   *    if and wherever such third-party acknowlegements normally appear.
31   *
32   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33   *    Foundation" must not be used to endorse or promote products derived
34   *    from this software without prior written permission. For written
35   *    permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache"
38   *    nor may "Apache" appear in their names without prior written
39   *    permission of the Apache Group.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   *
60   * TagXMLDoclet.java,v 1.1 2003/02/07 12:10:44 jstrachan Exp
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     // Test cases
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     // Implementation methods
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 }