Search in sources :

Example 21 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class AbstractMOSA method hasMethodCall.

/**
 * This method checks whether the test has only primitive type statements. Indeed,
 * crossover and mutation can lead to tests with no method calls (methods or constructors call),
 * thus, when executed they will never cover something in the class under test.
 *
 * @param test to check
 * @return true if the test has at least one method or constructor call (i.e., the test may
 * cover something when executed; false otherwise
 */
private boolean hasMethodCall(T test) {
    boolean flag = false;
    TestCase tc = ((TestChromosome) test).getTestCase();
    for (Statement s : tc) {
        if (s instanceof MethodStatement) {
            MethodStatement ms = (MethodStatement) s;
            boolean isTargetMethod = ms.getDeclaringClassName().equals(Properties.TARGET_CLASS);
            if (isTargetMethod)
                return true;
        }
        if (s instanceof ConstructorStatement) {
            ConstructorStatement ms = (ConstructorStatement) s;
            boolean isTargetMethod = ms.getDeclaringClassName().equals(Properties.TARGET_CLASS);
            if (isTargetMethod)
                return true;
        }
    }
    return flag;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) TestCase(org.evosuite.testcase.TestCase) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 22 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class MethodCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate fitness
 *
 * @param individual
 *            a {@link org.evosuite.testcase.ExecutableChromosome} object.
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    double fitness = 1.0;
    List<Integer> exceptionPositions = asSortedList(result.getPositionsWhereExceptionsWereThrown());
    for (Statement stmt : result.test) {
        if (!isValidPosition(exceptionPositions, stmt.getPosition())) {
            break;
        }
        if ((stmt instanceof MethodStatement || stmt instanceof ConstructorStatement)) {
            EntityWithParametersStatement ps = (EntityWithParametersStatement) stmt;
            String className = ps.getDeclaringClassName();
            String methodDesc = ps.getDescriptor();
            String methodName = ps.getMethodName() + methodDesc;
            if (this.className.equals(className) && this.methodName.equals(methodName)) {
                fitness = 0.0;
                break;
            }
        }
    }
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement)

Example 23 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class MethodNoExceptionCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate fitness
 *
 * @param individual
 *            a {@link org.evosuite.testcase.ExecutableChromosome} object.
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    double fitness = 1.0;
    List<Integer> exceptionPositions = new ArrayList<Integer>();
    if (Properties.BREAK_ON_EXCEPTION) {
        // we consider only the first thrown exception
        if (!result.getPositionsWhereExceptionsWereThrown().isEmpty()) {
            int firstPosition = Collections.min(result.getPositionsWhereExceptionsWereThrown());
            exceptionPositions.add(firstPosition);
        }
    } else {
        // we consider all thrown exceptions (if any)
        exceptionPositions.addAll(result.getPositionsWhereExceptionsWereThrown());
    }
    for (Statement stmt : result.test) {
        if (exceptionPositions.contains(stmt.getPosition())) {
            if (Properties.BREAK_ON_EXCEPTION)
                // if we look at the first exception, then no need to iterate over the remaining statements
                break;
            else
                // otherwise we simple skip statements throwing an exception
                continue;
        }
        if ((stmt instanceof MethodStatement || stmt instanceof ConstructorStatement)) {
            EntityWithParametersStatement ps = (EntityWithParametersStatement) stmt;
            String className = ps.getDeclaringClassName();
            String methodDesc = ps.getDescriptor();
            String methodName = ps.getMethodName() + methodDesc;
            if (this.className.equals(className) && this.methodName.equals(methodName)) {
                fitness = 0.0;
                break;
            }
        }
    }
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ArrayList(java.util.ArrayList)

Example 24 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class ParameterLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    Statement stmt = test.getTestCase().getStatement(statement);
    backup(test, stmt);
    if (stmt instanceof MethodStatement) {
        return doSearch(test, (MethodStatement) stmt, objective);
    } else if (stmt instanceof ConstructorStatement) {
        return doSearch(test, (ConstructorStatement) stmt, objective);
    } else if (stmt instanceof FieldStatement) {
        return doSearch(test, (FieldStatement) stmt, objective);
    } else {
        return false;
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement)

Example 25 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class LocalSearchArraySystemTest method getArrayTest.

private TestCase getArrayTest(int length) 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 arrayVar = test.addStatement(new ArrayStatement(test, int[].class, length));
    for (int i = 0; i < length; i++) {
        // Add value
        VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, 0));
        test.addStatement(new AssignmentStatement(test, new ArrayIndex(test, (ArrayReference) arrayVar, i), intVar));
    }
    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[] { arrayVar }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) GenericClass(org.evosuite.utils.generic.GenericClass) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestFactory(org.evosuite.testcase.TestFactory) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement)

Aggregations

MethodStatement (org.evosuite.testcase.statements.MethodStatement)54 VariableReference (org.evosuite.testcase.variable.VariableReference)40 GenericMethod (org.evosuite.utils.generic.GenericMethod)29 Method (java.lang.reflect.Method)25 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)22 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)22 Statement (org.evosuite.testcase.statements.Statement)19 GenericClass (org.evosuite.utils.generic.GenericClass)15 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)11 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 TestCase (org.evosuite.testcase.TestCase)7 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)5 FieldStatement (org.evosuite.testcase.statements.FieldStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 HashSet (java.util.HashSet)4 TestFactory (org.evosuite.testcase.TestFactory)4 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)4 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)4