Search in sources :

Example 21 with MethodCoverageTestFitness

use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.

the class TestMethodNamingComplexExamples method testIDNamingWithSameMethodGoals.

@Test
public void testIDNamingWithSameMethodGoals() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // method goals
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>()V");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "getPublicID()Ljava/lang/String;");
    TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "setPublicID(Ljava/lang/String;)LFooClass;");
    // output goals
    TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "getPublicID()", Type.getType("Ljava.lang.String;"), REF_NONNULL));
    TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "setPublicID(Ljava/lang/String;)", Type.getType("LFooClass;"), REF_NONNULL));
    DefaultTestCase test1 = new DefaultTestCase();
    // any statement to fool hashcode function
    test1.addStatement(new IntPrimitiveStatement(test1, 1));
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal2);
    test1.addCoveredGoal(goal4);
    DefaultTestCase test2 = new DefaultTestCase();
    // any statement to fool hashcode function
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal3);
    test2.addCoveredGoal(goal5);
    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);
    // TODO: testGetPublicID ?
    assertEquals("Generated test name differs from expected", "testGetPublicIDTakingNoArguments", nameTest1);
    // TODO: testSetPublicID ?
    assertEquals("Generated test name differs from expected", "testSetPublicIDTakingString", nameTest2);
}
Also used : 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 22 with MethodCoverageTestFitness

use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.

the class TestMethodNamingComplexExamples method testConstructors.

@Test
public void testConstructors() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // method goal
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
    // exception goal
    TestFitnessFunction goal2 = new ExceptionCoverageTestFitness("FooClass", "<init>(LFooClass;)V", ArrayIndexOutOfBoundsException.class, ExceptionCoverageTestFitness.ExceptionType.IMPLICIT);
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal2);
    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", "testCreatesFooClass", 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) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 23 with MethodCoverageTestFitness

use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.

the class TestMethodNamingComplexExamples method testOverloadedMethods.

@Test
public void testOverloadedMethods() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // Method goals
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "values([B)[B");
    TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "values([I)[B");
    // Output goals for method values
    TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([B)", Type.getType("[B"), ARRAY_EMPTY));
    TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([I)", Type.getType("[B"), ARRAY_NONEMPTY));
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal2);
    test1.addCoveredGoal(goal4);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal3);
    test2.addCoveredGoal(goal5);
    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", "testValuesTakingByteArray", nameTest1);
    assertEquals("Generated test name differs from expected", "testValuesTakingIntArray", nameTest2);
}
Also used : 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 24 with MethodCoverageTestFitness

use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.

the class CoverageGoalTestNameGenerationStrategy method getUniqueNonMethodGoals.

/**
 * There is nothing unique about the test, so we have to add goals until we find a unique name
 */
private Set<TestFitnessFunction> getUniqueNonMethodGoals(TestCase test, Map<TestCase, Set<TestFitnessFunction>> testToGoals) {
    Set<TestFitnessFunction> allGoals = new LinkedHashSet<>();
    for (Set<TestFitnessFunction> goals : testToGoals.values()) {
        allGoals.addAll(goals);
    }
    Set<TestFitnessFunction> goals = new LinkedHashSet<>();
    for (TestFitnessFunction goal : filterSupportedGoals(test.getCoveredGoals())) {
        if (goal instanceof MethodCoverageTestFitness)
            continue;
        if (goal instanceof MethodNoExceptionCoverageTestFitness)
            continue;
        if (!allGoals.contains(goal)) {
            goals.add(goal);
        }
    }
    return goals;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) MethodNoExceptionCoverageTestFitness(org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness)

Example 25 with MethodCoverageTestFitness

use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.

the class TestCoverageGoalNameGeneration method testMethodWithInputGoals.

@Test
public void testMethodWithInputGoals() {
    TestCase test1 = new DefaultTestCase();
    MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString");
    InputCoverageGoal inputGoal1 = new InputCoverageGoal("FooClass", "toString", 0, objectType(), REF_NULL);
    InputCoverageTestFitness goal2 = new InputCoverageTestFitness(inputGoal1);
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal2);
    TestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 0));
    test2.addCoveredGoal(goal1);
    InputCoverageGoal inputGoal2 = new InputCoverageGoal("FooClass", "toString", 0, objectType(), REF_NONNULL);
    InputCoverageTestFitness goal3 = new InputCoverageTestFitness(inputGoal2);
    test2.addCoveredGoal(goal3);
    List<TestCase> tests = new ArrayList<>();
    tests.add(test1);
    tests.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
    assertEquals("testToStringWithNull", naming.getName(test1));
    assertEquals("testToStringWithNonNull", naming.getName(test2));
}
Also used : InputCoverageTestFitness(org.evosuite.coverage.io.input.InputCoverageTestFitness) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) InputCoverageGoal(org.evosuite.coverage.io.input.InputCoverageGoal) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Aggregations

MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)48 Test (org.junit.Test)47 ArrayList (java.util.ArrayList)46 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)45 TestCase (org.evosuite.testcase.TestCase)45 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)36 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)35 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)18 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)18 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)11 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)11 InputCoverageGoal (org.evosuite.coverage.io.input.InputCoverageGoal)9 InputCoverageTestFitness (org.evosuite.coverage.io.input.InputCoverageTestFitness)9 MethodNoExceptionCoverageTestFitness (org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness)8 GoalComparator (org.evosuite.junit.naming.methods.GoalComparator)2 ClassWithOverloadedConstructor (com.examples.with.different.packagename.ClassWithOverloadedConstructor)1 Set (java.util.Set)1 Branch (org.evosuite.coverage.branch.Branch)1 BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)1 BranchCoverageTestFitness (org.evosuite.coverage.branch.BranchCoverageTestFitness)1