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.OperatorWrapCheck.MSG_LINE_NEW;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck.MSG_LINE_PREVIOUS;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
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.CheckstyleException;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
33  
34  public class OperatorWrapCheckTest
35      extends AbstractModuleTestSupport {
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/whitespace/operatorwrap";
39      }
40  
41      @Test
42      public void testDefault()
43              throws Exception {
44          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
45          final String[] expected = {
46              "17:19: " + getCheckMessage(MSG_LINE_NEW, "+"),
47              "18:15: " + getCheckMessage(MSG_LINE_NEW, "-"),
48              "26:18: " + getCheckMessage(MSG_LINE_NEW, "&&"),
49              "48:30: " + getCheckMessage(MSG_LINE_NEW, "&"),
50              "61:30: " + getCheckMessage(MSG_LINE_NEW, "&"),
51          };
52          verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
53      }
54  
55      @Test
56      public void testOpWrapEol()
57              throws Exception {
58          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
59          checkConfig.addAttribute("option", WrapOption.EOL.toString());
60          final String[] expected = {
61              "20:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "-"),
62              "24:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "&&"),
63              "29:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "&&"),
64          };
65          verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
66      }
67  
68      @Test
69      public void testNonDefOpsDefault()
70              throws Exception {
71          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
72          checkConfig.addAttribute("tokens", "METHOD_REF");
73          final String[] expected = {
74              "33:33: " + getCheckMessage(MSG_LINE_NEW, "::"),
75          };
76          verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
77      }
78  
79      @Test
80      public void testNonDefOpsWrapEol()
81              throws Exception {
82          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
83          checkConfig.addAttribute("tokens", "METHOD_REF");
84          checkConfig.addAttribute("option", WrapOption.EOL.toString());
85          final String[] expected = {
86              "31:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "::"),
87              "36:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "::"),
88          };
89          verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
90      }
91  
92      @Test
93      public void testAssignEol()
94              throws Exception {
95          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
96          checkConfig.addAttribute("tokens", "ASSIGN");
97          checkConfig.addAttribute("option", WrapOption.EOL.toString());
98          final String[] expected = {
99              "42:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "="),
100         };
101         verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
102     }
103 
104     @Test
105     public void testInvalidOption() throws Exception {
106         final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
107         checkConfig.addAttribute("option", "invalid_option");
108 
109         try {
110             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
111 
112             verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
113             fail("exception expected");
114         }
115         catch (CheckstyleException ex) {
116             final String messageStart = "cannot initialize module "
117                 + "com.puppycrawl.tools.checkstyle.TreeWalker - Cannot set property 'option' to "
118                 + "'invalid_option' in module";
119             assertTrue("Invalid exception message, should start with: " + messageStart,
120                 ex.getMessage().startsWith(messageStart));
121         }
122     }
123 }