Report implements Iterable<RuleViolation> { public void addRuleViolation(RuleViolation violation) { int line = violation.getBeginLine(); if (linesToSuppress.containsKey(line)) { suppressedRuleViolations.add(new SuppressedViolation(violation, true, linesToSuppress.get(line))); return; } if (violation.isSuppressed()) { suppressedRuleViolations.add(new SuppressedViolation(violation, false, null)); return; } int index = Collections.binarySearch(violations, violation, RuleViolation.DEFAULT_COMPARATOR); violations.add(index < 0 ? -index - 1 : index, violation); violationTree.addRuleViolation(violation); for (ThreadSafeReportListener listener : listeners) { listener.ruleViolationAdded(violation); } }  static Report createReport(RuleContext ctx, String fileName); void suppress(Map<Integer, String> lines); @Deprecated Map<String, Integer> getCountSummary(); @Deprecated ReportTree getViolationTree(); @Deprecated Map<String, Integer> getSummary(); void addListener(ThreadSafeReportListener listener); @Deprecated List<SuppressedViolation> getSuppressedRuleViolations(); void addRuleViolation(RuleViolation violation); @Deprecated void addMetric(Metric metric); void addConfigError(ConfigurationError error); void addError(ProcessingError error); void merge(Report r); @Deprecated boolean hasMetrics(); @Deprecated Iterator<Metric> metrics(); @Deprecated boolean isEmpty(); @Deprecated boolean hasErrors(); @Deprecated boolean hasConfigErrors(); @Deprecated boolean treeIsEmpty(); @Deprecated Iterator<RuleViolation> treeIterator(); @Deprecated @Override Iterator<RuleViolation> iterator(); final List<SuppressedViolation> getSuppressedViolations(); final List<RuleViolation> getViolations(); final List<ProcessingError> getProcessingErrors(); final List<ConfigurationError> getConfigurationErrors(); @Deprecated Iterator<ProcessingError> errors(); @Deprecated Iterator<ConfigurationError> configErrors(); @Deprecated int treeSize(); @Deprecated int size(); @Deprecated void start(); @Deprecated void end(); @Deprecated long getElapsedTimeInMillis(); List<ThreadSafeReportListener> getListeners(); void addListeners(List<ThreadSafeReportListener> allListeners);  }
@Test public void testSortedReportFile() throws IOException { Report r = new Report(); RuleContext ctx = new RuleContext(); ctx.setSourceCodeFile(new File("foo")); Node s = getNode(10, 5); Rule rule1 = new MockRule("name", "desc", "msg", "rulesetname"); r.addRuleViolation(new ParametricRuleViolation<>(rule1, ctx, s, rule1.getMessage())); ctx.setSourceCodeFile(new File("bar")); Node s1 = getNode(10, 5); Rule rule2 = new MockRule("name", "desc", "msg", "rulesetname"); r.addRuleViolation(new ParametricRuleViolation<>(rule2, ctx, s1, rule2.getMessage())); Renderer rend = new XMLRenderer(); String result = render(rend, r); assertTrue("sort order wrong", result.indexOf("bar") < result.indexOf("foo")); }
@Test public void testSortedReportLine() throws IOException { Report r = new Report(); RuleContext ctx = new RuleContext(); ctx.setSourceCodeFile(new File("foo1")); Node node1 = getNode(20, 5); Rule rule1 = new MockRule("rule1", "rule1", "msg", "rulesetname"); r.addRuleViolation(new ParametricRuleViolation<>(rule1, ctx, node1, rule1.getMessage())); ctx.setSourceCodeFile(new File("foo1")); Node node2 = getNode(10, 5); Rule rule2 = new MockRule("rule2", "rule2", "msg", "rulesetname"); r.addRuleViolation(new ParametricRuleViolation<>(rule2, ctx, node2, rule2.getMessage())); Renderer rend = new XMLRenderer(); String result = render(rend, r); assertTrue("sort order wrong", result.indexOf("rule2") < result.indexOf("rule1")); }