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      /**
24       * Logger.
25       */
26      private final WriterStub writer = new WriterStub();
27  
28      /**
29       * Object under test.
30       */
31      private final RequiredJavaDocTag tag = new RequiredJavaDocTag(
32          "since",
33          Pattern.compile(
34              "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
35          ),
36          this.writer
37      );
38  
39      @ParameterizedTest
40      @MethodSource("params")
41      void success(final String[] lines, final String reason, final Matcher<String> expected) {
42          this.tag.matchTagFormat(lines, 0, 2);
43          MatcherAssert.assertThat(
44              reason,
45              this.writer.formattedMessage(),
46              expected
47          );
48      }
49  
50      private static Stream<Arguments> params() {
51          return Stream.of(
52              Arguments.arguments(
53                  new String[] {" *", " * @since 0.3", " *"},
54                  "Empty string expected",
55                  Matchers.emptyString()
56              ),
57              Arguments.arguments(
58                  new String[] {" *", "    *    @since    0.3", " *"},
59                  "Empty string expected",
60                  Matchers.emptyString()
61              ),
62              Arguments.arguments(
63                  new String[] {" *", " * @sinc 0.3", " *"},
64                  "Expected tag not found",
65                  Matchers.equalTo("Missing '@since' tag in class/interface comment")
66              ),
67              Arguments.arguments(
68                  new String[] {" *", " * @since 0.3.4.4.", " *"},
69                  "Regular expression non-match expected",
70                  Matchers.equalTo(
71                      String.format(
72                          "Tag text '0.3.4.4.' does not match the pattern '%s'",
73                          "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
74                      )
75                  )
76              )
77          );
78      }
79  
80      /**
81       * Stub for {@link RequiredJavaDocTag.Reporter} class.
82       * @since 0.23.1
83       */
84      private static class WriterStub implements RequiredJavaDocTag.Reporter {
85  
86          /**
87           * Message.
88           */
89          private String message;
90  
91          /**
92           * Ctor.
93           */
94          WriterStub() {
95              this.message = "";
96          }
97  
98          @Override
99          public void log(final int line, final String msg, final Object... args) {
100             this.message = MessageFormat.format(msg, args);
101         }
102 
103         String formattedMessage() {
104             return this.message;
105         }
106     }
107 }