use of org.evosuite.utils.generic.GenericMethod 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.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testNoDirectInsertion.
@Test
public void testNoDirectInsertion() throws Exception {
TestChromosome tc = new TestChromosome();
TestFactory factory = TestFactory.getInstance();
GenericMethod gm = new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("noDirect", null), ConstraintVerifierTest.class);
// should not be possible to insert it
int pos = ConstraintVerifier.getAValidPositionForInsertion(gm, tc.getTestCase(), 0);
Assert.assertTrue(pos < 0);
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testInitializingBoundedVariable_wrong_noConstructor.
@Test
public void testInitializingBoundedVariable_wrong_noConstructor() throws Exception {
TestChromosome tc = new TestChromosome();
TestFactory factory = TestFactory.getInstance();
factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("getAFakeServletInstance"), ConstraintVerifierTest.class), 0, 0);
// initializing bounding variable method called on instance not generated with new
factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0);
Assert.assertEquals(2, tc.size());
Assert.assertFalse(ConstraintVerifier.verifyTest(tc));
}
use of org.evosuite.utils.generic.GenericMethod 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.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testGenerics_methodWithExtends.
@Test
public void testGenerics_methodWithExtends() throws NoSuchMethodException, ConstructionFailedException {
// first construct a test case for the Generic method
TestCase tc = new DefaultTestCase();
TestFactory.getInstance().addConstructor(tc, new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
VariableReference genericClass = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(ClassWithGeneric.class.getDeclaredConstructor(), ClassWithGeneric.class), 1, 0);
Method m = ClassWithGeneric.class.getDeclaredMethod("hello", Servlet.class);
GenericMethod gm = new GenericMethod(m, ClassWithGeneric.class);
TestFactory.getInstance().addMethodFor(tc, genericClass, gm, 2);
// Check if generic types were correctly analyzed/inferred
Type[] types = gm.getParameterTypes();
// only 1 input
assertEquals(1, types.length);
Type type = types[0];
Assert.assertNotNull(type);
TypeVariable<?> tv = (TypeVariable<?>) type;
assertEquals(1, tv.getBounds().length);
Class<?> upper = (Class<?>) tv.getBounds()[0];
assertEquals(Servlet.class, upper);
// Finally, visit the test
TestCodeVisitor visitor = new TestCodeVisitor();
// should not throw exception
tc.accept(visitor);
}
Aggregations