Search in sources :

Example 1 with VMError

use of org.evosuite.dse.VMError in project evosuite by EvoSuite.

the class TestRunnable method executeStatements.

/**
 * Iterate over all statements in the test case, and execute them one at a time
 *
 * @param result
 * @param out
 * @param num
 * @throws TimeoutException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws VMError
 * @throws EvosuiteError
 */
private void executeStatements(ExecutionResult result, PrintStream out, AtomicInteger num) throws TimeoutException, InvocationTargetException, IllegalAccessException, InstantiationException, VMError, EvosuiteError {
    for (Statement s : test) {
        if (Thread.currentThread().isInterrupted() || Thread.interrupted()) {
            logger.info("Thread interrupted at statement " + num + ": " + s.getCode());
            throw new TimeoutException();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Executing statement " + s.getCode());
        }
        ExecutionTracer.statementExecuted();
        informObservers_before(s);
        /*
			 * Here actually execute a statement of the SUT
			 */
        Throwable exceptionThrown = s.execute(scope, out);
        if (exceptionThrown != null) {
            // -------------------------------------------------------
            if (exceptionThrown instanceof VMError) {
                throw (VMError) exceptionThrown;
            }
            if (exceptionThrown instanceof EvosuiteError) {
                throw (EvosuiteError) exceptionThrown;
            }
            /*
				 * This is implemented in this way due to ExecutionResult.hasTimeout()
				 */
            if (exceptionThrown instanceof TestCaseExecutor.TimeoutExceeded) {
                logger.debug("Test timed out!");
                exceptionsThrown.put(test.size(), exceptionThrown);
                result.setThrownExceptions(exceptionsThrown);
                result.reportNewThrownException(test.size(), exceptionThrown);
                result.setTrace(ExecutionTracer.getExecutionTracer().getTrace());
                break;
            }
            // keep track if the exception and where it was thrown
            exceptionsThrown.put(num.get(), exceptionThrown);
            // --------------------------------------------------------
            if (ExecutionTracer.getExecutionTracer().getLastException() == exceptionThrown) {
                result.explicitExceptions.put(num.get(), true);
            } else {
                result.explicitExceptions.put(num.get(), false);
            }
            // --------------------------------------------------------
            printDebugInfo(s, exceptionThrown);
            /*
				 * If an exception is thrown, we stop the execution of the test case, because the internal state could be corrupted, and not
				 * possible to verify the behavior of any following function call. Predicate should be true by default
				 */
            if (Properties.BREAK_ON_EXCEPTION || exceptionThrown instanceof SystemExitException) {
                informObservers_after(s, exceptionThrown);
                break;
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Done statement " + s.getCode());
        }
        informObservers_after(s, exceptionThrown);
        num.incrementAndGet();
    }
    // end of loop
    informObservers_finished(result);
// TODO
}
Also used : Statement(org.evosuite.testcase.statements.Statement) SystemExitException(org.evosuite.runtime.System.SystemExitException) TimeoutException(java.util.concurrent.TimeoutException) VMError(org.evosuite.dse.VMError)

Example 2 with VMError

use of org.evosuite.dse.VMError in project evosuite by EvoSuite.

the class TestRunnable method call.

/**
 * {@inheritDoc}
 */
@Override
public ExecutionResult call() {
    exceptionsThrown.clear();
    runFinished = false;
    ExecutionResult result = new ExecutionResult(test, null);
    // TODO: Moved this to TestCaseExecutor so it is not part of the test execution timeout
    // Runtime.getInstance().resetRuntime();
    ExecutionTracer.enable();
    PrintStream out = (Properties.PRINT_TO_SYSTEM ? System.out : new PrintStream(byteStream));
    byteStream.reset();
    if (!Properties.PRINT_TO_SYSTEM) {
        LoggingUtils.muteCurrentOutAndErrStream();
    }
    threadStopper.startRecordingTime();
    /*
		 *  need AtomicInteger as we want to get latest updated value even if exception is thrown in the 'try' block.
		 *  we practically use it as wrapper for int, which we can then pass by reference
		 */
    AtomicInteger num = new AtomicInteger(0);
    try {
        if (Properties.REPLACE_CALLS) {
            ShutdownHookHandler.getInstance().initHandler();
        }
        executeStatements(result, out, num);
    } catch (ThreadDeath e) {
        // can't stop these guys
        logger.info("Found error in " + test.toCode(), e);
        // this needs to be propagated
        throw e;
    } catch (TimeoutException | TestCaseExecutor.TimeoutExceeded e) {
        logger.info("Test timed out!");
    } catch (Throwable e) {
        if (e instanceof EvosuiteError) {
            logger.info("Evosuite Error!", e);
            throw (EvosuiteError) e;
        }
        if (e instanceof VMError) {
            logger.info("VM Error!", e);
            throw (VMError) e;
        }
        logger.info("Exception at statement " + num + "! " + e);
        for (StackTraceElement elem : e.getStackTrace()) {
            logger.info(elem.toString());
        }
        if (e instanceof java.lang.reflect.InvocationTargetException) {
            logger.info("Cause: " + e.getCause().toString(), e);
            e = e.getCause();
        }
        if (e instanceof AssertionError && e.getStackTrace()[0].getClassName().contains(PackageInfo.getEvoSuitePackage())) {
            logger.error("Assertion Error in evosuitecode, for statement \n" + test.getStatement(num.get()).getCode() + " \n which is number: " + num + " testcase \n" + test.toCode(), e);
            throw (AssertionError) e;
        }
        logger.error("Suppressed/ignored exception during test case execution on class " + Properties.TARGET_CLASS + ": " + e.getMessage(), e);
    } finally {
        if (!Properties.PRINT_TO_SYSTEM) {
            LoggingUtils.restorePreviousOutAndErrStream();
        }
        if (Properties.REPLACE_CALLS) {
            /*
				 * For simplicity, we call it here. Ideally, we could call it among the
				 * statements, with "non-safe" version, to check if any exception is thrown.
				 * But that would be quite a bit of work, which maybe is not really warranted 
				 */
            ShutdownHookHandler.getInstance().safeExecuteAddedHooks();
        }
        runFinished = true;
    }
    result.setTrace(ExecutionTracer.getExecutionTracer().getTrace());
    result.setExecutionTime(System.currentTimeMillis() - threadStopper.getStartTime());
    result.setExecutedStatements(num.get());
    result.setThrownExceptions(exceptionsThrown);
    result.setReadProperties(org.evosuite.runtime.System.getAllPropertiesReadSoFar());
    result.setWasAnyPropertyWritten(org.evosuite.runtime.System.wasAnyPropertyWritten());
    return result;
}
Also used : PrintStream(java.io.PrintStream) VMError(org.evosuite.dse.VMError) InvocationTargetException(java.lang.reflect.InvocationTargetException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)2 VMError (org.evosuite.dse.VMError)2 PrintStream (java.io.PrintStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SystemExitException (org.evosuite.runtime.System.SystemExitException)1 Statement (org.evosuite.testcase.statements.Statement)1