View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import java.text.MessageFormat;
8   import java.util.regex.Pattern;
9   import java.util.stream.Stream;
10  import org.hamcrest.Matcher;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.params.ParameterizedTest;
14  import org.junit.jupiter.params.provider.Arguments;
15  import org.junit.jupiter.params.provider.MethodSource;
16  
17  /**
18   * Test case for {@link RequiredJavaDocTag} class.
19   * @since 0.23.1
20   */
21  final class RequiredJavaDocTagTest {
22  
23      @ParameterizedTest
24      @MethodSource("params")
25      void success(final String[] lines, final String reason, final Matcher<String> expected) {
26          final StringBuilder out = new StringBuilder();
27          new RequiredJavaDocTag(
28              "since",
29              Pattern.compile("(?<name>^ +\\* +@since)( +)(?<cont>.*)"),
30              Pattern.compile(
31                  "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
32              ),
33              (line, msg, args) -> out.append(MessageFormat.format(msg, args))
34          ).matchTagFormat(lines, 0, 2);
35          MatcherAssert.assertThat(
36              reason,
37              out.toString(),
38              expected
39          );
40      }
41  
42      private static Stream<Arguments> params() {
43          return Stream.of(
44              Arguments.arguments(
45                  new String[] {" *", " * @since 0.3", " *"},
46                  "Empty string expected",
47                  Matchers.emptyString()
48              ),
49              Arguments.arguments(
50                  new String[] {" *", "    *    @since    0.3", " *"},
51                  "Empty string expected",
52                  Matchers.emptyString()
53              ),
54              Arguments.arguments(
55                  new String[] {" *", " * @sinc 0.3", " *"},
56                  "Expected tag not found",
57                  Matchers.equalTo("Missing '@since' tag in class/interface comment")
58              ),
59              Arguments.arguments(
60                  new String[] {" *", " * @since 0.3.4.4.", " *"},
61                  "Regular expression non-match expected",
62                  Matchers.equalTo(
63                      String.format(
64                          "Tag text '0.3.4.4.' does not match the pattern '%s'",
65                          "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
66                      )
67                  )
68              ),
69              Arguments.arguments(
70                  new String[] {" *", " * @since", " *"},
71                  "Tag present without content should not report as missing",
72                  Matchers.equalTo(
73                      "Malformed '@since' tag, expected ' * @since <value>' format"
74                  )
75              ),
76              Arguments.arguments(
77                  new String[] {" *", " * @since ", " *"},
78                  "Tag present with empty content should not report as missing",
79                  Matchers.equalTo(
80                      "Malformed '@since' tag, expected ' * @since <value>' format"
81                  )
82              ),
83              Arguments.arguments(
84                  new String[] {" *", " *@since 0.3", " *"},
85                  "Tag without space after asterisk should not report as missing",
86                  Matchers.equalTo(
87                      "Malformed '@since' tag, expected ' * @since <value>' format"
88                  )
89              )
90          );
91      }
92  }