View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.TrailingCommentCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  import static org.junit.Assert.assertEquals;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
33  
34  public class TrailingCommentCheckTest extends AbstractModuleTestSupport {
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/trailingcomment";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final TrailingCommentCheck checkObj = new TrailingCommentCheck();
43          assertArrayEquals("Required tokens array is not empty",
44                  CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
45      }
46  
47      @Test
48      public void testGetAcceptableTokens() {
49          final TrailingCommentCheck checkObj = new TrailingCommentCheck();
50          assertArrayEquals("Acceptable tokens array is not empty",
51                  CommonUtils.EMPTY_INT_ARRAY, checkObj.getAcceptableTokens());
52      }
53  
54      @Test
55      public void testDefaults() throws Exception {
56          final DefaultConfiguration checkConfig = createModuleConfig(TrailingCommentCheck.class);
57          final String[] expected = {
58              "4: " + getCheckMessage(MSG_KEY),
59              "7: " + getCheckMessage(MSG_KEY),
60              "8: " + getCheckMessage(MSG_KEY),
61              "18: " + getCheckMessage(MSG_KEY),
62              "19: " + getCheckMessage(MSG_KEY),
63              "29: " + getCheckMessage(MSG_KEY),
64          };
65          verify(checkConfig, getPath("InputTrailingComment.java"), expected);
66      }
67  
68      @Test
69      public void testLegalComment() throws Exception {
70          final DefaultConfiguration checkConfig = createModuleConfig(TrailingCommentCheck.class);
71          checkConfig.addAttribute("legalComment", "^NOI18N$");
72          final String[] expected = {
73              "4: " + getCheckMessage(MSG_KEY),
74              "7: " + getCheckMessage(MSG_KEY),
75              "8: " + getCheckMessage(MSG_KEY),
76              "18: " + getCheckMessage(MSG_KEY),
77              "19: " + getCheckMessage(MSG_KEY),
78          };
79          verify(checkConfig, getPath("InputTrailingComment.java"), expected);
80      }
81  
82      @Test
83      public void testFormat() throws Exception {
84          final DefaultConfiguration checkConfig = createModuleConfig(TrailingCommentCheck.class);
85          checkConfig.addAttribute("format", "NOT MATCH");
86          final String[] expected = {
87              "4: " + getCheckMessage(MSG_KEY),
88              "5: " + getCheckMessage(MSG_KEY),
89              "6: " + getCheckMessage(MSG_KEY),
90              "7: " + getCheckMessage(MSG_KEY),
91              "8: " + getCheckMessage(MSG_KEY),
92              "13: " + getCheckMessage(MSG_KEY),
93              "14: " + getCheckMessage(MSG_KEY),
94              "15: " + getCheckMessage(MSG_KEY),
95              "18: " + getCheckMessage(MSG_KEY),
96              "19: " + getCheckMessage(MSG_KEY),
97              "26: " + getCheckMessage(MSG_KEY),
98              "29: " + getCheckMessage(MSG_KEY),
99          };
100         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
101     }
102 
103     @Test
104     public void testCallVisitToken() {
105         final TrailingCommentCheck check = new TrailingCommentCheck();
106         try {
107             check.visitToken(new DetailAST());
108             Assert.fail("IllegalStateException is expected");
109         }
110         catch (IllegalStateException ex) {
111             assertEquals("Error message is unexpected",
112                     "visitToken() shouldn't be called.", ex.getMessage());
113         }
114     }
115 }