Search in sources :

Example 46 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestConstantInliner method testNumericArrayIndexInlining.

@Test
public void testNumericArrayIndexInlining() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    PrimitiveStatement<?> primitiveStatement = PrimitiveStatement.getPrimitiveStatement(test, int.class);
    VariableReference intVar = test.addStatement(primitiveStatement);
    ArrayStatement as = new ArrayStatement(test, int[].class, 3);
    test.addStatement(as);
    ArrayReference arrayVar = as.getArrayReference();
    ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
    ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
    ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
    test.addStatement(new AssignmentStatement(test, ai0, intVar));
    test.addStatement(new AssignmentStatement(test, ai1, intVar));
    test.addStatement(new AssignmentStatement(test, ai2, intVar));
    ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(TrivialInt.class.getConstructor(), TrivialInt.class), new ArrayList<>());
    VariableReference sut = test.addStatement(sutCS);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(ai0);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    parameters = new ArrayList<>();
    parameters.add(ai1);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    parameters = new ArrayList<>();
    parameters.add(ai2);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    System.out.println(test.toCode());
    assertFalse(code.contains("trivialInt0.testMe(int"));
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) TrivialInt(com.examples.with.different.packagename.TrivialInt) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Test(org.junit.Test)

Example 47 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestConstantInliner method testStringEndingWithClass.

@Test
public void testStringEndingWithClass() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(StringConstantInliningExample.class.getConstructor(), StringConstantInliningExample.class), new ArrayList<VariableReference>());
    VariableReference objectVar = test.addStatement(cs);
    StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(test, "test.class");
    VariableReference stringParam = test.addStatement(stringStatement);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(stringParam);
    test.addStatement(new MethodStatement(test, new GenericMethod(StringConstantInliningExample.class.getMethods()[0], StringConstantInliningExample.class), objectVar, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    System.out.println(code);
    assertFalse(code.contains("foo(test.class)"));
    assertTrue(code.contains("foo(\"test.class\")"));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) StringConstantInliningExample(com.examples.with.different.packagename.StringConstantInliningExample) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 48 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class Archive method ignoreMethodCall.

/**
 * Informs {@link org.evosuite.setup.TestCluster} that a particular method of a particular class
 * has been fully covered, and therefore no need to generate any solution to cover any of its
 * targets.
 *
 * @param className name of the class which contains the method that has been fully covered and
 *        can be ignored
 * @param methodName name of the method that has been fully covered and can be ignored
 */
protected void ignoreMethodCall(String className, String methodName) {
    TestCluster cluster = TestCluster.getInstance();
    List<GenericAccessibleObject<?>> calls = cluster.getTestCalls();
    for (GenericAccessibleObject<?> call : calls) {
        if (!call.getDeclaringClass().getName().equals(className)) {
            continue;
        }
        if (call instanceof GenericMethod) {
            GenericMethod genericMethod = (GenericMethod) call;
            if (!methodName.startsWith(genericMethod.getName())) {
                continue;
            }
            String desc = Type.getMethodDescriptor(genericMethod.getMethod());
            if ((genericMethod.getName() + desc).equals(methodName)) {
                logger.info("Removing method " + methodName + " from cluster");
                cluster.removeTestCall(call);
                logger.info("Testcalls left: " + cluster.getNumTestCalls());
            }
        } else if (call instanceof GenericConstructor) {
            GenericConstructor genericConstructor = (GenericConstructor) call;
            if (!methodName.startsWith("<init>")) {
                continue;
            }
            String desc = Type.getConstructorDescriptor(genericConstructor.getConstructor());
            if (("<init>" + desc).equals(methodName)) {
                logger.info("Removing constructor " + methodName + " from cluster");
                cluster.removeTestCall(call);
                logger.info("Testcalls left: " + cluster.getNumTestCalls());
            }
        }
    }
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) TestCluster(org.evosuite.setup.TestCluster) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 49 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod 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 50 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod 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)

Aggregations

GenericMethod (org.evosuite.utils.generic.GenericMethod)110 Test (org.junit.Test)68 VariableReference (org.evosuite.testcase.variable.VariableReference)64 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)60 Method (java.lang.reflect.Method)57 GenericClass (org.evosuite.utils.generic.GenericClass)40 MethodStatement (org.evosuite.testcase.statements.MethodStatement)29 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 ArrayList (java.util.ArrayList)18 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)15 TestCase (org.evosuite.testcase.TestCase)12 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)11 Injector (org.evosuite.runtime.javaee.injection.Injector)10 Type (java.lang.reflect.Type)9 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)8 EvoServletState (org.evosuite.runtime.javaee.javax.servlet.EvoServletState)8 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)8 GenericField (org.evosuite.utils.generic.GenericField)8 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)7