Search in sources :

Example 16 with Statement

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

the class VariableReferenceImpl method getStPosition.

/**
 * {@inheritDoc}
 *
 * The position of the statement, defining this VariableReference, in the
 * testcase.
 *
 * TODO: Notify change listener also when return value changes
 */
@Override
public synchronized int getStPosition() {
    if (stPosition == null || changeListener.hasChanged()) {
        stPosition = null;
        for (int i = 0; i < testCase.size(); i++) {
            Statement stmt = testCase.getStatement(i);
            if (stmt.getReturnValue().equals(this)) {
                stPosition = i;
                break;
            }
        }
        if (stPosition == null) {
            String msg = "Bloody annoying bug \n";
            msg += "Test case has " + testCase.size() + " function calls \n";
            for (int i = 0; i < testCase.size(); i++) {
                msg += testCase.getStatement(i).getCode(null) + "\n";
            }
            msg += "failed to find type " + this.type.getTypeName() + "\n";
            throw new AssertionError(msg + "A VariableReferences position is only defined if the VariableReference is defined by a statement in the testCase");
        }
    } else {
        // somehow this could be null, leading to NPE. Synchronization issue?
        int position = stPosition;
        stPosition = null;
        stPosition = getStPosition();
        assert (stPosition == position);
    }
    return stPosition;
}
Also used : Statement(org.evosuite.testcase.statements.Statement)

Example 17 with Statement

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

the class JUnitTestCarvedChromosomeFactorySystemTest method testInnerConstructor.

@Test
public void testInnerConstructor() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.InnerConstructorTest.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.InnerConstructor.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertTrue(factory.hasCarvedTestCases());
    TestChromosome carved = factory.getChromosome();
    Assert.assertNotNull(carved);
    String code = carved.toString();
    Assert.assertNotNull(code);
    Assert.assertEquals(code, 2, carved.test.size());
    for (int i = 0; i < carved.test.size(); i++) {
        Statement stmt = carved.test.getStatement(i);
        boolean valid = stmt.isValid();
        Assert.assertTrue("Invalid stmt at position " + i, valid);
    }
    System.out.println(code);
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Statement(org.evosuite.testcase.statements.Statement) Test(org.junit.Test)

Example 18 with Statement

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

the class ComparisonTraceObserver method visit.

/* (non-Javadoc)
	 * @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
	 */
/**
 * {@inheritDoc}
 */
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
    try {
        Object object = var.getObject(scope);
        if (object == null)
            return;
        if (statement instanceof AssignmentStatement)
            return;
        if (statement instanceof PrimitiveStatement<?>)
            return;
        ComparisonTraceEntry entry = new ComparisonTraceEntry(var);
        int position = statement.getPosition();
        for (VariableReference other : scope.getElements(var.getType())) {
            Object otherObject = other.getObject(scope);
            // TODO: Create a matrix of object comparisons?
            if (otherObject == null)
                // TODO: Don't do this?
                continue;
            if (object == otherObject)
                // Don't compare with self?
                continue;
            int otherPos = other.getStPosition();
            if (otherPos >= position)
                // Don't compare with variables that are not defined - may happen with primitives?
                continue;
            Statement otherStatement = currentTest.getStatement(otherPos);
            if (statement instanceof PrimitiveStatement && otherStatement instanceof PrimitiveStatement)
                // Don't compare two primitives
                continue;
            if (otherStatement instanceof MethodStatement) {
                if (((MethodStatement) otherStatement).getMethodName().equals("hashCode"))
                    // No comparison against hashCode, as the hashCode return value will not be in the test
                    continue;
            }
            if (Properties.PURE_EQUALS) {
                String className = object.getClass().getCanonicalName();
                String methodName = "equals";
                String descriptor = Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class));
                CheapPurityAnalyzer cheapPurityAnalyzer = CheapPurityAnalyzer.getInstance();
                if (!cheapPurityAnalyzer.isPure(className, methodName, descriptor))
                    // Don't compare using impure equals(Object) methods
                    continue;
            }
            try {
                logger.debug("Comparison of " + var + " with " + other + " is: " + object.equals(otherObject));
                entry.addEntry(other, ComparisonTraceEntry.equals(object, otherObject));
            } catch (Throwable t) {
                logger.debug("Exception during equals: " + t);
            // ignore?
            }
            if (object instanceof Comparable<?>) {
            // TODO
            }
        }
        trace.addEntry(statement.getPosition(), var, entry);
    } catch (CodeUnderTestException e) {
        logger.debug("", e);
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement)

