Search in sources :

Example 41 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ConstraintVerifier method checkFunctionalMockUsage.

/**
 * No method should be called on the return value of a mock creation.
 * This is because a functional mock object should only be used as an input value.
 * Calling any method on it would make no sense
 *
 * @param st
 */
private static boolean checkFunctionalMockUsage(Statement st, TestCase tc) {
    if (!(st instanceof MethodStatement)) {
        return true;
    }
    MethodStatement ms = (MethodStatement) st;
    VariableReference callee = ms.getCallee();
    if (callee == null) {
        // ie, static method
        return true;
    }
    Statement source = tc.getStatement(callee.getStPosition());
    if (source instanceof FunctionalMockStatement) {
        logger.error("Mock object created at position " + source.getPosition() + " has a method called in position " + st.getPosition());
        return false;
    }
    return true;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference)

Example 42 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ConstraintVerifier method isValidPositionForInsertion.

public static boolean isValidPositionForInsertion(GenericAccessibleObject<?> obj, TestCase tc, int pos) throws IllegalArgumentException {
    Inputs.checkNull(obj, tc);
    /*
            if the given 'obj' (a method/constructor) belongs to a class for which there is an instance
            before "pos" which is bounded after "pos", then we cannot add it, as could break bounding
            constraints if such instance is chosen as callee for "obj". Note: we could force to never
            use such instance (ie use another one if exists, or create it), but that would complicate
            a lot all the algorithms in the test factory :(
         */
    List<VariableReference> possibleCallees = tc.getObjects(obj.getOwnerType(), pos);
    for (VariableReference ref : possibleCallees) {
        int boundPos = ConstraintHelper.getLastPositionOfBounded(ref, tc);
        if (boundPos >= pos) {
            return false;
        }
    }
    Constraints constraints = obj.getAccessibleObject().getAnnotation(Constraints.class);
    if (constraints == null) {
        return true;
    }
    if (!canBeInsertedRegardlessOfPosition(obj, tc)) {
        return false;
    }
    int minPos = getMinPosForAfter(obj, tc, tc.size());
    if (minPos < 0 || pos < minPos) {
        return false;
    }
    return true;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) Constraint(org.evosuite.symbolic.expr.Constraint)

Example 43 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ExecutionObserver method getDependentVariables.

/**
 * Determine the set of variables that somehow lead to this statement
 *
 * @param statement
 *            a {@link org.evosuite.testcase.statements.Statement} object.
 * @return a {@link java.util.Set} object.
 */
protected Set<VariableReference> getDependentVariables(Statement statement) {
    Set<VariableReference> dependencies = new HashSet<VariableReference>();
    for (VariableReference var : statement.getVariableReferences()) {
        dependencies.add(var);
        dependencies.addAll(currentTest.getDependencies(var));
    }
    return dependencies;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) HashSet(java.util.HashSet)

Example 44 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestCoverageGoalNameGeneration method testMultipleMethods.

@Test
public void testMultipleMethods() throws NoSuchMethodException {
    TestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(Object.class.getConstructor(), Object.class);
    VariableReference callee = test.addStatement(new ConstructorStatement(test, gc, new ArrayList<VariableReference>()));
    GenericMethod gm = new GenericMethod(Object.class.getMethod("toString"), Object.class);
    test.addStatement(new MethodStatement(test, gm, callee, new ArrayList<VariableReference>()));
    MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString()Ljava/lang/String;");
    test.addCoveredGoal(goal1);
    MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "foo()Ljava/lang/String;");
    test.addCoveredGoal(goal2);
    MethodCoverageTestFitness goal3 = new MethodCoverageTestFitness("FooClass", "bar()Ljava/lang/String;");
    test.addCoveredGoal(goal3);
    List<TestCase> tests = new ArrayList<>();
    tests.add(test);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
    String generatedName = naming.getName(test);
    assertEquals("testToString", generatedName);
// TODO: What should be the name now? Need some heuristic, currently sorted alphabetically
// Better heuristic would consider other things, like e.g. which method has more goals covered
// or which one is the last one called?
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) Test(org.junit.Test)

Example 45 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestLocalSearchMIMEType method createTestCase0.

private DefaultTestCase createTestCase0() throws NoSuchFieldException, SecurityException, NoSuchMethodException, ClassNotFoundException {
    TestCaseBuilder builder = new TestCaseBuilder();
    final Class<?> mimeTypeClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(MIMEType.class.getName());
    final Field memField = mimeTypeClass.getField("MEM");
    final Method toString = mimeTypeClass.getMethod("toString");
    VariableReference mIMEType0 = builder.appendStaticFieldStmt(memField);
    builder.appendMethod(mIMEType0, toString);
    System.out.println("Test Case #0=" + builder.toCode());
    return builder.getDefaultTestCase();
}
Also used : Field(java.lang.reflect.Field) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) MIMEType(com.examples.with.different.packagename.concolic.MIMEType) VariableReference(org.evosuite.testcase.variable.VariableReference) Method(java.lang.reflect.Method)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14