use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testBaseTest.
@Test
public void testBaseTest() throws Exception {
TestChromosome tc = new TestChromosome();
TestFactory factory = TestFactory.getInstance();
factory.addConstructor(tc.getTestCase(), new GenericConstructor(Object.class.getConstructor(), Object.class), 0, 0);
Assert.assertEquals(1, tc.size());
Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testUniqueConstructors.
@Test
public void testUniqueConstructors() throws Exception {
TestChromosome tc = new TestChromosome();
TestFactory factory = TestFactory.getInstance();
Properties.JEE = true;
factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
// doing it a second time should fail
try {
factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
Assert.fail();
} catch (Exception e) {
// expected
}
}
use of org.evosuite.utils.generic.GenericConstructor 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);
}
use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testInnerClassAbstractEnum.
/*
* There are some weird enum constructs in Closure, so we need to check that enum names
* don't contain the name of the anonymous class they might represent
*/
@Test
public void testInnerClassAbstractEnum() throws NoSuchMethodException, ConstructionFailedException {
// first construct a test case for the Generic method
TestCase tc = new DefaultTestCase();
VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(AbstractEnumUser.class.getDeclaredConstructor(), AbstractEnumUser.class), 0, 0);
EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, AbstractEnumInInnerClass.AnEnum.class);
primitiveStatement.setValue(AbstractEnumInInnerClass.AnEnum.FOO);
VariableReference enumObject = tc.addStatement(primitiveStatement);
Method m = AbstractEnumUser.class.getDeclaredMethod("foo", AbstractEnumInInnerClass.AnEnum.class);
GenericMethod gm = new GenericMethod(m, AbstractEnumUser.class);
MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject));
tc.addStatement(ms);
// Finally, visit the test
TestCodeVisitor visitor = new TestCodeVisitor();
// should not throw exception
tc.accept(visitor);
String code = visitor.getCode();
System.out.println(code);
assertFalse(code.contains("= AbstractEnumInInnerClass.AnEnum.1.FOO"));
assertTrue(code.contains("= AbstractEnumInInnerClass.AnEnum.FOO"));
}
use of org.evosuite.utils.generic.GenericConstructor in project evosuite by EvoSuite.
the class TestCodeVisitorTest method testInnerClassEnum.
@Test
public void testInnerClassEnum() throws Throwable {
// first construct a test case for the Generic method
TestCase tc = new DefaultTestCase();
VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(EnumUser.class.getDeclaredConstructor(), EnumUser.class), 0, 0);
EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, EnumInInnerClass.AnEnum.class);
primitiveStatement.setValue(EnumInInnerClass.AnEnum.FOO);
VariableReference enumObject = tc.addStatement(primitiveStatement);
Method m = EnumUser.class.getDeclaredMethod("foo", EnumInInnerClass.AnEnum.class);
GenericMethod gm = new GenericMethod(m, EnumUser.class);
MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject));
tc.addStatement(ms);
// Finally, visit the test
TestCodeVisitor visitor = new TestCodeVisitor();
// should not throw exception
tc.accept(visitor);
String code = visitor.getCode();
assertTrue(code.contains("= EnumInInnerClass.AnEnum.FOO"));
}
Aggregations