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