Search in sources :

Example 51 with GenericConstructor

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

the class ConstraintVerifierTest method testInitializingBoundedVariable_correct.

@Test
public void testInitializingBoundedVariable_correct() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    // initializing bounding variable method called directly after the new
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0);
    // method on servlet after the bounding variable initialization: it is ok
    factory.addMethodFor(tc.getTestCase(), servlet, new GenericMethod(FakeServlet.class.getDeclaredMethod("foo"), FakeServlet.class), 2);
    Assert.assertEquals(3, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) Injector(org.evosuite.runtime.javaee.injection.Injector) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 52 with GenericConstructor

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

the class ConstraintVerifierTest method testInitializingBoundedVariable_correct_severalCalls.

@Test
public void testInitializingBoundedVariable_correct_severalCalls() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    // both calls on same bounding variable
    factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("fakeInjection", Servlet.class), ConstraintVerifierTest.class), 1, 0);
    // this is an atMostOnce type
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 2, 0);
    Assert.assertEquals(3, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) Injector(org.evosuite.runtime.javaee.injection.Injector) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 53 with GenericConstructor

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

the class ConstraintVerifierTest method testInitializingBoundedVariable_wrong_atMostOnce.

@Test
public void testInitializingBoundedVariable_wrong_atMostOnce() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    // initializing bounding variable method called directly after the new
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0);
    Assert.assertEquals(2, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    // this should be invalid, as executePostConstruct can be used only once on same bounded variable
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 2, 0);
    Assert.assertEquals(3, tc.size());
    Assert.assertFalse(ConstraintVerifier.verifyTest(tc));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) Injector(org.evosuite.runtime.javaee.injection.Injector) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 54 with GenericConstructor

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

the class ConstraintVerifierTest method testNoNullInputs_notNull.

@Test
public void testNoNullInputs_notNull() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet", Servlet.class), EvoServletState.class), 1, 0);
    StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), "foo");
    tc.getTestCase().addStatement(foo);
    VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0);
    factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4);
    Assert.assertEquals(5, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) EvoServletConfig(org.evosuite.runtime.javaee.javax.servlet.EvoServletConfig) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 55 with GenericConstructor

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

the class ConstraintVerifierTest method testNoNullInputs_nullString.

@Test
public void testNoNullInputs_nullString() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet", Servlet.class), EvoServletState.class), 1, 0);
    // shouldn't be able to pass it to createDispatcher
    StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), null);
    tc.getTestCase().addStatement(foo);
    VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0);
    factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4);
    /*
            even if "foo" is null and we have the P of object reuse to 1, still "foo"
            will not be used, as "null" constraint is always enforced
         */
    Assert.assertEquals(tc.getTestCase().toCode(), 6, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) EvoServletConfig(org.evosuite.runtime.javaee.javax.servlet.EvoServletConfig) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Aggregations

GenericConstructor (org.evosuite.utils.generic.GenericConstructor)76 GenericMethod (org.evosuite.utils.generic.GenericMethod)61 VariableReference (org.evosuite.testcase.variable.VariableReference)56 Test (org.junit.Test)47 Method (java.lang.reflect.Method)30 GenericClass (org.evosuite.utils.generic.GenericClass)25 MethodStatement (org.evosuite.testcase.statements.MethodStatement)22 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)19 ArrayList (java.util.ArrayList)17 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)17 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)11 Injector (org.evosuite.runtime.javaee.injection.Injector)9 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)8 GenericField (org.evosuite.utils.generic.GenericField)8 AbstractEnumInInnerClass (com.examples.with.different.packagename.AbstractEnumInInnerClass)7 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)7 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)7 EnumInInnerClass (com.examples.with.different.packagename.EnumInInnerClass)6 FactoryExample (com.examples.with.different.packagename.FactoryExample)6 Type (java.lang.reflect.Type)6