use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.
the class TestExtractingVisitor method retrieveVariableReference.
private VariableReference retrieveVariableReference(InfixExpression infixExpr, Class<?> exprType) {
if (exprType == null) {
exprType = retrieveTypeClass(infixExpr);
}
VariableReference ref = new VariableReferenceImpl(testCase.getReference(), exprType);
VariableReference leftOperand = retrieveVariableReference(infixExpr.getLeftOperand(), null);
leftOperand.setOriginalCode(infixExpr.getLeftOperand().toString());
Operator operator = Operator.toOperator(infixExpr.getOperator().toString());
VariableReference rightOperand = retrieveVariableReference(infixExpr.getRightOperand(), null);
rightOperand.setOriginalCode(infixExpr.getRightOperand().toString());
PrimitiveExpression expr = new PrimitiveExpression(testCase.getReference(), ref, leftOperand, operator, rightOperand);
testCase.addStatement(expr);
return ref;
}
use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.
the class TestCaseCodeGenerator method createPlainInitStmt.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void createPlainInitStmt(final int logRecNo, final TestCase testCase) {
// NOTE: PLAIN INIT: has always one non-null param
// TODO: use primitives
final int oid = this.log.objectIds.get(logRecNo);
if (this.oidToVarRefMap.containsKey(oid)) {
// TODO this might happen because of Integer.valueOf(), for example. . Is this approach ok?
return;
}
final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
final Object value = this.log.params.get(logRecNo)[0];
if (// Class is a plain type according to log
value instanceof Class) {
// FIXME this code needs to get working
// try
// {
// final VariableReference varRef = new VariableReferenceImpl(testCase, Class.class);
// final VariableReference valueRef = new VariableReferenceImpl(testCase, getClassForName(type));
//
// final AssignmentStatement assign = new AssignmentStatement(testCase, varRef, valueRef);
// this.oidToVarRefMap.put(oid, testCase.addStatement(assign));
// }
// catch(final Exception e)
// {
// throw new RuntimeException(e);
// }
} else {
final PrimitiveStatement primitiveValue = PrimitiveStatement.getPrimitiveStatement(testCase, getClassForName(type));
primitiveValue.setValue(value);
final VariableReference varRef = testCase.addStatement(primitiveValue);
this.oidToVarRefMap.put(oid, varRef);
}
}
use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.
the class TestCaseCodeGenerator method createUnobservedInitStmt.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void createUnobservedInitStmt(final int logRecNo, final TestCase testCase) {
// NOTE: PLAIN INIT: has always one non-null param
// TODO: use primitives
final int oid = this.log.objectIds.get(logRecNo);
final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
try {
// Class.forName("com.thoughtworks.xstream.XStream", true, StaticTestCluster.classLoader);
final Class<?> xStreamType = getClassForName("com.thoughtworks.xstream.XStream");
// Class.forName(type, true, StaticTestCluster.classLoader);
final Class<?> typeClass = getClassForName(type);
final Object value = this.log.params.get(logRecNo)[0];
if (xStreamRef == null) {
final ConstructorStatement constr = new ConstructorStatement(testCase, new GenericConstructor(xStreamType.getConstructor(new Class<?>[0]), xStreamType), Collections.EMPTY_LIST);
xStreamRef = testCase.addStatement(constr);
}
// Class.forName("java.lang.String", true, StaticTestCluster.classLoader);
final Class<?> stringType = getClassForName("java.lang.String");
final PrimitiveStatement stringRep = PrimitiveStatement.getPrimitiveStatement(testCase, stringType);
stringRep.setValue(value);
final VariableReference stringRepRef = testCase.addStatement(stringRep);
final MethodStatement m = new MethodStatement(testCase, new GenericMethod(xStreamType.getMethod("fromXML", stringType), typeClass), xStreamRef, Arrays.asList(stringRepRef));
this.oidToVarRefMap.put(oid, testCase.addStatement(m));
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.
the class TestCaseCodeGenerator method createFieldWriteAccessStmt.
private void createFieldWriteAccessStmt(final int logRecNo, final TestCase testCase) {
// assumption: all necessary statements are created and there is one variable for reach referenced object
final Object[] methodArgs = this.log.params.get(logRecNo);
final int oid = this.log.objectIds.get(logRecNo);
final int captureId = this.log.captureIds.get(logRecNo);
final String fieldName = this.log.namesOfAccessedFields.get(captureId);
final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
try {
final Class<?> type = getClassForName(typeName);
final String fieldDesc = this.log.descList.get(logRecNo);
final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type));
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);
if (arg != null) {
this.oidToVarRefMap.put(arg, varRef);
}
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.
the class TestCaseCodeGenerator method createMethodCallStmt.
private void createMethodCallStmt(final int logRecNo, final TestCase testCase) {
// assumption: all necessary statements are created and there is one variable for reach referenced object
final int oid = this.log.objectIds.get(logRecNo);
final Object[] methodArgs = this.log.params.get(logRecNo);
final String methodName = this.log.methodNames.get(logRecNo);
final String methodDesc = this.log.descList.get(logRecNo);
final org.objectweb.asm.Type[] methodParamTypes = org.objectweb.asm.Type.getArgumentTypes(methodDesc);
final Class<?>[] methodParamTypeClasses = new Class[methodParamTypes.length];
for (int i = 0; i < methodParamTypes.length; i++) {
methodParamTypeClasses[i] = getClassFromType(methodParamTypes[i]);
}
final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
Class<?> type;
try {
// Class.forName(typeName, true, StaticTestCluster.classLoader);
type = getClassForName(typeName);
final ArrayList<VariableReference> args = new ArrayList<VariableReference>();
// is either an oid or null
Integer argOID;
for (int i = 0; i < methodArgs.length; i++) {
argOID = (Integer) methodArgs[i];
if (argOID == null) {
args.add(testCase.addStatement(new NullStatement(testCase, methodParamTypeClasses[i])));
} else {
args.add(this.oidToVarRefMap.get(argOID));
}
}
if (CaptureLog.OBSERVED_INIT.equals(methodName)) {
// Person var0 = new Person();
final ConstructorStatement constStmt = new ConstructorStatement(testCase, new GenericConstructor(type.getDeclaredConstructor(methodParamTypeClasses), type), args);
this.oidToVarRefMap.put(oid, testCase.addStatement(constStmt));
} else // ------------------ handling for ordinary method calls e.g. var1 = var0.doSth();
{
final Object returnValue = this.log.returnValues.get(logRecNo);
if (CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) {
final MethodStatement m = new MethodStatement(testCase, new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), type.getMethod(methodName, methodParamTypeClasses).getReturnType()), this.oidToVarRefMap.get(oid), args);
testCase.addStatement(m);
} else {
final org.objectweb.asm.Type returnType = org.objectweb.asm.Type.getReturnType(methodDesc);
// Person var0 = var.getPerson();
final MethodStatement m = new MethodStatement(testCase, new GenericMethod(this.getDeclaredMethod(type, methodName, methodParamTypeClasses), getClassFromType(returnType)), this.oidToVarRefMap.get(oid), args);
final Integer returnValueOID = (Integer) returnValue;
this.oidToVarRefMap.put(returnValueOID, testCase.addStatement(m));
}
}
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Aggregations