Search in sources :

Example 61 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ConstraintVerifierTest method testCanDelete.

@Test
public void testCanDelete() 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));
    // bounded variable can be deleted
    Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 0));
    // method using bounded variable should not be deleted
    Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1));
    boolean mutated = tc.deleteStatement(factory, 1);
    // should fail
    Assert.assertFalse(mutated);
    Assert.assertEquals(2, tc.size());
    mutated = tc.deleteStatement(factory, 0);
    Assert.assertTrue(mutated);
    // deleting first statement should have had effect of removing the second as well
    Assert.assertEquals(0, tc.size());
}
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 62 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ConstraintVerifierTest method testDeleteAfter.

@Test
public void testDeleteAfter() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    // get a var for EvoHttpServletRequest
    VariableReference req = factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("getNullEvoHttpServletRequest"), ConstraintVerifierTest.class), 0, 0);
    // make it a POST
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("asPOST"), EvoHttpServletRequest.class), 1, 0);
    // now add a call to 'asMultipartFormData' which does depend on POST
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("asMultipartFormData"), EvoHttpServletRequest.class), 2, 0);
    Assert.assertEquals(3, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    // deleting the last one should be fine, but not the POST, as asMultipartFormData has an 'after' dependency on it
    Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 2));
    // should not be able to delete POST directly
    Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1));
    // what about the first statement where the var is obtained? that should be valid to delete
    Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 0));
    boolean mutated = tc.deleteStatement(factory, 1);
    // should fail to delete POST
    Assert.assertFalse(mutated);
    Assert.assertEquals(3, tc.size());
    mutated = tc.deleteStatement(factory, 0);
    Assert.assertTrue(mutated);
    // should end up deleting everything
    Assert.assertEquals(0, tc.size());
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) EvoHttpServletRequest(org.evosuite.runtime.javaee.javax.servlet.http.EvoHttpServletRequest) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 63 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ConstraintVerifierTest method testInitializingBoundedVariable_wrong_callingMethodsBeforeInit.

@Test
public void testInitializingBoundedVariable_wrong_callingMethodsBeforeInit() 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.addMethodFor(tc.getTestCase(), servlet, new GenericMethod(FakeServlet.class.getDeclaredMethod("foo"), FakeServlet.class), 1);
    // initializing bounding variable method cannot be called here after "foo" is called on the 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 64 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class DowncastTest method testUnnecessaryDownCast.

@Test
public void testUnnecessaryDownCast() throws NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor());
    VariableReference int0 = builder.appendIntPrimitive(42);
    VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getANumber", new Class<?>[] { int.class }), int0);
    // This would be set during execution
    num0.setType(Integer.class);
    VariableReference boolean0 = builder.appendMethod(var, DowncastExample.class.getMethod("testMe", new Class<?>[] { Number.class }), num0);
    PrimitiveAssertion assertion = new PrimitiveAssertion();
    assertion.setSource(boolean0);
    assertion.setValue(false);
    DefaultTestCase test = builder.getDefaultTestCase();
    test.getStatement(boolean0.getStPosition()).addAssertion(assertion);
    test.removeDownCasts();
    assertEquals(Number.class, test.getStatement(2).getReturnClass());
}
Also used : DowncastExample(com.examples.with.different.packagename.test.DowncastExample) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) Test(org.junit.Test)

Example 65 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class DowncastTest method testDownCastNecessaryForInspectorAssertion.

@Test
public void testDownCastNecessaryForInspectorAssertion() throws NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor());
    VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getAbstractFoo"));
    // This would be set during execution
    num0.setType(ConcreteSubclass.class);
    DefaultTestCase test = builder.getDefaultTestCase();
    Inspector inspector = new Inspector(ConcreteSubclass.class, ConcreteSubclass.class.getMethod("getBar"));
    InspectorAssertion assertion = new InspectorAssertion(inspector, test.getStatement(num0.getStPosition()), num0, true);
    test.getStatement(num0.getStPosition()).addAssertion(assertion);
    test.removeDownCasts();
    System.out.println(test);
    assertEquals(ConcreteSubclass.class, test.getStatement(1).getReturnClass());
}
Also used : DowncastExample(com.examples.with.different.packagename.test.DowncastExample) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ConcreteSubclass(com.examples.with.different.packagename.test.ConcreteSubclass) Test(org.junit.Test)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14