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.SeparatorWrapCheck.MSG_LINE_NEW;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck.MSG_LINE_PREVIOUS;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import org.junit.Assert;
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 SeparatorWrapCheckTest
37          extends AbstractModuleTestSupport {
38      @Override
39      protected String getPackageLocation() {
40          return "com/puppycrawl/tools/checkstyle/checks/whitespace/separatorwrap";
41      }
42  
43      @Test
44      public void testDot()
45              throws Exception {
46          final DefaultConfiguration checkConfig = createModuleConfig(SeparatorWrapCheck.class);
47          checkConfig.addAttribute("option", "NL");
48          checkConfig.addAttribute("tokens", "DOT");
49          final String[] expected = {
50              "31:10: " + getCheckMessage(MSG_LINE_NEW, "."),
51          };
52          verify(checkConfig, getPath("InputSeparatorWrapForTestDot.java"), expected);
53      }
54  
55      @Test
56      public void testComma() throws Exception {
57          final DefaultConfiguration checkConfig = createModuleConfig(SeparatorWrapCheck.class);
58          checkConfig.addAttribute("option", "EOL");
59          checkConfig.addAttribute("tokens", "COMMA");
60          final String[] expected = {
61              "39:17: " + getCheckMessage(MSG_LINE_PREVIOUS, ","),
62          };
63          verify(checkConfig, getPath("InputSeparatorWrapForTestComma.java"), expected);
64      }
65  
66      @Test
67      public void testMethodRef() throws Exception {
68          final DefaultConfiguration checkConfig = createModuleConfig(SeparatorWrapCheck.class);
69          checkConfig.addAttribute("option", "NL");
70          checkConfig.addAttribute("tokens", "METHOD_REF");
71          final String[] expected = {
72              "17:56: " + getCheckMessage(MSG_LINE_NEW, "::"),
73          };
74          verify(checkConfig, getPath("InputSeparatorWrapForTestMethodRef.java"), expected);
75      }
76  
77      @Test
78      public void testGetDefaultTokens() {
79          final SeparatorWrapCheck separatorWrapCheckObj = new SeparatorWrapCheck();
80          final int[] actual = separatorWrapCheckObj.getDefaultTokens();
81          final int[] expected = {
82              TokenTypes.DOT,
83              TokenTypes.COMMA,
84          };
85          Assert.assertArrayEquals("Invalid default tokens", expected, actual);
86      }
87  
88      @Test
89      public void testInvalidOption() throws Exception {
90          final DefaultConfiguration checkConfig = createModuleConfig(SeparatorWrapCheck.class);
91          checkConfig.addAttribute("option", "invalid_option");
92  
93          try {
94              final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
95  
96              verify(checkConfig, getPath("InputSeparatorWrapForInvalidOption.java"), expected);
97              fail("exception expected");
98          }
99          catch (CheckstyleException ex) {
100             final String messageStart = "cannot initialize module "
101                 + "com.puppycrawl.tools.checkstyle.TreeWalker - Cannot set property 'option' to "
102                 + "'invalid_option' in module";
103             assertTrue("Invalid exception message, should start with: " + messageStart,
104                 ex.getMessage().startsWith(messageStart));
105         }
106     }
107 
108     @Test
109     public void testEllipsis() throws Exception {
110         final DefaultConfiguration checkConfig = createModuleConfig(SeparatorWrapCheck.class);
111         checkConfig.addAttribute("option", "EOL");
112         checkConfig.addAttribute("tokens", "ELLIPSIS");
113         final String[] expected = {
114             "11:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "..."),
115         };
116         verify(checkConfig, getPath("InputSeparatorWrapForEllipsis.java"), expected);
117     }
118 
119     @Test
120     public void testArrayDeclarator() throws Exception {
121         final DefaultConfiguration checkConfig = createModuleConfig(SeparatorWrapCheck.class);
122         checkConfig.addAttribute("option", "EOL");
123         checkConfig.addAttribute("tokens", "ARRAY_DECLARATOR");
124         final String[] expected = {
125             "9:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "["),
126         };
127         verify(checkConfig, getPath("InputSeparatorWrapForArrayDeclarator.java"), expected);
128     }
129 
130 }