use of org.evosuite.utils.generic.GenericConstructor 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.GenericConstructor 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.GenericConstructor in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testClashingImportNames.
@Test
public void testClashingImportNames() throws NoSuchMethodException, ConstructionFailedException {
TestCase tc = new DefaultTestCase();
TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class.getDeclaredConstructor(), com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class), 0, 0);
TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class.getDeclaredConstructor(), com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class), 1, 0);
TestCodeVisitor visitor = new TestCodeVisitor();
tc.accept(visitor);
System.out.println(visitor.getCode());
Set<Class<?>> imports = visitor.getImports();
// Imported
assertTrue(imports.contains(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class));
// Not imported as the fully qualified name is used
assertFalse(imports.contains(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class));
assertEquals("ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class));
assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class));
}
use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testClashingImportNamesSubClasses.
@Test
public void testClashingImportNamesSubClasses() throws NoSuchMethodException, ConstructionFailedException {
TestCase tc = new DefaultTestCase();
TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class.getDeclaredConstructor(), com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class), 0, 0);
TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class.getDeclaredConstructor(), com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class), 1, 0);
TestCodeVisitor visitor = new TestCodeVisitor();
tc.accept(visitor);
System.out.println(visitor.getCode());
Set<Class<?>> imports = visitor.getImports();
// Imported
assertTrue(imports.contains(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class));
// Not imported as the fully qualified name is used
assertFalse(imports.contains(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class));
assertEquals("ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class));
assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class));
assertEquals("ExampleWithInnerClass.Foo", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class));
assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class));
}
use of org.evosuite.utils.generic.GenericConstructor 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