Search in sources :

Example 16 with Mutation

use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.

the class TestGenerationResultBuilder method setTestCase.

public void setTestCase(String name, String code, TestCase testCase, String comment, ExecutionResult result) {
    testCode.put(name, code);
    testCases.put(name, testCase);
    Set<Failure> failures = new LinkedHashSet<Failure>();
    for (ContractViolation violation : testCase.getContractViolations()) {
        failures.add(new Failure(violation));
    }
    if (!Properties.CHECK_CONTRACTS && result.hasUndeclaredException()) {
        int position = result.getFirstPositionOfThrownException();
        Throwable exception = result.getExceptionThrownAtPosition(position);
        failures.add(new Failure(exception, position, testCase));
    }
    contractViolations.put(name, failures);
    testComments.put(name, comment);
    testLineCoverage.put(name, result.getTrace().getCoveredLines());
    uncoveredLines.removeAll(result.getTrace().getCoveredLines());
    Set<BranchInfo> branchCoverage = new LinkedHashSet<BranchInfo>();
    for (int branchId : result.getTrace().getCoveredFalseBranches()) {
        Branch branch = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(branchId);
        if (branch == null) {
            LoggingUtils.getEvoLogger().warn("Branch is null: " + branchId);
            continue;
        }
        BranchInfo info = new BranchInfo(branch.getClassName(), branch.getMethodName(), branch.getInstruction().getLineNumber(), false);
        branchCoverage.add(info);
    }
    for (int branchId : result.getTrace().getCoveredTrueBranches()) {
        Branch branch = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(branchId);
        if (branch == null) {
            LoggingUtils.getEvoLogger().warn("Branch is null: " + branchId);
            continue;
        }
        BranchInfo info = new BranchInfo(branch.getClassName(), branch.getMethodName(), branch.getInstruction().getLineNumber(), true);
        branchCoverage.add(info);
    }
    testBranchCoverage.put(name, branchCoverage);
    uncoveredBranches.removeAll(branchCoverage);
    Set<MutationInfo> mutationCoverage = new LinkedHashSet<MutationInfo>();
    for (Assertion assertion : testCase.getAssertions()) {
        for (Mutation m : assertion.getKilledMutations()) {
            mutationCoverage.add(new MutationInfo(m));
        }
    }
    testMutantCoverage.put(name, mutationCoverage);
    uncoveredMutants.removeAll(mutationCoverage);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Assertion(org.evosuite.assertion.Assertion) ContractViolation(org.evosuite.contracts.ContractViolation) Branch(org.evosuite.coverage.branch.Branch) Mutation(org.evosuite.coverage.mutation.Mutation)

Example 17 with Mutation

use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.

the class MutationAnalysisRunner method runChild.

@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
    logger.info("Running method " + method.getName());
    SimpleRunListener resultListener = new SimpleRunListener();
    notifier.addListener(resultListener);
    // First run without mutants
    ExecutionTracer.enable();
    super.runChild(method, notifier);
    boolean result = resultListener.hasFailure;
    logger.info("Result without mutant: " + result);
    if (result) {
        logger.info("Failure: " + resultListener.lastFailure.getMessage());
    }
    Set<Integer> touchedMutants = ExecutionTracer.getExecutionTracer().getTrace().getTouchedMutants();
    logger.info("Touched mutants: " + touchedMutants.size());
    // Now run it for all touched mutants
    for (Integer mutantID : touchedMutants) {
        // logger.info("Current mutant: "+mutantID);
        Mutation m = MutationPool.getMutant(mutantID);
        if (killedMutants.contains(m)) {
            // logger.info("Already dead: "+mutantID);
            continue;
        }
        ExecutionTracer.getExecutionTracer().clear();
        resultListener.hasFailure = false;
        MutationObserver.activateMutation(m);
        super.runChild(method, notifier);
        MutationObserver.deactivateMutation(m);
        // If killed
        if (resultListener.hasFailure != result) {
            logger.info("Now killed: " + mutantID);
            try {
                liveMutants.remove(m);
            } catch (Throwable t) {
                logger.info("Error: " + t);
                t.printStackTrace();
            }
            try {
                killedMutants.add(m);
            } catch (Throwable t) {
                logger.info("Error: " + t);
                t.printStackTrace();
            }
        // } else {
        // logger.info("Remains live: "+mutantID);
        }
    }
    notifier.removeListener(resultListener);
    logger.info("Done with " + method.getName());
}
Also used : Mutation(org.evosuite.coverage.mutation.Mutation)

Example 18 with Mutation

use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.

the class SimpleMutationAssertionGenerator method addAssertions.

/**
 * Add assertions to current test set for given set of mutants
 *
 * @param test
 *            a {@link org.evosuite.testcase.TestCase} object.
 * @param killed
 *            a {@link java.util.Set} object.
 * @param mutants
 *            a {@link java.util.Map} object.
 */
