/*
 * Created on Jul 15, 2003
 *
 */
package testsuite.test;

import org.apache.log4j.Logger;

import testsuite.bean.Beanable;

import testsuite.result.TestResult;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.Enumeration;
import java.util.Properties;


/**
 * <p>Super-class of all tests and benchs.</p>
 * <p><b>Warning :</b> if you want to create your tests and use them with <i>TestSuite</i>. The method <code>runTest()</code>
 * must be implemented like this :</p>
 * <p><code>
 * public void runTest(){<br/>
 * ... initTest() ... <br/>
 * ... preConditions() ... <br/>
 * ... // test's code ... <br/>
 * ... postConditions() ... <br/>
 * ... endTest() ... <br/>
 * }
 * </code></p>
 *
 * @author Alexandre di Costanzo
 *
 */
public abstract class AbstractTest implements Serializable, Beanable {

    /** The name of the test. */
    private String name = "AbstractTest with no name";

    /** The Description of the test. */
    private String description = "AbstractTest with no description.";

    /** Logger. */
    protected static Logger logger = null;

    /**
     * Construct a new <code>AbstractTest</code> with defaults settings and a default logger.
     */
    public AbstractTest() {
        logger = Logger.getLogger(getClass().getName());
    }

    /**
     * Construct a new <code>AbstractTest</code>.
     * @param logger the logger of this test
     * @param name name of this test
     */
    public AbstractTest(String name) {
        logger = Logger.getLogger(getClass().getName());
        this.name = name;
    }

    /**
     * Construct a new <code>AbstractTest</code>.
     * @param logger the logger of this test
     * @param name name of this test
     * @param description description of this test
     */
    public AbstractTest(String name, String description) {
        logger = Logger.getLogger(getClass().getName());
        this.name = name;
        this.description = description;
    }

    /**
     * To check if the pre-conditions of a test are valids or not.
     * @return <b>true</b> if the pre-conditions are verified, <b>false</b> else
     * @throws Exception if an error is up during the pre-conditions validating
     */
    public abstract boolean preConditions() throws Exception;

    /**
     * To check if the post-conditions of a test are valids or not.
     * @return <b>true</b> if the post-conditions are verified, <b>false</b> else
     * @throws Exception if an error is up during the post-conditions validating
     */
    public abstract boolean postConditions() throws Exception;

    /**
     * To initialise the test, for example, open a network connection.
     * @throws Exception if an error is up during the intialisation
     */
    public abstract void initTest() throws Exception;

    /**
     * To end the test, for example, close a network connection.
     * @throws Exception if an error is up during the ending
     */
    public abstract void endTest() throws Exception;

    /**
     * Run a test.
     */
    public abstract TestResult runTest();

    public abstract boolean isFailed();

    /**
     * Gets the description of a test.
     * @return a String of the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * Gets the name of a test.
     * @return a String of the test
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the description of a test.
     * @param description a String of the description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Sets the name of a test.
     * @param name a String of the name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the logger of a test.
     * @return Logger of a test
     */
    public Logger getLogger() {
        return logger;
    }

    /**
     * Return a String with the name and the description of a test.
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return name + " : " + description;
    }

    /**
     * @see testsuite.bean.Beanable#loadAttributes()
     */
    public void loadAttributes() throws IOException {
        String filename = getClass().getResource(getClass().getName() +
                ".class").getPath();
        filename = filename.replaceAll(".class", ".prop");
        loadAttributes(new File(filename.replaceAll("%20", " ")));
    }

    /**
     * @see testsuite.bean.Beanable#loadAttributes(java.io.File)
     */
    public void loadAttributes(File propsFile) throws IOException {
        Properties props = new Properties();
        FileInputStream in = new FileInputStream(propsFile);
        props.load(in);
        in.close();

        loadAttributes(props);
    }

    /**
     * @see testsuite.bean.Beanable#loadAttributes(java.util.Properties)
     */
    public void loadAttributes(Properties properties) {
        Class[] parameterTypes = { String.class };
        Enumeration enum = properties.propertyNames();
        Method setter = null;

        while (enum.hasMoreElements()) {
            String name = (String) enum.nextElement();
            String value = properties.getProperty(name);
            try {
                setter = getClass().getMethod("set" + name, parameterTypes);
                Object[] args = { value };
                try {
                    setter.invoke(this, args);
                } catch (IllegalArgumentException e1) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Problem in loading attributes", e1);
                    }
                } catch (IllegalAccessException e1) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Problem in loading attributes", e1);
                    }
                } catch (InvocationTargetException e1) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Problem in loading attributes", e1);
                    }
                }
            } catch (SecurityException e) {
                // do nothing
            } catch (NoSuchMethodException e) {
                // do nothing
            }
        }
    }
}

@@ -117,24 +117,66 @@
 .Beanable;%0A%0A
+import testsuite.manager.AbstractManager;%0A
 import tests
@@ -1266,24 +1266,69 @@
 er = null;%0A%0A
+%09protected AbstractManager manager = null;%0A%09%0A
     /**%0A    
@@ -6511,10 +6511,208 @@
 %7D%0A    %7D%0A
+    %0A%09/**%0A%09 * @return%0A%09 */%0A%09public AbstractManager getManager() %7B%0A%09%09return manager;%0A%09%7D%0A%0A%09/**%0A%09 * @param manager%0A%09 */%0A%09public void setManager(AbstractManager manager) %7B%0A%09%09this.manager = manager;%0A%09%7D%0A%0A
 %7D%0A

Add access to the manager