use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class DowncastTest method testDownCastNecessaryForField.
@Test
public void testDownCastNecessaryForField() throws NoSuchMethodException, NoSuchFieldException {
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();
PrimitiveFieldAssertion assertion = new PrimitiveFieldAssertion();
assertion.setValue(true);
assertion.setSource(num0);
assertion.setField(ConcreteSubclass.class.getField("fieldInConcreteClass"));
test.getStatement(num0.getStPosition()).addAssertion(assertion);
test.removeDownCasts();
System.out.println(test);
assertEquals(ConcreteSubclass.class, test.getStatement(1).getReturnClass());
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class DowncastTest method testDownCastUnnecessaryForInspectorAssertion.
@Test
public void testDownCastUnnecessaryForInspectorAssertion() 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("getFoo"));
InspectorAssertion assertion = new InspectorAssertion(inspector, test.getStatement(num0.getStPosition()), num0, true);
test.getStatement(num0.getStPosition()).addAssertion(assertion);
test.removeDownCasts();
System.out.println(test);
assertEquals(AbstractSuperclass.class, test.getStatement(1).getReturnClass());
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class DowncastTest method testFieldReferenceNeedsDowncast.
@Test
public void testFieldReferenceNeedsDowncast() throws NoSuchMethodException, NoSuchFieldException {
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);
VariableReference bool0 = builder.appendBooleanPrimitive(true);
DefaultTestCase test = builder.getDefaultTestCase();
FieldReference fr = new FieldReference(test, new GenericField(ConcreteSubclass.class.getField("fieldInConcreteClass"), ConcreteSubclass.class), num0);
AssignmentStatement statement = new AssignmentStatement(test, fr, bool0);
test.addStatement(statement);
test.removeDownCasts();
System.out.println(test);
FieldReference fr2 = (FieldReference) test.getStatement(3).getReturnValue();
assertEquals(ConcreteSubclass.class, fr2.getSource().getVariableClass());
}
use of org.evosuite.testcase.variable.VariableReference 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.testcase.variable.VariableReference 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"));
}
Aggregations