use of org.evosuite.coverage.exception.ExceptionCoverageTestFitness in project evosuite by EvoSuite.
the class TestCoverageGoalNameGeneration method testTwoTestsTwoMethodsOneWithException.
@Test
public void testTwoTestsTwoMethodsOneWithException() {
TestCase test1 = new DefaultTestCase();
TestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 0));
MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "foo");
test1.addCoveredGoal(goal1);
test2.addCoveredGoal(goal1);
MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "bar()I");
test1.addCoveredGoal(goal2);
test2.addCoveredGoal(goal2);
ExceptionCoverageTestFitness goal3 = new ExceptionCoverageTestFitness("FooClass", "bar()I", RuntimeException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT);
test1.addCoveredGoal(goal3);
MethodNoExceptionCoverageTestFitness goal4 = new MethodNoExceptionCoverageTestFitness("FooClass", "bar()I");
test2.addCoveredGoal(goal4);
List<TestCase> tests = new ArrayList<>();
tests.add(test1);
tests.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
String generatedName1 = naming.getName(test1);
String generatedName2 = naming.getName(test2);
assertEquals("testBarThrowsRuntimeException", generatedName1);
assertEquals("testBar", generatedName2);
}
use of org.evosuite.coverage.exception.ExceptionCoverageTestFitness in project evosuite by EvoSuite.
the class TestGoalComparator method testComparatorList.
@Test
public void testComparatorList() {
GoalComparator comparator = new GoalComparator();
MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString()");
ExceptionCoverageTestFitness goal2 = new ExceptionCoverageTestFitness("FooClass", "toString()", MockArithmeticException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT);
OutputCoverageGoal outputGoal = new OutputCoverageGoal("FooClass", "toString", Type.getType("Ljava.lang.String;"), IOCoverageConstants.REF_NONNULL);
OutputCoverageTestFitness goal3 = new OutputCoverageTestFitness(outputGoal);
List<TestFitnessFunction> goals = new ArrayList<>();
goals.add(goal1);
goals.add(goal2);
goals.add(goal3);
Collections.sort(goals, comparator);
assertEquals(goal2, goals.get(0));
}
use of org.evosuite.coverage.exception.ExceptionCoverageTestFitness in project evosuite by EvoSuite.
the class TestGoalComparator method testCompareExceptionMethod.
@Test
public void testCompareExceptionMethod() {
GoalComparator comparator = new GoalComparator();
MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString()");
ExceptionCoverageTestFitness goal2 = new ExceptionCoverageTestFitness("FooClass", "toString()", MockArithmeticException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT);
assertEquals(1, comparator.compare(goal1, goal2));
}
use of org.evosuite.coverage.exception.ExceptionCoverageTestFitness in project evosuite by EvoSuite.
the class TestMethodNamingComplexExamples method testConstructorAndOverloadedMethods.
@Test
public void testConstructorAndOverloadedMethods() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
// method goal
TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "values()[B");
TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "values([B)[B");
// exception goal
TestFitnessFunction goal4 = new ExceptionCoverageTestFitness("FooClass", "<init>(LFooClass;)V", ArrayIndexOutOfBoundsException.class, ExceptionCoverageTestFitness.ExceptionType.IMPLICIT);
// output goals
TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values()", Type.getType("[B"), ARRAY_NONEMPTY));
TestFitnessFunction goal6 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([B)", Type.getType("[B"), ARRAY_EMPTY));
DefaultTestCase test1 = new DefaultTestCase();
test1.addCoveredGoal(goal1);
test1.addCoveredGoal(goal2);
test1.addCoveredGoal(goal3);
test1.addCoveredGoal(goal5);
test1.addCoveredGoal(goal6);
DefaultTestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 2));
test2.addCoveredGoal(goal1);
test2.addCoveredGoal(goal2);
test2.addCoveredGoal(goal3);
test2.addCoveredGoal(goal4);
test2.addCoveredGoal(goal5);
test2.addCoveredGoal(goal6);
ArrayList<TestCase> testCases = new ArrayList<>();
testCases.add(test1);
testCases.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
String nameTest1 = naming.getName(test1);
String nameTest2 = naming.getName(test2);
assertEquals("Generated test name differs from expected", "testValuesTakingNoArgumentsReturningNonEmptyArray", nameTest1);
assertEquals("Generated test name differs from expected", "testFailsToCreateFooClassThrowsArrayIndexOutOfBoundsException", nameTest2);
}
use of org.evosuite.coverage.exception.ExceptionCoverageTestFitness in project evosuite by EvoSuite.
the class AbstractMOSA method deriveCoveredExceptions.
/**
* This method analyzes the execution results of a TestChromosome looking for generated exceptions.
* Such exceptions are converted in instances of the class {@link ExceptionCoverageTestFitness},
* which are additional covered goals when using as criterion {@link Properties.Criterion.EXCEPTION}
* @param t TestChromosome to analyze
* @return list of exception goals being covered by t
*/
public List<ExceptionCoverageTestFitness> deriveCoveredExceptions(T t) {
List<ExceptionCoverageTestFitness> covered_exceptions = new ArrayList<ExceptionCoverageTestFitness>();
TestChromosome testCh = (TestChromosome) t;
ExecutionResult result = testCh.getLastExecutionResult();
Map<String, Set<Class<?>>> implicitTypesOfExceptions = new LinkedHashMap<>();
Map<String, Set<Class<?>>> explicitTypesOfExceptions = new LinkedHashMap<>();
Map<String, Set<Class<?>>> declaredTypesOfExceptions = new LinkedHashMap<>();
for (Integer i : result.getPositionsWhereExceptionsWereThrown()) {
if (ExceptionCoverageHelper.shouldSkip(result, i)) {
continue;
}
Class<?> exceptionClass = ExceptionCoverageHelper.getExceptionClass(result, i);
// eg name+descriptor
String methodIdentifier = ExceptionCoverageHelper.getMethodIdentifier(result, i);
// was the exception originated by a direct call on the SUT?
boolean sutException = ExceptionCoverageHelper.isSutException(result, i);
if (sutException) {
boolean notDeclared = !ExceptionCoverageHelper.isDeclared(result, i);
if (notDeclared) {
/*
* we need to distinguish whether it is explicit (ie "throw" in the code, eg for validating
* input for pre-condition) or implicit ("likely" a real fault).
*/
boolean isExplicit = ExceptionCoverageHelper.isExplicit(result, i);
if (isExplicit) {
if (!explicitTypesOfExceptions.containsKey(methodIdentifier)) {
explicitTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
}
explicitTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
} else {
if (!implicitTypesOfExceptions.containsKey(methodIdentifier)) {
implicitTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
}
implicitTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
}
} else {
if (!declaredTypesOfExceptions.containsKey(methodIdentifier)) {
declaredTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
}
declaredTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
}
ExceptionCoverageTestFitness.ExceptionType type = ExceptionCoverageHelper.getType(result, i);
/*
* Add goal to list of fitness functions to solve
*/
ExceptionCoverageTestFitness goal = new ExceptionCoverageTestFitness(Properties.TARGET_CLASS, methodIdentifier, exceptionClass, type);
covered_exceptions.add(goal);
}
}
return covered_exceptions;
}
Aggregations