/** * <p>Setter for the field <code>lastRegressionExecutionResult</code>.</p> * * @param lastExecutionResult a {@link org.evosuite.testcase.execution.ExecutionResult} object. */ public void setLastRegressionExecutionResult(ExecutionResult lastExecutionResult) { theSameTestForTheOtherClassLoader.setLastExecutionResult(lastExecutionResult); }
public void setLastExecutionResult(ExecutionResult lastExecutionResult) { theTest.setLastExecutionResult(lastExecutionResult); }
private void restore(TestChromosome test, StringPrimitiveStatement p) { p.setValue(new String(oldValue)); test.setLastExecutionResult(oldResult); test.setChanged(oldChanged); }
private void restore(TestChromosome test, StringPrimitiveStatement p) { p.setValue(new String(oldValue)); test.setLastExecutionResult(oldResult); test.setChanged(oldChanged); }
/** * Iterate over all execution results and summarize statistics * * @param results * @param coveredLines * @return */ private boolean analyzeTraces(List<ExecutionResult> results, Set<Integer> coveredLines) { boolean hasTimeoutOrTestException = false; for (ExecutionResult result : results) { if (result.hasTimeout() || result.hasTestException()) { hasTimeoutOrTestException = true; continue; } TestChromosome test = new TestChromosome(); test.setTestCase(result.test); test.setLastExecutionResult(result); test.setChanged(false); for (Integer goalID : this.lineGoals.keySet()) { TestFitnessFunction goal = this.lineGoals.get(goalID); double fit = goal.getFitness(test, result); // archive is updated by the TestFitnessFunction class if (fit == 0.0) { coveredLines.add(goalID); // helper to count the number of covered goals this.toRemoveLines.add(goalID); // goal to not be considered by the next iteration of the evolutionary algorithm } } } return hasTimeoutOrTestException; }
/** * Helper function if this is used without a chromosome * * @param result * @return */ public boolean isCovered(ExecutionResult result) { TestChromosome chromosome = new TestChromosome(); chromosome.setTestCase(result.test); chromosome.setLastExecutionResult(result); chromosome.setChanged(false); return isCovered(chromosome, result); }
/** * Execute regression test case on both versions * * @param regressionTest regression test chromosome to be executed on both versions */ private void executeTest(RegressionTestChromosome regressionTest) { TestChromosome testChromosome = regressionTest.getTheTest(); TestChromosome otherChromosome = regressionTest.getTheSameTestForTheOtherClassLoader(); ExecutionResult result = regressionAssertionGenerator .runTest(testChromosome.getTestCase()); ExecutionResult otherResult = regressionAssertionGenerator .runTest(otherChromosome.getTestCase()); regressionTest.setLastExecutionResult(result); regressionTest.setLastRegressionExecutionResult(otherResult); testChromosome.setLastExecutionResult(result); otherChromosome.setLastExecutionResult(otherResult); }
/** {@inheritDoc} */ @Override public double getFitness(TestChromosome individual) { logger.trace("Executing test case on original"); ExecutionResult origResult = individual.getLastExecutionResult(); if (origResult == null || individual.isChanged()) { origResult = runTest(individual.test); individual.setLastExecutionResult(origResult); individual.setChanged(false); } double fitness = getFitness(individual, origResult); updateIndividual(this, individual, fitness); return fitness; }
/** * Return the number of covered goals * * @param testClass * @param allGoals * @return */ public static Set<TestFitnessFunction> getCoveredGoals(Class<?> testClass, List<TestFitnessFunction> allGoals) { // A dummy Chromosome TestChromosome dummy = new TestChromosome(); dummy.setChanged(false); // Execution result of a dummy Test Case ExecutionResult executionResult = new ExecutionResult(dummy.getTestCase()); Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>(); List<JUnitResult> results = executeTests(testClass); for (JUnitResult testResult : results) { executionResult.setTrace(testResult.getExecutionTrace()); dummy.setLastExecutionResult(executionResult); for(TestFitnessFunction goal : allGoals) { if(coveredGoals.contains(goal)) continue; else if (goal.isCovered(dummy)) coveredGoals.add(goal); } } return coveredGoals; }
/** * This method executes a given test case (i.e., TestChromosome) * * @param c test case (TestChromosome) to execute */ protected void runTest(T c){ if (!c.isChanged()) return; // run the test TestCase test = ((TestChromosome) c).getTestCase(); ExecutionResult result = TestCaseExecutor.runTest(test); ((TestChromosome) c).setLastExecutionResult(result); c.setChanged(false); // notify the fitness evaluation (i.e., the test is executed) notifyEvaluation(c); }
private ExecutionResult getExecutionResult(TestChromosome individual) { ExecutionResult origResult = individual.getLastExecutionResult(); if(origResult == null||individual.isChanged()) { origResult = runTest(individual.getTestCase()); individual.setLastExecutionResult(origResult); individual.setChanged(false); } return individual.getLastExecutionResult(); }
private boolean iterate(long delta, LocalSearchObjective<TestChromosome> objective, TestChromosome test, NumericalPrimitiveStatement<T> p, int statement) { boolean improvement = false; T oldValue = p.getValue(); ExecutionResult oldResult = test.getLastExecutionResult(); p.increment(delta); logger.info("Trying increment " + delta + " of " + p.getCode()); while (objective.hasImproved(test)) { oldValue = p.getValue(); oldResult = test.getLastExecutionResult(); improvement = true; delta = 2 * delta; p.increment(delta); logger.info("Trying increment " + delta + " of " + p.getCode()); } logger.info("No improvement on " + p.getCode()); p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); logger.info("Final value of this iteration: " + p.getValue()); return improvement; }
/** * First execute (if needed) the test cases to be sure to have latest correct data, * and then send it to Master */ public static void executedAndThenSendIndividualToMaster(TestSuiteChromosome testSuite) throws IllegalArgumentException{ if(testSuite == null){ throw new IllegalArgumentException("No defined test suite to send"); } if(!Properties.NEW_STATISTICS) return; /* * TODO: shouldn't a test that was never executed always be executed before sending? * ie, do we really need a separated public sendIndividualToMaster??? */ for (TestChromosome test : testSuite.getTestChromosomes()) { if (test.getLastExecutionResult() == null) { ExecutionResult result = TestCaseExecutor.runTest(test.getTestCase()); test.setLastExecutionResult(result); } } sendCoveredInfo(testSuite); sendExceptionInfo(testSuite); sendIndividualToMaster(testSuite); }
private boolean iterate(double delta, double factor, LocalSearchObjective<TestChromosome> objective, TestChromosome test, NumericalPrimitiveStatement<T> p, int statement) { boolean improvement = false; T oldValue = p.getValue(); ExecutionResult oldResult = test.getLastExecutionResult(); logger.info("Trying increment " + delta + " of " + p.getCode()); p.increment(delta); while (objective.hasImproved(test)) { oldValue = p.getValue(); oldResult = test.getLastExecutionResult(); test.setChanged(false); improvement = true; delta = factor * delta; //if (delta > 1) // return improvement; logger.info("Trying increment " + delta + " of " + p.getCode()); p.increment(delta); } p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); return improvement; } }
/** * <p> * isCovered * </p> * * @param tc * a {@link org.evosuite.testcase.TestChromosome} object. * @return a boolean. */ public boolean isCovered(TestChromosome tc) { if(tc.getTestCase().isGoalCovered(this)){ return true; } ExecutionResult result = tc.getLastExecutionResult(); if (result == null || tc.isChanged()) { result = runTest(tc.test); tc.setLastExecutionResult(result); tc.setChanged(false); } return isCovered(tc, result); }
/** {@inheritDoc} */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) { EnumPrimitiveStatement p = (EnumPrimitiveStatement) test.getTestCase().getStatement(statement); ExecutionResult oldResult = test.getLastExecutionResult(); oldValue = p.getValue(); for (Object value : p.getEnumValues()) { p.setValue(value); if (!objective.hasImproved(test)) { // Restore original p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); } else { logger.debug("Finished local search with result " + p.getCode()); return true; } } return false; }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) { PrimitiveStatement<Boolean> p = (PrimitiveStatement<Boolean>) test.getTestCase().getStatement(statement); ExecutionResult oldResult = test.getLastExecutionResult(); oldValue = p.getValue(); p.setValue(!oldValue); if (!objective.hasImproved(test)) { // Restore original p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); return false; } else { return true; } }
@Override public boolean isNotWorse() { ExecutionResult lastResult = individual.getLastExecutionResult(); individual.setChanged(true); individual.getTestCase().clearCoveredGoals(); double newFitness = fitness.getFitness(individual); boolean worse = false; if(fitness.isMaximizationFunction()) { if (newFitness < lastFitness) worse = true; } else { if (newFitness > lastFitness) worse = true; } if (!worse) { lastFitness = newFitness; individual.setFitness(fitness, lastFitness); return true; } else { individual.setFitness(fitness, lastFitness); individual.setLastExecutionResult(lastResult); return false; } } }
@Test public void testRuntimeException() { String targetClass = SourceExceptions.class.getCanonicalName(); Properties.TARGET_CLASS = targetClass; Properties.REPLACE_CALLS = true; Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.LINE, Properties.Criterion.EXCEPTION}; EvoSuite evosuite = new EvoSuite(); String[] command = new String[]{"-generateSuite", "-class", targetClass}; Object result = evosuite.parseCommandLine(command); GeneticAlgorithm<?> ga = getGAFromResult(result); TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual(); Assert.assertNotNull(best); TestCaseExecutor.getInstance().initExecutor(); for(TestChromosome test : best.getTestChromosomes()) { ExecutionResult executionResult = TestCaseExecutor.getInstance().runTest(test.getTestCase()); test.setLastExecutionResult(executionResult); } String code = best.toString(); Assert.assertTrue("Code:\n"+code, code.contains("verifyException(\"com.examples.with.different.packagename.mock.java.lang.SourceExceptions\",")); Assert.assertTrue("Code:\n"+code, code.contains("verifyException(\"com.examples.with.different.packagename.mock.java.lang.SourceExceptions$Foo\",")); } }
@Override public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) { NullStatement nullStatement = (NullStatement) test.getTestCase().getStatement(statement); TestCase newTest = test.getTestCase(); TestCase oldTest = newTest.clone(); ExecutionResult oldResult = test.getLastExecutionResult(); //double oldFitness = test.getFitness(); Map<FitnessFunction<?>, Double> oldFitnesses = test.getFitnessValues(); Map<FitnessFunction<?>, Double> oldLastFitnesses = test.getPreviousFitnessValues(); try { TestFactory.getInstance().attemptGeneration(newTest, nullStatement.getReturnType(), statement); if (!objective.hasImproved(test)) { test.setTestCase(oldTest); test.setLastExecutionResult(oldResult); //test.setFitness(oldFitness); test.setFitnessValues(oldFitnesses); test.setPreviousFitnessValues(oldLastFitnesses); } else { return true; } } catch (ConstructionFailedException e) { // If we can't construct it, then ignore } return false; }