use of org.evosuite.utils.generic.GenericMethod 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));
}
use of org.evosuite.utils.generic.GenericMethod 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));
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testDelete_multipleVarsThatCouldBeReused.
@Test
public void testDelete_multipleVarsThatCouldBeReused() 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);
// now do it again
VariableReference secondServlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 2, 0);
factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 3, 0);
MethodStatement mt = (MethodStatement) tc.getTestCase().getStatement(3);
// be sure it is using the second servlet
mt.replace(servlet, secondServlet);
Assert.assertEquals(4, 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));
// bounded variable can be deleted
Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 2));
// method using bounded variable should not be deleted
Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 3));
boolean mutated = tc.deleteStatement(factory, 2);
Assert.assertTrue(mutated);
/*
deleting the bounded variable in position 2 should lead to also delete the initializing variable in 3,
and not end up with 3 re-using 0
*/
Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
Assert.assertEquals(2, tc.size());
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testPostConstructIssue.
@Test
public void testPostConstructIssue() 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);
// this will also add a statement for the Class
factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class, Class.class), Injector.class), 1, 0);
Assert.assertEquals(3, tc.size());
Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
boolean mutated = tc.deleteStatement(factory, 2);
// should fail to delete the post construct
Assert.assertFalse(mutated);
Assert.assertEquals(3, tc.size());
Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1));
mutated = tc.deleteStatement(factory, 1);
// eliminating the Class.class var should fail as well, as post construct accepts no null
Assert.assertFalse(mutated);
Assert.assertEquals(3, tc.size());
mutated = tc.deleteStatement(factory, 0);
// eliminating the servlet is fine
Assert.assertTrue(mutated);
// tricky, as Class.class is not bounded to the servlet
Assert.assertEquals(1, tc.size());
Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testGenerics_staticMethod.
@Test
public void testGenerics_staticMethod() throws NoSuchMethodException, ConstructionFailedException {
// first construct a test case for the Generic method
TestCase tc = new DefaultTestCase();
TestFactory.getInstance().addConstructor(tc, new GenericConstructor(Object.class.getDeclaredConstructor(), Object.class), 0, 0);
Method m = TestCodeVisitorTest.class.getDeclaredMethod("bar", Object.class);
GenericMethod gm = new GenericMethod(m, TestCodeVisitorTest.class);
TestFactory.getInstance().addMethod(tc, gm, 1, 0);
// 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);
WildcardTypeImpl wt = (WildcardTypeImpl) type;
assertEquals(0, wt.getLowerBounds().length);
assertEquals(1, wt.getUpperBounds().length);
Class<?> upper = (Class<?>) wt.getUpperBounds()[0];
assertEquals(Object.class, upper);
// Finally, visit the test
TestCodeVisitor visitor = new TestCodeVisitor();
// should not throw exception
tc.accept(visitor);
System.out.println(visitor.getCode());
}
Aggregations