use of org.evosuite.testcase.DefaultTestCase in project evosuite by EvoSuite.
the class TestMethodNamingComplexExamples method testOverloadedMethodsNoArgs.
@Test
public void testOverloadedMethodsNoArgs() 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()[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()", 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", "testValuesTakingNoArguments", nameTest2);
}
use of org.evosuite.testcase.DefaultTestCase 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);
}
use of org.evosuite.testcase.DefaultTestCase 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);
}
use of org.evosuite.testcase.DefaultTestCase 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);
}
use of org.evosuite.testcase.DefaultTestCase in project evosuite by EvoSuite.
the class TestLocalSearchMIMEType method testFitness.
@Test
public void testFitness() throws NoSuchFieldException, SecurityException, NoSuchMethodException, ClassNotFoundException {
Properties.RESET_STATIC_FINAL_FIELDS = false;
Properties.TEST_ARCHIVE = false;
Properties.LOCAL_SEARCH_PROBABILITY = 1.0;
Properties.LOCAL_SEARCH_RATE = 1;
Properties.LOCAL_SEARCH_BUDGET_TYPE = Properties.LocalSearchBudgetType.TESTS;
Properties.LOCAL_SEARCH_BUDGET = 100;
Properties.DSE_SOLVER = SolverType.EVOSUITE_SOLVER;
Properties.STOPPING_CONDITION = StoppingCondition.MAXTIME;
Properties.SEARCH_BUDGET = 120;
Properties.TARGET_CLASS = MIMEType.class.getName();
String classPath = ClassPathHandler.getInstance().getTargetProjectClasspath();
DependencyAnalysis.analyzeClass(MIMEType.class.getName(), Arrays.asList(classPath));
TestSuiteChromosome suite = new TestSuiteChromosome();
DefaultTestCase test0 = createTestCase0();
DefaultTestCase test1 = createTestCase1();
DefaultTestCase test2 = createTestCase2();
DefaultTestCase test3 = createTestCase3();
DefaultTestCase test4 = createTestCase4();
DefaultTestCase test5 = createTestCase5();
suite.addTest(test0);
suite.addTest(test1);
suite.addTest(test2);
suite.addTest(test3);
suite.addTest(test4);
suite.addTest(test5);
TestSuiteFitnessFunction lineCoverage = FitnessFunctions.getFitnessFunction(Criterion.LINE);
TestSuiteFitnessFunction branchCoverage = FitnessFunctions.getFitnessFunction(Criterion.BRANCH);
TestSuiteFitnessFunction exceptionCoverage = FitnessFunctions.getFitnessFunction(Criterion.EXCEPTION);
TestSuiteFitnessFunction weakMutationCoverage = FitnessFunctions.getFitnessFunction(Criterion.WEAKMUTATION);
TestSuiteFitnessFunction outputCoverage = FitnessFunctions.getFitnessFunction(Criterion.OUTPUT);
TestSuiteFitnessFunction methodCoverage = FitnessFunctions.getFitnessFunction(Criterion.METHOD);
TestSuiteFitnessFunction methodNoExceptionCoverage = FitnessFunctions.getFitnessFunction(Criterion.METHODNOEXCEPTION);
TestSuiteFitnessFunction cbranchCoverage = FitnessFunctions.getFitnessFunction(Criterion.CBRANCH);
List<TestSuiteFitnessFunction> fitnessFunctions = new ArrayList<TestSuiteFitnessFunction>();
fitnessFunctions.add(lineCoverage);
fitnessFunctions.add(branchCoverage);
fitnessFunctions.add(exceptionCoverage);
fitnessFunctions.add(weakMutationCoverage);
fitnessFunctions.add(outputCoverage);
fitnessFunctions.add(methodCoverage);
fitnessFunctions.add(methodNoExceptionCoverage);
fitnessFunctions.add(cbranchCoverage);
for (TestSuiteFitnessFunction ff : fitnessFunctions) {
suite.addFitness(ff);
}
for (TestSuiteFitnessFunction ff : fitnessFunctions) {
double oldFitness = ff.getFitness(suite);
System.out.println(ff.toString() + "->" + oldFitness);
}
double oldFitness = suite.getFitness();
System.out.println("oldFitness->" + oldFitness);
System.out.println("oldSize->" + suite.getTests().size());
DefaultLocalSearchObjective objective = new DefaultLocalSearchObjective<>();
for (TestSuiteFitnessFunction ff : fitnessFunctions) {
objective.addFitnessFunction(ff);
}
boolean hasImproved = suite.localSearch(objective);
System.out.println("hasImproved=" + hasImproved);
for (TestSuiteFitnessFunction ff : fitnessFunctions) {
double newFitness = ff.getFitness(suite);
System.out.println(ff.toString() + "->" + newFitness);
}
double newFitness = suite.getFitness();
System.out.println("newFitness->" + newFitness);
System.out.println("newSize->" + suite.getTests().size());
assertTrue(newFitness <= oldFitness);
}
Aggregations