Search in sources :

Example 6 with TestFactory

use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.

the class RegressionSuiteMinimizer method minimizeSuite.

private void minimizeSuite(RegressionTestSuiteChromosome suite) {
    // logger.warn("minimizeSuite:\n{}", suite);
    Iterator<TestChromosome> it = suite.getTestChromosomes().iterator();
    int testCount = 0;
    while (it.hasNext()) {
        if (isTimeoutReached()) {
            logger.warn("minimization timeout reached. skipping minimization");
            break;
        }
        // logger.warn("##########################   TEST{}   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
        // testCount);
        RegressionTestChromosome test = (RegressionTestChromosome) it.next();
        // see if the assertion is still failing after removing the statement
        for (int i = test.getTheTest().size() - 1; i >= 0; i--) {
            if (isTimeoutReached()) {
                logger.warn("minimization timeout reached. skipping minimization");
                break;
            }
            logger.debug("Current size: " + suite.size() + "/" + suite.totalLengthOfTestCases());
            logger.debug("Deleting statement {} " + test.getTheTest().getTestCase().getStatement(i).getCode() + " from test", i);
            TestChromosome originalTestChromosome = (TestChromosome) test.getTheTest().clone();
            executeTest(test);
            /*
         * if(test.getLastExecutionResult()==null ||
				 * test.getLastRegressionExecutionResult()==null){
				 * logger.error("test execution result was null"); //continue; }
				 */
            // originalTestChromosome.setLastExecutionResult(test.getLastExecutionResult());
            int preRemovalAssertions = numFailingAssertions(test);
            try {
                TestFactory testFactory = TestFactory.getInstance();
                testFactory.deleteStatementGracefully(test.getTheTest().getTestCase(), i);
                test.getTheTest().setChanged(true);
            } catch (ConstructionFailedException e) {
                test.getTheTest().setChanged(false);
                test.getTheTest().setTestCase(originalTestChromosome.getTestCase());
                logger.error("Deleting failed");
                continue;
            }
            RegressionTestChromosome rtc = new RegressionTestChromosome();
            rtc.setTest((TestChromosome) test.getTheTest().clone());
            // rtc.updateClassloader();
            executeTest(rtc);
            int postRemovalAssertions = numFailingAssertions(rtc);
            // preRemovalAssertions, postRemovalAssertions);
            if (postRemovalAssertions == preRemovalAssertions) {
                test.updateClassloader();
                // the change had no effect
                continue;
            } else {
                // Restore previous state
                logger.debug("Can't remove statement " + originalTestChromosome.getTestCase().getStatement(i).getCode());
                test.getTheTest().setTestCase(originalTestChromosome.getTestCase());
                test.getTheTest().setLastExecutionResult(originalTestChromosome.getLastExecutionResult());
                test.getTheTest().setChanged(false);
            }
        }
        test.updateClassloader();
        if (test.getTheTest().isChanged()) {
            executeTest(test);
        }
        testCount++;
    }
}
Also used : TestFactory(org.evosuite.testcase.TestFactory) TestChromosome(org.evosuite.testcase.TestChromosome) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 7 with TestFactory

use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.

the class RegressionSuiteMinimizer method removeDuplicateExceptions.

