use of org.evosuite.ga.ConstructionFailedException in project evosuite by EvoSuite.
the class TestFactory method insertRandomReflectionCall.
private boolean insertRandomReflectionCall(TestCase test, int position, int recursionDepth) throws ConstructionFailedException {
logger.debug("Recursion depth: " + recursionDepth);
if (recursionDepth > Properties.MAX_RECURSION) {
logger.debug("Max recursion depth reached");
throw new ConstructionFailedException("Max recursion depth reached");
}
int length = test.size();
List<VariableReference> parameters = null;
Statement st = null;
if (reflectionFactory.nextUseField()) {
Field field = reflectionFactory.nextField();
parameters = satisfyParameters(test, null, // we need a reference to the SUT, and one to a variable of same type of chosen field
Arrays.asList((Type) reflectionFactory.getReflectedClass(), (Type) field.getType()), null, position, recursionDepth + 1, true, false, true);
try {
st = new PrivateFieldStatement(test, reflectionFactory.getReflectedClass(), field.getName(), parameters.get(0), parameters.get(1));
} catch (NoSuchFieldException e) {
logger.error("Reflection problem: " + e, e);
throw new ConstructionFailedException("Reflection problem");
}
} else {
// method
Method method = reflectionFactory.nextMethod();
List<Type> list = new ArrayList<>();
list.add(reflectionFactory.getReflectedClass());
list.addAll(Arrays.asList(method.getGenericParameterTypes()));
// Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
parameters = satisfyParameters(test, null, list, null, position, recursionDepth + 1, true, false, true);
VariableReference callee = parameters.remove(0);
st = new PrivateMethodStatement(test, reflectionFactory.getReflectedClass(), method, callee, parameters, Modifier.isStatic(method.getModifiers()));
}
int newLength = test.size();
position += (newLength - length);
test.addStatement(st, position);
return true;
}
use of org.evosuite.ga.ConstructionFailedException in project evosuite by EvoSuite.
the class NullReferenceSearch method doSearch.
/* (non-Javadoc)
* @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
*/
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
NullStatement nullStatement = (NullStatement) test.getTestCase().getStatement(statement);
TestCase newTest = test.getTestCase();
TestCase oldTest = newTest.clone();
ExecutionResult oldResult = test.getLastExecutionResult();
// double oldFitness = test.getFitness();
Map<FitnessFunction<?>, Double> oldFitnesses = test.getFitnessValues();
Map<FitnessFunction<?>, Double> oldLastFitnesses = test.getPreviousFitnessValues();
try {
TestFactory.getInstance().attemptGeneration(newTest, nullStatement.getReturnType(), statement);
if (!objective.hasImproved(test)) {
test.setTestCase(oldTest);
test.setLastExecutionResult(oldResult);
// test.setFitness(oldFitness);
test.setFitnessValues(oldFitnesses);
test.setPreviousFitnessValues(oldLastFitnesses);
} else {
return true;
}
} catch (ConstructionFailedException e) {
// If we can't construct it, then ignore
}
return false;
}
use of org.evosuite.ga.ConstructionFailedException in project evosuite by EvoSuite.
the class AllMethodsTestChromosomeFactory method getRandomTestCase.
/**
* Create a random individual
*
* @param size
*/
private TestCase getRandomTestCase(int size) {
boolean tracerEnabled = ExecutionTracer.isEnabled();
if (tracerEnabled)
ExecutionTracer.disable();
TestCase test = getNewTestCase();
int num = 0;
// Choose a random length in 0 - size
int length = Randomness.nextInt(size);
while (length == 0) length = Randomness.nextInt(size);
// Then add random stuff
while (test.size() < length && num < Properties.MAX_ATTEMPTS) {
if (remainingMethods.size() == 0) {
reset();
}
GenericAccessibleObject<?> call = Randomness.choice(remainingMethods);
attemptedMethods.add(call);
remainingMethods.remove(call);
try {
TestFactory testFactory = TestFactory.getInstance();
if (call.isMethod()) {
testFactory.addMethod(test, (GenericMethod) call, test.size(), 0);
} else if (call.isConstructor()) {
testFactory.addConstructor(test, (GenericConstructor) call, test.size(), 0);
} else {
assert (false) : "Found test call that is neither method nor constructor";
}
} catch (ConstructionFailedException e) {
}
num++;
}
if (logger.isDebugEnabled())
logger.debug("Randomized test case:" + test.toCode());
if (tracerEnabled)
ExecutionTracer.enable();
return test;
}
Aggregations