private void addAssertions(TestCase test, Set<Integer> killed, Map<Integer, Mutation> mutants) {
    if (test.isEmpty())
        return;
    logger.debug("Generating assertions");
    int s1 = killed.size();
    logger.debug("Running on original");
    ExecutionResult origResult = runTest(test);
    if (origResult.hasTimeout() || origResult.hasTestException()) {
        logger.debug("Skipping test, as it has timeouts or exceptions");
        return;
    }
    Map<Mutation, List<OutputTrace<?>>> mutationTraces = new HashMap<Mutation, List<OutputTrace<?>>>();
    List<Mutation> executedMutants = new ArrayList<Mutation>();
    for (Integer mutationId : origResult.getTrace().getTouchedMutants()) {
        if (!mutants.containsKey(mutationId)) {
        // logger.warn("Mutation ID unknown: " + mutationId);
        // logger.warn(mutants.keySet().toString());
        } else
            executedMutants.add(mutants.get(mutationId));
    }
    Randomness.shuffle(executedMutants);
    logger.debug("Executed mutants: " + origResult.getTrace().getTouchedMutants());
    int numExecutedMutants = 0;
    for (Mutation m : executedMutants) {
        numExecutedMutants++;
        if (!TimeController.getInstance().isThereStillTimeInThisPhase()) {
            logger.info("Reached maximum time to generate assertions!");
            break;
        }
        assert (m != null);
        if (MutationTimeoutStoppingCondition.isDisabled(m)) {
            killed.add(m.getId());
            continue;
        }
        if (timedOutMutations.containsKey(m)) {
            if (timedOutMutations.get(m) >= Properties.MUTATION_TIMEOUTS) {
                logger.debug("Skipping timed out mutant");
                killed.add(m.getId());
                continue;
            }
        }
        if (exceptionMutations.containsKey(m)) {
            if (exceptionMutations.get(m) >= Properties.MUTATION_TIMEOUTS) {
                logger.debug("Skipping mutant with exceptions");
                killed.add(m.getId());
                continue;
            }
        }
        if (Properties.MAX_MUTANTS_PER_TEST > 0 && numExecutedMutants > Properties.MAX_MUTANTS_PER_TEST)
            break;
        /*
			if (killed.contains(m.getId())) {
				logger.info("Skipping dead mutant");
				continue;
			}
			*/
        logger.debug("Running test on mutation {}", m.getMutationName());
        ExecutionResult mutantResult = runTest(test, m);
        int numKilled = 0;
        for (Class<?> observerClass : observerClasses) {
            if (mutantResult.getTrace(observerClass) == null || origResult.getTrace(observerClass) == null)
                continue;
            numKilled += origResult.getTrace(observerClass).getAssertions(test, mutantResult.getTrace(observerClass));
        }
        List<OutputTrace<?>> traces = new ArrayList<OutputTrace<?>>(mutantResult.getTraces());
        mutationTraces.put(m, traces);
        if (mutantResult.hasTimeout()) {
            logger.debug("Increasing timeout count!");
            if (!timedOutMutations.containsKey(m)) {
                timedOutMutations.put(m, 1);
            } else {
                timedOutMutations.put(m, timedOutMutations.get(m) + 1);
            }
            MutationTimeoutStoppingCondition.timeOut(m);
        } else if (!mutantResult.noThrownExceptions() && origResult.noThrownExceptions()) {
            logger.debug("Increasing exception count.");
            if (!exceptionMutations.containsKey(m)) {
                exceptionMutations.put(m, 1);
            } else {
                exceptionMutations.put(m, exceptionMutations.get(m) + 1);
            }
            MutationTimeoutStoppingCondition.raisedException(m);
        }
        if (numKilled > 0 || mutantResult.hasTimeout() || (!mutantResult.noThrownExceptions() && origResult.noThrownExceptions())) {
            killed.add(m.getId());
        }
    }
    List<Assertion> assertions = test.getAssertions();
    logger.info("Got " + assertions.size() + " assertions");
    Map<Integer, Set<Integer>> killMap = new HashMap<Integer, Set<Integer>>();
    int num = 0;
    for (Assertion assertion : assertions) {
        Set<Integer> killedMutations = new HashSet<Integer>();
        for (Mutation m : executedMutants) {
            boolean isKilled = false;
            if (mutationTraces.containsKey(m)) {
                for (OutputTrace<?> trace : mutationTraces.get(m)) {
                    if (trace.isDetectedBy(assertion)) {
                        isKilled = true;
                        break;
                    }
                }
            }
            if (isKilled) {
                killedMutations.add(m.getId());
                assertion.addKilledMutation(m);
            }
        }
        killMap.put(num, killedMutations);
        // logger.info("Assertion " + num + " kills mutants " + killedMutations);
        num++;
    }
    int killedBefore = getNumKilledMutants(test, mutationTraces, executedMutants);
    logger.debug("Need to kill mutants: " + killedBefore);
    logger.debug(killMap.toString());
    minimize(test, executedMutants, assertions, killMap);
    int killedAfter = getNumKilledMutants(test, mutationTraces, executedMutants);
    int s2 = killed.size() - s1;
    assert (killedBefore == killedAfter) : "Mutants killed before / after / should be: " + killedBefore + "/" + killedAfter + "/" + s2 + ": " + test.toCode();
    logger.info("Mutants killed before / after / should be: " + killedBefore + "/" + killedAfter + "/" + s2);
    logger.info("Assertions in this test: " + test.getAssertions().size());
    if (primitiveWithoutAssertion(test.getStatement(test.size() - 1))) {
        logger.info("Last statement has primitive return value but no assertions: " + test.toCode());
        for (Assertion assertion : assertions) {
            if (assertion instanceof PrimitiveAssertion) {
                if (assertion.getStatement().equals(test.getStatement(test.size() - 1))) {
                    logger.debug("Adding a primitive assertion " + assertion);
                    test.getStatement(test.size() - 1).addAssertion(assertion);
                    break;
                }
            }
        }
        filterInspectorPrimitiveDuplication(test.getStatement(test.size() - 1));
    }
    // IF there are no mutant killing assertions on the last statement, still assert something
    if (test.getStatement(test.size() - 1).getAssertions().isEmpty() || justNullAssertion(test.getStatement(test.size() - 1))) {
        logger.info("Last statement has no assertions: " + test.toCode());
        logger.info("Assertions to choose from: " + assertions.size());
        if (test.getStatement(test.size() - 1).getAssertions().isEmpty()) {
            logger.debug("Last statement: " + test.getStatement(test.size() - 1).getCode());
        }
        if (origResult.isThereAnExceptionAtPosition(test.size() - 1))
            logger.debug("Exception on last statement!");
        if (justNullAssertion(test.getStatement(test.size() - 1)))
            logger.debug("Just null assertions on last statement: " + test.toCode());
        boolean haveAssertion = false;
        for (Assertion assertion : assertions) {
            if (assertion instanceof PrimitiveAssertion) {
                if (assertion.getStatement().equals(test.getStatement(test.size() - 1))) {
                    logger.debug("Adding a primitive assertion " + assertion);
                    test.getStatement(test.size() - 1).addAssertion(assertion);
                    haveAssertion = true;
                    break;
                }
            }
        }
        if (!haveAssertion) {
            logger.info("Could not find a primitive assertion, continuing search");
            for (Assertion assertion : assertions) {
                if (assertion instanceof NullAssertion)
                    continue;
                if (assertion.getStatement().equals(test.getStatement(test.size() - 1))) {
                    logger.info("Adding an assertion: " + assertion);
                    test.getStatement(test.size() - 1).addAssertion(assertion);
                    haveAssertion = true;
                    break;
                }
            }
        }
        // if (!test.hasAssertions()) {
        if (!haveAssertion) {
            logger.info("After second round we still have no assertion");
            Method inspectorMethod = null;
            if (test.getStatement(test.size() - 1) instanceof MethodStatement) {
                MethodStatement methodStatement = (MethodStatement) test.getStatement(test.size() - 1);
                Method method = methodStatement.getMethod().getMethod();
                if (method.getParameterTypes().length == 0) {
                    if (method.getReturnType().isPrimitive() && !method.getReturnType().equals(void.class)) {
                        inspectorMethod = method;
                    }
                }
            }
            for (OutputTrace<?> trace : origResult.getTraces()) {
                trace.getAllAssertions(test);
            }
            Set<Assertion> target = new HashSet<Assertion>(test.getStatement(test.size() - 1).getAssertions());
            logger.debug("Found assertions: " + target.size());
            test.removeAssertions();
            // test.addAssertions(clone);
            VariableReference targetVar = test.getStatement(test.size() - 1).getReturnValue();
            if (!targetVar.isVoid()) {
                logger.debug("Return value is non void: " + targetVar.getClassName());
                int maxAssertions = 1;
                int numAssertions = 0;
                for (Assertion ass : target) {
                    if (ass.getReferencedVariables().contains(targetVar) && !(ass instanceof NullAssertion)) {
                        if (ass instanceof InspectorAssertion) {
                            if (((InspectorAssertion) ass).inspector.getMethod().equals(inspectorMethod)) {
                                continue;
                            }
                        }
                        test.getStatement(test.size() - 1).addAssertion(ass);
                        logger.debug("Adding assertion " + ass.getCode());
                        if (++numAssertions >= maxAssertions)
                            break;
                    } else {
                        logger.debug("Assertion does not contain target: " + ass.getCode());
                    }
                }
                if (numAssertions == 0) {
                    for (Assertion ass : target) {
                        if (ass.getReferencedVariables().contains(targetVar)) {
                            test.getStatement(test.size() - 1).addAssertion(ass);
                            logger.debug("Adding assertion " + ass.getCode());
                            if (++numAssertions >= maxAssertions)
                                break;
                        } else {
                            logger.debug("Assertion does not contain target: " + ass.getCode());
                        }
                    }
                }
            } else {
                logger.debug("Return value is void");
                Set<VariableReference> targetVars = test.getStatement(test.size() - 1).getVariableReferences();
                int maxAssertions = 1;
                int numAssertions = 0;
                for (Assertion ass : target) {
                    Set<VariableReference> vars = ass.getReferencedVariables();
                    vars.retainAll(targetVars);
                    if (!vars.isEmpty()) {
                        test.getStatement(test.size() - 1).addAssertion(ass);
                        if (++numAssertions >= maxAssertions)
                            break;
                    }
                }
            }
            logger.info("1. Done with assertions");
        }
        logger.info("2. Done with assertions");
        filterInspectorPrimitiveDuplication(test.getStatement(test.size() - 1));
    }
    if (!origResult.noThrownExceptions()) {
        if (!test.getStatement(test.size() - 1).getAssertions().isEmpty()) {
            logger.debug("Removing assertions after exception");
            test.getStatement(test.size() - 1).removeAssertions();
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) Method(java.lang.reflect.Method) Mutation(org.evosuite.coverage.mutation.Mutation)

Example 19 with Mutation

use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.

the class MutationAssertionGenerator method getNumKilledMutants.

/**
 * @param test
 * @param mutation_traces
 * @param executedMutants
 * @return
 */
protected int getNumKilledMutants(TestCase test, Map<Mutation, List<OutputTrace<?>>> mutation_traces, List<Mutation> executedMutants) {
    List<Assertion> assertions;
    Set<Integer> killed = new HashSet<Integer>();
    assertions = test.getAssertions();
    for (Assertion assertion : assertions) {
        for (Mutation m : executedMutants) {
            boolean isKilled = false;
            if (mutation_traces.containsKey(m)) {
                int i = 0;
                for (OutputTrace<?> trace : mutation_traces.get(m)) {
                    isKilled = trace.isDetectedBy(assertion);
                    if (isKilled) {
                        logger.debug("Mutation killed: " + m.getId() + " by trace " + i++);
                        killed.add(m.getId());
                        break;
                    }
                    i++;
                }
            } else {
                isKilled = true;
            }
        }
    }
    logger.debug("Killed mutants: " + killed);
    return killed.size();
}
Also used : Mutation(org.evosuite.coverage.mutation.Mutation) HashSet(java.util.HashSet)

Example 20 with Mutation

use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.

the class MutationAssertionGenerator method setupClassLoader.

/**
 * If we are not doing mutation testing anyway, then we need to reinstrument
 * the code to get the mutants now
 *
 * @param suite
 */
@Override
public void setupClassLoader(TestSuiteChromosome suite) {
    oldCriterion = Arrays.copyOf(Properties.CRITERION, Properties.CRITERION.length);
    if (!ArrayUtil.contains(oldCriterion, Criterion.MUTATION) && !ArrayUtil.contains(oldCriterion, Criterion.WEAKMUTATION) && !ArrayUtil.contains(oldCriterion, Criterion.ONLYMUTATION) && !ArrayUtil.contains(oldCriterion, Criterion.STRONGMUTATION)) {
        Properties.CRITERION = new Criterion[] { Criterion.MUTATION };
    }
    if (Properties.RESET_STATIC_FIELDS) {
        final boolean reset_all_classes = Properties.RESET_ALL_CLASSES_DURING_ASSERTION_GENERATION;
        ClassReInitializer.getInstance().setReInitializeAllClasses(reset_all_classes);
    }
    changeClassLoader(suite);
    for (Mutation m : MutationPool.getMutants()) {
        mutants.put(m.getId(), m);
    }
}
Also used : Mutation(org.evosuite.coverage.mutation.Mutation)

Aggregations

Mutation (org.evosuite.coverage.mutation.Mutation)20 LinkedList (java.util.LinkedList)10 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)7 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)6 InsnList (org.objectweb.asm.tree.InsnList)6 InsnNode (org.objectweb.asm.tree.InsnNode)6 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)6 HashSet (java.util.HashSet)4 Type (org.objectweb.asm.Type)4 LabelNode (org.objectweb.asm.tree.LabelNode)4 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)3 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)3 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)3 LinkedHashSet (java.util.LinkedHashSet)2 Branch (org.evosuite.coverage.branch.Branch)2 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1