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());
}
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());
}
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));
}
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());
}
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());
}
Aggregations