Search in sources :

Example 1 with ConstantValue

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

the class ConstantInliner method afterStatement.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.testcase.ExecutionObserver#statement(org.evosuite.testcase.
	 * StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
	 */
/**
 * {@inheritDoc}
 */
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    try {
        for (VariableReference var : statement.getVariableReferences()) {
            if (var.equals(statement.getReturnValue()) || var.equals(statement.getReturnValue().getAdditionalVariableReference()))
                continue;
            Object object = var.getObject(scope);
            if (var.isPrimitive()) {
                ConstantValue value = new ConstantValue(test, var.getGenericClass());
                value.setValue(object);
                // logger.info("Statement before inlining: " +
                // statement.getCode());
                statement.replace(var, value);
            // logger.info("Statement after inlining: " +
            // statement.getCode());
            } else if (var.isString() && object != null) {
                ConstantValue value = new ConstantValue(test, var.getGenericClass());
                try {
                    String val = StringEscapeUtils.unescapeJava(new String(object.toString()));
                    if (val.length() < Properties.MAX_STRING) {
                        value.setValue(val);
                        statement.replace(var, value);
                    }
                } catch (IllegalArgumentException e) {
                    // Exceptions may happen if strings are not valid
                    // unicode
                    logger.info("Cannot escape invalid string: " + object);
                }
            // logger.info("Statement after inlining: " +
            // statement.getCode());
            } else if (var.isArrayIndex()) {
                // then replace the array index with that object
                for (VariableReference otherVar : scope.getElements(var.getType())) {
                    Object otherObject = otherVar.getObject(scope);
                    if (otherObject == object && !otherVar.isArrayIndex() && otherVar.getStPosition() < statement.getPosition()) {
                        statement.replace(var, otherVar);
                        break;
                    }
                }
            } else {
                // the assertion for now
                if (object == null) {
                    if (statement instanceof MethodStatement) {
                        MethodStatement ms = (MethodStatement) statement;
                        if (var.equals(ms.getCallee())) {
                            // Don't put null in callee's, the compiler will not accept it
                            continue;
                        }
                    } else if (statement instanceof FieldStatement) {
                        FieldStatement fs = (FieldStatement) statement;
                        if (var.equals(fs.getSource())) {
                            // Don't put null in source, the compiler will not accept it
                            continue;
                        }
                    }
                    ConstantValue value = new ConstantValue(test, var.getGenericClass());
                    value.setValue(null);
                    // logger.info("Statement before inlining: " +
                    // statement.getCode());
                    statement.replace(var, value);
                // logger.info("Statement after inlining: " +
                // statement.getCode());
                }
            }
        }
    } catch (CodeUnderTestException e) {
        logger.warn("Not inlining test: " + e.getCause());
    // throw new AssertionError("This case isn't handled yet: " +
    // e.getCause()
    // + ", " + Arrays.asList(e.getStackTrace()));
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) FieldStatement(org.evosuite.testcase.statements.FieldStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 2 with ConstantValue

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

the class TestOverloading method testOverloadedConstructor.

@Test
public void testOverloadedConstructor() {
    Class<?> clazz = ClassWithOverloadedMethods.class;
    Constructor<?> constructor1 = clazz.getConstructors()[0];
    Constructor<?> constructor2 = clazz.getConstructors()[1];
    GenericConstructor genericConstructor1 = new GenericConstructor(constructor1, clazz);
    GenericConstructor genericConstructor2 = new GenericConstructor(constructor2, clazz);
    TestCase test = new DefaultTestCase();
    ConstantValue intValue = new ConstantValue(test, int.class);
    VariableReference integerVar = new VariableReferenceImpl(test, Integer.class);
    List<VariableReference> parameters = Arrays.asList(intValue, integerVar);
    assertTrue(genericConstructor1.isOverloaded(parameters));
    assertTrue(genericConstructor2.isOverloaded(parameters));
}
Also used : ClassWithOverloadedMethods(com.examples.with.different.packagename.utils.generic.ClassWithOverloadedMethods) VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) ConstantValue(org.evosuite.testcase.variable.ConstantValue) Test(org.junit.Test)

Example 3 with ConstantValue

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

the class TestOverloading method testOverloadedMethod.

@Test
public void testOverloadedMethod() {
    Class<?> clazz = ClassWithOverloadedMethods.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 integerVar = new VariableReferenceImpl(test, Integer.class);
    List<VariableReference> parameters1 = Arrays.asList(intValue);
    List<VariableReference> parameters2 = Arrays.asList(integerVar);
    assertTrue(genericMethod1.isOverloaded());
    assertTrue(genericMethod2.isOverloaded());
    assertTrue(genericMethod1.isOverloaded(parameters1));
    assertTrue(genericMethod2.isOverloaded(parameters1));
    assertTrue(genericMethod1.isOverloaded(parameters2));
    assertTrue(genericMethod2.isOverloaded(parameters2));
}
Also used : ClassWithOverloadedMethods(com.examples.with.different.packagename.utils.generic.ClassWithOverloadedMethods) 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) Test(org.junit.Test)

Example 4 with ConstantValue

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

the class TestOverloading method testNotOverloadedConstructor.

@Test
public void testNotOverloadedConstructor() {
    Class<?> clazz = ClassWithoutOverloadedMethods.class;
    Constructor<?> constructor1 = clazz.getConstructors()[0];
    Constructor<?> constructor2 = clazz.getConstructors()[1];
    GenericConstructor genericConstructor1 = new GenericConstructor(constructor1, clazz);
    GenericConstructor genericConstructor2 = new GenericConstructor(constructor2, 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(genericConstructor1.isOverloaded(parameters1));
    assertFalse(genericConstructor2.isOverloaded(parameters2));
    assertFalse(genericConstructor1.isOverloaded(parameters2));
    assertFalse(genericConstructor2.isOverloaded(parameters1));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) ConstantValue(org.evosuite.testcase.variable.ConstantValue) ClassWithoutOverloadedMethods(com.examples.with.different.packagename.utils.generic.ClassWithoutOverloadedMethods) Test(org.junit.Test)

Example 5 with ConstantValue

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

the class FunctionalMockStatement method fillWithNullRefs.

public void fillWithNullRefs() {
    for (int i = 0; i < parameters.size(); i++) {
        VariableReference ref = parameters.get(i);
        if (ref == null) {
            Class<?> expected = getExpectedParameterType(i);
            Object value = null;
            if (expected.isPrimitive()) {
                // can't fill a primitive with null
                if (expected.equals(Integer.TYPE)) {
                    value = 0;
                } else if (expected.equals(Float.TYPE)) {
                    value = 0f;
                } else if (expected.equals(Double.TYPE)) {
                    value = 0d;
                } else if (expected.equals(Long.TYPE)) {
                    value = 0L;
                } else if (expected.equals(Boolean.TYPE)) {
                    value = false;
                } else if (expected.equals(Short.TYPE)) {
                    value = Short.valueOf("0");
                } else if (expected.equals(Character.TYPE)) {
                    value = 'a';
                }
            }
            parameters.set(i, new ConstantValue(tc, new GenericClass(expected), value));
        }
    }
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

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