Example 19 with Statement

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

the class ContainsTraceObserver method visit.

/* (non-Javadoc)
	 * @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
	 */
/**
 * {@inheritDoc}
 */
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
    try {
        Object object = var.getObject(scope);
        if (object == null)
            return;
        if (statement instanceof AssignmentStatement)
            return;
        if (statement instanceof PrimitiveStatement<?>)
            return;
        // Only relevant for Collections
        if (!(object instanceof Collection))
            return;
        Collection collectionObject = (Collection) object;
        List<GenericClass> parameterClasses = var.getGenericClass().getParameterClasses();
        // Need to know exact type
        if (parameterClasses.size() != 1)
            return;
        java.lang.reflect.Type parameterType = parameterClasses.get(0).getType();
        ContainsTraceEntry entry = new ContainsTraceEntry(var);
        int position = statement.getPosition();
        Set<VariableReference> otherVariables = new LinkedHashSet<>();
        otherVariables.addAll(scope.getElements(parameterType));
        for (int i = 0; i <= statement.getPosition(); i++) {
            for (VariableReference candidateVar : currentTest.getStatement(i).getVariableReferences()) {
                if (candidateVar instanceof ConstantValue && candidateVar.isAssignableTo(parameterType)) {
                    otherVariables.add(candidateVar);
                }
            }
        }
        for (VariableReference other : otherVariables) {
            Object otherObject;
            if (other instanceof ConstantValue)
                otherObject = ((ConstantValue) other).getValue();
            else
                otherObject = other.getObject(scope);
            if (otherObject == null)
                // TODO: Don't do this?
                continue;
            int otherPos = other.getStPosition();
            if (otherPos > position)
                // Don't compare with variables that are not defined - may happen with primitives?
                continue;
            Statement otherStatement = currentTest.getStatement(otherPos);
            if (otherStatement instanceof MethodStatement) {
                if (((MethodStatement) otherStatement).getMethodName().equals("hashCode"))
                    // No comparison against hashCode, as the hashCode return value will not be in the test
                    continue;
            }
            try {
                logger.debug("Checking whether {} contains {} is: {}", var, other, collectionObject.contains(otherObject));
                entry.addEntry(other, collectionObject.contains(otherObject));
            } catch (Throwable t) {
                logger.debug("Exception during equals: " + t);
            // ignore?
            }
            if (object instanceof Comparable<?>) {
            // TODO
            }
        }
        trace.addEntry(statement.getPosition(), var, entry);
    } catch (CodeUnderTestException e) {
        logger.debug("", e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericClass(org.evosuite.utils.generic.GenericClass) Collection(java.util.Collection) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 20 with Statement

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

the class MethodCoverageSuiteFitness method handleConstructorExceptions.

/**
 * If there is an exception in a super-constructor, then the corresponding
 * constructor might not be included in the execution trace
 *
 * @param suite
 * @param results
 * @param calledMethods
 */
protected void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Set<String> calledMethods) {
    if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions())
        return;
    Integer exceptionPosition = result.getFirstPositionOfThrownException();
    Statement 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 (methodCoverageMap.containsKey(name) && !calledMethods.contains(name)) {
            TestFitnessFunction goal = methodCoverageMap.get(name);
            // only include methods being called
            test.getTestCase().addCoveredGoal(goal);
            calledMethods.add(name);
            this.toRemoveMethods.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)

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