use of org.evosuite.testcase.statements.reflection.ReflectionFactory in project evosuite by EvoSuite.
the class TestFactory method insertRandomCall.
/**
* Insert a random call for the UUT at the given position
*
* @param test
* @param position
*/
public boolean insertRandomCall(TestCase test, int position) {
int previousLength = test.size();
String name = "";
currentRecursion.clear();
logger.debug("Inserting random call at position {}", position);
try {
if (reflectionFactory == null) {
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
reflectionFactory = new ReflectionFactory(targetClass);
}
if (reflectionFactory.hasPrivateFieldsOrMethods() && TimeController.getInstance().getPhasePercentage() >= Properties.REFLECTION_START_PERCENT && (Randomness.nextDouble() < Properties.P_REFLECTION_ON_PRIVATE || TestCluster.getInstance().getNumTestCalls() == 0)) {
logger.debug("Going to insert random reflection call");
return insertRandomReflectionCall(test, position, 0);
}
GenericAccessibleObject<?> o = TestCluster.getInstance().getRandomTestCall(test);
if (o == null) {
logger.warn("Have no target methods to test");
return false;
} else if (o.isConstructor()) {
if (InstanceOnlyOnce.canInstantiateOnlyOnce(o.getDeclaringClass()) && ConstraintHelper.countNumberOfNewInstances(test, o.getDeclaringClass()) != 0) {
return false;
}
GenericConstructor c = (GenericConstructor) o;
logger.debug("Adding constructor call {}", c.getName());
name = c.getName();
addConstructor(test, c, position, 0);
} else if (o.isMethod()) {
GenericMethod m = (GenericMethod) o;
logger.debug("Adding method call {}", m.getName());
name = m.getName();
if (!m.isStatic()) {
logger.debug("Getting callee of type {}", m.getOwnerClass().getTypeName());
VariableReference callee = null;
Type target = m.getOwnerType();
if (!test.hasObject(target, position)) {
// no FM for SUT
callee = createObject(test, target, position, 0, null, false, false, true);
position += test.size() - previousLength;
previousLength = test.size();
} else {
callee = test.getRandomNonNullObject(target, position);
// This may also be an inner class, in this case we can't use a SUT instance
// if (!callee.isAssignableTo(m.getDeclaringClass())) {
// callee = test.getRandomNonNullObject(m.getDeclaringClass(), position);
// }
}
logger.debug("Got callee of type {}", callee.getGenericClass().getTypeName());
if (!TestUsageChecker.canUse(m.getMethod(), callee.getVariableClass())) {
logger.debug("Cannot call method {} with callee of type {}", m, callee.getClassName());
throw new ConstructionFailedException("Cannot apply method to this callee");
}
addMethodFor(test, callee, m.copyWithNewOwner(callee.getGenericClass()), position);
} else {
// We only use this for static methods to avoid using wrong constructors (?)
addMethod(test, m, position, 0);
}
} else if (o.isField()) {
GenericField f = (GenericField) o;
name = f.getName();
logger.debug("Adding field {}", f.getName());
if (Randomness.nextBoolean()) {
addFieldAssignment(test, f, position, 0);
} else {
addField(test, f, position, 0);
}
} else {
logger.error("Got type other than method or constructor!");
return false;
}
return true;
} catch (ConstructionFailedException e) {
// TODO: Check this! - TestCluster replaced
// TestCluster.getInstance().checkDependencies(o);
logger.debug("Inserting statement {} has failed. Removing statements: {}", name, e);
// TODO: Doesn't work if position != test.size()
int lengthDifference = test.size() - previousLength;
for (int i = lengthDifference - 1; i >= 0; i--) {
// we need to remove them in order, so that the testcase is at all time consistent
if (logger.isDebugEnabled()) {
logger.debug(" Removing statement: " + test.getStatement(position + i).getCode());
}
test.remove(position + i);
}
return false;
}
}
Aggregations