All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jmms.testing.DefaultTestCase Maven / Gradle / Ivy

/*
 * Copyright 2018 the original author or authors.
 *
 * 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.
 */

package jmms.testing;

import jmms.core.model.MetaTestCase;
import leap.lang.logging.Log;
import leap.lang.logging.LogFactory;
import leap.lang.time.StopWatch;

public class DefaultTestCase extends AbstractTestCase {

    private static final Log log = LogFactory.get(DefaultTestCase.class);

    private final TestStep[] steps;

    public DefaultTestCase(MetaTestCase meta, TestStep[] steps) {
        super(meta);
        this.steps = steps;
    }

    public TestStep[] getSteps() {
        return steps;
    }

    @Override
    public TestResult runTestCase(TestContext context, TestListener listener) throws Throwable {
        TestResult result = TestResult.pass(meta.getTitle());

        StopWatch sw0 = StopWatch.startNew();

        for(TestStep step : steps) {
            StepResult sr;
            context.setCurrentStep(step);

            try {
                listener.beforeRunStep(context, this, step);
                StopWatch sw = StopWatch.startNew();

                try {
                    sr = step.runTestStep(context, listener);
                    if (null == sr) {
                        sr = StepResult.pass(step.getMeta().getTitleOrExec());
                    }
                } catch (Throwable e) {
                    log.error(e.getMessage(), e);
                    sr = StepResult.fail(step.getMeta().getTitleOrExec(), e);
                }

                sr.setDuration(sw.getElapsedMilliseconds());
                result.addStepResult(sr);
                listener.finishRunStep(context, this, step, sr);
            }finally {
                context.setCurrentStep(null);
                context.setPreviousStep(step);
            }

            if (sr.isFail()) {
                result.setState(sr.getState());
                result.setMessage(sr.getMessage());
                break;
            }
        }

        result.setDuration(sw0.getElapsedMilliseconds());
        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy