Ejemplo n.º 1
0
 protected void saveViolations(DroolsFile resource, SensorContext context, VerifierReport report) {
   List<Violation> violations = new ArrayList<Violation>();
   for (Severity severity : Severity.values()) {
     Collection<VerifierMessageBase> messages = report.getBySeverity(severity);
     for (VerifierMessageBase base : messages) {
       Rule rule = findRule(base);
       // ignore violations from report, if rule not activated in Sonar
       if (rule != null) {
         if (context.getResource(resource) != null) {
           int line = getLineNumber(resource, base);
           Violation violation =
               Violation.create(rule, resource).setLineId(line).setMessage(base.getMessage());
           violations.add(violation);
         }
       }
     }
   }
   context.saveViolations(violations);
 }
Ejemplo n.º 2
0
  @Test
  public void testPatterns() throws Exception {
    StatelessSession session =
        getStatelessSession(this.getClass().getResourceAsStream("Rules.drl"));

    session.setAgendaFilter(new RuleNameMatchesAgendaFilter("Rule that is always true"));

    VerifierReport result = VerifierReportFactory.newVerifierReport();
    Collection<Object> data = new ArrayList<Object>();

    session.setGlobal("result", result);

    // This rule is always true.
    VerifierRule rule1 = VerifierComponentMockFactory.createRule1();

    SubRule rp1 = new SubRule(rule1, 0);
    AlwaysTrue alwaysTrue1 = new AlwaysTrue(rp1);

    SubRule rp2 = new SubRule(rule1, 1);
    AlwaysTrue alwaysTrue2 = new AlwaysTrue(rp2);

    // This rule is okay.
    VerifierRule rule2 = VerifierComponentMockFactory.createRule2();

    SubRule rp3 = new SubRule(rule2, 0);

    SubRule rp4 = new SubRule(rule2, 1);
    AlwaysTrue alwaysTrue4 = new AlwaysTrue(rp4);

    data.add(rule1);
    data.add(rp1);
    data.add(rp2);
    data.add(alwaysTrue1);
    data.add(alwaysTrue2);

    data.add(rule2);
    data.add(rp3);
    data.add(rp4);
    data.add(alwaysTrue4);

    session.executeWithResults(data);

    Iterator<VerifierMessageBase> iter = result.getBySeverity(Severity.WARNING).iterator();

    boolean works = false;
    while (iter.hasNext()) {
      Object o = (Object) iter.next();
      if (o instanceof VerifierMessage) {
        VerifierMessage message = (VerifierMessage) o;
        if (message.getFaulty().equals(rule1)) {
          works = true;
        } else {
          fail("There can be only one. (And this is not the one)");
        }
      }
    }

    assertEquals(0, result.getBySeverity(Severity.ERROR).size());
    assertEquals(1, result.getBySeverity(Severity.WARNING).size());
    assertEquals(0, result.getBySeverity(Severity.NOTE).size());
    assertTrue(works);
  }
Ejemplo n.º 3
0
  @Test
  @Ignore("08-APR-2011 temporally ignoring -Rikkola-")
  public void testCauseTrace() throws Exception {

    VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();

    // Check that the builder works.
    assertFalse(vBuilder.hasErrors());
    assertEquals(0, vBuilder.getErrors().size());

    Verifier verifier = vBuilder.newVerifier();

    verifier.addResourcesToVerify(
        new ClassPathResource("Causes.drl", getClass()), ResourceType.DRL);

    assertFalse(verifier.hasErrors());
    assertEquals(0, verifier.getErrors().size());

    boolean works = verifier.fireAnalysis();

    assertTrue(works);

    VerifierReport result = verifier.getResult();
    assertNotNull(result);
    assertEquals(0, result.getBySeverity(Severity.ERROR).size());
    Collection<VerifierMessageBase> warnings = result.getBySeverity(Severity.WARNING);
    Collection<VerifierMessageBase> redundancyWarnings = new ArrayList<VerifierMessageBase>();

    for (VerifierMessageBase verifierMessageBase : warnings) {
      if (verifierMessageBase.getMessageType().equals(MessageType.REDUNDANCY)) {
        redundancyWarnings.add(verifierMessageBase);
      }
    }

    assertEquals(1, redundancyWarnings.size());

    VerifierMessage message = (VerifierMessage) redundancyWarnings.toArray()[0];

    //        System.out.println( message );

    assertEquals(2, message.getImpactedRules().size());

    assertTrue(message.getImpactedRules().values().contains("Your First Rule"));
    assertTrue(message.getImpactedRules().values().contains("Your Second Rule"));

    Cause[] causes = message.getCauses().toArray(new Cause[message.getCauses().size()]);

    assertEquals(1, causes.length);
    causes = causes[0].getCauses().toArray(new Cause[causes[0].getCauses().size()]);

    assertEquals(2, causes.length);

    causes = causes[0].getCauses().toArray(new Cause[causes[0].getCauses().size()]);

    assertEquals(1, causes.length);

    causes = causes[0].getCauses().toArray(new Cause[causes[0].getCauses().size()]);

    assertEquals(1, causes.length);

    causes = causes[0].getCauses().toArray(new Cause[causes[0].getCauses().size()]);

    assertEquals(2, causes.length);

    assertEquals(0, result.getBySeverity(Severity.NOTE).size());
  }