Search in sources :

Example 11 with ConstructorStatement

use of org.evosuite.testcase.statements.ConstructorStatement in project evosuite by EvoSuite.

the class TestCaseBuilder method appendConstructor.

public VariableReference appendConstructor(Constructor<?> constructor, VariableReference... parameters) {
    List<VariableReference> parameter_list = Arrays.asList(parameters);
    ConstructorStatement constructorStmt = new ConstructorStatement(tc, new GenericConstructor(constructor, constructor.getDeclaringClass()), parameter_list);
    tc.addStatement(constructorStmt);
    return constructorStmt.getReturnValue();
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor)

Example 12 with ConstructorStatement

use of org.evosuite.testcase.statements.ConstructorStatement in project evosuite by EvoSuite.

the class OverloadSystemTest method testIsOverloadedInstance.

@Test
public void testIsOverloadedInstance() throws NoSuchMethodException, SecurityException {
    Method m1 = Overload.class.getMethod("execute", Overload.class, Overload.class);
    Method m2 = Overload.class.getMethod("execute", Overload.class, Object.class);
    GenericMethod gm1 = new GenericMethod(m1, Overload.class);
    GenericMethod gm2 = new GenericMethod(m2, Overload.class);
    TestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(Overload.class.getConstructors()[0], Overload.class);
    ConstructorStatement cs = new ConstructorStatement(test, gc, new ArrayList<VariableReference>());
    VariableReference overloadInstance = test.addStatement(cs);
    ConstructorStatement ocs = new ConstructorStatement(test, new GenericConstructor(Object.class.getConstructors()[0], Object.class), new ArrayList<VariableReference>());
    VariableReference objectInstance = test.addStatement(ocs);
    List<VariableReference> vars1 = new ArrayList<VariableReference>();
    vars1.add(overloadInstance);
    vars1.add(overloadInstance);
    List<VariableReference> vars2 = new ArrayList<VariableReference>();
    vars2.add(overloadInstance);
    vars2.add(objectInstance);
    Assert.assertFalse(gm1.isOverloaded(vars1));
    Assert.assertTrue(gm2.isOverloaded(vars1));
    Assert.assertTrue(gm1.isOverloaded(vars2));
    Assert.assertFalse(gm2.isOverloaded(vars2));
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 13 with ConstructorStatement

use of org.evosuite.testcase.statements.ConstructorStatement in project evosuite by EvoSuite.

the class Issre13SystemTest method testOnSpecificTest.

@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
    // force using DSE
    Properties.DSE_PROBABILITY = 1.0;
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    // String string0 = "baz5";
    VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
    // DseFoo dseFoo0 = new DseFoo();
    GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
    ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
    VariableReference fooVar = test.addStatement(fooConstructorStatement);
    Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
    GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    // DseBar dseBar0 = new DseBar(string0);
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
    VariableReference callee = test.addStatement(constructorStatement);
    // dseBar0.coverMe(dseFoo0);
    Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
    test.addStatement(ms);
    System.out.println(test);
    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
    BranchCoverageMap.getInstance().searchStarted(null);
    assertEquals(4.0, fitness.getFitness(suite), 0.1F);
    suite.addTest(test);
    assertEquals(1.0, fitness.getFitness(suite), 0.1F);
    System.out.println("Test suite: " + suite);
    TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
    LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
    localObjective.addFitnessFunction(fitness);
    localSearch.doSearch(suite, localObjective);
    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals(0.0, fitness.getFitness(suite), 0.1F);
    BranchCoverageMap.getInstance().searchFinished(null);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) DseBar(com.examples.with.different.packagename.localsearch.DseBar) TestSuiteLocalSearch(org.evosuite.testsuite.localsearch.TestSuiteLocalSearch) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) GenericClass(org.evosuite.utils.generic.GenericClass) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DseFoo(com.examples.with.different.packagename.localsearch.DseFoo) Test(org.junit.Test)

Example 14 with ConstructorStatement

use of org.evosuite.testcase.statements.ConstructorStatement in project evosuite by EvoSuite.

the class MethodCoverageSuiteFitness method handleConstructorExceptions.

/**
 * If there is an exception in a super-constructor, then the corresponding
 * constructor might not be included in the execution trace
 *
 * @param suite
 * @param results
 * @param calledMethods
 */
protected void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Set<String> calledMethods) {
    if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions())
        return;
    Integer exceptionPosition = result.getFirstPositionOfThrownException();
    Statement statement = result.test.getStatement(exceptionPosition);
    if (statement instanceof ConstructorStatement) {
        ConstructorStatement c = (ConstructorStatement) statement;
        String className = c.getConstructor().getName();
        String methodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
        String name = className + "." + methodName;
        if (methodCoverageMap.containsKey(name) && !calledMethods.contains(name)) {
            TestFitnessFunction goal = methodCoverageMap.get(name);
            // only include methods being called
            test.getTestCase().addCoveredGoal(goal);
            calledMethods.add(name);
            this.toRemoveMethods.add(name);
            if (Properties.TEST_ARCHIVE) {
                Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
            }
        }
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 15 with ConstructorStatement

use of org.evosuite.testcase.statements.ConstructorStatement in project evosuite by EvoSuite.

the class AbstractMOSA method hasMethodCall.

/**
 * This method checks whether the test has only primitive type statements. Indeed,
 * crossover and mutation can lead to tests with no method calls (methods or constructors call),
 * thus, when executed they will never cover something in the class under test.
 *
 * @param test to check
 * @return true if the test has at least one method or constructor call (i.e., the test may
 * cover something when executed; false otherwise
 */
private boolean hasMethodCall(T test) {
    boolean flag = false;
    TestCase tc = ((TestChromosome) test).getTestCase();
    for (Statement s : tc) {
        if (s instanceof MethodStatement) {
            MethodStatement ms = (MethodStatement) s;
            boolean isTargetMethod = ms.getDeclaringClassName().equals(Properties.TARGET_CLASS);
            if (isTargetMethod)
                return true;
        }
        if (s instanceof ConstructorStatement) {
            ConstructorStatement ms = (ConstructorStatement) s;
            boolean isTargetMethod = ms.getDeclaringClassName().equals(Properties.TARGET_CLASS);
            if (isTargetMethod)
                return true;
        }
    }
    return flag;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) TestCase(org.evosuite.testcase.TestCase) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)36 MethodStatement (org.evosuite.testcase.statements.MethodStatement)22 VariableReference (org.evosuite.testcase.variable.VariableReference)22 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)19 GenericMethod (org.evosuite.utils.generic.GenericMethod)15 Method (java.lang.reflect.Method)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 Statement (org.evosuite.testcase.statements.Statement)10 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 GenericClass (org.evosuite.utils.generic.GenericClass)9 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)7 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)6 FlagExample1 (com.examples.with.different.packagename.FlagExample1)5 BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)5 TestCase (org.evosuite.testcase.TestCase)4 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)4 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)3 DefUseCoverageFactory (org.evosuite.coverage.dataflow.DefUseCoverageFactory)3 DefUseCoverageSuiteFitness (org.evosuite.coverage.dataflow.DefUseCoverageSuiteFitness)3