private static void validateAnalyzerMessage(
     Map<IssueAttribute, String> attrs, AnalyzerMessage analyzerMessage) {
   Double effortToFix = analyzerMessage.getCost();
   if (effortToFix != null) {
     assertEquals(Integer.toString(effortToFix.intValue()), attrs, IssueAttribute.EFFORT_TO_FIX);
   }
   AnalyzerMessage.TextSpan textSpan = analyzerMessage.primaryLocation();
   assertEquals(normalizeColumn(textSpan.startCharacter), attrs, IssueAttribute.START_COLUMN);
   assertEquals(Integer.toString(textSpan.endLine), attrs, IssueAttribute.END_LINE);
   assertEquals(normalizeColumn(textSpan.endCharacter), attrs, IssueAttribute.END_COLUMN);
   if (attrs.containsKey(IssueAttribute.SECONDARY_LOCATIONS)) {
     List<AnalyzerMessage> secondaryLocations = analyzerMessage.secondaryLocations;
     Multiset<String> actualLines = HashMultiset.create();
     for (AnalyzerMessage secondaryLocation : secondaryLocations) {
       actualLines.add(Integer.toString(secondaryLocation.getLine()));
     }
     List<String> expected =
         Lists.newArrayList(
             Splitter.on(",")
                 .omitEmptyStrings()
                 .trimResults()
                 .split(attrs.get(IssueAttribute.SECONDARY_LOCATIONS)));
     List<String> unexpected = new ArrayList<>();
     for (String actualLine : actualLines) {
       if (expected.contains(actualLine)) {
         expected.remove(actualLine);
       } else {
         unexpected.add(actualLine);
       }
     }
     if (!expected.isEmpty() || !unexpected.isEmpty()) {
       Fail.fail("Secondary locations: expected: " + expected + " unexpected:" + unexpected);
     }
   }
 }
 private static void updateEndLine(int expectedLine, EnumMap<IssueAttribute, String> attr) {
   if (attr.containsKey(IssueAttribute.END_LINE)) {
     String endLineStr = attr.get(IssueAttribute.END_LINE);
     if (endLineStr.charAt(0) == '+') {
       int endLine = Integer.parseInt(endLineStr);
       attr.put(IssueAttribute.END_LINE, Integer.toString(expectedLine + endLine));
     } else {
       Fail.fail("endLine attribute should be relative to the line and must be +N with N integer");
     }
   }
 }
