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);
}
}
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);
}
}
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;
}
}
}
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());
}
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);
}
Aggregations