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.api;
21  
22  import static org.mockito.Mockito.spy;
23  import static org.mockito.Mockito.times;
24  import static org.mockito.Mockito.verify;
25  
26  import java.io.File;
27  import java.nio.charset.Charset;
28  import java.util.Arrays;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
36  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
37  
38  public class AbstractCheckTest extends AbstractPathTestSupport {
39  
40      @Override
41      protected String getPackageLocation() {
42          return "com/puppycrawl/tools/checkstyle/api/abstractcheck";
43      }
44  
45      @Test
46      public void testGetRequiredTokens() {
47          final AbstractCheck check = new AbstractCheck() {
48              @Override
49              public int[] getDefaultTokens() {
50                  return CommonUtils.EMPTY_INT_ARRAY;
51              }
52  
53              @Override
54              public int[] getAcceptableTokens() {
55                  return getDefaultTokens();
56              }
57  
58              @Override
59              public int[] getRequiredTokens() {
60                  return getDefaultTokens();
61              }
62          };
63          // Eventually it will become clear abstract method
64          Assert.assertArrayEquals("Invalid number of tokens, should be empty",
65                  CommonUtils.EMPTY_INT_ARRAY, check.getRequiredTokens());
66      }
67  
68      @Test
69      public void testGetAcceptable() {
70          final AbstractCheck check = new AbstractCheck() {
71              @Override
72              public int[] getDefaultTokens() {
73                  return CommonUtils.EMPTY_INT_ARRAY;
74              }
75  
76              @Override
77              public int[] getAcceptableTokens() {
78                  return getDefaultTokens();
79              }
80  
81              @Override
82              public int[] getRequiredTokens() {
83                  return getDefaultTokens();
84              }
85          };
86          // Eventually it will become clear abstract method
87          Assert.assertArrayEquals("Invalid number of tokens, should be empty",
88                  CommonUtils.EMPTY_INT_ARRAY, check.getAcceptableTokens());
89      }
90  
91      @Test
92      public void testVisitToken() {
93          final AbstractCheck check = new AbstractCheck() {
94              @Override
95              public int[] getDefaultTokens() {
96                  return CommonUtils.EMPTY_INT_ARRAY;
97              }
98  
99              @Override
100             public int[] getAcceptableTokens() {
101                 return CommonUtils.EMPTY_INT_ARRAY;
102             }
103 
104             @Override
105             public int[] getRequiredTokens() {
106                 return CommonUtils.EMPTY_INT_ARRAY;
107             }
108         };
109         final AbstractCheck checkSpy = spy(check);
110         // Eventually it will become clear abstract method
111         checkSpy.visitToken(null);
112 
113         verify(checkSpy, times(1)).visitToken(null);
114     }
115 
116     @Test
117     public void testGetLine() throws Exception {
118         final AbstractCheck check = new AbstractCheck() {
119             @Override
120             public int[] getDefaultTokens() {
121                 return CommonUtils.EMPTY_INT_ARRAY;
122             }
123 
124             @Override
125             public int[] getAcceptableTokens() {
126                 return getDefaultTokens();
127             }
128 
129             @Override
130             public int[] getRequiredTokens() {
131                 return getDefaultTokens();
132             }
133         };
134         check.setFileContents(new FileContents(new FileText(
135             new File(getPath("InputAbstractCheckTestFileContents.java")),
136             Charset.defaultCharset().name())));
137 
138         Assert.assertEquals("Invalid line content", " * I'm a javadoc", check.getLine(3));
139     }
140 
141     @Test
142     public void testGetTabWidth() {
143         final AbstractCheck check = new AbstractCheck() {
144             @Override
145             public int[] getDefaultTokens() {
146                 return CommonUtils.EMPTY_INT_ARRAY;
147             }
148 
149             @Override
150             public int[] getAcceptableTokens() {
151                 return getDefaultTokens();
152             }
153 
154             @Override
155             public int[] getRequiredTokens() {
156                 return getDefaultTokens();
157             }
158         };
159         final int tabWidth = 4;
160         check.setTabWidth(tabWidth);
161 
162         Assert.assertEquals("Invalid tab width", tabWidth, check.getTabWidth());
163     }
164 
165     @Test
166     public void testGetClassLoader() {
167         final AbstractCheck check = new AbstractCheck() {
168             @Override
169             public int[] getDefaultTokens() {
170                 return CommonUtils.EMPTY_INT_ARRAY;
171             }
172 
173             @Override
174             public int[] getAcceptableTokens() {
175                 return getDefaultTokens();
176             }
177 
178             @Override
179             public int[] getRequiredTokens() {
180                 return getDefaultTokens();
181             }
182         };
183         final ClassLoader classLoader = ClassLoader.getSystemClassLoader();
184         check.setClassLoader(classLoader);
185 
186         Assert.assertEquals("Invalid classloader", classLoader, check.getClassLoader());
187     }
188 
189     @Test
190     public void testGetAcceptableTokens() {
191         final int[] defaultTokens = {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
192         final int[] acceptableTokens = {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
193         final int[] requiredTokens = {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
194         final AbstractCheck check = new AbstractCheck() {
195             @Override
196             public int[] getDefaultTokens() {
197                 return defaultTokens;
198             }
199 
200             @Override
201             public int[] getAcceptableTokens() {
202                 return acceptableTokens;
203             }
204 
205             @Override
206             public int[] getRequiredTokens() {
207                 return requiredTokens;
208             }
209         };
210 
211         Assert.assertArrayEquals("Invalid default tokens",
212                 defaultTokens, check.getDefaultTokens());
213         Assert.assertArrayEquals("Invalid acceptable tokens",
214                 defaultTokens, check.getAcceptableTokens());
215         Assert.assertArrayEquals("Invalid required tokens",
216                 requiredTokens, check.getRequiredTokens());
217     }
218 
219     @Test
220     public void testClearMessages() {
221         final AbstractCheck check = new DummyAbstractCheck();
222 
223         check.log(0, "key", "args");
224         Assert.assertEquals("Invalid message size", 1, check.getMessages().size());
225         check.clearMessages();
226         Assert.assertEquals("Invalid message size", 0, check.getMessages().size());
227     }
228 
229     private static final class DummyAbstractCheck extends AbstractCheck {
230 
231         private static final int[] DUMMY_ARRAY = {6};
232 
233         @Override
234         public int[] getDefaultTokens() {
235             return Arrays.copyOf(DUMMY_ARRAY, 1);
236         }
237 
238         @Override
239         public int[] getAcceptableTokens() {
240             return Arrays.copyOf(DUMMY_ARRAY, 1);
241         }
242 
243         @Override
244         public int[] getRequiredTokens() {
245             return Arrays.copyOf(DUMMY_ARRAY, 1);
246         }
247 
248         @Override
249         protected Map<String, String> getCustomMessages() {
250             final Map<String, String> messages = new HashMap<>();
251             messages.put("key", "value");
252             return messages;
253         }
254 
255     }
256 
257 }