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.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  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/whitespace/operatorwrap";
40      }
41  
42      @Test
43      public void testDefault()
44              throws Exception {
45          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
46          final String[] expected = {
47              "17:19: " + getCheckMessage(MSG_LINE_NEW, "+"),
48              "18:15: " + getCheckMessage(MSG_LINE_NEW, "-"),
49              "26:18: " + getCheckMessage(MSG_LINE_NEW, "&&"),
50              "48:30: " + getCheckMessage(MSG_LINE_NEW, "&"),
51              "61:30: " + getCheckMessage(MSG_LINE_NEW, "&"),
52          };
53          verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
54      }
55  
56      @Test
57      public void testOpWrapEol()
58              throws Exception {
59          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
60          checkConfig.addAttribute("option", WrapOption.EOL.toString());
61          final String[] expected = {
62              "20:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "-"),
63              "24:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "&&"),
64              "29:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "&&"),
65          };
66          verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
67      }
68  
69      @Test
70      public void testNonDefOpsDefault()
71              throws Exception {
72          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
73          checkConfig.addAttribute("tokens", "METHOD_REF");
74          final String[] expected = {
75              "33:33: " + getCheckMessage(MSG_LINE_NEW, "::"),
76          };
77          verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
78      }
79  
80      @Test
81      public void testNonDefOpsWrapEol()
82              throws Exception {
83          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
84          checkConfig.addAttribute("tokens", "METHOD_REF");
85          checkConfig.addAttribute("option", WrapOption.EOL.toString());
86          final String[] expected = {
87              "31:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "::"),
88              "36:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "::"),
89          };
90          verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
91      }
92  
93      @Test
94      public void testAssignEol()
95              throws Exception {
96          final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
97          checkConfig.addAttribute("tokens", "ASSIGN");
98          checkConfig.addAttribute("option", WrapOption.EOL.toString());
99          final String[] expected = {
100             "42:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "="),
101         };
102         verify(checkConfig, getPath("InputOperatorWrap.java"), expected);
103     }
104 
105     @Test
106     public void testInvalidOption() throws Exception {
107         final DefaultConfiguration checkConfig = createModuleConfig(OperatorWrapCheck.class);
108         checkConfig.addAttribute("option", "invalid_option");
109 
110         try {
111             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
112 
113             verify(checkConfig, getPath("InputOperatorWrap.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: " + messageStart,
121                 ex.getMessage().startsWith(messageStart));
122         }
123     }
124 
125 }