use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createCollectionInitStmt.
@Override
public void createCollectionInitStmt(final CaptureLog log, final int logRecNo) {
try {
final int oid = log.objectIds.get(logRecNo);
final Object[] params = log.params.get(logRecNo);
String collTypeName = log.getTypeName(oid);
Class<?> collType = getClassForName(collTypeName);
// -- determine if an alternative collection must be used for code generation
final boolean isPublic = java.lang.reflect.Modifier.isPublic(collType.getModifiers());
if (!isPublic || !hasDefaultConstructor(collType)) {
if (Set.class.isAssignableFrom(collType)) {
collTypeName = HashSet.class.getName();
collType = HashSet.class;
} else if (List.class.isAssignableFrom(collType)) {
collTypeName = ArrayList.class.getName();
collType = ArrayList.class;
} else if (Queue.class.isAssignableFrom(collType)) {
collTypeName = ArrayDeque.class.getName();
collType = ArrayDeque.class;
} else {
CodeGeneratorException.propagateError("[logRecNo = %s] - collection %s is not supported", logRecNo, collType);
}
}
// -- create code for instantiating collection
final List<VariableReference> noParams = Collections.emptyList();
final ConstructorStatement constrStmt = new ConstructorStatement(testCase, new GenericConstructor(collType.getConstructor(new Class<?>[0]), collType), noParams);
final VariableReference collRef = testCase.addStatement(constrStmt);
this.oidToVarRefMap.put(oid, collRef);
// --- fill collection
MethodStatement methodStmt;
// is either an oid or null
Integer argOID;
ArrayList<VariableReference> paramList;
Method method;
for (int i = 0; i < params.length; i++) {
paramList = new ArrayList<VariableReference>(1);
argOID = (Integer) params[i];
if (argOID == null || !this.oidToVarRefMap.containsKey(argOID)) {
VariableReference var = testCase.addStatement(new NullStatement(testCase, Object.class));
paramList.add(var);
} else {
VariableReference var = this.oidToVarRefMap.get(argOID);
paramList.add(var);
}
method = collType.getMethod("add", Object.class);
methodStmt = new MethodStatement(testCase, new GenericMethod(method, collType), collRef, paramList);
testCase.addStatement(methodStmt);
}
} catch (final Exception e) {
CodeGeneratorException.propagateError("[logRecNo = %s] - an unexpected error occurred while creating collection init stmt", logRecNo, e);
}
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createUnobservedInitStmt.
@SuppressWarnings({ "rawtypes" })
@Override
public void createUnobservedInitStmt(CaptureLog log, int logRecNo) {
// NOTE: PLAIN INIT: has always one non-null param
// TODO: use primitives
final int oid = log.objectIds.get(logRecNo);
try {
final Object value = log.params.get(logRecNo)[0];
final PrimitiveStatement stringRep = new ImmutableStringPrimitiveStatement(testCase, (String) value);
final VariableReference stringRepRef = testCase.addStatement(stringRep);
final MethodStatement m = new MethodStatement(testCase, new GenericMethod(EvoSuiteXStream.class.getMethod("fromString", new Class<?>[] { String.class }), EvoSuiteXStream.class), null, Arrays.asList(stringRepRef));
this.oidToVarRefMap.put(oid, testCase.addStatement(m));
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class ReferenceLocalSearch method changeParameters.
/**
* Switch parameter/callee variables with other available objects
*
* @param test
* @param statement
* @return
*/
private boolean changeParameters(TestChromosome test, int statement) {
logger.debug("Changing parameters");
Statement stmt = test.getTestCase().getStatement(statement);
if (stmt instanceof MethodStatement) {
return replaceMethodParameter(test, (MethodStatement) stmt);
} else if (stmt instanceof ConstructorStatement) {
return replaceConstructorParameter(test, (ConstructorStatement) stmt);
} else if (stmt instanceof FieldStatement) {
return replaceFieldSource(test, (FieldStatement) stmt);
} else {
return false;
}
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class ChangeMutationSystemTest method getIntTest.
private TestCase getIntTest(int x) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, x));
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { intVar }));
test.addStatement(ms);
return test;
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class ChangeMutationSystemTest method getTwoIntTest.
private TestCase getTwoIntTest(int x, int y) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
VariableReference intVar1 = test.addStatement(new IntPrimitiveStatement(test, x));
VariableReference intVar2 = test.addStatement(new IntPrimitiveStatement(test, y));
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { intVar1, intVar2 }));
test.addStatement(ms);
return test;
}
Aggregations