package org.apache.maven.struts;

/* ====================================================================
 *   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.IOException;
import java.util.List;

import junit.framework.TestCase;

/**
 * Unit tests for {@link Struts10WarFile}
 *
 * @author dion
 * @version $Id: Struts10WarFileTest.java,v 1.2 2004/03/02 15:36:14 evenisse Exp $
 */
public class Struts10WarFileTest extends TestCase {

    /** instance to be tested */
    private Struts10WarFile instance;
    /** test war file */
    private String testWarFileName;

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

    /**
     * Initialize per test data
     * @throws Exception when there is an unexpected problem
     */
    public void setUp() throws Exception
    {
        String baseDir = System.getProperty("basedir");
        assertNotNull("The system property basedir was not defined.", baseDir);
        testWarFileName = baseDir + "/src/test-resources/struts/struts-example.war";
    }

    /** Test that the constructor succeeds
     * @throws Exception when there is an unexpected problem
     */
    public void testConstructor() throws Exception
    {
        instance = new Struts10WarFile(testWarFileName);
    }

    /** test that the config is defaulted correctly
     * @throws Exception when there is an unexpected problem
     */
    public void testDefaultConfig() throws Exception
    {
        testConstructor();
        assertEquals("Default config isn't WEB-INF/struts-config.xml",
            Struts10WarFile.DEFAULT_CONFIG,
            instance.getConfig());
    }

    /** test the the config property works
     * @throws Exception when there is an unexpected problem
     */
    public void testConfig() throws Exception
    {
        testDefaultConfig();
        String dummyConfig = "WEB-INF/struts.xml";
        instance.setConfig(dummyConfig);
        assertEquals("Set or get for config failed", dummyConfig,
            instance.getConfig());
    }

    /** test retrieving the struts config as a JarEntry
     * @throws Exception when there is an unexpected problem
     */
    public void testConfigEntry() throws Exception
    {
        testConstructor();
        assertNotNull("config entry was null", instance.getStrutsConfigEntry());
    }

    /** test the retrieval of form beans
     * @throws Exception when there is an unexpected problem
     */
    public void testFormBeans() throws Exception
    {
        testConstructor();
        assertEquals("Number of form beans returned was wrong", 3,
            instance.getFormBeans().size());
        List formBeans = instance.getFormBeans();
        assertEquals("first form bean isn't logonForm", "logonForm",
            ((FormBean)formBeans.get(0)).getName());
        assertEquals("second form bean isn't registrationForm",
            "registrationForm",
            ((FormBean)formBeans.get(1)).getName());
        assertEquals("third form bean isn't subscriptionForm",
            "subscriptionForm",
            ((FormBean)formBeans.get(2)).getName());
        assertEquals("form beans type is not default",
            "org.apache.struts.action.ActionFormBean",
            instance.getFormBeansType());
    }

    /** test the retrieval of actions
     * @throws Exception when there is an unexpected problem
     */
    public void testActions() throws Exception
    {
        testConstructor();
        assertEquals("Number of actions returned was wrong", 14,
            instance.getActions().size());
        List actions = instance.getActions();
        String[] types = new String[]
            {
            "org.apache.struts.webapp.example.EditRegistrationAction",
            "org.apache.struts.webapp.example.EditSubscriptionAction",
            "org.apache.struts.webapp.example.LogoffAction",
            "org.apache.struts.webapp.example.LogonAction",
            "org.apache.struts.webapp.example.SaveRegistrationAction",
            "org.apache.struts.webapp.example.SaveSubscriptionAction",
            null,
            "org.apache.struts.actions.AddFormBeanAction",
            "org.apache.struts.actions.AddForwardAction",
            "org.apache.struts.actions.AddMappingAction",
            "org.apache.struts.actions.ReloadAction",
            "org.apache.struts.actions.RemoveFormBeanAction",
            "org.apache.struts.actions.RemoveForwardAction",
            "org.apache.struts.actions.RemoveMappingAction"
            };
        for (int index = 0; index < types.length; index++)
        {
            assertEquals("action number " + index + " isn't " + types[index],
                types[index], ((Action) actions.get(index)).getType());
        }
    }

    /** test the forwards
     * @throws Exception when there is an unexpected problem
     */
    public void testForwards() throws Exception
    {
        testConstructor();
        assertEquals("Global forwards type is not correct",
            "org.apache.struts.action.ActionForward",
            instance.getGlobalForwardsType());
        assertEquals("Wrong number of forwards found", 3,
            instance.getForwards().size());
        String[] names = new String[] {"logoff", "logon", "success"};
        Forward forward = null;
        List forwards = instance.getForwards();
        for (int index = 0; index < names.length; index++)
        {
            forward = (Forward) forwards.get(index);
            assertEquals("Forward number " + index + " isn't "+ names[index],
                names[index], forward.getName());
        }
    }

    /** test the action servlet name property
     * @throws Exception when there is an unexpected problem
     */
    public void testActionServletName() throws Exception
    {
        testConstructor();
        assertEquals("default action servlet name is not 'action'", "action",
            Struts10WarFile.DEFAULT_ACTIONSERVLET_NAME);
        String dummy = "dummyName";
        instance.setActionServletName(dummy);
        assertEquals("action servlet name property get or set failed", dummy,
            instance.getActionServletName());
    }

    /** test the action servlet pattern method
     * @throws Exception when there is an unexpected problem
     */
    public void testActionServletPattern() throws Exception
    {
        testConstructor();
        assertEquals("action servlet pattern is wrong", "*.do",
            instance.getActionServletPattern());
    }
}
