/** * Execute a test case on a mutant * * @param test The test case that should be executed * @param mutant The mutant on which the test case shall be executed */ private ExecutionResult runTest(TestCase test, Mutation mutant) { ExecutionResult result = new ExecutionResult(test, mutant); // resetObservers(); comparisonObserver.clear(); sameObserver.clear(); primitiveObserver.clear(); inspectorObserver.clear(); fieldObserver.clear(); nullObserver.clear(); try { logger.debug("Executing test"); MutationObserver.activateMutation(mutant); result = TestCaseExecutor.getInstance().execute(test); MutationObserver.deactivateMutation(mutant); int num = test.size(); MaxStatementsStoppingCondition.statementsExecuted(num); result.setTrace(comparisonObserver.getTrace(), ComparisonTraceEntry.class); result.setTrace(sameObserver.getTrace(), SameTraceEntry.class); result.setTrace(primitiveObserver.getTrace(), PrimitiveTraceEntry.class); result.setTrace(inspectorObserver.getTrace(), InspectorTraceEntry.class); result.setTrace(fieldObserver.getTrace(), PrimitiveFieldTraceEntry.class); result.setTrace(nullObserver.getTrace(), NullTraceEntry.class); } catch (Exception e) { throw new Error(e); } return result; }
/** * Get all possible assertions * * @param test a {@link org.evosuite.testcase.TestCase} object. * @return a int. */ public int getAllAssertions(TestCase test) { int num = 0; for (Integer statement : trace.keySet()) { for (Integer var : trace.get(statement).keySet()) { for (Assertion assertion : trace.get(statement).get(var).getAssertions()) { assert (assertion.isValid()) : "Invalid assertion: " + assertion.getCode() + ", " + assertion.value; if (test.sizeWithAssertions() >= Properties.MAX_LENGTH_TEST_CASE) { return num; } test.getStatement(statement).addAssertion(assertion); num++; } } } return num; }
/** * Get all possible assertions * * @param test a {@link org.evosuite.testcase.TestCase} object. * @return a int. */ public int getAllAssertions(TestCase test, int statement) { int num = 0; if (!trace.containsKey(statement)) return 0; for (Integer var : trace.get(statement).keySet()) { for (Assertion assertion : trace.get(statement).get(var).getAssertions()) { assert (assertion.isValid()) : "Invalid assertion: " + assertion.getCode() + ", " + assertion.value; test.getStatement(statement).addAssertion(assertion); num++; } } return num; }
/** * Get all assertions based on trace differences * * @param test a {@link org.evosuite.testcase.TestCase} object. * @param other a {@link org.evosuite.assertion.OutputTrace} object. * @return a int. */ public int getAssertions(TestCase test, OutputTrace<?> other) { int num = 0; for (Integer statement : trace.keySet()) { if (other.trace.containsKey(statement)) { logger.debug("Other trace contains " + statement); for (Integer var : trace.get(statement).keySet()) { logger.debug("Variable " + var); for (Assertion assertion : trace.get(statement).get(var).getAssertions(other.trace.get(statement).get(var))) { assert (assertion.isValid()) : "Invalid assertion: " + assertion.getCode() + ", " + assertion.value; test.getStatement(statement).addAssertion(assertion); num++; } } } } return num; }
@Override public boolean doSearch( TestChromosome individual, LocalSearchObjective<TestChromosome> objective) { logger.info("Test before local search: " + individual.getTestCase().toCode()); boolean improved = false; // Only apply local search up to the point where an exception was thrown // TODO: Check whether this conflicts with test expansion int lastPosition = individual.size() - 1; if (individual.getLastExecutionResult() != null && !individual.isChanged()) { Integer lastPos = individual.getLastExecutionResult().getFirstPositionOfThrownException(); if (lastPos != null) lastPosition = lastPos.intValue(); } TestCase test = individual.getTestCase(); // We count down to make the code work when lines are // added during the search (see NullReferenceSearch). for (int i = lastPosition; i >= 0; i--) { if (LocalSearchBudget.getInstance().isFinished()) break; if (objective.isDone()) { break; } if (i >= individual.size()) { logger.warn("Test size decreased unexpectedly during local search, aborting local search"); logger.warn(individual.getTestCase().toCode()); break; } final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); final Statement statement = test.getStatement(i); if (!test.hasReferences(statement.getReturnValue()) && !statement.getReturnClass().equals(targetClass)) { logger.info( "Return value of statement " + i + " is not referenced and not SUT, not doing local search"); continue; } StatementLocalSearch search = StatementLocalSearch.getLocalSearchFor(statement); if (search != null) { logger.info( "Applying local search of type " + search.getClass() + " to statement " + statement + " / " + individual.getTestCase().getStatement(i)); if (search.doSearch(individual, i, (LocalSearchObjective<TestChromosome>) objective)) { improved = true; } // i = s.getPosition(); logger.debug( "Old position was: " + i + ", adjusting to: " + (i + search.getPositionDelta())); i += search.getPositionDelta(); test = individual.getTestCase(); } else { /* * No statement local search has been produced for this * statement. Skipping. */ continue; } } LocalSearchBudget.getInstance().countLocalSearchOnTest(); // logger.warn("Test after local search: " + // individual.getTestCase().toCode()); // Return true iif search was successful return improved; // TODO: Handle arrays in local search // TODO: mutating an int might have an effect on array lengths }