use of org.evosuite.assertion.Assertion 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.Assertion 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.Assertion 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);
}
}
use of org.evosuite.assertion.Assertion in project evosuite by EvoSuite.
the class AbstractStatement method copyAssertions.
/**
* {@inheritDoc}
*
* Create copies of all attached assertions
*/
@Override
public Set<Assertion> copyAssertions(TestCase newTestCase, int offset) {
Set<Assertion> copy = new LinkedHashSet<Assertion>();
for (Assertion a : assertions) {
if (a == null) {
logger.info("Assertion is null!");
logger.info("Statement has assertions: " + assertions.size());
} else
copy.add(a.copy(newTestCase, offset));
}
return copy;
}
use of org.evosuite.assertion.Assertion in project evosuite by EvoSuite.
the class RegressionAssertionCounter method checkForAssertions.
private static int checkForAssertions(Boolean removeAssertions, Boolean noExecution, RegressionAssertionGenerator assertionGenerator, RegressionTestChromosome regressionTest) {
int totalCount = 0;
if (!noExecution) {
ExecutionResult result1 = assertionGenerator.runTest(regressionTest.getTheTest().getTestCase());
ExecutionResult result2 = assertionGenerator.runTest(regressionTest.getTheSameTestForTheOtherClassLoader().getTestCase());
if (result1.test == null || result2.test == null || result1.hasTimeout() || result2.hasTimeout()) {
logger.warn("=============================== HAD TIMEOUT ===============================");
} else {
int exceptionDiffs = RegressionExceptionHelper.compareExceptionDiffs(result1.getCopyOfExceptionMapping(), result2.getCopyOfExceptionMapping());
if (exceptionDiffs > 0) {
logger.debug("Had {} different exceptions! ({})", exceptionDiffs, totalCount);
}
totalCount += exceptionDiffs;
for (Class<?> observerClass : RegressionAssertionGenerator.observerClasses) {
if (result1.getTrace(observerClass) != null) {
result1.getTrace(observerClass).getAssertions(regressionTest.getTheTest().getTestCase(), result2.getTrace(observerClass));
}
}
}
}
int assertionCount = regressionTest.getTheTest().getTestCase().getAssertions().size();
totalCount += assertionCount;
// Store assertion comments for later flakiness check
if (assertionCount > 0) {
List<Assertion> assertions = regressionTest.getTheTest().getTestCase().getAssertions();
List<String> assertionComments = new ArrayList<>();
for (Assertion assertion : assertions) {
logger.warn("+++++ Assertion: {} {}", assertion.getCode(), assertion.getComment());
assertionComments.add(assertion.getComment());
}
RegressionAssertionCounter.assertionComments.put(regressionTest.getTheTest().getTestCase().toCode().hashCode(), assertionComments);
if (assertions.size() == 0) {
logger.warn("=========> NO ASSERTIONS!!!");
} else {
logger.warn("Assertions ^^^^^^^^^");
}
}
if (removeAssertions) {
regressionTest.getTheTest().getTestCase().removeAssertions();
}
return totalCount;
}
Aggregations