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