package org.apache.maven.javadoc;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

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

/**
 * Test case.
 *
 * @author Steven Caswell
 * @version $Id$
 */
public class JavadocWarningsTextToXmlTest extends TestCase
{
    private static final String INPUT_FILE_NAME_EXCEPTION = "Input file name must be specified";
    private static final String OUTPUT_FILE_NAME_EXCEPTION = "Output file name must be specified";

    /**
     * Creates a new JavadocWarningsTextToXmlTest object.
     *
     */
    public JavadocWarningsTextToXmlTest(final String name)
    {
        super(name);
    }

    public static void main(final String[] main)
    {
        TestRunner.run(suite());
    }

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

    /**
     * Initialize per test data
     *
     * @throws Exception when there is an unexpected problem
     */
    public void setUp() throws Exception
    {
        String baseDir = System.getProperty("basedir");

        System.out.println("Base dir : " + baseDir);
        assertNotNull("The system property basedir was not defined.", baseDir);
    }

    public void testBuild() throws Exception
    {
        JavadocWarningsTextToXml bean = new JavadocWarningsTextToXml();

        bean.setVerbose(true);
        bean.setInputFileName("target/test-classes/report.txt");
        bean.setOutputFileName("test.xml");
        bean.setOutputEncoding("ISO-8859-1");
        bean.build();

        // Read and test the output file contents
        BufferedReader reader = new BufferedReader(new FileReader("test.xml"));
        String line = reader.readLine();

        assertTrue("line 1 start",
            line.startsWith("<?xml version=\"1.0\" encoding=\""));
        assertTrue("line 1 end", line.endsWith("ISO-8859-1\"?>"));
        line = reader.readLine();
        assertEquals("line 2", "<javadoc>", line);
        line = reader.readLine();
        assertTrue("line 3 start", line.startsWith("<file name"));
        assertTrue("line 3 end", line.endsWith("Javadoc1.java\">"));
        line = reader.readLine();
        assertEquals("line 4",
            "<error line=\"8\" severity=\"warning\" message=\"@inheritDoc used but method1() does not override or implement any method.\"/>",
            line);
        line = reader.readLine();
        assertEquals("line 5", "</file>", line);
        line = reader.readLine();
        assertTrue("line 6 start", line.startsWith("<file name"));
        assertTrue("line 6 end", line.endsWith("Javadoc2.java\">"));
        line = reader.readLine();
        assertEquals("line 7",
            "<error line=\"8\" severity=\"warning\" message=\"@todo is an unknown tag.\"/>",
            line);
        line = reader.readLine();
        assertEquals("line 8",
            "<error line=\"15\" severity=\"warning\" message=\"Tag @link: malformed: &quot;Javadoc1#method1(&quot;\"/>",
            line);
        line = reader.readLine();
        assertEquals("line 9", "</file>", line);
        line = reader.readLine();
        assertEquals("line 10", "</javadoc>", line);

        reader.close();

        // Get rid of the xml file
        File file = new File("test.xml");

        file.delete();
    }

    public void testInputFile()
        throws Exception
    {
        JavadocWarningsTextToXml bean = new JavadocWarningsTextToXml();

        bean.setOutputFileName("test.xml");
        bean.setOutputEncoding("ISO-8859-1]");

        try
        {
            bean.build();
        }
        catch (IllegalArgumentException e)
        {
            assertEquals("exception message", INPUT_FILE_NAME_EXCEPTION,
                e.getMessage());
        }
    }

    public void testInputFileNotFound()
        throws Exception
    {
        JavadocWarningsTextToXml bean = new JavadocWarningsTextToXml();

        bean.setInputFileName("junk.dat");
        bean.setOutputFileName("test.xml");
        bean.setOutputEncoding("ISO-8859-1]");

        try
        {
            bean.build();
            fail("Expected null pointer exception for input file name");
        }
        catch (FileNotFoundException e)
        {
            ; // expected file not found
        }
    }

    public void testOutputFile()
        throws Exception
    {
        JavadocWarningsTextToXml bean = new JavadocWarningsTextToXml();

        bean.setInputFileName("report.txt");
        bean.setOutputEncoding("ISO-8859-1]");

        try
        {
            bean.build();
            fail("Expected null pointer exception for input file name");
        }
        catch (IllegalArgumentException e)
        {
            assertEquals("exception message", OUTPUT_FILE_NAME_EXCEPTION,
                e.getMessage());
        }
    }
}
