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   *
20   * @since 0.23.1
21   */
22  final class RequiredJavaDocTagTest {
23  
24      @ParameterizedTest
25      @MethodSource("params")
26      void success(final String[] lines, final String reason, final Matcher<String> expected) {
27          final StringBuilder out = new StringBuilder();
28          new RequiredJavaDocTag(
29              "since",
30              Pattern.compile("(?<name>^ +\\* +@since)( +)(?<cont>.*)"),
31              Pattern.compile(
32                  "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
33              ),
34              (line, msg, args) -> out.append(MessageFormat.format(msg, args))
35          ).matchTagFormat(lines, 0, 2);
36          MatcherAssert.assertThat(
37              reason,
38              out.toString(),
39              expected
40          );
41      }
42  
43      private static Stream<Arguments> params() {
44          return Stream.of(
45              Arguments.arguments(
46                  new String[] {" *", " * @since 0.3", " *"},
47                  "Empty string expected",
48                  Matchers.emptyString()
49              ),
50              Arguments.arguments(
51                  new String[] {" *", "    *    @since    0.3", " *"},
52                  "Empty string expected",
53                  Matchers.emptyString()
54              ),
55              Arguments.arguments(
56                  new String[] {" *", " * @sinc 0.3", " *"},
57                  "Expected tag not found",
58                  Matchers.equalTo("Missing '@since' tag in class/interface comment")
59              ),
60              Arguments.arguments(
61                  new String[] {" *", " * @since 0.3.4.4.", " *"},
62                  "Regular expression non-match expected",
63                  Matchers.equalTo(
64                      String.format(
65                          "Tag text '0.3.4.4.' does not match the pattern '%s'",
66                          "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
67                      )
68                  )
69              ),
70              Arguments.arguments(
71                  new String[] {" *", " * @since", " *"},
72                  "Tag present without content should not report as missing",
73                  Matchers.equalTo(
74                      "Malformed '@since' tag, expected ' * @since <value>' format"
75                  )
76              ),
77              Arguments.arguments(
78                  new String[] {" *", " * @since ", " *"},
79                  "Tag present with empty content should not report as missing",
80                  Matchers.equalTo(
81                      "Malformed '@since' tag, expected ' * @since <value>' format"
82                  )
83              ),
84              Arguments.arguments(
85                  new String[] {" *", " *@since 0.3", " *"},
86                  "Tag without space after asterisk should not report as missing",
87                  Matchers.equalTo(
88                      "Malformed '@since' tag, expected ' * @since <value>' format"
89                  )
90              )
91          );
92      }
93  }