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; }
/** {@inheritDoc} */ @Override public void addAssertion(Assertion assertion) { if (assertion == null) { logger.warn("Trying to add null assertion!"); } else { logger.debug("Adding assertion " + assertion.getCode()); assert (assertion.isValid()) : "Invalid assertion detected: " + assertion.getCode() + ", " + assertion.getSource() + ", " + assertion.getValue(); assertion.setStatement(this); assertions.add(assertion); } }
/** * Remove assertNonNull assertions for all cases where we have further * assertions * * @param test */ protected void filterRedundantNonnullAssertions(TestCase test) { Set<Assertion> redundantAssertions = new HashSet<Assertion>(); for (Statement statement : test) { if (statement instanceof ConstructorStatement) { ConstructorStatement cs = (ConstructorStatement) statement; for (Assertion a : cs.getAssertions()) { if (a instanceof NullAssertion) { if (cs.getAssertions().size() > 0) { for (Assertion a2 : cs.getAssertions()) { if (a2.getSource() == cs.getReturnValue()) redundantAssertions.add(a); } } else if (isUsedAsCallee(test, cs.getReturnValue())) { redundantAssertions.add(a); } } } } } for (Assertion a : redundantAssertions) { test.removeAssertion(a); } }