View Javadoc
1   /*
2    * Copyright (c) 2011-2025 Yegor Bugayenko
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle;
32  
33  import java.text.MessageFormat;
34  import java.util.regex.Pattern;
35  import java.util.stream.Stream;
36  import org.hamcrest.Matcher;
37  import org.hamcrest.MatcherAssert;
38  import org.hamcrest.Matchers;
39  import org.junit.jupiter.params.ParameterizedTest;
40  import org.junit.jupiter.params.provider.Arguments;
41  import org.junit.jupiter.params.provider.MethodSource;
42  
43  /**
44   * Test case for {@link RequiredJavaDocTag} class.
45   * @since 0.23.1
46   */
47  final class RequiredJavaDocTagTest {
48  
49      /**
50       * Logger.
51       */
52      private final WriterStub writer = new WriterStub();
53  
54      /**
55       * Object under test.
56       */
57      private final RequiredJavaDocTag tag = new RequiredJavaDocTag(
58          "since",
59          Pattern.compile(
60              "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
61          ),
62          this.writer
63      );
64  
65      @ParameterizedTest
66      @MethodSource("params")
67      void success(final String[] lines, final String reason, final Matcher<String> expected) {
68          this.tag.matchTagFormat(lines, 0, 2);
69          MatcherAssert.assertThat(
70              reason,
71              this.writer.formattedMessage(),
72              expected
73          );
74      }
75  
76      private static Stream<Arguments> params() {
77          return Stream.of(
78              Arguments.arguments(
79                  new String[] {" *", " * @since 0.3", " *"},
80                  "Empty string expected",
81                  Matchers.emptyString()
82              ),
83              Arguments.arguments(
84                  new String[] {" *", "    *    @since    0.3", " *"},
85                  "Empty string expected",
86                  Matchers.emptyString()
87              ),
88              Arguments.arguments(
89                  new String[] {" *", " * @sinc 0.3", " *"},
90                  "Expected tag not found",
91                  Matchers.equalTo("Missing '@since' tag in class/interface comment")
92              ),
93              Arguments.arguments(
94                  new String[] {" *", " * @since 0.3.4.4.", " *"},
95                  "Regular expression non-match expected",
96                  Matchers.equalTo(
97                      String.format(
98                          "Tag text '0.3.4.4.' does not match the pattern '%s'",
99                          "^\\d+(\\.\\d+){1,2}(\\.[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
100                     )
101                 )
102             )
103         );
104     }
105 
106     /**
107      * Stub for {@link RequiredJavaDocTag.Reporter} class.
108      * @since 0.23.1
109      */
110     private static class WriterStub implements RequiredJavaDocTag.Reporter {
111 
112         /**
113          * Message.
114          */
115         private String message;
116 
117         /**
118          * Ctor.
119          */
120         WriterStub() {
121             this.message = "";
122         }
123 
124         @Override
125         public void log(final int line, final String msg, final Object... args) {
126             this.message = MessageFormat.format(msg, args);
127         }
128 
129         String formattedMessage() {
130             return this.message;
131         }
132     }
133 }