@Override public void changeClassLoader(ClassLoader loader) { super.changeClassLoader(loader); inspector.changeClassLoader(loader); } }
/** * Return a copy of the assertion, which is valid in newTestCase * * @param newTestCase * a {@link org.evosuite.testcase.TestCase} object. * @return a {@link org.evosuite.assertion.Assertion} object. */ public Assertion clone(TestCase newTestCase) { return copy(newTestCase, 0); }
/** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; SameAssertion other = (SameAssertion) obj; if (dest == null) { if (other.dest != null) return false; } else if (!dest.equals(other.dest)) return false; return true; }
/** {@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); } }
assertion.addKilledMutation(m); for (Assertion assertion : assertions) { if (assertion instanceof PrimitiveAssertion) { if (assertion.getStatement().equals(test.getStatement(test.size() - 1))) { logger.debug("Adding a primitive assertion " + assertion); test.getStatement(test.size() - 1).addAssertion(assertion); for (Assertion assertion : assertions) { if (assertion instanceof PrimitiveAssertion) { if (assertion.getStatement().equals(test.getStatement(test.size() - 1))) { logger.debug("Adding a primitive assertion " + assertion); test.getStatement(test.size() - 1).addAssertion(assertion); if (assertion.getStatement().equals(test.getStatement(test.size() - 1))) { logger.info("Adding an assertion: " + assertion); test.getStatement(test.size() - 1).addAssertion(assertion); int numAssertions = 0; for (Assertion ass : target) { if (ass.getReferencedVariables().contains(targetVar) && !(ass instanceof NullAssertion)) { logger.debug("Adding assertion " + ass.getCode()); if (++numAssertions >= maxAssertions) break; } else { logger.debug("Assertion does not contain target: " + ass.getCode());
/** * Get all possible assertions * * @param test * a {@link org.evosuite.testcase.TestCase} object. * @return a int. */ public int getAllAssertions(TestCase test, int statement) { int num = 0; if (!trace.containsKey(statement)) return 0; for (Integer var : trace.get(statement).keySet()) { for (Assertion assertion : trace.get(statement).get(var).getAssertions()) { assert (assertion.isValid()) : "Invalid assertion: " + assertion.getCode() + ", " + assertion.value; test.getStatement(statement).addAssertion(assertion); num++; } } return num; }
/** {@inheritDoc} */ @Override public Set<VariableReference> getReferencedVariables() { Set<VariableReference> vars = super.getReferencedVariables(); vars.add(source); vars.add(containedVariable); return vars; } }
List<String> assertionComments = new ArrayList<>(); for (Assertion assertion : assertions) { logger.warn("+++++ Assertion: {} {}", assertion.getCode(), assertion.getComment()); assertionComments.add(assertion.getComment());
throw new RuntimeException("Unknown assertion type: " + assertion); if (assertion.hasComment()) testCode += assertion.getComment();
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 String getAssertionCode() { String ret_val = ""; for (Assertion a : assertions) { if (a != null) ret_val += a.getCode() + "\n"; } return ret_val; }
/** {@inheritDoc} */ @Override public void addAssertions(TestCase other) { for (int i = 0; i < statements.size() && i < other.size(); i++) { for (Assertion a : other.getStatement(i).getAssertions()) { if (!statements.get(i).getAssertions().contains(a)) if (a != null) statements.get(i).getAssertions().add(a.clone(this)); } } }
for(Mutation m : assertion.getKilledMutations()) { mutationCoverage.add(new MutationInfo(m));
/** * Get all assertions based on trace differences * * @param test * a {@link org.evosuite.testcase.TestCase} object. * @param other * a {@link org.evosuite.assertion.OutputTrace} object. * @return a int. */ public int getAssertions(TestCase test, OutputTrace<?> other) { int num = 0; for (Integer statement : trace.keySet()) { if (other.trace.containsKey(statement)) { logger.debug("Other trace contains " + statement); for (Integer var : trace.get(statement).keySet()) { logger.debug("Variable " + var); for (Assertion assertion : trace.get(statement).get(var).getAssertions(other.trace.get(statement).get(var))) { assert (assertion.isValid()) : "Invalid assertion: " + assertion.getCode() + ", " + assertion.value; test.getStatement(statement).addAssertion(assertion); num++; } } } } return num; }
/** * <p> * getAssertionReferences * </p> * * @return a {@link java.util.Set} object. */ protected Set<VariableReference> getAssertionReferences() { Set<VariableReference> variables = new LinkedHashSet<VariableReference>(); for (Assertion assertion : assertions) { variables.addAll(assertion.getReferencedVariables()); } return variables; }
/** * 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); } }
/** * Get all possible assertions * * @param test * a {@link org.evosuite.testcase.TestCase} object. * @return a int. */ public int getAllAssertions(TestCase test) { int num = 0; for (Integer statement : trace.keySet()) { for (Integer var : trace.get(statement).keySet()) { for (Assertion assertion : trace.get(statement).get(var).getAssertions()) { assert (assertion.isValid()) : "Invalid assertion: " + assertion.getCode() + ", " + assertion.value; if (test.sizeWithAssertions() >= Properties.MAX_LENGTH_TEST_CASE) { return num; } test.getStatement(statement).addAssertion(assertion); num++; } } } return num; }
/** * Returns true if the statement has nothing but null assertions * * @param statement * @return */ protected boolean justNullAssertion(Statement statement) { Set<Assertion> assertions = statement.getAssertions(); if (assertions.isEmpty()) return false; else { Iterator<Assertion> iterator = assertions.iterator(); VariableReference ret = statement.getReturnValue(); VariableReference callee = null; if (statement instanceof MethodStatement) { callee = ((MethodStatement) statement).getCallee(); } boolean just = true; while (iterator.hasNext()) { Assertion ass = iterator.next(); if (!(ass instanceof NullAssertion)) { if (ass.getReferencedVariables().contains(ret) || ass.getReferencedVariables().contains(callee)) { just = false; break; } } } return just; } }
/** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; EqualsAssertion other = (EqualsAssertion) obj; if (dest == null) { if (other.dest != null) return false; } else if (!dest.equals(other.dest)) return false; return true; }
/** * {@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; }