TaskScanner { Report scanTasks(final Iterator<String> lines, final IssueBuilder builder) { Report report = new Report(); if (isPatternInvalid) { report.logError("%s", errors.toString()); return report; } for (int lineNumber = 1; lines.hasNext(); lineNumber++) { String line = lines.next(); for (Severity severity : Severity.getPredefinedValues()) { if (patterns.containsKey(severity)) { Matcher matcher = patterns.get(severity).matcher(line); if (matcher.matches() && matcher.groupCount() == 2) { String message = matcher.group(2).trim(); builder.setMessage(StringUtils.removeStart(message, ":").trim()); String tag = matcher.group(1); if (isUppercase) { builder.setType(StringUtils.upperCase(tag)); } else { builder.setType(tag); } report.add(builder.setSeverity(severity).setLineStart(lineNumber).build()); } } } } return report; } TaskScanner(final @CheckForNull String highTags, final @CheckForNull String normalTags,
            final @CheckForNull String lowTags,
            final CaseMode caseMode, final MatcherMode matcherMode); String getErrors(); Report scan(final Path file, final Charset charset);  }
@Test void issue22744() { Report tasks = new TaskScannerBuilder() .setHighTasks("FIXME") .setNormalTasks("TODO") .setLowTasks("") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read("issue22744.java", "windows-1251"), ISSUE_BUILDER); assertThat(tasks).hasSize(2); assertThat(tasks.get(0)).hasSeverity(Severity.WARNING_HIGH) .hasType("FIXME") .hasLineStart(4) .hasMessage("\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 Jenkins"); assertThat(tasks.get(1)).hasSeverity(Severity.WARNING_NORMAL) .hasType("TODO") .hasLineStart(5) .hasMessage( "\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c"); }
@Test void issue12782() { Report tasks = new TaskScannerBuilder() .setHighTasks("!!!!!") .setNormalTasks("!!!") .setLowTasks("") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read("issue12782.txt"), ISSUE_BUILDER); assertThat(tasks).hasSize(3); }
@Test void shouldScanFileWithWords() { Report tasks = new TaskScannerBuilder() .setHighTasks("WARNING") .setNormalTasks("TODO") .setLowTasks("@todo") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read("tasks-words-test.txt"), ISSUE_BUILDER); assertThat(tasks).hasSize(12); assertThatReportHasSeverities(tasks, 0, 0, 7, 5); }
@Test void shouldIgnoreCaseInSource() { Report tasks = new TaskScannerBuilder() .setNormalTasks("todo") .setCaseMode(CaseMode.IGNORE_CASE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read(CASE_TEST_FILE), ISSUE_BUILDER); assertThat(tasks).hasSize(9); for (Issue task : tasks) { assertThat(task).hasType("TODO"); } }
@Test void shouldIgnoreCaseInTag() { Report tasks = new TaskScannerBuilder() .setNormalTasks("Todo, TodoS") .setCaseMode(CaseMode.IGNORE_CASE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read(CASE_TEST_FILE), ISSUE_BUILDER); assertThat(tasks).hasSize(12); for (Issue task : tasks) { assertThat(task.getType()).startsWith("TODO"); } }
@Test void shouldUseDefaults() { Report tasks = new TaskScannerBuilder().setHighTasks("FIXME") .setNormalTasks("TODO") .setLowTasks("@deprecated") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read(FILE_WITH_TASKS), ISSUE_BUILDER); assertThat(tasks).hasSize(2); assertThatReportHasSeverities(tasks, 0, 1, 1, 0); assertThat(tasks.get(0)).hasMessage(PRIORITY_NORMAL_MESSAGE); assertThat(tasks.get(1)).hasMessage(PRIORITY_HIGH_MESSAGE); }
@Test void shouldFindHighPriority() { Report tasks = new TaskScannerBuilder().setHighTasks(FIXME) .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read(FILE_WITH_TASKS), ISSUE_BUILDER); assertThat(tasks).hasSize(1); assertThatReportHasSeverities(tasks, 0, 1, 0, 0); }
@Test void shouldIgnoreSpaceInTags() { Report tasks = new TaskScannerBuilder().setHighTasks(" FIXME , TODO ") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read(FILE_WITH_TASKS), ISSUE_BUILDER); assertThat(tasks).hasSize(2); assertThatReportHasSeverities(tasks, 0, 2, 0, 0); }
@Test void shouldHaveTwoItemsWithHighPriority() { Report tasks = new TaskScannerBuilder().setHighTasks("FIXME,TODO") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read(FILE_WITH_TASKS), ISSUE_BUILDER); assertThat(tasks).hasSize(2); assertThatReportHasSeverities(tasks, 0, 2, 0, 0); }
@Test void shouldIdentifyTags() { String text = "FIXME: this is a fixme"; Report high = new TaskScannerBuilder() .setHighTasks("FIXME,TODO") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(readFromString(text), ISSUE_BUILDER); assertThat(high).hasSize(1); assertThat(high.get(0)).hasType(FIXME); Report normal = new TaskScannerBuilder() .setNormalTasks("XXX, HELP, FIXME, TODO") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(readFromString(text), ISSUE_BUILDER); assertThat(normal).hasSize(1); assertThat(normal.get(0)).hasType(FIXME); }
@Test void shouldScanAllPriorities() { Report tasks = new TaskScannerBuilder().setHighTasks(FIXME) .setNormalTasks("FIXME,TODO") .setLowTasks("TODO") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read(FILE_WITH_TASKS), ISSUE_BUILDER); assertThat(tasks).hasSize(4); assertThatReportHasSeverities(tasks, 0, 1, 2, 1); }
@Test void shouldScanFileWithoutTasks() { Report tasks = new TaskScannerBuilder().setHighTasks("FIXME") .setNormalTasks("TODO") .setLowTasks("@deprecated") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.STRING_MATCH) .build() .scanTasks(read("file-without-tasks.txt"), ISSUE_BUILDER); assertThat(tasks).hasSize(0); }
@Test void shouldParseRegularExpressionsIssue17225() { Report tasks = new TaskScannerBuilder() .setHighTasks("^.*(TODO(?:[0-9]*))(.*)$") .setCaseMode(CaseMode.CASE_SENSITIVE) .setMatcherMode(MatcherMode.REGEXP_MATCH) .build() .scanTasks(read("regexp.txt"), ISSUE_BUILDER); assertThat(tasks).hasSize(5); assertThat(tasks.get(0)).hasSeverity(Severity.WARNING_HIGH) .hasType("TODO1") .hasLineStart(1) .hasMessage("erstes"); assertThat(tasks.get(1)).hasSeverity(Severity.WARNING_HIGH) .hasType("TODO2") .hasLineStart(2) .hasMessage("zweites"); assertThat(tasks.get(2)).hasSeverity(Severity.WARNING_HIGH) .hasType("TODO3") .hasLineStart(3) .hasMessage("drittes"); assertThat(tasks.get(3)).hasSeverity(Severity.WARNING_HIGH) .hasType("TODO4") .hasLineStart(4) .hasMessage("viertes"); assertThat(tasks.get(4)).hasSeverity(Severity.WARNING_HIGH) .hasType("TODO20") .hasLineStart(5) .hasMessage("zwanzigstes"); }