Search in sources :

Example 11 with FieldReference

use of org.evosuite.testcase.variable.FieldReference in project evosuite by EvoSuite.

the class ContractViolation method same.

/**
 * Determine if we have already seen an instance of this violation
 *
 * @param other
 *            a {@link org.evosuite.contracts.ContractViolation} object.
 * @return a boolean.
 */
public boolean same(ContractViolation other) {
    // Same contract?
    if (!contract.getClass().equals(other.contract.getClass()))
        return false;
    // Same type of statement?
    if (!statement.getClass().equals(other.statement.getClass()))
        return false;
    // Same exception type?
    if (exception != null && other.exception != null) {
        if (!exception.getClass().equals(other.exception.getClass()))
            return false;
    }
    // Same method call / constructor?
    if (statement instanceof MethodStatement) {
        MethodStatement ms1 = (MethodStatement) statement;
        MethodStatement ms2 = (MethodStatement) other.statement;
        if (ms1.getMethod().getMethod().equals(ms2.getMethod().getMethod())) {
            return true;
        }
    } else if (statement instanceof ConstructorStatement) {
        ConstructorStatement ms1 = (ConstructorStatement) statement;
        ConstructorStatement ms2 = (ConstructorStatement) other.statement;
        if (ms1.getConstructor().getConstructor().equals(ms2.getConstructor().getConstructor())) {
            return true;
        }
    } else if (statement instanceof AssignmentStatement) {
        VariableReference var1 = statement.getReturnValue();
        VariableReference var2 = other.statement.getReturnValue();
        if (var1 instanceof FieldReference && var2 instanceof FieldReference) {
            if (((FieldReference) var1).getField().getField().equals(((FieldReference) var2).getField().getField()))
                return true;
        }
    }
    return false;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement)

Example 12 with FieldReference

use of org.evosuite.testcase.variable.FieldReference in project evosuite by EvoSuite.

the class DowncastTest method testFieldReferenceDoesNotNeedDowncast.

@Test
public void testFieldReferenceDoesNotNeedDowncast() 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(AbstractSuperclass.class.getField("fieldInAbstractClass"), AbstractSuperclass.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(AbstractSuperclass.class, fr2.getSource().getVariableClass());
}
Also used : DowncastExample(com.examples.with.different.packagename.test.DowncastExample) AbstractSuperclass(com.examples.with.different.packagename.test.AbstractSuperclass) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Aggregations

FieldReference (org.evosuite.testcase.variable.FieldReference)12 VariableReference (org.evosuite.testcase.variable.VariableReference)11 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)7 GenericField (org.evosuite.utils.generic.GenericField)6 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)4 MethodStatement (org.evosuite.testcase.statements.MethodStatement)3 Test (org.junit.Test)3 DowncastExample (com.examples.with.different.packagename.test.DowncastExample)2 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)2 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 ClassWithField (com.examples.with.different.packagename.coverage.ClassWithField)1 AbstractSuperclass (com.examples.with.different.packagename.test.AbstractSuperclass)1 ConcreteSubclass (com.examples.with.different.packagename.test.ConcreteSubclass)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 EntityWithParametersStatement (org.evosuite.testcase.statements.EntityWithParametersStatement)1