Search in sources :

Example 66 with VariableReference

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());
}
Also used : DowncastExample(com.examples.with.different.packagename.test.DowncastExample) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ConcreteSubclass(com.examples.with.different.packagename.test.ConcreteSubclass) Test(org.junit.Test)

Example 67 with VariableReference

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());
}
Also used : DowncastExample(com.examples.with.different.packagename.test.DowncastExample) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ConcreteSubclass(com.examples.with.different.packagename.test.ConcreteSubclass) Test(org.junit.Test)

Example 68 with VariableReference

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());
}
Also used : DowncastExample(com.examples.with.different.packagename.test.DowncastExample) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) ConcreteSubclass(com.examples.with.different.packagename.test.ConcreteSubclass) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 69 with VariableReference

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);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) EnumInInnerClass(com.examples.with.different.packagename.EnumInInnerClass) AbstractEnumInInnerClass(com.examples.with.different.packagename.AbstractEnumInInnerClass) Test(org.junit.Test)

Example 70 with VariableReference

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"));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) AbstractEnumInInnerClass(com.examples.with.different.packagename.AbstractEnumInInnerClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) AbstractEnumUser(com.examples.with.different.packagename.AbstractEnumUser) Test(org.junit.Test)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14