View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.PreferCodeOrSnippetJavadocInlineTagCheck.MSG_KEY_MULTI_LINE;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.PreferCodeOrSnippetJavadocInlineTagCheck.MSG_KEY_SINGLE_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 PreferCodeOrSnippetJavadocInlineTagCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return
37              "/com/puppycrawl/tools/checkstyle/checks/javadoc/prefercodeorsnippetjavadocinlinetag";
38      }
39  
40      @Test
41      public void testGetAcceptableTokens() {
42          final PreferCodeOrSnippetJavadocInlineTagCheck checkObj =
43              new PreferCodeOrSnippetJavadocInlineTagCheck();
44          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN};
45          assertWithMessage("Default acceptable tokens are invalid")
46                  .that(checkObj.getAcceptableTokens())
47                  .isEqualTo(expected);
48      }
49  
50      @Test
51      public void testGetRequiredTokens() {
52          final PreferCodeOrSnippetJavadocInlineTagCheck checkObj =
53              new PreferCodeOrSnippetJavadocInlineTagCheck();
54          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN};
55          assertWithMessage("Default required tokens are invalid")
56                  .that(checkObj.getRequiredTokens())
57                  .isEqualTo(expected);
58      }
59  
60      @Test
61      public void testPreTag() throws Exception {
62          final String[] expected = {
63              "14:8: " + getCheckMessage(MSG_KEY_SINGLE_LINE, "pre"),
64              "30:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "pre"),
65              "54:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "pre"),
66              "57:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "pre"),
67          };
68          verifyWithInlineConfigParser(
69              getPath("InputPreferCodeOrSnippetJavadocInlineTagPre.java"), expected);
70      }
71  
72      @Test
73      public void testCodeTag() throws Exception {
74          final String[] expected = {
75              "14:8: " + getCheckMessage(MSG_KEY_SINGLE_LINE, "code"),
76              "30:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "code"),
77              "54:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "code"),
78              "57:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "code"),
79              "67:8: " + getCheckMessage(MSG_KEY_SINGLE_LINE, "code"),
80          };
81          verifyWithInlineConfigParser(
82              getPath("InputPreferCodeOrSnippetJavadocInlineTagCode.java"), expected);
83      }
84  
85      @Test
86      public void testMixTags() throws Exception {
87          final String[] expected = {
88              "14:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "pre"),
89              "31:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "pre"),
90              "48:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "code"),
91              "65:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "pre"),
92          };
93          verifyWithInlineConfigParser(
94              getPath("InputPreferCodeOrSnippetJavadocInlineTagMix.java"), expected);
95      }
96  
97      @Test
98      public void testMixTags2() throws Exception {
99          final String[] expected = {
100             "14:8: " + getCheckMessage(MSG_KEY_SINGLE_LINE, "pre"),
101             "30:8: " + getCheckMessage(MSG_KEY_MULTI_LINE, "pre"),
102         };
103         verifyWithInlineConfigParser(
104             getPath("InputPreferCodeOrSnippetJavadocInlineTagMix2.java"), expected);
105     }
106 
107     @Test
108     public void testOtherTags() throws Exception {
109         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
110         verifyWithInlineConfigParser(
111                 getPath("InputPreferCodeOrSnippetJavadocInlineTagOther.java"), expected);
112     }
113 
114     @Test
115     public void testCompactSourceFile() throws Exception {
116         final String[] expected = {
117             "12:4: " + getCheckMessage(MSG_KEY_SINGLE_LINE, "pre"),
118             "19:4: " + getCheckMessage(MSG_KEY_SINGLE_LINE, "code"),
119             "36:4: " + getCheckMessage(MSG_KEY_MULTI_LINE, "pre"),
120         };
121         verifyWithInlineConfigParser(
122             getNonCompilablePath(
123                 "compact/InputPreferCodeOrSnippetJavadocInlineTagCompactSourceFile.java"),
124                     expected);
125     }
126 
127 }