DAO Medge Code Examples
Back to the Main Page

Abstract Test Class

This is the base class for all of the test classes.

The user name for all in "test_suite" this is used in who_changed etc, it is not the database login.

package au.id.medge.daotester.testsuite;

import au.id.medge.dao.DAO;
import au.id.medge.dao.utilities.DAOException;
import au.id.medge.daotester.dao.DAOTesterDAO;
import java.sql.SQLException;

public abstract class AbstractTestSuite {
    private static final String TAB = "\t";
    private DAOTesterDAO daoTesterDAO;
    private final String testName;
    private final boolean disconnect = false;

    public AbstractTestSuite(String testName) {
        this.testName = testName;
    }

    protected DAO getDAO() {
        if (daoTesterDAO == null) {
            daoTesterDAO = new DAOTesterDAO("test_suite");
        }
        return daoTesterDAO.getDAO();
    }

    protected void disconnect() throws DAOException, SQLException {
        if (disconnect) {
            daoTesterDAO.getDAO().disconnect();
        }
    }
    
    private void reportFailure(Throwable ex) {
        reportFailure(ex, 0);
    }

    private void reportFailure(Throwable ex, int depth) {
        for (int i = 0;  i < depth; i++) {
            System.err.print(TAB);
        }
        System.err.println(ex.getMessage());
        for (StackTraceElement stackTrace : ex.getStackTrace()) {
            for (int i = 0;  i < depth; i++) {
                System.err.print(TAB);
            }
            System.err.print(TAB);
            System.err.print(stackTrace.getClassName());
            System.err.print(".");
            System.err.print(stackTrace.getModuleName());
            System.err.print("(");
            System.err.print(stackTrace.getLineNumber());
            System.err.println(")");
        }
        
        if (ex.getCause() != null) {
            reportFailure(ex.getCause(), depth + 1);
        }
    }

    protected void reportFailure(String failure) {
        System.err.println("Failure - " + testName);
        System.err.println(TAB + failure);
    }

    protected void reportFailure(String failure, Throwable ex) {
        System.err.println("Failure - " + testName);
        System.err.println(TAB + failure);
        reportFailure(ex);
    }

    protected void reportSuccess(String success) {
        System.out.println("Success - " + testName);
        System.out.println(TAB + success);
    }
    
    public abstract void runTestSuite();
    
}
Back to the Main Page