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

com.peachapisecurity.tg.junit4.JUnit4PeachRunner Maven / Gradle / Ivy

The newest version!

/*
 * Copyright 2017 Peach Tech
 * 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 com.peachapisecurity.tg.junit4;

import com.peachapisecurity.api.PeachState;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.internal.runners.statements.Fail;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

public class JUnit4PeachRunner extends BlockJUnit4ClassRunner {

    PeachContext _context = null;
    Description _currentDescription = null;
    
    public JUnit4PeachRunner(PeachContext context, Class klass) throws org.junit.runners.model.InitializationError {
        super(klass);
        
        _context = context;
    }

    @Override
    protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
        
        if(_context.debug)
            System.out.println(">> runChild: " + method.getName());
        
        RunNotifier fauxNotifier = new RunNotifier();
        Description description = describeChild(method);
        if (isIgnored(method)) {
            notifier.fireTestIgnored(description);
        } else {
            _currentDescription = description;
            
            EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description);
            eachNotifier.fireTestStarted();
            eachNotifier.fireTestFinished();
            
            Statement statement;
            try {
                statement = methodBlock(method);
            }
            catch (Throwable ex) {
                statement = new Fail(ex);
            }
            
            if(_context.state == PeachState.Error)
            {
                if(_context.debug)
                    System.out.println("<< runChild: Error state, returning");
                
                return;
            }
            
            // rest state if needed
            _context.state = PeachState.Continue;
            
            while(_context.state.equals(PeachState.Continue))
            {
                if(_context.debug)
                    System.out.println("runChild.runLeaf()");
            
                runLeaf(statement, description, fauxNotifier);
                
                if(_context.state == PeachState.Unknown)
                {
                    if(_context.debug)
                        System.out.println("runChild: runLeaf exit w/o calling testTeardown");
                    
                    _context.state = _context.proxy.testTeardown();
                }
            }
            
            if(_context.state == PeachState.Error)
            {
                if(_context.debug)
                    System.out.println("runChild: Error state, notifier.pleaseStope.");
                
                notifier.pleaseStop();
            }
        }
        
        if(_context.debug)
            System.out.println("<< runChild");
    }
    
    @Override
    protected List computeTestMethods() {
        List methods = getTestClass().getAnnotatedMethods(Test.class);
        
        return methods;
    }

    @Override
    protected Statement methodInvoker(FrameworkMethod method, Object test) {
        if(_context.debug)
            System.out.println(">> methodInvoker");
        
        return new PeachInvokeMethod(_context, method.getName(), method, test);
    }
    
    @Override
    protected Statement withBefores(FrameworkMethod method, Object target,
        Statement statement) {
        
        if(_context.debug)
            System.out.println(">> withBefores");
        
        List befores = getTestClass().getAnnotatedMethods(
            Before.class);
        
        return befores.isEmpty() ? statement : new PeachRunBefores(_context, statement,
            befores, target);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy