use of org.evosuite.assertion.InspectorAssertion in project evosuite by EvoSuite.
the class DefaultTestCase method assertionsNeedDownCast.
private boolean assertionsNeedDownCast(Statement s, VariableReference var, Class<?> abstractClass) {
for (Assertion assertion : s.getAssertions()) {
if (assertion instanceof InspectorAssertion && assertion.getSource().equals(var)) {
InspectorAssertion inspectorAssertion = (InspectorAssertion) assertion;
Method inspectorMethod = inspectorAssertion.getInspector().getMethod();
if (!ClassUtils.hasMethod(abstractClass, inspectorMethod.getName(), inspectorMethod.getParameterTypes())) {
return true;
}
} else if (assertion instanceof PrimitiveFieldAssertion && assertion.getSource().equals(var)) {
PrimitiveFieldAssertion fieldAssertion = (PrimitiveFieldAssertion) assertion;
if (!fieldAssertion.getField().getDeclaringClass().isAssignableFrom(abstractClass)) {
return true;
}
}
}
return false;
}
use of org.evosuite.assertion.InspectorAssertion in project evosuite by EvoSuite.
the class TestGenericAccessibleObject method testClassLoaderChange.
@Test
public void testClassLoaderChange() throws NoSuchMethodException, SecurityException, ConstructionFailedException {
Class<?> targetClass = com.examples.with.different.packagename.generic.GenericClassTwoParameters.class;
Method creatorMethod = targetClass.getMethod("create", new Class<?>[] {});
Method targetMethod = targetClass.getMethod("get", new Class<?>[] { Object.class });
Method inspectorMethod = targetClass.getMethod("testMe", new Class<?>[] {});
Constructor<?> intConst = Integer.class.getConstructor(new Class<?>[] { int.class });
GenericClass listOfInteger = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GenericClassTwoParameters<Integer, Integer>>() {
}.getType());
GenericMethod genericCreatorMethod = new GenericMethod(creatorMethod, targetClass).getGenericInstantiationFromReturnValue(listOfInteger);
System.out.println(genericCreatorMethod.getGeneratedClass().toString());
GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass).copyWithNewOwner(genericCreatorMethod.getGeneratedClass());
System.out.println(genericMethod.getGeneratedClass().toString());
DefaultTestCase test = new DefaultTestCase();
MethodStatement ms1 = new MethodStatement(test, genericCreatorMethod, (VariableReference) null, new ArrayList<VariableReference>());
test.addStatement(ms1);
IntPrimitiveStatement ps1 = (IntPrimitiveStatement) PrimitiveStatement.getPrimitiveStatement(test, int.class);
test.addStatement(ps1);
GenericConstructor intConstructor = new GenericConstructor(intConst, Integer.class);
List<VariableReference> constParam = new ArrayList<VariableReference>();
constParam.add(ps1.getReturnValue());
ConstructorStatement cs1 = new ConstructorStatement(test, intConstructor, constParam);
// test.addStatement(cs1);
List<VariableReference> callParam = new ArrayList<VariableReference>();
callParam.add(ps1.getReturnValue());
MethodStatement ms2 = new MethodStatement(test, genericMethod, ms1.getReturnValue(), callParam);
test.addStatement(ms2);
Inspector inspector = new Inspector(targetClass, inspectorMethod);
Assertion assertion = new InspectorAssertion(inspector, ms2, ms1.getReturnValue(), 0);
ms2.addAssertion(assertion);
String code = test.toCode();
ClassLoader loader = new InstrumentingClassLoader();
Properties.TARGET_CLASS = targetClass.getCanonicalName();
Properties.CRITERION = new Criterion[1];
Properties.CRITERION[0] = Criterion.MUTATION;
DefaultTestCase testCopy = test.clone();
testCopy.changeClassLoader(loader);
String code2 = testCopy.toCode();
Assert.assertEquals(code, code2);
Assert.assertEquals(code, test.toCode());
testCopy.removeAssertion(assertion);
Assert.assertEquals(code, test.toCode());
// test.removeAssertion(assertion);
test.removeAssertions();
System.out.println(test.toCode());
}
use of org.evosuite.assertion.InspectorAssertion in project evosuite by EvoSuite.
the class RegressionSuiteMinimizer method removeDuplicateAssertions.
private void removeDuplicateAssertions(RegressionTestSuiteChromosome suite) {
Iterator<TestChromosome> it = suite.getTestChromosomes().iterator();
Map<String, List<String>> uniqueAssertions = new HashMap<String, List<String>>();
// int i = -1;
while (it.hasNext()) {
// i++;
RegressionTestChromosome test = (RegressionTestChromosome) it.next();
boolean changed = false;
boolean hadAssertion = false;
// keep track of new unique assertions, and if not unique, remove the assertion
for (Assertion a : test.getTheTest().getTestCase().getAssertions()) {
String aClass = a.getClass().getSimpleName();
List<String> aTypes = uniqueAssertions.get(aClass);
if (aTypes == null) {
aTypes = new ArrayList<String>();
}
String aType = "";
if (a instanceof InspectorAssertion) {
InspectorAssertion ia = (InspectorAssertion) a;
try {
aType = ia.getInspector().getMethod().getName();
} catch (NullPointerException e) {
// technically this should not happen
Statement s = ia.getStatement();
if (s instanceof MethodStatement) {
aType = ((MethodStatement) s).getMethod().getName();
}
}
}
if (aTypes.contains(aType)) {
// logger.warn("removing non-unique assertion: {}-{}", aClass, aType);
changed = true;
a.getStatement().getPosition();
test.getTheTest().getTestCase().removeAssertion(a);
continue;
}
aTypes.add(aType);
uniqueAssertions.put(aClass, aTypes);
hadAssertion = true;
}
if (changed) {
test.updateClassloader();
}
}
if (uniqueAssertions.size() > 0) {
logger.warn("unique assertions: {}", uniqueAssertions);
}
}
Aggregations