tk.elevenk.restfulrobot.testsuite.TestSuite.java Source code

Java tutorial

Introduction

Here is the source code for tk.elevenk.restfulrobot.testsuite.TestSuite.java

Source

/* This source code file is part of RESTfulRobot
Copyright (C) 2014 John Krause (Eleven-K Software)
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.
    
You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package tk.elevenk.restfulrobot.testsuite;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import tk.elevenk.restfulrobot.RestfulRobot;
import tk.elevenk.restfulrobot.data.DataPool;
import tk.elevenk.restfulrobot.reporting.Reporter;
import tk.elevenk.restfulrobot.testcase.TestCase;

/**
 * The AdapterTestSuite serves as a container testSuite object to hold the list
 * of TestCases associated with each TestSuite file. The init function, when
 * called by the MainDriver, loops through each TestCase and runs it. All
 * functionality is contained within each TestCase object.
 * 
 * All TestCase objects contain a setUp(), run(), and tearDown(), and are run in
 * that order. setUp() and tearDown() can be empty.
 */
public class TestSuite {

    // ******************************************************************************************
    // * INSTANCE VARIABLES
    // *******************************************************************************************/
    public String baseURL, name;
    private ArrayList<TestCase> testCases = null;
    private Reporter reporter;
    private Logger logger;
    private int passes, failures, errors;
    private String timestamp, id;
    private float runtime;
    private ScriptEngine engine;
    private DataPool dataPool;

    // ******************************************************************************************
    // * CONSTRUCTOR
    // *******************************************************************************************/

    public TestSuite(String name, String baseURL, ArrayList<TestCase> testCases) {
        this.testCases = testCases;
        this.baseURL = baseURL;
        this.name = name;
        this.engine = new ScriptEngineManager().getEngineByName("javascript");
        this.dataPool = new DataPool();
        this.reporter = Reporter.getInstance();
    }

    // ******************************************************************************************
    // METHODS
    // *******************************************************************************************

    /**
     * Runs the test cases in the test suite
     */
    public void executeTestCases() {

        // initialize variables
        this.passes = this.failures = this.errors = 0;
        this.id = this.toString();
        this.timestamp = new Timestamp(Calendar.getInstance().getTime().getTime()).toString();
        long testSuiteTime = System.nanoTime();

        // add this test suite to the reporter if it has test cases
        if (this.testCases != null && this.testCases.size() > 0) {
            reporter.addTestSuite(this);

            // prepare the engine
            engine.put("DataPool", this.dataPool);
        }

        // loop through each test case and run them
        for (TestCase testCase : testCases) {

            // set current test case in data pool
            dataPool.setCurrentTestCase(testCase);

            // set the test suite of the test case to this suite
            testCase.setTestSuite(this);

            // add api definitions to script engine
            engine.put("Test", testCase);
            engine.put("Request", testCase.request);
            engine.put("Response", testCase.response);
            engine.put("defaults", null);
            engine.put("none", null);

            // set logger based on test case that is running
            logger = LogManager.getLogger(testCase.getClass().getName());
            logger.info("Starting test case of type " + testCase.getTestType());

            // run test script
            try {
                testCase.runTest(engine);
            } catch (Exception e) {
                logger.error(ExceptionUtils.getStackTrace(e));
            }
            // remove test case from engine context after completed
            engine.getBindings(ScriptContext.ENGINE_SCOPE).remove("Test");

        }

        // set runtime of test suite
        runtime = (System.nanoTime() - testSuiteTime) / RestfulRobot.TIME_DIVISOR;

    }

    /******************************************************************************************
     * GETTERS/SETTERS
     *******************************************************************************************/

    public ArrayList<TestCase> getTestCases() {
        return testCases;
    }

    public void setTestCases(ArrayList<TestCase> testCases) {
        this.testCases = testCases;
    }
}