Search in sources :

Example 1 with FieldReference

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

the class SymbolicObserver method after.

private void after(AssignmentStatement s, Scope scope) {
    VariableReference lhs = s.getReturnValue();
    VariableReference rhs = s.getValue();
    ReferenceExpressionPair readResult = read(rhs, scope);
    if (lhs instanceof FieldReference) {
        writeField((FieldReference) lhs, readResult, scope);
    } else if (lhs instanceof ArrayIndex) {
        writeArray((ArrayIndex) lhs, readResult, scope);
    } else {
        writeVariable(lhs, readResult);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 2 with FieldReference

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

the class EvoTestCaseCodeGenerator method createFieldWriteAccessStmt.

@Override
public void createFieldWriteAccessStmt(CaptureLog log, int logRecNo) {
    // assumption: all necessary statements are created and there is one variable for each referenced object
    final Object[] methodArgs = log.params.get(logRecNo);
    final int oid = log.objectIds.get(logRecNo);
    final int captureId = log.captureIds.get(logRecNo);
    final String fieldName = log.getNameOfAccessedFields(captureId);
    final String typeName = log.getTypeName(oid);
    try {
        final Class<?> type = getClassForName(typeName);
        final String fieldDesc = log.descList.get(logRecNo);
        final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
        final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type), this.oidToVarRefMap.get(oid));
        final AssignmentStatement assignment;
        final Integer arg = (Integer) methodArgs[0];
        if (arg == null) {
            final NullStatement nullStmt = new NullStatement(testCase, fieldType);
            final VariableReference nullReference = testCase.addStatement(nullStmt);
            assignment = new AssignmentStatement(testCase, targetFieldRef, nullReference);
        } else {
            assignment = new AssignmentStatement(testCase, targetFieldRef, this.oidToVarRefMap.get(arg));
        }
        final VariableReference varRef = testCase.addStatement(assignment);
        logger.debug("Adding assignment statement: " + assignment.getCode());
        if (arg != null) {
            this.oidToVarRefMap.put(arg, varRef);
        }
    } catch (final Exception e) {
        CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating field write access stmt. Log: %s", logRecNo, log);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) NullStatement(org.evosuite.testcase.statements.NullStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 3 with FieldReference

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

the class TestCarvingExecutionObserver method afterStatement.

/**
 * own comment..
 */
@Override
public void afterStatement(final Statement statement, final Scope scope, final Throwable exception) {
    if (statement instanceof AssignmentStatement) {
        final AssignmentStatement assign = (AssignmentStatement) statement;
        final VariableReference left = assign.getReturnValue();
        if (left instanceof FieldReference) {
            final FieldReference fieldRef = (FieldReference) left;
            final GenericField field = fieldRef.getField();
            FieldRegistry.notifyModification(field.isStatic() ? null : scope.getObject(fieldRef.getSource()), this.captureId, Type.getInternalName(field.getDeclaringClass()), field.getName(), Type.getDescriptor(field.getField().getType()));
            // PUTFIELDRegistry creates PUTXXX as well as corresponding GETXXX statements
            this.captureId -= 2;
        }
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 4 with FieldReference

use of org.evosuite.testcase.variable.FieldReference 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 5 with FieldReference

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

the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.

@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
    Class<?> sut = ClassWithField.class;
    DefaultTestCase tc = new DefaultTestCase();
    // ClassWithField classWithField0 = new ClassWithField();
    GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
    ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
    VariableReference obj = tc.addStatement(constructorStatement);
    // classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
    FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
    Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
    GenericMethod gm = new GenericMethod(m, sut);
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    // classWithField0.BOOLEAN_FIELD = false;
    VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
    tc.addStatement(new AssignmentStatement(tc, field, boolRef));
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    Properties.TARGET_CLASS = sut.getCanonicalName();
    Properties.JUNIT_TESTS = true;
    TestSuiteChromosome testSuite = new TestSuiteChromosome();
    testSuite.addTest(tc);
    FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
    assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
    assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
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) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ClassWithField(com.examples.with.different.packagename.coverage.ClassWithField) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) 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