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