package org.apache.maven.abbot;

/* ====================================================================
 *   Copyright 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 junit.extensions.abbot.ScriptFixture;
import junit.extensions.abbot.ScriptTestSuite;
import junit.framework.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * TestAll is the entry point for all Abbot functional tests.
 * It executes all xml files located in the passed src/abbot/ dir.
 */
public class AbbotTestAll extends ScriptFixture
{
    /**
     * Location of Abbot scripts
     */
    private static final String TESTPATH = 
        System.getProperty("maven.abbot.src.dir");

    /**
     * Should we recurse in the directory where Abbot scripts are located?
     */
    private static final boolean RECURSE =
        Boolean.getBoolean("maven.abbot.recurse");

    /**
     * Custom ScriptTestSuite to only accept xml files.
     */
    public static class MavenScriptTestSuite extends ScriptTestSuite
    {
        public MavenScriptTestSuite(Class fixtureClass, String dirName, 
            boolean recurse)
        {
            super(fixtureClass, dirName, recurse);
        }

        public MavenScriptTestSuite(Class fixtureClass, String[] filenames)
        {
            super(fixtureClass, filenames);
        }

        public boolean accept(File file)
		{
		    return file.getName().endsWith(".xml");
		}        
    }
    
    /**
     * Return test suite containing all scripts.
     */
    public static Test suite()
    {
        MavenScriptTestSuite suite;
        String[] files = getScriptFileNames();
        
        if (files == null)
        {        
            suite = new MavenScriptTestSuite(ScriptFixture.class, TESTPATH, 
                RECURSE);
        }
        else
        {
            suite = new MavenScriptTestSuite(ScriptFixture.class, files);
        }
        
        return suite;
    }

    /**
     * @see ScriptFixture#ScriptFixture(java.lang.String)
     */
    public AbbotTestAll(String name)
    {
        super(name);
    }

    /**
     * @return the file list (as a String array) specified by the optional 
     *         <code>maven.abbot.src.files</code> Maven property or null
     *         if the property is not defined. Also returns null if there
     *         are no files defined in the <code>maven.abbot.src.files</code>
     *         property. 
     */
    protected static String[] getScriptFileNames()
    {
        String files = System.getProperty("maven.abbot.src.files");
        String[] result = null;

        if (files != null)
        {
            List fileList = new ArrayList();
            StringTokenizer st = new StringTokenizer(files, ",");
            while (st.hasMoreTokens())
            {
                fileList.add(st.nextToken());
            }

            if (!fileList.isEmpty())
            {
                result = (String[]) fileList.toArray(new String[0]);
            }
        }

        return result;
    }
        
}
