package org.apache.maven.j2ee;

/* ====================================================================
 * 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 acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 *    "Apache Maven" 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",
 *    "Apache Maven", nor may "Apache" appear in their name, without
 *    prior written permission of the Apache Software Foundation.
 *
 * 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/>.
 */

import junit.framework.TestCase;

import org.apache.maven.TestConstants;

/**
 * Unit test for <code>WarValidator</code>.
 *
 * @author <a href="mailto:dion@multitask.com.au">dIon Gillard</a>
 * @version $Id$
 */
public class WarValidatorTest extends TestCase
{

    /** the validator being tested */
    private WarValidator validator = null;
    /** file name for the empty war*/
    private String emptyWarFile = null;
    /** file name for manifest-only war */
    private String manifestOnlyWarFile = null;
    /** file name for simple war (manifest & web.xml)*/
    private String simpleWarFile = null;
    /** file name for dummy war */
    private String dummyWarFile = null;
    /** file name for dummy jsp war (dummy war + a jsp-file entry) */
    private String dummyJspWarFile;
    /** file name for dummy taglib war (dummy war + 2 taglibs) */
    private String dummyTaglibWarFile;
    /** file name for error pages war (dummy war + 2 error pages) */
    private String errorPagesWarFile;
    /** file name for form login config war (dummy war + login config) */
    private String loginPagesWarFile;

    /**
     * Creates a new instance of WarValidatorTest
     * @param testName the name of the test
     */
    public WarValidatorTest(String testName)
    {
        super(testName);
    }

    /**
     * Initialize per test data
     * @throws Exception when there is an unexpected problem
     */
    public void setUp() throws Exception
    {
        validator = new WarValidator();
        String baseDir = System.getProperty("basedir");
        assertNotNull("The system property basedir was not defined.", baseDir);
        String j2eeDir = baseDir + TestConstants.J2EE_DIR;
        emptyWarFile = j2eeDir + "empty.war";
        manifestOnlyWarFile = j2eeDir + "manifest-only.war";
        simpleWarFile = j2eeDir + "simple.war";
        dummyWarFile = j2eeDir + "dummy-servlet.war";
        dummyJspWarFile = j2eeDir + "dummy-jsp.war";
        dummyTaglibWarFile = j2eeDir + "dummy-taglib.war";
        errorPagesWarFile = j2eeDir + "error-pages.war";
        loginPagesWarFile = j2eeDir + "login-pages.war";
    }

    /**
     * test that the warFileName property is mandatory for validation
     * @throws Exception when there is an unexpected problem
     */
    public void testWarFileMandatory() throws Exception
    {
        try
        {
            validator.execute();
            fail("war validator executed without a war file");
        }
        catch (NullPointerException e)
        {
            // this is the expected behaviour
        }
    }

    /**
     * test that an empty war file produces an error
     * @throws Exception when there is an unexpected problem
     */
    public void testEmptyWar() throws Exception
    {
        try
        {
            validator.setFailOnError(true);
            validator.setWarFileName(emptyWarFile);
            validator.addFormatter(new ValidationFormatter());
            validator.execute();
            fail("Empty war file (with no META-INF, or WEB-INF/web.xml) has"
                + " succeeded");
        }
        catch (IllegalStateException e)
        {
            // this is the expected behaviour
        }
    }

    /**
     * test that the failOnError property when set to false doesn't throw
     * an exception.
     * @throws Exception when there is an unexpected problem
     */
    public void testFailOnError() throws Exception
    {
        try
        {
            validator.setFailOnError(false);
            validator.setWarFileName(emptyWarFile);
            validator.addFormatter(new ValidationFormatter());
            validator.execute();
            assertTrue("Error not fired for an empty war file "
                + "(with no META-INF, or WEB-INF/web.xml)",
                validator.getStatus().isError());
        }
        catch (IllegalStateException e)
        {
            fail("Empty war file (with no META-INF, or WEB-INF/web.xml) failed"
                + " the build process with failOnError=false");
        }
    }

    /**
     * test that a manifest only war file produces a warning
     * @throws Exception when there is an unexpected problem
     */
    public void testManifestOnlyWar() throws Exception
    {
        validator.setWarFileName(manifestOnlyWarFile);
        validator.addFormatter(new ValidationFormatter());
        validator.execute();
        assertTrue("Manifest only war file (with no WEB-INF/web.xml) has"
            + " succeeded without a warning", 
            validator.getStatus().isWarning());
    }

    /**
     * test that a simple war (MANIFEST and web.xml) file produces no warning
     * @throws Exception when there is an unexpected problem
     */
    public void testSimpleWar() throws Exception
    {
        validator.setWarFileName(simpleWarFile);
        validator.addFormatter(new ValidationFormatter());
        validator.execute();
        assertTrue("Simple war file has generated a warning",
            !validator.getStatus().isWarning());
        assertTrue("Simple war file has generated an error",
            !validator.getStatus().isError());
    }

    /**
     * test that a dummy war file produces no warning and an error
     * (as there is an intentionally bad class (java.servlet.Servlet) in
     * web.xml)
     * @throws Exception when there is an unexpected problem
     */
    public void testDummyWar() throws Exception
    {
        validator.setWarFileName(dummyWarFile);
        validator.addFormatter(new ValidationFormatter());
        validator.setFailOnError(false);
        validator.execute();
        assertTrue("Dummy war file has generated a warning",
            !validator.getStatus().isWarning());
        assertTrue("Dummy war file has not generated an error",
            validator.getStatus().isError());
    }

    /**
     * FIXME: Need to finish coding
     * 
     * @throws Exception when there is an unexpected problem
     */
    public void testDummyJspWar() throws Exception
    {
        validator.setWarFileName(dummyJspWarFile);
        validator.addFormatter(new ValidationFormatter());
        validator.setFailOnError(false);
        validator.execute();
        assertTrue("Dummy jsp war file has not generated an error",
            validator.getStatus().isError());
    }
}
