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.JavadocBlockTagLocationCheck.MSG_BLOCK_TAG_LOCATION;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class JavadocBlockTagLocationCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/javadoc/javadocblocktaglocation";
36      }
37  
38      @Test
39      public void testGetAcceptableTokens() {
40          final JavadocBlockTagLocationCheck checkObj = new JavadocBlockTagLocationCheck();
41          final int[] expected = {
42              JavadocTokenTypes.TEXT,
43          };
44          assertWithMessage("Default acceptable tokens are invalid")
45              .that(checkObj.getAcceptableJavadocTokens())
46              .isEqualTo(expected);
47      }
48  
49      @Test
50      public void testCorrect() throws Exception {
51          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
52  
53          verifyWithInlineConfigParser(
54                  getPath("InputJavadocBlockTagLocationCorrect.java"), expected);
55      }
56  
57      /**
58       * This test is needed to indicate the difference between Javadoc and Checkstyle parsers
59       * when a block tag is inside an inline tag.
60       * Javadoc tool parser sees a block tag here, while Checkstyle parser treat the
61       * inner block tag as plain text. If ever the checkstyle parser is changed,
62       * this test and the check itself should be reviewed.
63       *
64       * @throws Exception if exception occurs during verification process.
65       */
66      @Test
67      public void testMultilineCodeBlock() throws Exception {
68          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
69  
70          verifyWithInlineConfigParser(
71                  getPath("InputJavadocBlockTagLocationMultilineCodeBlock.java"), expected);
72      }
73  
74      @Test
75      public void testIncorrect() throws Exception {
76          final String[] expected = {
77              "15: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "author"),
78              "16: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "since"),
79              "17: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "param"),
80              "19: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "throws"),
81              "20: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "see"),
82              "21: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "return"),
83              "21: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "throws"),
84          };
85          verifyWithInlineConfigParser(
86                  getPath("InputJavadocBlockTagLocationIncorrect.java"), expected);
87      }
88  
89      @Test
90      public void testCustomTags() throws Exception {
91          final String[] expected = {
92              "14: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "apiNote"),
93              "14: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "implNote"),
94              "14: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "implSpec"),
95              "16: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "apiNote"),
96              "17: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "implNote"),
97              "18: " + getCheckMessage(MSG_BLOCK_TAG_LOCATION, "implSpec"),
98          };
99          verifyWithInlineConfigParser(
100                 getPath("InputJavadocBlockTagLocationCustomTags.java"), expected);
101     }
102 
103 }