Search in sources :

Example 31 with Statement

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

the class CoverageGoalTestNameGenerationStrategy method chooseRepresentativeGoal.

/**
 * Out of a set of multiple goals, select one that is representative.
 * Assumes that goals is not empty, and all items in goals have the same type
 * @param test
 * @param goals
 * @return
 */
private TestFitnessFunction chooseRepresentativeGoal(TestCase test, Collection<TestFitnessFunction> goals) {
    Map<String, Integer> methodToPosition = new LinkedHashMap<>();
    for (Statement st : test) {
        if (st instanceof MethodStatement) {
            MethodStatement ms = (MethodStatement) st;
            String name = ms.getMethodName() + ms.getDescriptor();
            methodToPosition.put(name, st.getPosition());
        } else if (st instanceof ConstructorStatement) {
            ConstructorStatement cs = (ConstructorStatement) st;
            String name = "<init>" + cs.getDescriptor();
            methodToPosition.put(name, st.getPosition());
        }
    }
    // Randomness.choice(goals);
    TestFitnessFunction chosenGoal = goals.iterator().next();
    int chosenPosition = -1;
    for (TestFitnessFunction goal : goals) {
        if (methodToPosition.containsKey(goal.getTargetMethod())) {
            int position = methodToPosition.get(goal.getTargetMethod());
            if (position >= chosenPosition) {
                chosenPosition = position;
                chosenGoal = goal;
            }
        }
    }
    return chosenGoal;
}
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) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 32 with Statement

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

the class BranchCoverageSuiteFitness method handleConstructorExceptions.

/**
 * If there is an exception in a superconstructor, then the corresponding
 * constructor might not be included in the execution trace
 *
 * @param results
 * @param callCount
 */
private void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Map<String, Integer> callCount) {
    if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions()) {
        return;
    }
    Integer exceptionPosition = result.getFirstPositionOfThrownException();
    // TODO: Not sure why that can happen
    if (exceptionPosition >= result.test.size()) {
        return;
    }
    Statement statement = null;
    if (result.test.hasStatement(exceptionPosition)) {
        statement = result.test.getStatement(exceptionPosition);
    }
    if (statement instanceof ConstructorStatement) {
        ConstructorStatement c = (ConstructorStatement) statement;
        String className = c.getConstructor().getName();
        String methodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
        String name = className + "." + methodName;
        if (!callCount.containsKey(name)) {
            callCount.put(name, 1);
            if (branchlessMethodCoverageMap.containsKey(name)) {
                TestFitnessFunction goal = branchlessMethodCoverageMap.get(name);
                test.getTestCase().addCoveredGoal(goal);
                toRemoveRootBranches.add(name);
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
                }
            }
        }
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 33 with Statement

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

the class HashCodeReturnsNormallyContract method addAssertionAndComments.

@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
    TestCase test = statement.getTestCase();
    int position = statement.getPosition();
    VariableReference a = variables.get(0);
    try {
        Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
        GenericMethod method = new GenericMethod(hashCodeMethod, a.getGenericClass());
        Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] {}));
        test.addStatement(st1, position + 1);
        st1.addComment("Throws exception: " + exception.getMessage());
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 34 with Statement

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

the class JUnitTheoryContract method addAssertionAndComments.

@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
    TestCase test = statement.getTestCase();
    int position = statement.getPosition();
    VariableReference a = variables.get(0);
    int pos = a.getStPosition();
    try {
        Constructor<?> defaultConstructor = theoryReceiver.getClass().getConstructor();
        GenericConstructor constructor = new GenericConstructor(defaultConstructor, theoryReceiver.getClass());
        Statement st1 = new ConstructorStatement(test, constructor, new ArrayList<VariableReference>());
        VariableReference receiver = test.addStatement(st1, position + 1);
        Statement st2 = new MethodStatement(test, theoryMethod, receiver, Arrays.asList(new VariableReference[] { test.getStatement(pos).getReturnValue() }));
        test.addStatement(st2, position + 2);
        st2.addComment("Violates theory: " + theoryMethod.getName());
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        logger.warn("Error while creating contract violation: " + e);
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        logger.warn("Error while creating contract violation: " + e);
        e.printStackTrace();
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) GenericConstructor(org.evosuite.utils.generic.GenericConstructor)

Example 35 with Statement

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

the class EqualsHashcodeContract method addAssertionAndComments.

@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
    TestCase test = statement.getTestCase();
    VariableReference a = variables.get(0);
    VariableReference b = variables.get(1);
    try {
        Method equalsMethod = a.getGenericClass().getRawClass().getMethod("equals", new Class<?>[] { Object.class });
        Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
        GenericMethod genericEqualsMethod = new GenericMethod(equalsMethod, a.getGenericClass());
        GenericMethod genericHashCodeMethod = new GenericMethod(hashCodeMethod, a.getGenericClass());
        // Create x = a.equals(b)
        Statement st1 = new MethodStatement(test, genericEqualsMethod, a, Arrays.asList(new VariableReference[] { b }));
        VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
        // Create y = a.hashCode();
        Statement st2 = new MethodStatement(test, genericHashCodeMethod, a, Arrays.asList(new VariableReference[] {}));
        VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
        // Create z = b.hashCode();
        Statement st3 = new MethodStatement(test, genericHashCodeMethod, b, Arrays.asList(new VariableReference[] {}));
        VariableReference z = test.addStatement(st3, statement.getPosition() + 3);
        // Create w = z == z
        VariableReference w = new VariableReferenceImpl(test, boolean.class);
        PrimitiveExpression exp = new PrimitiveExpression(test, w, y, Operator.EQUALS, z);
        w = test.addStatement(exp, statement.getPosition() + 4);
        Statement newStatement = test.getStatement(w.getStPosition());
        // Create assertEquals(x, w)
        EqualsAssertion assertion = new EqualsAssertion();
        assertion.setStatement(newStatement);
        assertion.setSource(x);
        assertion.setDest(w);
        assertion.setValue(true);
        newStatement.addAssertion(assertion);
        newStatement.addComment("Violates contract equals - hashcode");
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) EqualsAssertion(org.evosuite.assertion.EqualsAssertion)

Aggregations

Statement (org.evosuite.testcase.statements.Statement)48 MethodStatement (org.evosuite.testcase.statements.MethodStatement)25 VariableReference (org.evosuite.testcase.variable.VariableReference)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)16 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)11 TestCase (org.evosuite.testcase.TestCase)8 JUnitTestCarvedChromosomeFactory (org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 FieldStatement (org.evosuite.testcase.statements.FieldStatement)7 Method (java.lang.reflect.Method)6 TestChromosome (org.evosuite.testcase.TestChromosome)6 GenericMethod (org.evosuite.utils.generic.GenericMethod)6 FunctionalMockStatement (org.evosuite.testcase.statements.FunctionalMockStatement)5 NullStatement (org.evosuite.testcase.statements.NullStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)4 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)4 TestFactory (org.evosuite.testcase.TestFactory)3 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)3