@Override
    public Configuration getConfiguration(RuleLoaderContext ruleLoaderContext) {
      Set<String> catchallTags = Collections.singleton("catchall");
      Set<String> otherTags = new HashSet<>();
      otherTags.add("tag1");
      otherTags.add("tag2");
      otherTags.add("tag3");

      return ConfigurationBuilder.begin()
          .addRule()
          .when(JavaClass.references("java.util.{*}"))
          .perform(
              Hint.titled("java.util.* found")
                  .withText("Catchall hint is here")
                  .withEffort(7)
                  .withTags(catchallTags))
          .addRule()
          .when(JavaClass.references("java.net.URL"))
          .perform(
              Hint.titled("java.net.URL")
                  .withText("Java Net URL is here (no catchall")
                  .withEffort(13)
                  .withTags(otherTags))
          .addRule()
          .when(JavaClass.references("java.util.HashMap"))
          .perform(
              Hint.titled("java.util.HashMap")
                  .withText("Java Net URL is here (no catchall")
                  .withEffort(42));
    }
Ejemplo n.º 2
0
  @Override
  public Hint processElement(ParserContext handlerManager, Element element)
      throws ConfigurationException {
    String title = $(element).attr("title");
    String severityStr = $(element).attr("severity");
    String message = $(element).attr("message");
    String in = $(element).attr("in");
    Set<String> tags = new HashSet<>();

    if (StringUtils.isBlank(message)) {
      StringBuilder messageBuilder = new StringBuilder();
      List<Element> children = $(element).children().get();
      for (Element child : children) {
        if (child.getNodeName().equals("message")) {
          messageBuilder.append(handlerManager.processElement(child));
        }
      }
      message = messageBuilder.toString();
      // remove the leading spaces as these can mess with markdown formatting
      message = trimLeadingAndTrailingSpaces(message);
    }

    if (StringUtils.isBlank(message)) {
      throw new WindupException(
          "Error, 'hint' element must have a non-empty 'message' attribute or element");
    }

    String effortStr = $(element).attr("effort");

    HintText hint;
    if (!StringUtils.isBlank(title)) {
      hint = Hint.in(in).titled(title).withText(message);
    } else {
      hint = Hint.in(in).withText(message);
    }

    if (StringUtils.isNotBlank(severityStr)) {
      Severity severity = Severity.valueOf(severityStr.toUpperCase());
      hint.withSeverity(severity);
    }

    if (!StringUtils.isBlank(effortStr)) {
      try {
        int effort = Integer.parseInt(effortStr);
        hint.withEffort(effort);
      } catch (NumberFormatException e) {
        throw new WindupException("Could not parse effort level: " + effortStr + " as an integer!");
      }
    }

    List<Element> children = $(element).children().get();
    for (Element child : children) {
      switch (child.getNodeName()) {
        case "link":
          Link link = handlerManager.processElement(child);
          hint.with(link);
          break;
        case "tag":
          tags.add(child.getTextContent());
          break;
      }
    }

    hint.withTags(tags);

    return (Hint) hint;
  }
Ejemplo n.º 3
0
  @Test
  public void testHintHandler() throws Exception {
    File fXmlFile = new File(HINT_XML_FILE);
    RuleLoaderContext loaderContext =
        new RuleLoaderContext(Collections.singleton(fXmlFile.toPath()), null);
    ParserContext parser = new ParserContext(furnace, loaderContext);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    List<Element> hintList = $(doc).children("hint").get();

    Assert.assertEquals(3, hintList.size());

    Element firstHint = hintList.get(0);
    Hint hint = parser.processElement(firstHint);
    Assert.assertEquals("testVariable", hint.getVariableName());
    Assert.assertEquals(5, hint.getEffort());
    Assert.assertNull(IssueCategoryRegistry.OPTIONAL, hint.getIssueCategory());
    Assert.assertEquals("test message", hint.getHintText().toString());
    Assert.assertEquals(1, hint.getLinks().size());
    List<Link> links = hint.getLinks();
    Assert.assertEquals("someUrl", links.get(0).getLink());
    Assert.assertEquals("someDescription", links.get(0).getTitle());

    Element secondHint = hintList.get(1);
    hint = parser.processElement(secondHint);
    Assert.assertEquals(null, hint.getVariableName());
    Assert.assertEquals(0, hint.getEffort());
    Assert.assertEquals(IssueCategoryRegistry.MANDATORY, hint.getIssueCategory().getCategoryID());
    Assert.assertEquals("test-message", hint.getHintText().toString());
    Assert.assertEquals(3, hint.getLinks().size());
    links = hint.getLinks();
    Assert.assertEquals("url1", links.get(0).getLink());
    Assert.assertEquals("url2", links.get(1).getLink());
    Assert.assertEquals("url3", links.get(2).getLink());
    Assert.assertEquals("description1", links.get(0).getTitle());
    Assert.assertEquals("description2", links.get(1).getTitle());
    Assert.assertEquals("description3", links.get(2).getTitle());

    Set<String> tags = hint.getTags();
    Assert.assertTrue(tags.contains("java-ee-6"));
    Assert.assertTrue(tags.contains("jpa-2"));
    Assert.assertTrue(tags.contains("jpa"));
    Assert.assertFalse(tags.contains("foo"));
  }