@Test
  public void register_rules_at_startup() throws Exception {
    verifyRulesInDb();

    Result<Rule> searchResult = index.search(new RuleQuery(), new QueryOptions());
    assertThat(searchResult.getTotal()).isEqualTo(3);
    assertThat(searchResult.getHits()).hasSize(3);

    Rule rule = index.getByKey(RuleTesting.XOO_X1);
    assertThat(rule.severity()).isEqualTo(Severity.MINOR);
    assertThat(rule.name()).isEqualTo("x1 name");
    assertThat(rule.htmlDescription()).isEqualTo("x1 desc");
    assertThat(rule.systemTags()).contains("tag1");

    assertThat(rule.params()).hasSize(1);
    assertThat(rule.param("acceptWhitespace").type()).isEqualTo(RuleParamType.BOOLEAN);
    assertThat(rule.param("acceptWhitespace").defaultValue()).isEqualTo("false");
    assertThat(rule.param("acceptWhitespace").description())
        .isEqualTo("Accept whitespaces on the line");

    assertThat(rule.debtSubCharacteristicKey())
        .isEqualTo(RulesDefinition.SubCharacteristics.INTEGRATION_TESTABILITY);
    assertThat(rule.debtRemediationFunction().type())
        .isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
    assertThat(rule.debtRemediationFunction().coefficient()).isEqualTo("1h");
    assertThat(rule.debtRemediationFunction().offset()).isEqualTo("30min");
    assertThat(rule.effortToFixDescription()).isEqualTo("x1 effort to fix");
  }
예제 #2
0
  @Test
  public void update_manual_rule() {
    // Create manual rule
    RuleDto manualRule =
        RuleTesting.newManualRule("My manual")
            .setName("Old name")
            .setDescription("Old description")
            .setSeverity(Severity.INFO);
    ruleDao.insert(dbSession, manualRule);

    dbSession.commit();

    // Update manual rule
    RuleUpdate update =
        RuleUpdate.createForManualRule(manualRule.getKey())
            .setName("New name")
            .setMarkdownDescription("New description")
            .setSeverity(Severity.CRITICAL);
    updater.update(update, userSessionRule);

    dbSession.clearCache();

    // Verify manual rule is updated
    Rule manualRuleReloaded = ruleIndex.getByKey(manualRule.getKey());
    assertThat(manualRuleReloaded).isNotNull();
    assertThat(manualRuleReloaded.name()).isEqualTo("New name");
    assertThat(manualRuleReloaded.htmlDescription()).isEqualTo("New description");
    assertThat(manualRuleReloaded.severity()).isEqualTo(Severity.CRITICAL);
  }
  @Test
  public void update_rule() {
    verifyRulesInDb();

    // The plugin X1 will be updated
    rulesDefinition.includeX1 = false;
    rulesDefinition.includeX1bis = true;
    tester.get(Platform.class).executeStartupTasks();

    Rule rule = index.getByKey(RuleTesting.XOO_X1);
    assertThat(rule.severity()).isEqualTo(Severity.INFO);
    assertThat(rule.name()).isEqualTo("x1 name updated");
    assertThat(rule.htmlDescription()).isEqualTo("x1 desc updated");
    assertThat(rule.systemTags()).contains("tag1", "tag2");

    assertThat(rule.params()).hasSize(2);

    assertThat(rule.param("acceptWhitespace").type()).isEqualTo(RuleParamType.BOOLEAN);
    assertThat(rule.param("acceptWhitespace").defaultValue()).isEqualTo("true");
    assertThat(rule.param("acceptWhitespace").description())
        .isEqualTo("Accept whitespaces on the line updated");

    // New parameter
    assertThat(rule.param("format").type()).isEqualTo(RuleParamType.TEXT);
    assertThat(rule.param("format").defaultValue()).isEqualTo("txt");
    assertThat(rule.param("format").description()).isEqualTo("Format");

    assertThat(rule.debtSubCharacteristicKey())
        .isEqualTo(RulesDefinition.SubCharacteristics.INSTRUCTION_RELIABILITY);
    assertThat(rule.debtRemediationFunction().type())
        .isEqualTo(DebtRemediationFunction.Type.LINEAR);
    assertThat(rule.debtRemediationFunction().coefficient()).isEqualTo("2h");
    assertThat(rule.debtRemediationFunction().offset()).isNull();
    assertThat(rule.effortToFixDescription()).isEqualTo("x1 effort to fix updated");
  }
예제 #4
0
  @Test
  public void update_custom_rule() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam1 =
        RuleParamDto.createFor(templateRule)
            .setName("regex")
            .setType("STRING")
            .setDescription("Reg ex")
            .setDefaultValue(".*");
    RuleParamDto templateRuleParam2 =
        RuleParamDto.createFor(templateRule)
            .setName("format")
            .setType("STRING")
            .setDescription("Format");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);

    // Create custom rule
    RuleDto customRule =
        RuleTesting.newCustomRule(templateRule)
            .setName("Old name")
            .setDescription("Old description")
            .setSeverity(Severity.MINOR)
            .setStatus(RuleStatus.BETA);
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue(null));

    dbSession.commit();

    // Update custom rule
    RuleUpdate update =
        RuleUpdate.createForCustomRule(customRule.getKey())
            .setName("New name")
            .setMarkdownDescription("New description")
            .setSeverity("MAJOR")
            .setStatus(RuleStatus.READY)
            .setParameters(ImmutableMap.of("regex", "b.*"));
    updater.update(update, userSessionRule);

    dbSession.clearCache();

    // Verify custom rule is updated
    Rule customRuleReloaded = ruleIndex.getByKey(customRule.getKey());
    assertThat(customRuleReloaded).isNotNull();
    assertThat(customRuleReloaded.name()).isEqualTo("New name");
    assertThat(customRuleReloaded.htmlDescription()).isEqualTo("New description");
    assertThat(customRuleReloaded.severity()).isEqualTo("MAJOR");
    assertThat(customRuleReloaded.status()).isEqualTo(RuleStatus.READY);
    assertThat(customRuleReloaded.params()).hasSize(2);

    assertThat(customRuleReloaded.params().get(0).defaultValue()).isEqualTo("b.*");
    assertThat(customRuleReloaded.params().get(1).defaultValue()).isNull();
  }