예제 #3
0
  @Test(expected = NullPointerException.class)
  public void shouldNotAcceptNullValues() {
    // given
    SmallMap<String, Integer> newSmallMap = SmallMap.newSmallMap();

    // when
    newSmallMap.put("abc", null);

    // then
    Fail.fail("NullPointerException expected");
  }
 private static String extractAttributes(String comment, Map<IssueAttribute, String> attr) {
   String attributesSubstr = StringUtils.substringBetween(comment, "[[", "]]");
   if (!StringUtils.isEmpty(attributesSubstr)) {
     Iterable<String> attributes = Splitter.on(";").split(attributesSubstr);
     for (String attribute : attributes) {
       String[] split = StringUtils.split(attribute, '=');
       if (split.length == 2 && CheckVerifier.ATTRIBUTE_MAP.containsKey(split[0])) {
         attr.put(CheckVerifier.ATTRIBUTE_MAP.get(split[0]), split[1]);
       } else {
         Fail.fail("// Noncompliant attributes not valid: " + attributesSubstr);
       }
     }
   }
   return attributesSubstr;
 }
  @Test
  public void loadInto_FromNonClassPath() {
    // prepare
    Load01 cfg = new Load01();

    // execute
    TestConfigurationBuilder sut =
        TestConfigurationBuilder.buildTestConfig().sourcePath("/other/path");
    try {
      sut.loadInto(cfg);
      Fail.fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("currently only classpath: is a supported location");
    }
  }
  @Test
  public void shouldNotAcceptnInvalidGroup() {
    // given
    Agent underTest = null;
    String invalidValue = null;

    // when
    try {
      underTest = new Agent(1, "", "", 1, invalidValue);
      Fail.fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException e) {
      // then
      assertThat(underTest).isNull();
    }
  }
  @Test
  public void shouldNotAcceptInvalidLastCheck() {
    // given
    Agent underTest = null;
    int invalidValue = -1;

    // when
    try {
      underTest = new Agent(1, "", "", invalidValue, "");
      Fail.fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException e) {
      // then
      assertThat(underTest).isNull();
    }
  }
 private void assertMultipleIssue(Set<AnalyzerMessage> issues) throws AssertionError {
   Preconditions.checkState(!issues.isEmpty(), "At least one issue expected");
   List<Integer> unexpectedLines = Lists.newLinkedList();
   boolean isLinear = isLinear(issues.iterator().next());
   for (AnalyzerMessage issue : issues) {
     validateIssue(expected, unexpectedLines, issue, isLinear);
   }
   if (!expected.isEmpty() || !unexpectedLines.isEmpty()) {
     Collections.sort(unexpectedLines);
     String expectedMsg = !expected.isEmpty() ? ("Expected " + expected) : "";
     String unexpectedMsg =
         !unexpectedLines.isEmpty()
             ? ((expectedMsg.isEmpty() ? "" : ", ") + "Unexpected at " + unexpectedLines)
             : "";
     Fail.fail(expectedMsg + unexpectedMsg);
   }
 }
 private static void validateIssue(
     Multimap<Integer, Map<IssueAttribute, String>> expected,
     List<Integer> unexpectedLines,
     AnalyzerMessage issue,
     boolean isLinear) {
   int line = issue.getLine();
   if (expected.containsKey(line)) {
     Map<IssueAttribute, String> attrs = Iterables.getLast(expected.get(line));
     assertEquals(issue.getMessage(), attrs, IssueAttribute.MESSAGE);
     Double cost = issue.getCost();
     if (cost != null) {
       assertEquals(Integer.toString(cost.intValue()), attrs, IssueAttribute.EFFORT_TO_FIX);
     } else if (isLinear) {
       Fail.fail("A cost should be provided for a rule with linear remediation function");
     }
     validateAnalyzerMessage(attrs, issue);
     expected.remove(line, attrs);
   } else {
     unexpectedLines.add(line);
   }
 }
  protected void collectExpectedIssues(String comment, int line) {
    String expectedStart = getExpectedIssueTrigger();
    if (comment.startsWith(expectedStart)) {
      String cleanedComment = StringUtils.remove(comment, expectedStart);

      EnumMap<IssueAttribute, String> attr = new EnumMap<>(IssueAttribute.class);
      String expectedMessage = StringUtils.substringBetween(cleanedComment, "{{", "}}");
      if (StringUtils.isNotEmpty(expectedMessage)) {
        attr.put(IssueAttribute.MESSAGE, expectedMessage);
      }
      int expectedLine = line;
      String attributesSubstr = extractAttributes(comment, attr);

      cleanedComment =
          StringUtils.stripEnd(
              StringUtils.remove(
                  StringUtils.remove(cleanedComment, "[[" + attributesSubstr + "]]"),
                  "{{" + expectedMessage + "}}"),
              " \t");
      if (StringUtils.startsWith(cleanedComment, "@")) {
        final int lineAdjustment;
        final char firstChar = cleanedComment.charAt(1);
        final int endIndex = cleanedComment.indexOf(' ');
        if (endIndex == -1) {
          lineAdjustment = Integer.parseInt(cleanedComment.substring(2));
        } else {
          lineAdjustment = Integer.parseInt(cleanedComment.substring(2, endIndex));
        }
        if (firstChar == '+') {
          expectedLine += lineAdjustment;
        } else if (firstChar == '-') {
          expectedLine -= lineAdjustment;
        } else {
          Fail.fail("Use only '@+N' or '@-N' to shifts messages.");
        }
      }
      updateEndLine(expectedLine, attr);
      expected.put(expectedLine, attr);
    }
  }