private void removeDuplicateExceptions(RegressionTestSuiteChromosome suite) {
    Set<String> uniqueExceptions = new HashSet<>();
    Map<String, String> exceptionStatementMapping = new HashMap<>();
    List<TestChromosome> chromosomes = suite.getTestChromosomes();
    for (int i = 0; i < chromosomes.size(); i++) {
        RegressionTestChromosome test = (RegressionTestChromosome) chromosomes.get(i);
        boolean changed = false;
        boolean hadAssertion = test.getTheTest().getTestCase().getAssertions().size() > 0;
        ExecutionResult resultA = test.getLastExecutionResult();
        ExecutionResult resultB = test.getLastRegressionExecutionResult();
        // re-execute the tests and check
        if (resultA == null || resultB == null) {
            executeTest(test);
            resultA = test.getLastExecutionResult();
            resultB = test.getLastRegressionExecutionResult();
            if (resultA == null || resultB == null) {
                continue;
            }
        }
        Map<Integer, Throwable> exceptionMapA = resultA.getCopyOfExceptionMapping();
        Map<Integer, Throwable> exceptionMapB = resultB.getCopyOfExceptionMapping();
        // exceptionMapA.size(), exceptionMapB.size());
        if (!resultA.noThrownExceptions() || !resultB.noThrownExceptions()) {
            double exDiff = RegressionExceptionHelper.compareExceptionDiffs(exceptionMapA, exceptionMapB);
            logger.warn("Test{} - Difference in number of exceptions: {}", i, exDiff);
            if (exDiff > 0) {
                /*
           * Three scenarios:
           * 1. Same exception, different messages
           * 2. Different exception in A
           * 3. Different exception in B
           */
                for (Entry<Integer, Throwable> ex : exceptionMapA.entrySet()) {
                    Throwable exA = ex.getValue();
                    // unique statement key over all tests (to avoid removing the same exception twice)
                    String exKey = i + ":" + ex.getKey();
                    // exceptionA signatures
                    String exception = RegressionExceptionHelper.simpleExceptionName(test, ex.getKey(), exA);
                    String exSignatureA = RegressionExceptionHelper.getExceptionSignature(exA, Properties.TARGET_CLASS);
                    String signaturePair = exSignatureA + ",";
                    Throwable exB = exceptionMapB.get(ex.getKey());
                    if (exB != null) {
                        // if the same statement in B also throws an exception
                        // exceptionB signatures
                        String exceptionB = RegressionExceptionHelper.simpleExceptionName(test, ex.getKey(), exB);
                        String exSignatureB = RegressionExceptionHelper.getExceptionSignature(exA, Properties.TARGET_CLASS);
                        signaturePair += exSignatureB;
                        if (exception.equals(exceptionB) || exSignatureA.equals(exSignatureB)) {
                            // We will be taking care of removing this exception when checking from A to B
                            // so there isn't a need to check again from B to A
                            exceptionMapB.remove(ex.getKey());
                        }
                    }
                    logger.warn("Test{}, uniqueExceptions: {}", i, uniqueExceptions);
                    logger.warn("checking exception: {} at {}", exception, ex.getKey());
                    List<String> signatures = Arrays.asList(exception, signaturePair);
                    for (String sig : signatures) {
                        // Compare exceptions on the merit of their message or signature
                        if (uniqueExceptions.contains(sig) && exceptionStatementMapping.get(sig) != exKey && !hadAssertion) {
                            TestChromosome originalTestChromosome = (TestChromosome) test.getTheTest().clone();
                            try {
                                TestFactory testFactory = TestFactory.getInstance();
                                testFactory.deleteStatementGracefully(test.getTheTest().getTestCase(), ex.getKey());
                                test.getTheTest().setChanged(true);
                                logger.warn("Removed exceptionA throwing line {}", ex.getKey());
                            } catch (ConstructionFailedException e) {
                                test.getTheTest().setChanged(false);
                                test.getTheTest().setTestCase(originalTestChromosome.getTestCase());
                                logger.error("ExceptionA deletion failed");
                                continue;
                            }
                            changed = true;
                            break;
                        } else {
                            uniqueExceptions.add(sig);
                            exceptionStatementMapping.put(sig, exKey);
                        }
                    }
                }
                // Check from the other way around (B to A)
                for (Entry<Integer, Throwable> ex : exceptionMapB.entrySet()) {
                    String exception = RegressionExceptionHelper.simpleExceptionName(test, ex.getKey(), ex.getValue());
                    String exKey = i + ":" + ex.getKey();
                    logger.warn("Test{}, uniqueExceptions: {}", i, uniqueExceptions);
                    logger.warn("checking exceptionB: {} at {}", exception, ex.getKey());
                    if (uniqueExceptions.contains(exception) && exceptionStatementMapping.get(exception) != exKey && !hadAssertion && test.getTheTest().getTestCase().hasStatement(ex.getKey())) {
                        TestChromosome originalTestChromosome = (TestChromosome) test.getTheTest().clone();
                        try {
                            TestFactory testFactory = TestFactory.getInstance();
                            logger.warn("removing statementB: {}", test.getTheTest().getTestCase().getStatement(ex.getKey()));
                            testFactory.deleteStatementGracefully(test.getTheTest().getTestCase(), ex.getKey());
                            test.getTheTest().setChanged(true);
                            logger.warn("removed exceptionB throwing line {}", ex.getKey());
                        } catch (ConstructionFailedException e) {
                            test.getTheTest().setChanged(false);
                            test.getTheTest().setTestCase(originalTestChromosome.getTestCase());
                            logger.error("ExceptionB deletion failed");
                            continue;
                        }
                        changed = true;
                    } else {
                        uniqueExceptions.add(exception);
                        exceptionStatementMapping.put(exception, exKey);
                    }
                }
            }
        }
        if (changed) {
            test.updateClassloader();
            executeTest(test);
            i--;
        }
    }
    if (uniqueExceptions.size() > 0) {
        logger.warn("unique exceptions: {}", uniqueExceptions);
    }
}
Also used : HashMap(java.util.HashMap) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) TestFactory(org.evosuite.testcase.TestFactory) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 8 with TestFactory

use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.

the class ContractViolation method minimizeTest.

/**
 * Remove all statements that do not contribute to the contract violation
 */
