/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.*;
import org.codehaus.plexus.util.*;

boolean result = true;
try
{
    String encoding = "UTF-8";

    Reader fileReader = null;

    File buildLog = new File( basedir, "build.log" );
    String logContent;
    try
    {
        fileReader = ReaderFactory.newReader( buildLog, encoding );
        logContent = IOUtil.toString( fileReader );
    }
    finally
    {
        IOUtil.close( fileReader );
    }

    File javaFile = new File( basedir, "/src/main/java/fix/test/App.java" );
    String content;
    try
    {
        fileReader = ReaderFactory.newReader( javaFile, encoding );
        content = IOUtil.toString( fileReader );
    }
    finally
    {
        IOUtil.close( fileReader );
    }

    result = result && ( StringUtils.countMatches( content, " * App class" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @generatorClass toto" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @todo review it" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @version 1.0" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @author <a href=\"mailto:vincent.siveton@gmail.com\">Vincent Siveton</a>" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @since 1.0" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * The main method" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param args  an array of strings that contains the arguments" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * <p>sampleMethod</p>" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param str a {@link java.lang.String} object." ) == 1 );

    javaFile = new File( basedir, "/src/main/java/fix/test/ClassWithNoJavadoc.java" );
    try
    {
        fileReader = ReaderFactory.newReader( javaFile, encoding );
        content = IOUtil.toString( fileReader );
    }
    finally
    {
        IOUtil.close( fileReader );
    }

    result = result && ( StringUtils.countMatches( content, " * <p>ClassWithNoJavadoc class.</p>" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @version $Id: $" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @since 1.0" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * <p>main</p>" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param args an array of {@link java.lang.String} objects." ) == 1 );
    // private sampleMethod
    result = result && ( StringUtils.countMatches( content, "     * <p>sampleMethod</p>" ) == 0 );
    result = result && ( StringUtils.countMatches( content, "     * @param str a {@link java.lang.String} object." ) == 0 );

    javaFile = new File( basedir, "/src/main/java/fix/test/ClassWithJavadoc.java" );
    try
    {
        fileReader = ReaderFactory.newReader( javaFile, encoding );
        content = IOUtil.toString( fileReader );
    }
    finally
    {
        IOUtil.close( fileReader );
    }

    result = result && ( StringUtils.countMatches( content, " * Some Javadoc." ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @since 1.0" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "    /** Constant <code>MY_STRING_CONSTANT=\"value\"</code> */" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "    /** Constant <code>MY_INT_CONSTANT=1</code> */" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "    /** Constant <code>EOL=\"System.getProperty( line.separator )\"</code> */" ) == 1 );
    // private constant
    result = result && ( StringUtils.countMatches( content, "    /** Constant <code>MY_PRIVATE_CONSTANT=\"\"</code> */" ) == 0 );
    result = result && ( StringUtils.countMatches( content, "     * <p>main</p>" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param args an array of {@link java.lang.String} objects." ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * <p>methodWithMissingParameters</p>" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param str a {@link java.lang.String} object." ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param b a boolean." ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param i a int." ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @return a {@link java.lang.String} object." ) == 1 );

    // check unknown param
    result = result && ( StringUtils.countMatches( logContent, "[WARNING] Fixed unknown param 'str' defined in fix.test.ClassWithJavadoc#methodWithWrongJavadocParameters(aString)" ) == 1 );

    javaFile = new File( basedir, "/src/main/java/fix/test/InterfaceWithNoJavadoc.java" );
    try
    {
        fileReader = ReaderFactory.newReader( javaFile, encoding );
        content = IOUtil.toString( fileReader );
    }
    finally
    {
        IOUtil.close( fileReader );
    }

    result = result && ( StringUtils.countMatches( content, " * <p>InterfaceWithNoJavadoc interface.</p>" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @version $Id: $" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @since 1.0" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "    /** Constant <code>MY_STRING_CONSTANT=\"value\"</code> */" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * <p>method</p>" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param aString a {@link java.lang.String} object." ) == 1 );

    javaFile = new File( basedir, "/src/main/java/fix/test/InterfaceWithJavadoc.java" );
    try
    {
        fileReader = ReaderFactory.newReader( javaFile, encoding );
        content = IOUtil.toString( fileReader );
    }
    finally
    {
        IOUtil.close( fileReader );
    }

    result = result && ( StringUtils.countMatches( content, " * Some Javadoc." ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @version $Id: $" ) == 1 );
    result = result && ( StringUtils.countMatches( content, " * @since 1.0" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "    /** comment */" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * My method" ) == 1 );
    result = result && ( StringUtils.countMatches( content, "     * @param aString a {@link java.lang.String} object." ) == 1 );

}
catch( Throwable e )
{
    e.printStackTrace();
    result = false;
}

return result;
