Package org.ozoneDB.test

Source Code of org.ozoneDB.test.OzoneTestRunner

// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.
//
// $Id: OzoneTestRunner.java,v 1.2 2001/12/30 16:57:49 per_nyfelt Exp $

package org.ozoneDB.test;

import java.io.File;
import java.io.PrintWriter;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestResult;
import junit.runner.BaseTestRunner;
import junit.runner.TestSuiteLoader;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.ozoneDB.ExternalDatabase;
import org.ozoneDB.Setup;
import org.ozoneDB.tools.Install;

/**
* OzoneTestRunner is the JUnit test runner for Ozone
* environment.
*
* @author <a href="http://www.softwarebuero.de/">SMB</a>
* @author <a href="mailto:david@d11e.com">David Li</a>
* @version $Revision: 1.2 $Date: 2001/12/30 16:57:49 $
*/
public class OzoneTestRunner extends BaseTestRunner {

    /**
     * log4j logger
     */
    private static Category fLog = Category.getInstance(OzoneTestRunner.class);

    /**
     * Print the usage of the program
     */
    private static void printUsage() {
        System.out.println("usage: ojvm "
                           + OzoneTestRunner.class.getName()
                           + " {-local | -remote [-host=HOST] [-port=PORT]} [-debug]"
                           + " TestCase");
        System.exit(1);
    }

    /**
     * The database
     */
    private ExternalDatabase fDatabase;

    /**
     * whether to use remote database
     */
    private boolean fIsRemote = false;

    /**
     * whether to use local database
     */
    private boolean fIsLocal = false;

    /**
     * whether to do debugging print out.
     */
    private boolean fDebug;

    /**
     * database server hostname
     */
    private String fDBHost = "localhost";

    /**
     * database server port
     */
    private int fDBPort = 3333;

    /**
     * default dir to create test database
     */
    private File fLocalTestDir = new File(System.getProperty("java.io.tmpdir")
                                          + File.separator + "OzoneTest");

    /**
     * name of the test suite to run
     */
    private String fTestSuite;

    /**
     * Constructor
     */
    private OzoneTestRunner(String[] args) {
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-local")) {
                fIsLocal = true;
            } else if (args[i].equals("-remote")) {
                fIsRemote = true;
            } else if (args[i].startsWith("-host=")) {
                fDBHost = args[i].substring(6);
            } else if (args[i].startsWith("-port=")) {
                fDBPort = Integer.parseInt(args[i].substring(6));
            } else if (args[i].equals("-debug")) {
                fDebug = true;
                Category.getRoot().setPriority(Priority.DEBUG);
            } else {
                fTestSuite = args[i];
            }
        }
    }

    /**
     * initialize the database
     * @param dbURL the database url
     */
    private void initDB(String dbURL) throws Exception {
        fLog.debug("initDB(): open database at '" + dbURL + "'");
        fDatabase = ExternalDatabase.openDatabase(dbURL);
        fDatabase.reloadClasses();
    }

    /**
     * Initialize a remote database.
     * @param host hostname of the database machine
     * @param port port number of the db server
     */
    private void initRemoteDB(String host, int port) throws Exception {
        initDB("ozonedb:remote://" + fDBHost + ":" + fDBPort);
    }

    /**
     * The test class loader for the runner.
     */
    private static TestSuiteLoader fTestSuiteLoader = new OzoneTestSuiteLoader();

    /**
     * overrideing this method from BaseTestRunner is necessary
     * because of the default behavior of getLoader creates new
     * classloader everytime it's called. This cause problem with
     * Ozone.
     */
    public TestSuiteLoader getLoader() {
        fLog.debug("getLoader ()");
        return fTestSuiteLoader;
    }

    /**
     * initialize a local database
     * if one doesn't exists, create it.
     */
    private void initLocalDB() throws Exception {
        fLocalTestDir.mkdirs();
        File dbDir = File.createTempFile("OZT", null, fLocalTestDir);
        Setup defaults = new Setup(null);
        defaults.addProperties(System.getProperties(), "ozoneDB.");
        Install.createDB(dbDir.getPath() + ".dir", defaults, new PrintWriter(System.out, true));

        initDB("ozonedb:local://" + dbDir.getPath() + ".dir");
    }

    /**
     * set up the database
     */
    private void setUpDatabase() throws Exception {
        fLog.info("Test: remote=" + fIsRemote + " local=" + fIsLocal);
        if (fIsLocal)
            initLocalDB();
        else if (fIsRemote)
            initRemoteDB(fDBHost, fDBPort);
    }

    /**
     * run the test suite
     */
    private TestResult doRun() throws Exception {
        TestResult result = new TestResult();
        Test suite = getTest(fTestSuite);
        result.addListener(this);
        try {
            setUpDatabase();
            suite.run(result);
        } finally {
            if (fDatabase != null)
                fDatabase.close();
        }
        return result;
    }


    /**
     * main
     */
    public static void main(String[] args) {

        BasicConfigurator.configure();
        Category.getRoot().setPriority(Priority.INFO);

        if (args.length == 0) {
            printUsage();
        }

        try {
            OzoneTestRunner runner = new OzoneTestRunner(args);
            TestResult result = runner.doRun();
            fLog.info("run: " + result.runCount()
                      + " error: " + result.errorCount()
                      + " failure: " + result.failureCount());
        } catch (Exception e) {
            fLog.error("run fails", e);
        }
    }

    /*
     * junit.runner.BaseTestRunner methods
     */
    protected void runFailed(String message) {
    }

    /*
     * junit.framework.TestListener methods
     */
    public void addError(Test test, Throwable t) {
        fLog.info("addError(): " + test, t);
    }

    public void addFailure(Test test, AssertionFailedError t) {
        fLog.info("addFailure(): " + test, t);
    }

    public void endTest(Test test) {
        fLog.debug("endTest(): " + test);
    }

    public void startTest(Test test) {
        fLog.info("startTest(): " + test);
        if (test instanceof OzoneTestCase) {
            ((OzoneTestCase) test).setDB(fDatabase);
        }
    }
}
TOP

Related Classes of org.ozoneDB.test.OzoneTestRunner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.