View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.javadoc;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocContentLocationCheck.MSG_JAVADOC_CONTENT_FIRST_LINE;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocContentLocationCheck.MSG_JAVADOC_CONTENT_SECOND_LINE;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class JavadocContentLocationCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/javadoc/javadoccontentlocation";
37      }
38  
39      @Test
40      public void testGetAcceptableTokens() {
41          final JavadocContentLocationCheck checkObj = new JavadocContentLocationCheck();
42          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN };
43          assertWithMessage("Acceptable tokens are invalid")
44              .that(checkObj.getAcceptableTokens())
45              .isEqualTo(expected);
46      }
47  
48      @Test
49      public void testGetDefaultTokens() {
50          final JavadocContentLocationCheck checkObj = new JavadocContentLocationCheck();
51          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN };
52          assertWithMessage("Default tokens are invalid")
53              .that(checkObj.getDefaultTokens())
54              .isEqualTo(expected);
55      }
56  
57      @Test
58      public void testDefault() throws Exception {
59          final String[] expected = {
60              "17:5: " + getCheckMessage(MSG_JAVADOC_CONTENT_SECOND_LINE),
61              "21:5: " + getCheckMessage(MSG_JAVADOC_CONTENT_SECOND_LINE),
62          };
63          verifyWithInlineConfigParser(
64                  getPath("InputJavadocContentLocationDefault.java"), expected);
65      }
66  
67      @Test
68      public void testFirstLine() throws Exception {
69          final String[] expected = {
70              "12:5: " + getCheckMessage(MSG_JAVADOC_CONTENT_FIRST_LINE),
71              "21:5: " + getCheckMessage(MSG_JAVADOC_CONTENT_FIRST_LINE),
72          };
73          verifyWithInlineConfigParser(
74                  getPath("InputJavadocContentLocationFirstLine.java"), expected);
75      }
76  
77      @Test
78      public void testPackage() throws Exception {
79          final String[] expected = {
80              "8:1: " + getCheckMessage(MSG_JAVADOC_CONTENT_SECOND_LINE),
81          };
82          verifyWithInlineConfigParser(
83                  getPath("package-info.java"), expected);
84      }
85  
86      @Test
87      public void testInterface() throws Exception {
88          final String[] expected = {
89              "10:1: " + getCheckMessage(MSG_JAVADOC_CONTENT_FIRST_LINE),
90          };
91          verifyWithInlineConfigParser(
92                  getPath("InputJavadocContentLocationInterface.java"), expected);
93      }
94  
95      @Test
96      public void testOptionalSpacesAndAsterisks() throws Exception {
97          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
98          verifyWithInlineConfigParser(
99                  getPath("InputJavadocContentLocationTrailingSpace.java"), expected);
100     }
101 
102     @Test
103     public void testTrimOptionProperty() throws Exception {
104         final String[] expected = {
105             "12:5: " + getCheckMessage(MSG_JAVADOC_CONTENT_FIRST_LINE),
106             "21:5: " + getCheckMessage(MSG_JAVADOC_CONTENT_FIRST_LINE),
107         };
108         verifyWithInlineConfigParser(
109                 getPath("InputJavadocContentLocationTrimOptionProperty.java"), expected);
110     }
111 
112     @Test
113     public void testDefault2() throws Exception {
114         final String[] expected = {
115             "9:1: " + getCheckMessage(MSG_JAVADOC_CONTENT_SECOND_LINE),
116         };
117         verifyWithInlineConfigParser(
118                 getPath("InputJavadocContentLocation.java"), expected);
119     }
120 }