/*
 * 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.*;

assertContains( String content, String value )
{
    if ( StringUtils.countMatches( content, value ) != 1 )
    {
        throw new IllegalArgumentException( "not found expected text: '" + value + "'" );
    }
}

assertNotContains( String content, String value )
{
    if ( StringUtils.countMatches( content, value ) != 0 )
    {
        throw new IllegalArgumentException( "found unexpected text: '" + value + "'" );
    }
}

boolean result = true;
try
{
    File buildLog = new File( basedir, "build.log" );
    String logContent = FileUtils.fileRead( buildLog, "UTF-8" );

    File javaFile = new File( basedir, "/src/main/java/fix/test/App.java" );
    String content = FileUtils.fileRead( javaFile, "UTF-8" );

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

    javaFile = new File( basedir, "/src/main/java/fix/test/ClassWithNoJavadoc.java" );
    content = FileUtils.fileRead( javaFile, "UTF-8" );

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

    javaFile = new File( basedir, "/src/main/java/fix/test/ClassWithJavadoc.java" );
    content = FileUtils.fileRead( javaFile, "UTF-8" );

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

    // check unknown param
    assertContains( logContent, "[WARNING] Fixed unknown param 'str' defined in fix.test.ClassWithJavadoc#methodWithWrongJavadocParameters(aString)" );

    javaFile = new File( basedir, "/src/main/java/fix/test/InterfaceWithNoJavadoc.java" );
    content = FileUtils.fileRead( javaFile, "UTF-8" );

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

    javaFile = new File( basedir, "/src/main/java/fix/test/InterfaceWithJavadoc.java" );
    content = FileUtils.fileRead( javaFile, "UTF-8" );

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

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

return result;
