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.header;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck.MSG_MISMATCH;
23  import static com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck.MSG_MISSING;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  import static org.mockito.Matchers.anyObject;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.lang.reflect.InvocationTargetException;
32  import java.lang.reflect.Method;
33  
34  import org.junit.Rule;
35  import org.junit.Test;
36  import org.junit.rules.TemporaryFolder;
37  import org.junit.runner.RunWith;
38  import org.powermock.api.mockito.PowerMockito;
39  import org.powermock.core.classloader.annotations.PrepareForTest;
40  import org.powermock.modules.junit4.PowerMockRunner;
41  import org.powermock.reflect.Whitebox;
42  
43  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
44  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
45  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
46  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
47  
48  @RunWith(PowerMockRunner.class)
49  @PrepareForTest({ HeaderCheck.class, HeaderCheckTest.class, AbstractHeaderCheck.class })
50  public class HeaderCheckTest extends AbstractModuleTestSupport {
51  
52      @Rule
53      public final TemporaryFolder temporaryFolder = new TemporaryFolder();
54  
55      @Override
56      protected String getPackageLocation() {
57          return "com/puppycrawl/tools/checkstyle/checks/header/header";
58      }
59  
60      @Test
61      public void testStaticHeader() throws Exception {
62          final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
63          checkConfig.addAttribute("headerFile", getPath("InputHeaderjava.header"));
64          checkConfig.addAttribute("ignoreLines", "");
65          final String[] expected = {
66              "1: " + getCheckMessage(MSG_MISSING),
67          };
68          verify(checkConfig, getPath("InputHeader.java"), expected);
69      }
70  
71      @Test
72      public void testNoHeader() throws Exception {
73          final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
74  
75          createChecker(checkConfig);
76          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
77          verify(checkConfig, getPath("InputHeaderRegexp.java"), expected);
78      }
79  
80      @Test
81      public void testWhitespaceHeader() throws Exception {
82          final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
83          checkConfig.addAttribute("header", "\n    \n");
84  
85          createChecker(checkConfig);
86          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
87          verify(checkConfig, getPath("InputHeaderRegexp.java"), expected);
88      }
89  
90      @Test
91      public void testNonExistentHeaderFile() throws Exception {
92          final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
93          checkConfig.addAttribute("headerFile", getPath("nonExistent.file"));
94          try {
95              createChecker(checkConfig);
96              fail("CheckstyleException is expected");
97          }
98          catch (CheckstyleException ex) {
99              final String messageStart = "cannot initialize module"
100                 + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
101                 + " - illegal value ";
102             final String causeMessageStart = "Unable to find: ";
103 
104             assertTrue("Invalid exception message, should start with: " + messageStart,
105                 ex.getMessage().startsWith(messageStart));
106             assertTrue("Invalid exception message, should start with: " + causeMessageStart,
107                 ex.getCause().getCause().getCause().getMessage().startsWith(causeMessageStart));
108         }
109     }
110 
111     @Test
112     public void testInvalidCharset() throws Exception {
113         final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
114         checkConfig.addAttribute("headerFile", getPath("InputHeaderjava.header"));
115         checkConfig.addAttribute("charset", "XSO-8859-1");
116         try {
117             createChecker(checkConfig);
118             fail("CheckstyleException is expected");
119         }
120         catch (CheckstyleException ex) {
121             assertEquals("Invalid exception message", "cannot initialize module"
122                     + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
123                     + " - Cannot set property 'charset' to 'XSO-8859-1' in module"
124                     + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck",
125                     ex.getMessage());
126         }
127     }
128 
129     @Test
130     public void testEmptyFilename() throws Exception {
131         final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
132         checkConfig.addAttribute("headerFile", "");
133         try {
134             createChecker(checkConfig);
135             fail("Checker creation should not succeed with invalid headerFile");
136         }
137         catch (CheckstyleException ex) {
138             assertEquals("Invalid exception message", "cannot initialize module"
139                     + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
140                     + " - Cannot set property 'headerFile' to '' in module"
141                     + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck",
142                     ex.getMessage());
143         }
144     }
145 
146     @Test
147     public void testNullFilename() throws Exception {
148         final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
149         checkConfig.addAttribute("headerFile", null);
150         try {
151             createChecker(checkConfig);
152             fail("Checker creation should not succeed with null headerFile");
153         }
154         catch (CheckstyleException ex) {
155             assertEquals("Invalid exception message", "cannot initialize module"
156                     + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
157                     + " - Cannot set property 'headerFile' to 'null' in module"
158                     + " com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck",
159                     ex.getMessage());
160         }
161     }
162 
163     @Test
164     public void testNotMatch() throws Exception {
165         final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
166         checkConfig.addAttribute("headerFile", getPath("InputHeaderjava.header"));
167         checkConfig.addAttribute("ignoreLines", "");
168         final String[] expected = {
169             "2: " + getCheckMessage(MSG_MISMATCH,
170                     "// checkstyle: Checks Java source code for adherence to a set of rules."),
171         };
172         verify(checkConfig, getPath("InputHeaderjava2.header"), expected);
173     }
174 
175     @Test
176     public void testIgnore() throws Exception {
177         final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
178         checkConfig.addAttribute("headerFile", getPath("InputHeaderjava.header"));
179         checkConfig.addAttribute("ignoreLines", "2");
180         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
181         verify(checkConfig, getPath("InputHeaderjava2.header"), expected);
182     }
183 
184     @Test
185     public void testSetHeaderTwice() {
186         final HeaderCheck check = new HeaderCheck();
187         check.setHeader("Header");
188         try {
189             check.setHeader("Header2");
190             fail("ConversionException is expected");
191         }
192         catch (IllegalArgumentException ex) {
193             assertEquals("Invalid exception message", "header has already been set - "
194                     + "set either header or headerFile, not both", ex.getMessage());
195         }
196     }
197 
198     @Test
199     public void testIoExceptionWhenLoadingHeader() throws Exception {
200         final HeaderCheck check = PowerMockito.spy(new HeaderCheck());
201         PowerMockito.doThrow(new IOException("expected exception")).when(check, "loadHeader",
202                 anyObject());
203 
204         try {
205             check.setHeader("header");
206             fail("Exception expected");
207         }
208         catch (IllegalArgumentException ex) {
209             assertTrue("Invalid exception cause", ex.getCause() instanceof IOException);
210             assertEquals("Invalid exception message", "unable to load header", ex.getMessage());
211         }
212     }
213 
214     @Test
215     public void testIoExceptionWhenLoadingHeaderFile() throws Exception {
216         final HeaderCheck check = PowerMockito.spy(new HeaderCheck());
217         PowerMockito.doThrow(new IOException("expected exception")).when(check, "loadHeader",
218                 anyObject());
219 
220         check.setHeaderFile(CommonUtils.getUriByFilename(getPath("InputHeaderRegexp.java")));
221 
222         final Method loadHeaderFile = AbstractHeaderCheck.class.getDeclaredMethod("loadHeaderFile");
223         loadHeaderFile.setAccessible(true);
224         try {
225             loadHeaderFile.invoke(check);
226             fail("Exception expected");
227         }
228         catch (InvocationTargetException ex) {
229             assertTrue("Invalid exception cause", ex.getCause() instanceof CheckstyleException);
230             assertTrue("Invalid exception cause message",
231                 ex.getCause().getMessage().startsWith("unable to load header file "));
232         }
233     }
234 
235     @Test
236     public void testCacheHeaderFile() throws Exception {
237         final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
238         checkConfig.addAttribute("headerFile", getPath("InputHeaderjava.header"));
239 
240         final DefaultConfiguration checkerConfig = createRootConfig(checkConfig);
241         final File cacheFile = temporaryFolder.newFile();
242         checkerConfig.addAttribute("cacheFile", cacheFile.getPath());
243 
244         final String[] expected = {
245             "1: " + getCheckMessage(MSG_MISSING),
246         };
247 
248         verify(checkerConfig, getPath("InputHeader.java"), expected);
249         // One more time to use cache.
250         verify(checkerConfig, getPath("InputHeader.java"), expected);
251 
252     }
253 
254     @Test
255     public void testCacheHeaderWithoutFile() throws Exception {
256         final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
257         checkConfig.addAttribute("header", "Test");
258 
259         final DefaultConfiguration checkerConfig = createRootConfig(checkConfig);
260         final File cacheFile = temporaryFolder.newFile();
261         checkerConfig.addAttribute("cacheFile", cacheFile.getPath());
262 
263         final String[] expected = {
264             "1: " + getCheckMessage(MSG_MISMATCH, "Test"),
265         };
266 
267         verify(checkerConfig, getPath("InputHeader.java"), expected);
268     }
269 
270     @Test
271     public void testIgnoreLinesSorted() throws Exception {
272         final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
273         checkConfig.addAttribute("headerFile", getPath("InputHeaderjava.header"));
274         checkConfig.addAttribute("ignoreLines", "4,2,3");
275         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
276         verify(checkConfig, getPath("InputHeaderjava3.header"), expected);
277     }
278 
279     @Test
280     public void testLoadHeaderFileTwice() throws Exception {
281         final HeaderCheck check = new HeaderCheck();
282         check.setHeader("Header");
283         try {
284             Whitebox.invokeMethod(check, "loadHeaderFile");
285             fail("ConversionException is expected");
286         }
287         catch (IllegalArgumentException ex) {
288             assertEquals("Invalid exception message", "header has already been set - "
289                     + "set either header or headerFile, not both", ex.getMessage());
290         }
291     }
292 }