Search in sources :

Example 6 with ConstantValue

use of org.evosuite.testcase.variable.ConstantValue 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 7 with ConstantValue

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

the class InputObserver method afterStatement.

/* (non-Javadoc)
     * @see org.evosuite.testcase.ExecutionObserver#afterStatement(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
     */
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    if (statement instanceof EntityWithParametersStatement) {
        EntityWithParametersStatement parameterisedStatement = (EntityWithParametersStatement) statement;
        List<VariableReference> parRefs = parameterisedStatement.getParameterReferences();
        List<Object> argObjects = new ArrayList<>(parRefs.size());
        for (VariableReference parRef : parRefs) {
            Object parObject = null;
            try {
                if (parRef instanceof ArrayIndex || parRef instanceof FieldReference) {
                    parObject = parRef.getObject(scope);
                } else if (parRef instanceof ConstantValue) {
                    parObject = ((ConstantValue) parRef).getValue();
                } else {
                    parObject = parRef.getObject(scope);
                }
            } catch (CodeUnderTestException e) {
                e.printStackTrace();
            }
            argObjects.add(parObject);
        }
        assert parRefs.size() == argObjects.size();
        String className = parameterisedStatement.getDeclaringClassName();
        String methodDesc = parameterisedStatement.getDescriptor();
        String methodName = parameterisedStatement.getMethodName();
        inputCoverage.put(statement.getPosition(), InputCoverageGoal.createCoveredGoalsFromParameters(className, methodName, methodDesc, argObjects));
    // argumentsValues.put((EntityWithParametersStatement) statement, argObjects);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ConstantValue(org.evosuite.testcase.variable.ConstantValue) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 8 with ConstantValue

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

the class PrivateMethodStatement method getReflectionParams.

private static List<VariableReference> getReflectionParams(TestCase tc, Class<?> klass, Method method, VariableReference callee, List<VariableReference> inputs) {
    List<VariableReference> list = new ArrayList<>(3 + inputs.size() * 2);
    list.add(new ConstantValue(tc, new GenericClass(Class.class), klass));
    list.add(callee);
    list.add(new ConstantValue(tc, new GenericClass(String.class), method.getName()));
    Class<?>[] parameterTypes = method.getParameterTypes();
    assert (parameterTypes.length == inputs.size());
    for (int parameterNum = 0; parameterNum < parameterTypes.length; parameterNum++) {
        VariableReference vr = inputs.get(parameterNum);
        list.add(vr);
        list.add(new ConstantValue(tc, new GenericClass(Class.class), parameterTypes[parameterNum]));
    }
    return list;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) ArrayList(java.util.ArrayList) GenericClass(org.evosuite.utils.generic.GenericClass) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 9 with ConstantValue

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

the class EqualsNullContract method addAssertionAndComments.

@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
    EqualsAssertion assertion = new EqualsAssertion();
    assertion.setStatement(statement);
    VariableReference var = variables.get(0);
    assertion.setSource(var);
    assertion.setDest(new ConstantValue(statement.getTestCase(), Object.class));
    // assertion.setDest(new NullReference(statement.getTestCase(), var.getType()));
    assertion.setValue(false);
    statement.addAssertion(assertion);
    statement.addComment("Violates contract equals(null)");
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) ConstantValue(org.evosuite.testcase.variable.ConstantValue) EqualsAssertion(org.evosuite.assertion.EqualsAssertion)

Example 10 with ConstantValue

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

the class TestOverloading method testNotOverloadedMethod.

@Test
public void testNotOverloadedMethod() {
    Class<?> clazz = ClassWithoutOverloadedMethods.class;
    Method method1 = clazz.getMethods()[0];
    Method method2 = clazz.getMethods()[1];
    GenericMethod genericMethod1 = new GenericMethod(method1, clazz);
    GenericMethod genericMethod2 = new GenericMethod(method2, clazz);
    TestCase test = new DefaultTestCase();
    ConstantValue intValue = new ConstantValue(test, int.class);
    VariableReference stringVar = new VariableReferenceImpl(test, String.class);
    List<VariableReference> parameters1 = Arrays.asList(intValue);
    List<VariableReference> parameters2 = Arrays.asList(stringVar);
    assertFalse(genericMethod1.isOverloaded());
    assertFalse(genericMethod2.isOverloaded());
    assertFalse(genericMethod1.isOverloaded(parameters1));
    assertFalse(genericMethod2.isOverloaded(parameters1));
    assertFalse(genericMethod1.isOverloaded(parameters2));
    assertFalse(genericMethod2.isOverloaded(parameters2));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) Method(java.lang.reflect.Method) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) ConstantValue(org.evosuite.testcase.variable.ConstantValue) ClassWithoutOverloadedMethods(com.examples.with.different.packagename.utils.generic.ClassWithoutOverloadedMethods) Test(org.junit.Test)

Aggregations

ConstantValue (org.evosuite.testcase.variable.ConstantValue)12 VariableReference (org.evosuite.testcase.variable.VariableReference)12 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)5 TestCase (org.evosuite.testcase.TestCase)5 Test (org.junit.Test)5 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)4 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)3 GenericClass (org.evosuite.utils.generic.GenericClass)3 ClassWithOverloadedMethods (com.examples.with.different.packagename.utils.generic.ClassWithOverloadedMethods)2 ClassWithoutOverloadedMethods (com.examples.with.different.packagename.utils.generic.ClassWithoutOverloadedMethods)2 Method (java.lang.reflect.Method)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 BigFraction (com.examples.with.different.packagename.utils.generic.BigFraction)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 EqualsAssertion (org.evosuite.assertion.EqualsAssertion)1 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)1 EntityWithParametersStatement (org.evosuite.testcase.statements.EntityWithParametersStatement)1 FieldStatement (org.evosuite.testcase.statements.FieldStatement)1