Search in sources :

Example 11 with ExceptionCoverageTestFitness

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);
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) MethodNoExceptionCoverageTestFitness(org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) MethodNoExceptionCoverageTestFitness(org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 12 with ExceptionCoverageTestFitness

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));
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) GoalComparator(org.evosuite.junit.naming.methods.GoalComparator) Test(org.junit.Test)

Example 13 with ExceptionCoverageTestFitness

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));
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) GoalComparator(org.evosuite.junit.naming.methods.GoalComparator) Test(org.junit.Test)

Example 14 with ExceptionCoverageTestFitness

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);
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 15 with ExceptionCoverageTestFitness

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;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)16 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)11 TestCase (org.evosuite.testcase.TestCase)11 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 MethodNoExceptionCoverageTestFitness (org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness)8 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)8 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)4 GoalComparator (org.evosuite.junit.naming.methods.GoalComparator)3 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)2 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)2 ClassWithOverloadedConstructor (com.examples.with.different.packagename.ClassWithOverloadedConstructor)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 InputCoverageGoal (org.evosuite.coverage.io.input.InputCoverageGoal)1 InputCoverageTestFitness (org.evosuite.coverage.io.input.InputCoverageTestFitness)1 TestChromosome (org.evosuite.testcase.TestChromosome)1