public void minimizeTest() {
    if (isMinimized)
        return;
    /**
     * Factory method that handles statement deletion
     */
    TestFactory testFactory = TestFactory.getInstance();
    if (Properties.INLINE) {
        ConstantInliner inliner = new ConstantInliner();
        inliner.inline(test);
    }
    TestCase origTest = test.clone();
    List<Integer> positions = new ArrayList<Integer>();
    for (VariableReference var : variables) positions.add(var.getStPosition());
    int oldLength = test.size();
    boolean changed = true;
    while (changed) {
        changed = false;
        for (int i = test.size() - 1; i >= 0; i--) {
            // TODO - why??
            if (i >= test.size())
                continue;
            if (positions.contains(i))
                continue;
            try {
                boolean deleted = testFactory.deleteStatement(test, i);
                if (!deleted) {
                    continue;
                }
                if (!contract.fails(test)) {
                    test = origTest.clone();
                } else {
                    changed = true;
                    for (int j = 0; j < positions.size(); j++) {
                        if (positions.get(j) > i) {
                            positions.set(j, positions.get(j) - (oldLength - test.size()));
                        }
                    }
                    origTest = test.clone();
                    oldLength = test.size();
                }
            } catch (ConstructionFailedException e) {
                test = origTest.clone();
            }
        }
    }
    statement = test.getStatement(test.size() - 1);
    for (int i = 0; i < variables.size(); i++) {
        variables.set(i, test.getStatement(positions.get(i)).getReturnValue());
    }
    contract.addAssertionAndComments(statement, variables, exception);
    isMinimized = true;
}
Also used : ConstantInliner(org.evosuite.testcase.ConstantInliner) VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFactory(org.evosuite.testcase.TestFactory) ArrayList(java.util.ArrayList) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 9 with TestFactory

use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.

the class ReferenceLocalSearch method addCall.

/**
 * Add a method call on the return value of the object at position statement
 *
 * @param test
 * @param statement
 */
private boolean addCall(TestChromosome test, int statement) {
    logger.debug("Adding call");
    TestFactory factory = TestFactory.getInstance();
    Statement theStatement = test.getTestCase().getStatement(statement);
    VariableReference var = theStatement.getReturnValue();
    int oldLength = test.size();
    factory.insertRandomCallOnObjectAt(test.getTestCase(), var, statement + 1);
    test.setChanged(test.size() != oldLength);
    return false;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestFactory(org.evosuite.testcase.TestFactory) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement)

Example 10 with TestFactory

use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.

the class AllMethodsTestChromosomeFactory method getRandomTestCase.

/**
 * Create a random individual
 *
 * @param size
 */
private TestCase getRandomTestCase(int size) {
    boolean tracerEnabled = ExecutionTracer.isEnabled();
    if (tracerEnabled)
        ExecutionTracer.disable();
    TestCase test = getNewTestCase();
    int num = 0;
    // Choose a random length in 0 - size
    int length = Randomness.nextInt(size);
    while (length == 0) length = Randomness.nextInt(size);
    // Then add random stuff
    while (test.size() < length && num < Properties.MAX_ATTEMPTS) {
        if (remainingMethods.size() == 0) {
            reset();
        }
        GenericAccessibleObject<?> call = Randomness.choice(remainingMethods);
        attemptedMethods.add(call);
        remainingMethods.remove(call);
        try {
            TestFactory testFactory = TestFactory.getInstance();
            if (call.isMethod()) {
                testFactory.addMethod(test, (GenericMethod) call, test.size(), 0);
            } else if (call.isConstructor()) {
                testFactory.addConstructor(test, (GenericConstructor) call, test.size(), 0);
            } else {
                assert (false) : "Found test call that is neither method nor constructor";
            }
        } catch (ConstructionFailedException e) {
        }
        num++;
    }
    if (logger.isDebugEnabled())
        logger.debug("Randomized test case:" + test.toCode());
    if (tracerEnabled)
        ExecutionTracer.enable();
    return test;
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFactory(org.evosuite.testcase.TestFactory) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Aggregations

TestFactory (org.evosuite.testcase.TestFactory)13 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)8 VariableReference (org.evosuite.testcase.variable.VariableReference)7 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)6 MethodStatement (org.evosuite.testcase.statements.MethodStatement)6 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)5 Method (java.lang.reflect.Method)4 TestCase (org.evosuite.testcase.TestCase)4 GenericClass (org.evosuite.utils.generic.GenericClass)4 GenericMethod (org.evosuite.utils.generic.GenericMethod)4 NullStatement (org.evosuite.testcase.statements.NullStatement)3 Statement (org.evosuite.testcase.statements.Statement)3 TestChromosome (org.evosuite.testcase.TestChromosome)2 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)2 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)2 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 FieldStatement (org.evosuite.testcase.statements.FieldStatement)2 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1