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.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         };
102         verifyWithInlineConfigParser(getPath("InputNonEmptyAtclauseDescriptionTwo.java"), expected);
103     }
104 
105     /**
106      * This tests that the check does not fail when the input file contains malformed characters
107      * for a particular charset. The test file contains the character {@code ü} which is not
108      * mappable to US-ASCII. It makes sure that malformed characters than cannot be mapped are
109      * replaced with the default replacement character using {@link CodingErrorAction#REPLACE}.
110      *
111      * @throws Exception exception
112      */
113     @SuppressForbidden
114     @Test
115     public void testDecoderOnMalformedInput() throws Exception {
116         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
117         final DefaultConfiguration checkConfig =
118                 createModuleConfig(NonEmptyAtclauseDescriptionCheck.class);
119 
120         final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
121         treeWalkerConfig.addChild(checkConfig);
122 
123         final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig);
124         checkerConfig.addChild(treeWalkerConfig);
125         checkerConfig.addProperty("charset", "US-ASCII");
126 
127         verify(checkerConfig,
128                 getPath("InputNonEmptyAtclauseDescriptionDifferentCharset.java"), expected);
129     }
130 }