View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.NonEmptyAtclauseDescriptionCheck.MSG_KEY;
24  
25  import java.nio.charset.CodingErrorAction;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.TreeWalker;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34  import de.thetaphi.forbiddenapis.SuppressForbidden;
35  
36  public class NonEmptyAtclauseDescriptionCheckTest
37          extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/javadoc/nonemptyatclausedescription";
42      }
43  
44      @Test
45      public void testGetAcceptableTokens() {
46          final NonEmptyAtclauseDescriptionCheck checkObj =
47              new NonEmptyAtclauseDescriptionCheck();
48          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN};
49          assertWithMessage("Default acceptable tokens are invalid")
50                  .that(checkObj.getAcceptableTokens())
51                  .isEqualTo(expected);
52      }
53  
54      @Test
55      public void testGetRequiredTokens() {
56          final NonEmptyAtclauseDescriptionCheck checkObj =
57              new NonEmptyAtclauseDescriptionCheck();
58          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN};
59          assertWithMessage("Default required tokens are invalid")
60                  .that(checkObj.getRequiredTokens())
61                  .isEqualTo(expected);
62      }
63  
64      @Test
65      public void testCheckOne() throws Exception {
66          final String[] expected = {
67              // this is a case with description that is sequences of spaces
68              "37: " + getCheckMessage(MSG_KEY),
69              // this is a case with description that is sequences of spaces
70              "38: " + getCheckMessage(MSG_KEY),
71              // this is a case with description that is sequences of spaces
72              "39: " + getCheckMessage(MSG_KEY),
73              // this is a case with description that is sequences of spaces
74              "50: " + getCheckMessage(MSG_KEY),
75              // this is a case with description that is sequences of spaces
76              "51: " + getCheckMessage(MSG_KEY),
77              // this is a case with description that is sequences of spaces
78              "52: " + getCheckMessage(MSG_KEY),
79              "92: " + getCheckMessage(MSG_KEY),
80              "93: " + getCheckMessage(MSG_KEY),
81              "94: " + getCheckMessage(MSG_KEY),
82              "95: " + getCheckMessage(MSG_KEY),
83              "96: " + getCheckMessage(MSG_KEY),
84              "97: " + getCheckMessage(MSG_KEY),
85          };
86          verifyWithInlineConfigParser(getPath("InputNonEmptyAtclauseDescriptionOne.java"), expected);
87      }
88  
89      @Test
90      public void testCheckTwo() throws Exception {
91          final String[] expected = {
92              "16: " + getCheckMessage(MSG_KEY),
93              "17: " + getCheckMessage(MSG_KEY),
94              "18: " + getCheckMessage(MSG_KEY),
95              "19: " + getCheckMessage(MSG_KEY),
96              "20: " + getCheckMessage(MSG_KEY),
97              "51: " + getCheckMessage(MSG_KEY),
98              "60: " + getCheckMessage(MSG_KEY),
99              "75: " + getCheckMessage(MSG_KEY),
100             "77: " + getCheckMessage(MSG_KEY),
101             "88: " + getCheckMessage(MSG_KEY),
102             "97: " + getCheckMessage(MSG_KEY),
103         };
104         verifyWithInlineConfigParser(getPath("InputNonEmptyAtclauseDescriptionTwo.java"), expected);
105     }
106 
107     /**
108      * This tests that the check does not fail when the input file contains malformed characters
109      * for a particular charset. The test file contains the character {@code ü} which is not
110      * mappable to US-ASCII. It makes sure that malformed characters than cannot be mapped are
111      * replaced with the default replacement character using {@link CodingErrorAction#REPLACE}.
112      *
113      * @throws Exception exception
114      */
115     @SuppressForbidden
116     @Test
117     public void testDecoderOnMalformedInput() throws Exception {
118         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
119         final DefaultConfiguration checkConfig =
120                 createModuleConfig(NonEmptyAtclauseDescriptionCheck.class);
121 
122         final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
123         treeWalkerConfig.addChild(checkConfig);
124 
125         final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig);
126         checkerConfig.addChild(treeWalkerConfig);
127         checkerConfig.addProperty("charset", "US-ASCII");
128 
129         verify(checkerConfig,
130                 getPath("InputNonEmptyAtclauseDescriptionDifferentCharset.java"), expected);
131     }
132 
133     @Test
134     public void testExample3() throws Exception {
135         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
136         verifyWithInlineConfigParser(
137                 getPath("InputNonEmptyAtclauseDescriptionThree.java"), expected);
138     }
139 }