View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.whitespace;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForIteratorPadCheck.MSG_WS_FOLLOWED;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForIteratorPadCheck.MSG_WS_NOT_FOLLOWED;
24  import static org.junit.Assert.assertArrayEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import org.junit.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
35  
36  public class EmptyForIteratorPadCheckTest
37      extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/whitespace/emptyforiteratorpad";
42      }
43  
44      @Test
45      public void testGetRequiredTokens() {
46          final EmptyForIteratorPadCheck checkObj = new EmptyForIteratorPadCheck();
47          final int[] expected = {TokenTypes.FOR_ITERATOR};
48          assertArrayEquals("Default required tokens are invalid",
49              expected, checkObj.getRequiredTokens());
50      }
51  
52      @Test
53      public void testDefault() throws Exception {
54          final DefaultConfiguration checkConfig = createModuleConfig(EmptyForIteratorPadCheck.class);
55          final String[] expected = {
56              "27:31: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
57              "43:32: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
58              "55:11: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
59          };
60          verify(checkConfig, getPath("InputEmptyForIteratorPad.java"), expected);
61      }
62  
63      @Test
64      public void testSpaceOption() throws Exception {
65          final DefaultConfiguration checkConfig = createModuleConfig(EmptyForIteratorPadCheck.class);
66          checkConfig.addAttribute("option", PadOption.SPACE.toString());
67          final String[] expected = {
68              "23:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
69          };
70          verify(checkConfig, getPath("InputEmptyForIteratorPad.java"), expected);
71      }
72  
73      @Test
74      public void testGetAcceptableTokens() {
75          final EmptyForIteratorPadCheck emptyForIteratorPadCheckObj = new EmptyForIteratorPadCheck();
76          final int[] actual = emptyForIteratorPadCheckObj.getAcceptableTokens();
77          final int[] expected = {
78              TokenTypes.FOR_ITERATOR,
79          };
80          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
81      }
82  
83      @Test
84      public void testInvalidOption() throws Exception {
85          final DefaultConfiguration checkConfig = createModuleConfig(EmptyForIteratorPadCheck.class);
86          checkConfig.addAttribute("option", "invalid_option");
87  
88          try {
89              final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
90  
91              verify(checkConfig, getPath("InputEmptyForIteratorPad.java"), expected);
92              fail("exception expected");
93          }
94          catch (CheckstyleException ex) {
95              final String messageStart = "cannot initialize module "
96                  + "com.puppycrawl.tools.checkstyle.TreeWalker - Cannot set property 'option' to "
97                  + "'invalid_option' in module";
98              assertTrue("Invalid exception message, should start with: ",
99                  ex.getMessage().startsWith(messageStart));
100         }
101     }
102 
103 }