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.EmptyForInitializerPadCheck.MSG_NOT_PRECEDED;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForInitializerPadCheck.MSG_PRECEDED;
24  import static org.junit.Assert.assertArrayEquals;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
36  
37  public class EmptyForInitializerPadCheckTest
38      extends AbstractModuleTestSupport {
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/whitespace/emptyforinitializerpad";
42      }
43  
44      @Test
45      public void testGetRequiredTokens() {
46          final EmptyForInitializerPadCheck checkObj = new EmptyForInitializerPadCheck();
47          final int[] expected = {TokenTypes.FOR_INIT};
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 =
55                  createModuleConfig(EmptyForInitializerPadCheck.class);
56          final String[] expected = {
57              "48:14: " + getCheckMessage(MSG_PRECEDED, ";"),
58          };
59          verify(checkConfig, getPath("InputEmptyForInitializerPadDefaultConfig.java"), expected);
60      }
61  
62      @Test
63      public void testSpaceOption() throws Exception {
64          final DefaultConfiguration checkConfig =
65                  createModuleConfig(EmptyForInitializerPadCheck.class);
66          checkConfig.addAttribute("option", PadOption.SPACE.toString());
67          final String[] expected = {
68              "51:13: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
69          };
70          verify(checkConfig, getPath("InputEmptyForInitializerPad.java"), expected);
71      }
72  
73      @Test
74      public void testGetAcceptableTokens() {
75          final EmptyForInitializerPadCheck emptyForInitializerPadCheckObj =
76              new EmptyForInitializerPadCheck();
77          final int[] actual = emptyForInitializerPadCheckObj.getAcceptableTokens();
78          final int[] expected = {
79              TokenTypes.FOR_INIT,
80          };
81          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
82      }
83  
84      /* Additional test for jacoco, since valueOf()
85       * is generated by javac and jacoco reports that
86       * valueOf() is uncovered.
87       */
88      @Test
89      public void testPadOptionValueOf() {
90          final PadOption option = PadOption.valueOf("NOSPACE");
91          assertEquals("Result of valueOf is invalid", PadOption.NOSPACE, option);
92      }
93  
94      /* Additional test for jacoco, since valueOf()
95       * is generated by javac and jacoco reports that
96       * valueOf() is uncovered.
97       */
98      @Test
99      public void testWrapOptionValueOf() {
100         final WrapOption option = WrapOption.valueOf("EOL");
101         assertEquals("Result of valueOf is invalid", WrapOption.EOL, option);
102     }
103 
104     @Test
105     public void testInvalidOption() throws Exception {
106         final DefaultConfiguration checkConfig =
107                 createModuleConfig(EmptyForInitializerPadCheck.class);
108         checkConfig.addAttribute("option", "invalid_option");
109 
110         try {
111             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
112 
113             verify(checkConfig, getPath("InputEmptyForInitializerPad.java"), expected);
114             fail("exception expected");
115         }
116         catch (CheckstyleException ex) {
117             final String messageStart = "cannot initialize module "
118                 + "com.puppycrawl.tools.checkstyle.TreeWalker - Cannot set property 'option' to "
119                 + "'invalid_option' in module";
120             assertTrue("Invalid exception message, should start with: ",
121                 ex.getMessage().startsWith(messageStart));
122         }
123     }
124 }