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.GenericWhitespaceCheck.MSG_WS_FOLLOWED;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.MSG_WS_ILLEGAL_FOLLOW;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.MSG_WS_NOT_PRECEDED;
25  import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.MSG_WS_PRECEDED;
26  import static org.junit.Assert.assertArrayEquals;
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.fail;
29  
30  import org.junit.Test;
31  
32  import antlr.CommonHiddenStreamToken;
33  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
35  import com.puppycrawl.tools.checkstyle.api.DetailAST;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
38  
39  public class GenericWhitespaceCheckTest
40      extends AbstractModuleTestSupport {
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/whitespace/genericwhitespace";
44      }
45  
46      @Test
47      public void testGetRequiredTokens() {
48          final GenericWhitespaceCheck checkObj = new GenericWhitespaceCheck();
49          final int[] expected = {
50              TokenTypes.GENERIC_START,
51              TokenTypes.GENERIC_END,
52          };
53          assertArrayEquals("Default required tokens are invalid",
54              expected, checkObj.getRequiredTokens());
55      }
56  
57      @Test
58      public void testDefault() throws Exception {
59          final DefaultConfiguration checkConfig = createModuleConfig(GenericWhitespaceCheck.class);
60          final String[] expected = {
61              "16:13: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
62              "16:15: " + getCheckMessage(MSG_WS_FOLLOWED, "<"),
63              "16:23: " + getCheckMessage(MSG_WS_PRECEDED, ">"),
64              "16:43: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
65              "16:45: " + getCheckMessage(MSG_WS_FOLLOWED, "<"),
66              "16:53: " + getCheckMessage(MSG_WS_PRECEDED, ">"),
67              "17:13: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
68              "17:15: " + getCheckMessage(MSG_WS_FOLLOWED, "<"),
69              "17:20: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
70              "17:22: " + getCheckMessage(MSG_WS_FOLLOWED, "<"),
71              "17:30: " + getCheckMessage(MSG_WS_PRECEDED, ">"),
72              "17:32: " + getCheckMessage(MSG_WS_FOLLOWED, ">"),
73              "17:32: " + getCheckMessage(MSG_WS_PRECEDED, ">"),
74              "17:52: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
75              "17:54: " + getCheckMessage(MSG_WS_FOLLOWED, "<"),
76              "17:59: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
77              "17:61: " + getCheckMessage(MSG_WS_FOLLOWED, "<"),
78              "17:69: " + getCheckMessage(MSG_WS_PRECEDED, ">"),
79              "17:71: " + getCheckMessage(MSG_WS_FOLLOWED, ">"),
80              "17:71: " + getCheckMessage(MSG_WS_PRECEDED, ">"),
81              "30:17: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "<"),
82              "30:21: " + getCheckMessage(MSG_WS_ILLEGAL_FOLLOW, ">"),
83              "42:21: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
84              "42:30: " + getCheckMessage(MSG_WS_FOLLOWED, ">"),
85              "60:60: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "&"),
86              "63:60: " + getCheckMessage(MSG_WS_FOLLOWED, ">"),
87          };
88          verify(checkConfig, getPath("InputGenericWhitespaceDefault.java"), expected);
89      }
90  
91      @Test
92      public void testAtTheStartOfTheLine() throws Exception {
93          final DefaultConfiguration checkConfig = createModuleConfig(GenericWhitespaceCheck.class);
94          final String[] expected = {
95              "10:1: " + getCheckMessage(MSG_WS_PRECEDED, ">"),
96              "12:1: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
97          };
98          verify(checkConfig, getPath("InputGenericWhitespaceAtStartOfTheLine.java"), expected);
99      }
100 
101     @Test
102     public void testNestedGeneric() throws Exception {
103         final DefaultConfiguration checkConfig = createModuleConfig(GenericWhitespaceCheck.class);
104         final String[] expected = {
105             "11:2: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "&"),
106         };
107         verify(checkConfig, getPath("InputGenericWhitespaceNested.java"), expected);
108     }
109 
110     @Test
111     public void testList() throws Exception {
112         final DefaultConfiguration checkConfig = createModuleConfig(GenericWhitespaceCheck.class);
113         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
114         verify(checkConfig, getPath("InputGenericWhitespaceList.java"), expected);
115     }
116 
117     @Test
118     public void testInnerClass() throws Exception {
119         final DefaultConfiguration checkConfig = createModuleConfig(GenericWhitespaceCheck.class);
120         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
121         verify(checkConfig, getPath("InputGenericWhitespaceInnerClass.java"), expected);
122     }
123 
124     @Test
125     public void testMethodReferences() throws Exception {
126         final DefaultConfiguration checkConfig = createModuleConfig(GenericWhitespaceCheck.class);
127         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
128         verify(checkConfig, getPath("InputGenericWhitespaceMethodRef1.java"), expected);
129     }
130 
131     @Test
132     public void testMethodReferences2() throws Exception {
133         final DefaultConfiguration checkConfig = createModuleConfig(GenericWhitespaceCheck.class);
134         final String[] expected = {
135             "10:70: " + getCheckMessage(MSG_WS_FOLLOWED, ">"),
136         };
137         verify(checkConfig, getPath("InputGenericWhitespaceMethodRef2.java"), expected);
138     }
139 
140     @Test
141     public void testGenericEndsTheLine() throws Exception {
142         final DefaultConfiguration checkConfig = createModuleConfig(GenericWhitespaceCheck.class);
143         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
144         verify(checkConfig, getPath("InputGenericWhitespaceEndsTheLine.java"), expected);
145     }
146 
147     @Test
148     public void testGetAcceptableTokens() {
149         final GenericWhitespaceCheck genericWhitespaceCheckObj = new GenericWhitespaceCheck();
150         final int[] actual = genericWhitespaceCheckObj.getAcceptableTokens();
151         final int[] expected = {
152             TokenTypes.GENERIC_START,
153             TokenTypes.GENERIC_END,
154         };
155         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
156     }
157 
158     @Test
159     public void testWrongTokenType() {
160         final GenericWhitespaceCheck genericWhitespaceCheckObj = new GenericWhitespaceCheck();
161         final DetailAST ast = new DetailAST();
162         ast.initialize(new CommonHiddenStreamToken(TokenTypes.INTERFACE_DEF, "interface"));
163         try {
164             genericWhitespaceCheckObj.visitToken(ast);
165             fail("exception expected");
166         }
167         catch (IllegalArgumentException ex) {
168             assertEquals("Invalid exception message",
169                 "Unknown type interface[0x-1]", ex.getMessage());
170         }
171     }
172 }