/*
 * /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
 * 1.1
 * 2003/02/07 12:10:44
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 * 
 * TagXMLDoclet.java,v 1.1 2003/02/07 12:10:44 jstrachan Exp
 */

package org.apache.maven.html2xdoc;

import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

import org.cyberneko.html.parsers.SAXParser;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * A test harness for the HTML to XDOC converter 
 * 
 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 */
public class TestHtml2Xdoc extends TestCase {
    
    protected boolean verbose = false;
    
    public static void main( String[] args ) {
        TestRunner.run( suite() );
    }

    public static Test suite() {
        return new TestSuite( TestHtml2Xdoc.class );
    }

    public TestHtml2Xdoc(String testName) {
        super(testName);
    }

    // Test cases
    //-------------------------------------------------------------------------
    public void testOne() throws Exception {
        assertConversion("missingParaBug.html", "missingParaBug.xml");
        assertConversion("linkInHeading.html", "linkInHeading.xml");
        assertConversion("codeinpara.html", "codeinpara.xml");
		assertConversion("input1.html", "output1.xml");
		assertConversion("h1h2.html", "h1h2.xml");
        assertConversion("h2h3.html", "h1h2.xml");
        assertConversion("h2h4.html", "h1h2.xml");
        assertConversion("h3h4.html", "h1h2.xml");
		assertConversion("link.html", "link.xml");
        assertConversion("comment.html", "comment.xml");
    }
    
    // Implementation methods
    //-------------------------------------------------------------------------
    protected void assertConversion(String input, String output) throws Exception {
        Html2XdocBean converter = createConverter();
        Document inputDoc = parseHtml(input);
        
        
        Document expectedDoc = parse(output);
        
        Document actualDoc = converter.convert(inputDoc);

        if (verbose) {        
            System.out.println("Comparing: " + input + " to: " + output);
            System.out.println("Parsed: " + inputDoc.asXML());
            System.out.println("Generated: " + actualDoc.asXML());
            System.out.println();
            System.out.println();
        }
        
        assertEqual("Output for: " + input + " does not match: " + output, expectedDoc, actualDoc);
    }

    /**
     * Asserts that the given two documents are equal
     * 
     * @param string
     * @param expectedDoc
     * @param actualDoc
     */
    protected void assertEqual(
        String message,
        Document expectedDoc,
        Document actualDoc) throws IOException {

		String expectedText = getPrettyPrintText(expectedDoc);
		String actualText = getPrettyPrintText(actualDoc);
		
		if (!expectedText.equals(actualText)) {
            System.out.println("Expected: " + expectedText);	
            System.out.println("Actual: " + actualText);	
		}
        assertEquals(message, expectedText, actualText);    
    }

    /**
     * @param expectedDoc
     * @return Object
     */
    protected String getPrettyPrintText(Document doc) throws IOException {
        OutputFormat format = OutputFormat.createPrettyPrint();
        StringWriter buffer = new StringWriter();
        XMLWriter writer = new XMLWriter(buffer, format);
        writer.write(doc);
        writer.close();
        return buffer.toString();
    }

    /**
     * Parses the given String URI on the classpath and returns the docuemnt
     * 
     * @param input
     * @return Document
     */
    protected Document parse(String input) throws Exception {
        URL url = getClassURL(input);
        SAXReader saxReader = new SAXReader();   
        return saxReader.read(url);
    }

    /**
     * Parses the given HTML using a String URI on the classpath 
     * and returns the docuemnt
     * 
     * @param input
     * @return Document
     */
    protected Document parseHtml(String input) throws Exception {
        URL url = getClassURL(input);
        SAXParser htmlParser = new SAXParser();
        htmlParser.setProperty(
            "http://cyberneko.org/html/properties/names/elems",
            "lower"
        );
        htmlParser.setProperty(
            "http://cyberneko.org/html/properties/names/attrs",
            "lower"
        );
        SAXReader saxReader = new SAXReader(htmlParser);
        return saxReader.read(url);
    }

	protected URL getClassURL(String input) throws Exception {
        URL url = getClass().getResource(input);
        assertTrue("Could not find resource on classpath for: " + input, url != null);
        return url;
	}
	
    protected Html2XdocBean createConverter() {
        return new Html2XdocBean();
    }
}