예제 #5
0
  private void makeDiff(String lang) throws IOException {

    final List<Rule> oldRules = new ArrayList<Rule>(); // rules in old grammar.xml
    final List<Rule> newRules = new ArrayList<Rule>(); // rules in new grammar.xml
    final List<Rule> modifiedRules = new ArrayList<Rule>();

    for (int i = 0; i < 2; i++) {

      final List<Rule> rules;
      if (i == 0) {
        rules = oldRules;
      } else {
        rules = newRules;
      }

      final Scanner scanner =
          new Scanner(new FileReader(i == 0 ? "tools/ltdiff/old" : "tools/ltdiff/new"));

      Rule r = new Rule();

      // loop through all lines
      while (scanner.hasNextLine()) {
        String line = scanner.nextLine();

        if (line.contains("id=\"") && line.contains("rule")) {

          if (!line.contains(
              "name=\"")) { // merge with the following line if the name is there (e.g. sk)
            line += scanner.nextLine();
          }

          if (r.numberOfExamples() > 0) {
            rules.add(r);
            r = new Rule();
          }

          r.id = line;
          r.name = line;

          r.id = r.id.replaceAll(".*id=\"", "").replaceAll("\".*", "");
          r.name = r.name.replaceAll(".*name=\"", "").replaceAll("\".*", "");

          for (Rule rule : rules) { // ensure that the name is unique
            if (r.name.equals(rule.name)) {
              r.name += " ";
            }
          }

        } else if (line.contains("type=\"correct\"")) {

          while (!line.contains(
              "</example>")) { // merge with the following line(s) if the example continues there
            line += scanner.nextLine();
          }
          r.correct.add(
              line.replaceAll("marker", "b")
                  .replaceAll(".*<example.*?>", "")
                  .replaceAll("</example>.*", ""));

        } else if (line.contains("type=\"incorrect\"")) {

          while (!line.contains("</example>")) {
            line += scanner.nextLine();
          }
          r.incorrect.add(
              line.replaceAll("marker", "b")
                  .replaceAll(".*<example.*?>", "")
                  .replaceAll("</example>.*", ""));
        }
      } // while(readLine)

      scanner.close();
    }

    // sort rules by name
    Collections.sort(oldRules);
    Collections.sort(newRules);

    // create html file containing the tr elements

    final FileWriter fileWriter = new FileWriter("changes_" + lang + ".html");
    final BufferedWriter out = new BufferedWriter(fileWriter);

    for (Rule newRule1 : newRules) {

      boolean found = false;

      for (int j = 0; j < oldRules.size() && !found; j++) {

        if (newRule1.id.equals(oldRules.get(j).id) || newRule1.name.equals(oldRules.get(j).name)) {

          found = true;

          if (newRule1.numberOfExamples()
              > oldRules
                  .get(j)
                  .numberOfExamples()) { // if the new rules has more examples, it is considered to
                                         // be improved

            final Rule r = newRule1;

            for (int k = 0;
                k < r.correct.size();
                k++) { // remove examples which already exist in old rule

              for (int l = 0; l < oldRules.get(j).correct.size() && r.correct.size() > 0; l++) {

                if (r.correct.get(k).equals(oldRules.get(j).correct.get(l))) {

                  r.correct.remove(k);
                  if (k > 0) k--;
                } // if examples equal
              } // for each old correct example
            } // for each new correct example

            for (int k = 0;
                k < r.incorrect.size();
                k++) { // remove examples which already exist in old rule

              for (int l = 0; l < oldRules.get(j).incorrect.size() && r.incorrect.size() > 0; l++) {

                if (r.incorrect.get(k).equals(oldRules.get(j).incorrect.get(l))) {

                  r.incorrect.remove(k);
                  if (k > 0) k--;
                } // if examples equal
              } // for each old incorrect example
            } // for each new incorrect example

            modifiedRules.add(r);
          } // if new rules has more examples
        } // if new rule is not new
      } // for each old rule

      if (!found) {
        out.write(
            "<tr class=\"new\"><td>4NEWRULE</td><td>"
                + newRule1.name
                + newRule1.getExamples(false)
                + "</td></tr>\n");
      }
    } // for each new rule

    for (Rule modifiedRule : modifiedRules) {
      out.write(
          "<tr class=\"modified\"><td>6IMPROVEDRULE</td><td>"
              + modifiedRule.name
              + modifiedRule.getExamples(true)
              + "</td></tr>\n");
    }

    for (Rule oldRule : oldRules) {
      boolean found = false;
      for (Rule newRule : newRules) {
        if (newRule.id.equals(oldRule.id) || newRule.name.equals(oldRule.name)) {
          found = true;
        }
      }
      if (!found && !oldRule.name.contains("<")) {
        out.write(
            "<tr class=\"removed\"><td>5REMOVEDRULE</td><td>" + oldRule.name + "</td></tr>\n");
      }
    }

    out.close();
  }