Search in sources :

Example 41 with ConstructionFailedException

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;
}
Also used : PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) ArrayList(java.util.ArrayList) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) GenericMethod(org.evosuite.utils.generic.GenericMethod) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) GenericField(org.evosuite.utils.generic.GenericField) CaptureType(com.googlecode.gentyref.CaptureType)

Example 42 with ConstructionFailedException

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;
}
Also used : NullStatement(org.evosuite.testcase.statements.NullStatement) TestCase(org.evosuite.testcase.TestCase) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) FitnessFunction(org.evosuite.ga.FitnessFunction) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 43 with ConstructionFailedException

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;
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFactory(org.evosuite.testcase.TestFactory) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Aggregations

ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)43 GenericClass (org.evosuite.utils.generic.GenericClass)16 PrivateFieldStatement (org.evosuite.testcase.statements.reflection.PrivateFieldStatement)14 PrivateMethodStatement (org.evosuite.testcase.statements.reflection.PrivateMethodStatement)14 CaptureType (com.googlecode.gentyref.CaptureType)11 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)11 GenericMethod (org.evosuite.utils.generic.GenericMethod)10 ArrayList (java.util.ArrayList)8 GenericField (org.evosuite.utils.generic.GenericField)7 TestFactory (org.evosuite.testcase.TestFactory)6 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)6 TestCase (org.evosuite.testcase.TestCase)4 VariableReference (org.evosuite.testcase.variable.VariableReference)4 Type (java.lang.reflect.Type)3 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 TypeVariable (java.lang.reflect.TypeVariable)2 WildcardType (java.lang.reflect.WildcardType)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2