use of org.evosuite.utils.generic.GenericClass 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));
}
}
}
use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.
the class ConstantValue method changeClassLoader.
@Override
public void changeClassLoader(ClassLoader loader) {
super.changeClassLoader(loader);
if (value instanceof Class<?>) {
GenericClass genericClass = new GenericClass((Class<?>) value);
genericClass.changeClassLoader(loader);
value = genericClass.getRawClass();
}
}
use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.
the class MethodDescriptor method executeMatcher.
public Object executeMatcher(int i) throws IllegalArgumentException {
if (i < 0 || i >= getNumberOfInputParameters()) {
throw new IllegalArgumentException("Invalid index: " + i);
}
Type[] types = method.getParameterTypes();
Type type = types[i];
try {
if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
return Mockito.anyInt();
} else if (type.equals(Long.TYPE) || type.equals(Long.class)) {
return Mockito.anyLong();
} else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) {
return Mockito.anyBoolean();
} else if (type.equals(Double.TYPE) || type.equals(Double.class)) {
return Mockito.anyDouble();
} else if (type.equals(Float.TYPE) || type.equals(Float.class)) {
return Mockito.anyFloat();
} else if (type.equals(Short.TYPE) || type.equals(Short.class)) {
return Mockito.anyShort();
} else if (type.equals(Character.TYPE) || type.equals(Character.class)) {
return Mockito.anyChar();
} else if (type.equals(String.class)) {
return Mockito.anyString();
} else if (TypeUtils.isAssignable(type, List.class)) {
return Mockito.anyList();
} else if (TypeUtils.isAssignable(type, Set.class)) {
return Mockito.anySet();
} else if (TypeUtils.isAssignable(type, Map.class)) {
return Mockito.anyMap();
} else if (TypeUtils.isAssignable(type, Collection.class)) {
return Mockito.anyCollection();
} else if (TypeUtils.isAssignable(type, Iterable.class)) {
return Mockito.anyIterable();
} else {
GenericClass gc = new GenericClass(type);
return Mockito.nullable(gc.getRawClass());
}
} catch (Exception e) {
logger.error("Failed to executed Mockito matcher n{} of type {} in {}.{}: {}", i, type, className, methodName, e.getMessage());
throw new EvosuiteError(e);
}
}
use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.
the class Issre13SystemTest method testOnSpecificTest.
@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
// force using DSE
Properties.DSE_PROBABILITY = 1.0;
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
// String string0 = "baz5";
VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
// DseFoo dseFoo0 = new DseFoo();
GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
VariableReference fooVar = test.addStatement(fooConstructorStatement);
Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
// DseBar dseBar0 = new DseBar(string0);
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
VariableReference callee = test.addStatement(constructorStatement);
// dseBar0.coverMe(dseFoo0);
Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
test.addStatement(ms);
System.out.println(test);
TestSuiteChromosome suite = new TestSuiteChromosome();
BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
BranchCoverageMap.getInstance().searchStarted(null);
assertEquals(4.0, fitness.getFitness(suite), 0.1F);
suite.addTest(test);
assertEquals(1.0, fitness.getFitness(suite), 0.1F);
System.out.println("Test suite: " + suite);
TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
localObjective.addFitnessFunction(fitness);
localSearch.doSearch(suite, localObjective);
System.out.println("Fitness: " + fitness.getFitness(suite));
System.out.println("Test suite: " + suite);
assertEquals(0.0, fitness.getFitness(suite), 0.1F);
BranchCoverageMap.getInstance().searchFinished(null);
}
use of org.evosuite.utils.generic.GenericClass 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);
}
}
Aggregations