All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.assertion.ContainsTraceObserver Maven / Gradle / Ivy

The newest version!
package org.evosuite.assertion;

import org.evosuite.testcase.execution.CodeUnderTestException;
import org.evosuite.testcase.execution.ExecutionResult;
import org.evosuite.testcase.execution.Scope;
import org.evosuite.testcase.statements.AssignmentStatement;
import org.evosuite.testcase.statements.MethodStatement;
import org.evosuite.testcase.statements.PrimitiveStatement;
import org.evosuite.testcase.statements.Statement;
import org.evosuite.testcase.variable.ConstantValue;
import org.evosuite.testcase.variable.VariableReference;
import org.evosuite.utils.generic.GenericClass;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ContainsTraceObserver extends AssertionTraceObserver {

    /* (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 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 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)
                    continue; // TODO: Don't do this?

                int otherPos = other.getStPosition();
                if(otherPos > position)
                    continue; // Don't compare with variables that are not defined - may happen with primitives?

                Statement otherStatement = currentTest.getStatement(otherPos);

                if(otherStatement instanceof MethodStatement) {
                    if(((MethodStatement)otherStatement).getMethodName().equals("hashCode"))
                        continue; // No comparison against hashCode, as the hashCode return value will not be in the test
                }

                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);
        }

    }

    @Override
    public void testExecutionFinished(ExecutionResult r, Scope s) {
        // do nothing
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy