1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.ant;
21
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.net.URL;
32 import java.nio.charset.StandardCharsets;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.apache.commons.io.FileUtils;
39 import org.apache.tools.ant.AntClassLoader;
40 import org.apache.tools.ant.BuildException;
41 import org.apache.tools.ant.Project;
42 import org.apache.tools.ant.types.FileSet;
43 import org.apache.tools.ant.types.Path;
44 import org.apache.tools.ant.types.Reference;
45 import org.apache.tools.ant.types.resources.FileResource;
46 import org.junit.Test;
47 import org.powermock.reflect.Whitebox;
48
49 import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
50 import com.puppycrawl.tools.checkstyle.DefaultLogger;
51 import com.puppycrawl.tools.checkstyle.Definitions;
52 import com.puppycrawl.tools.checkstyle.XMLLogger;
53 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
54 import com.puppycrawl.tools.checkstyle.internal.testmodules.CheckerStub;
55 import com.puppycrawl.tools.checkstyle.internal.testmodules.TestRootModuleChecker;
56
57 public class CheckstyleAntTaskTest extends AbstractPathTestSupport {
58
59 private static final String FLAWLESS_INPUT =
60 "InputCheckstyleAntTaskFlawless.java";
61 private static final String VIOLATED_INPUT =
62 "InputCheckstyleAntTaskError.java";
63 private static final String WARNING_INPUT =
64 "InputCheckstyleAntTaskWarning.java";
65 private static final String CONFIG_FILE =
66 "InputCheckstyleAntTaskTestChecks.xml";
67 private static final String CUSTOM_ROOT_CONFIG_FILE =
68 "InputCheckstyleAntTaskConfigCustomRootModule.xml";
69 private static final String NOT_EXISTING_FILE = "target/not_existing.xml";
70 private static final String FAILURE_PROPERTY_VALUE = "myValue";
71
72 @Override
73 protected String getPackageLocation() {
74 return "com/puppycrawl/tools/checkstyle/ant/checkstyleanttask/";
75 }
76
77 private CheckstyleAntTask getCheckstyleAntTask() throws IOException {
78 return getCheckstyleAntTask(CONFIG_FILE);
79 }
80
81 private CheckstyleAntTask getCheckstyleAntTask(String configFile) throws IOException {
82 final CheckstyleAntTask antTask = new CheckstyleAntTask();
83 antTask.setConfig(getPath(configFile));
84 antTask.setProject(new Project());
85 return antTask;
86 }
87
88 @Test
89 public final void testDefaultFlawless() throws IOException {
90 TestRootModuleChecker.reset();
91 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
92 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
93 antTask.execute();
94
95 assertTrue("Checker is not processed",
96 TestRootModuleChecker.isProcessed());
97 }
98
99 @Test
100 public final void testPathsOneFile() throws IOException {
101
102 TestRootModuleChecker.reset();
103
104 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
105 final FileSet examinationFileSet = new FileSet();
106 examinationFileSet.setFile(new File(getPath(FLAWLESS_INPUT)));
107 final Path sourcePath = new Path(antTask.getProject());
108 sourcePath.addFileset(examinationFileSet);
109 antTask.addPath(sourcePath);
110
111
112 antTask.execute();
113
114
115 assertTrue("Checker is not processed",
116 TestRootModuleChecker.isProcessed());
117 final List<File> filesToCheck = TestRootModuleChecker.getFilesToCheck();
118 assertThat("There are more files to check than expected",
119 filesToCheck.size(), is(1));
120 assertThat("The path of file differs from expected",
121 filesToCheck.get(0).getAbsolutePath(), is(getPath(FLAWLESS_INPUT)));
122 }
123
124 @Test
125 public final void testPathsFileWithLogVerification() throws IOException {
126
127 TestRootModuleChecker.reset();
128 final CheckstyleAntTaskLogStub antTask = new CheckstyleAntTaskLogStub();
129 antTask.setConfig(getPath(CUSTOM_ROOT_CONFIG_FILE));
130 antTask.setProject(new Project());
131 final FileSet examinationFileSet = new FileSet();
132 examinationFileSet.setFile(new File(getPath(FLAWLESS_INPUT)));
133 final Path sourcePath = new Path(antTask.getProject());
134 sourcePath.addFileset(examinationFileSet);
135 antTask.addPath(sourcePath);
136 antTask.addPath(new Path(new Project()));
137
138
139 antTask.execute();
140
141
142 final List<String> loggedMessages = antTask.getLoggedMessages();
143
144 assertEquals("Scanning path was not logged", 1, loggedMessages.stream().filter(
145 msg -> msg.startsWith("1) Scanning path")).count());
146
147 assertEquals("Scanning path was not logged", 1, loggedMessages.stream().filter(
148 msg -> msg.startsWith("1) Adding 1 files from path")).count());
149
150 assertEquals("Scanning empty was logged", 0, loggedMessages.stream().filter(
151 msg -> msg.startsWith("2) Adding 0 files from path ")).count());
152
153 assertTrue("Checker is not processed",
154 TestRootModuleChecker.isProcessed());
155 final List<File> filesToCheck = TestRootModuleChecker.getFilesToCheck();
156 assertThat("There are more files to check than expected",
157 filesToCheck.size(), is(1));
158 assertThat("The path of file differs from expected",
159 filesToCheck.get(0).getAbsolutePath(), is(getPath(FLAWLESS_INPUT)));
160 }
161
162 @Test
163 public final void testPathsDirectoryWithNestedFile() throws IOException {
164
165 TestRootModuleChecker.reset();
166
167 final CheckstyleAntTaskLogStub antTask = new CheckstyleAntTaskLogStub();
168 antTask.setConfig(getPath(CUSTOM_ROOT_CONFIG_FILE));
169 antTask.setProject(new Project());
170
171 final FileResource fileResource = new FileResource(
172 antTask.getProject(), getPath(""));
173 final Path sourcePath = new Path(antTask.getProject());
174 sourcePath.add(fileResource);
175 antTask.addPath(sourcePath);
176
177
178 antTask.execute();
179
180
181 assertTrue("Checker is not processed",
182 TestRootModuleChecker.isProcessed());
183 final List<File> filesToCheck = TestRootModuleChecker.getFilesToCheck();
184 assertThat("There are more files to check than expected",
185 filesToCheck.size(), is(9));
186 assertThat("The path of file differs from expected",
187 filesToCheck.get(6).getAbsolutePath(), is(getPath(FLAWLESS_INPUT)));
188 assertEquals("Amount of logged messages in unexpected",
189 8, antTask.getLoggedMessages().size());
190 }
191
192 @Test
193 public final void testCustomRootModule() throws IOException {
194 TestRootModuleChecker.reset();
195
196 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
197 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
198 antTask.execute();
199
200 assertTrue("Checker is not processed",
201 TestRootModuleChecker.isProcessed());
202 }
203
204 @Test
205 public final void testFileSet() throws IOException {
206 TestRootModuleChecker.reset();
207 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
208 final FileSet examinationFileSet = new FileSet();
209 examinationFileSet.setFile(new File(getPath(FLAWLESS_INPUT)));
210 antTask.addFileset(examinationFileSet);
211 antTask.execute();
212
213 assertTrue("Checker is not processed",
214 TestRootModuleChecker.isProcessed());
215 final List<File> filesToCheck = TestRootModuleChecker.getFilesToCheck();
216 assertThat("There are more files to check than expected",
217 filesToCheck.size(), is(1));
218 assertThat("The path of file differs from expected",
219 filesToCheck.get(0).getAbsolutePath(), is(getPath(FLAWLESS_INPUT)));
220 }
221
222 @Test
223 public final void testNoConfigFile() throws IOException {
224 final CheckstyleAntTask antTask = new CheckstyleAntTask();
225 antTask.setProject(new Project());
226 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
227 try {
228 antTask.execute();
229 fail("Exception is expected");
230 }
231 catch (BuildException ex) {
232 assertEquals("Error message is unexpected",
233 "Must specify 'config'.", ex.getMessage());
234 }
235 }
236
237 @Test
238 public final void testNonExistentConfig() throws IOException {
239 final CheckstyleAntTask antTask = new CheckstyleAntTask();
240 antTask.setConfig(getPath(NOT_EXISTING_FILE));
241 antTask.setProject(new Project());
242 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
243 try {
244 antTask.execute();
245 fail("Exception is expected");
246 }
247 catch (BuildException ex) {
248 assertTrue("Error message is unexpected",
249 ex.getMessage().startsWith("Unable to create Root Module: config"));
250 }
251 }
252
253 @Test
254 public final void testEmptyConfigFile() throws IOException {
255 final CheckstyleAntTask antTask = new CheckstyleAntTask();
256 antTask.setConfig(getPath("InputCheckstyleAntTaskEmptyConfig.xml"));
257 antTask.setProject(new Project());
258 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
259 try {
260 antTask.execute();
261 fail("Exception is expected");
262 }
263 catch (BuildException ex) {
264 assertTrue("Error message is unexpected",
265 ex.getMessage().startsWith("Unable to create Root Module: config"));
266 }
267 }
268
269 @Test
270 public final void testNoFile() throws IOException {
271 final CheckstyleAntTask antTask = getCheckstyleAntTask();
272 try {
273 antTask.execute();
274 fail("Exception is expected");
275 }
276 catch (BuildException ex) {
277 assertEquals("Error message is unexpected",
278 "Must specify at least one of 'file' or nested 'fileset' or 'path'.",
279 ex.getMessage());
280 }
281 }
282
283 @Test
284 public final void testMaxWarningExceeded() throws IOException {
285 final CheckstyleAntTask antTask = getCheckstyleAntTask();
286 antTask.setFile(new File(getPath(WARNING_INPUT)));
287 antTask.setMaxWarnings(0);
288 try {
289 antTask.execute();
290 fail("Exception is expected");
291 }
292 catch (BuildException ex) {
293 assertEquals("Error message is unexpected",
294 "Got 0 errors and 1 warnings.", ex.getMessage());
295 }
296 }
297
298 @Test
299 public final void testMaxErrors() throws IOException {
300 TestRootModuleChecker.reset();
301
302 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
303 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
304 antTask.setMaxErrors(2);
305 antTask.execute();
306
307 assertTrue("Checker is not processed",
308 TestRootModuleChecker.isProcessed());
309 }
310
311 @Test
312 public final void testFailureProperty() throws IOException {
313 final CheckstyleAntTask antTask = new CheckstyleAntTask();
314 antTask.setConfig(getPath(CONFIG_FILE));
315 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
316
317 final Project project = new Project();
318 final String failurePropertyName = "myProperty";
319 project.setProperty(failurePropertyName, FAILURE_PROPERTY_VALUE);
320
321 antTask.setProject(project);
322 antTask.setFailureProperty(failurePropertyName);
323 try {
324 antTask.execute();
325 fail("Exception is expected");
326 }
327 catch (BuildException ignored) {
328 final Map<String, Object> hashtable = project.getProperties();
329 final Object propertyValue = hashtable.get(failurePropertyName);
330 assertEquals("Number of errors is unexpected",
331 "Got 2 errors and 0 warnings.", propertyValue);
332 }
333 }
334
335 @Test
336 public final void testOverrideProperty() throws IOException {
337 TestRootModuleChecker.reset();
338
339 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
340 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
341 final CheckstyleAntTask.Property property = new CheckstyleAntTask.Property();
342 property.setKey("lineLength.severity");
343 property.setValue("ignore");
344 antTask.addProperty(property);
345 antTask.execute();
346
347 assertTrue("Checker is not processed",
348 TestRootModuleChecker.isProcessed());
349 }
350
351 @Test
352 public final void testExecuteIgnoredModules() throws IOException {
353 final CheckstyleAntTask antTask = getCheckstyleAntTask();
354 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
355 antTask.setFailOnViolation(false);
356 antTask.setExecuteIgnoredModules(true);
357
358 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
359 final File outputFile = new File("target/ant_task_plain_output.txt");
360 formatter.setTofile(outputFile);
361 final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
362 formatterType.setValue("plain");
363 formatter.setType(formatterType);
364 formatter.createListener(null);
365
366 antTask.addFormatter(formatter);
367 antTask.execute();
368
369 final LocalizedMessage auditStartedMessage = new LocalizedMessage(1,
370 Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.auditStarted",
371 null, null,
372 getClass(), null);
373 final LocalizedMessage auditFinishedMessage = new LocalizedMessage(1,
374 Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.auditFinished",
375 null, null,
376 getClass(), null);
377
378 final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
379 final String errorMessage = "Content of file with violations differs from expected";
380 assertEquals(errorMessage, auditStartedMessage.getMessage(), output.get(0));
381 assertTrue(errorMessage, output.get(1).startsWith("[WARN]"));
382 assertTrue(errorMessage, output.get(1).endsWith("InputCheckstyleAntTaskError.java:4: "
383 + "@incomplete=Some javadoc [WriteTag]"));
384 assertTrue(errorMessage, output.get(2).startsWith("[ERROR]"));
385 assertTrue(errorMessage, output.get(2).endsWith("InputCheckstyleAntTaskError.java:7: "
386 + "Line is longer than 70 characters (found 80). [LineLength]"));
387 assertTrue(errorMessage, output.get(3).startsWith("[ERROR]"));
388 assertTrue(errorMessage, output.get(3).endsWith("InputCheckstyleAntTaskError.java:9: "
389 + "Line is longer than 70 characters (found 81). [LineLength]"));
390 assertEquals(errorMessage, auditFinishedMessage.getMessage(), output.get(4));
391 }
392
393 @Test
394 public final void testConfigurationByUrl() throws IOException {
395 final CheckstyleAntTask antTask = new CheckstyleAntTask();
396 antTask.setProject(new Project());
397 final URL url = new File(getPath(CONFIG_FILE)).toURI().toURL();
398 antTask.setConfig(url.toString());
399 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
400
401 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
402 final File outputFile = new File("target/ant_task_config_by_url.txt");
403 formatter.setTofile(outputFile);
404 final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
405 formatterType.setValue("plain");
406 formatter.setType(formatterType);
407 formatter.createListener(null);
408 antTask.addFormatter(formatter);
409
410 antTask.execute();
411
412 final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
413 final int sizeOfOutputWithNoViolations = 2;
414 assertEquals("No violations expected", sizeOfOutputWithNoViolations, output.size());
415 }
416
417 @Test
418 public final void testConfigurationByResource() throws IOException {
419 final CheckstyleAntTask antTask = new CheckstyleAntTask();
420 antTask.setProject(new Project());
421 antTask.setConfig(getPath(CONFIG_FILE));
422 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
423
424 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
425 final File outputFile = new File("target/ant_task_config_by_url.txt");
426 formatter.setTofile(outputFile);
427 final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
428 formatterType.setValue("plain");
429 formatter.setType(formatterType);
430 formatter.createListener(null);
431 antTask.addFormatter(formatter);
432
433 antTask.execute();
434
435 final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
436 final int sizeOfOutputWithNoViolations = 2;
437 assertEquals("No violations expected", sizeOfOutputWithNoViolations, output.size());
438 }
439
440 @Test
441 public final void testSimultaneousConfiguration() throws IOException {
442 final File file = new File(getPath(CONFIG_FILE));
443 final URL url = file.toURI().toURL();
444 final String expected = "Attribute 'config' has already been set";
445 try {
446 final CheckstyleAntTask antTask = new CheckstyleAntTask();
447 antTask.setConfig(url.toString());
448 antTask.setConfig(file.toString());
449 fail("Exception is expected");
450 }
451 catch (BuildException ex) {
452 assertEquals("Error message is unexpected",
453 expected, ex.getMessage());
454 }
455 }
456
457 @Test
458 public final void testSetPropertiesFile() throws IOException {
459 TestRootModuleChecker.reset();
460
461 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
462 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
463 antTask.setProperties(new File(getPath(
464 "InputCheckstyleAntTaskCheckstyleAntTest.properties")));
465 antTask.execute();
466
467 assertEquals("Property is not set",
468 "ignore", TestRootModuleChecker.getProperty());
469 }
470
471 @Test
472 public final void testSetPropertiesNonExistentFile() throws IOException {
473 final CheckstyleAntTask antTask = getCheckstyleAntTask();
474 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
475 antTask.setProperties(new File(getPath(NOT_EXISTING_FILE)));
476 try {
477 antTask.execute();
478 fail("Exception is expected");
479 }
480 catch (BuildException ex) {
481 assertTrue("Error message is unexpected",
482 ex.getMessage().startsWith("Error loading Properties file"));
483 }
484 }
485
486 @Test
487 public final void testXmlOutput() throws IOException {
488 final CheckstyleAntTask antTask = getCheckstyleAntTask();
489 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
490 antTask.setFailOnViolation(false);
491 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
492 final File outputFile = new File("target/log.xml");
493 formatter.setTofile(outputFile);
494 final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
495 formatterType.setValue("xml");
496 formatter.setType(formatterType);
497 antTask.addFormatter(formatter);
498 antTask.execute();
499
500 final List<String> expected = FileUtils.readLines(
501 new File(getPath("ExpectedCheckstyleAntTaskXmlOutput.xml")),
502 StandardCharsets.UTF_8);
503 final List<String> actual = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
504 for (int i = 0; i < expected.size(); i++) {
505 final String line = expected.get(i);
506 if (!line.startsWith("<checkstyle version") && !line.startsWith("<file")) {
507 assertEquals("Content of file with violations differs from expected",
508 line, actual.get(i));
509 }
510 }
511 }
512
513 @Test
514 public final void testCreateListenerException() throws IOException {
515 final CheckstyleAntTask antTask = getCheckstyleAntTask();
516 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
517 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
518 final File outputFile = new File("target/");
519 formatter.setTofile(outputFile);
520 antTask.addFormatter(formatter);
521 try {
522 antTask.execute();
523 fail("Exception is expected");
524 }
525 catch (BuildException ex) {
526 assertTrue("Error message is unexpected",
527 ex.getMessage().startsWith("Unable to create listeners: formatters"));
528 }
529 }
530
531 @Test
532 public final void testCreateListenerExceptionWithXmlLogger() throws IOException {
533 final CheckstyleAntTask antTask = getCheckstyleAntTask();
534 antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
535 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
536 final File outputFile = new File("target/");
537 formatter.setTofile(outputFile);
538 final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
539 formatterType.setValue("xml");
540 formatter.setType(formatterType);
541 antTask.addFormatter(formatter);
542 try {
543 antTask.execute();
544 fail("Exception is expected");
545 }
546 catch (BuildException ex) {
547 assertTrue("Error message is unexpected",
548 ex.getMessage().startsWith("Unable to create listeners: formatters"));
549 }
550 }
551
552 @Test
553 public void testSetInvalidType() {
554 final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
555 try {
556 formatterType.setValue("foo");
557 fail("Exception is expected");
558 }
559 catch (BuildException ex) {
560 assertEquals("Error message is unexpected",
561 "foo is not a legal value for this attribute", ex.getMessage());
562 }
563 }
564
565 @Test
566 public void testSetClassName() {
567 final String customName = "customName";
568 final CheckstyleAntTask.Listener listener = new CheckstyleAntTask.Listener();
569 listener.setClassname(customName);
570 assertEquals("Class name is unexpected",
571 customName, listener.getClassname());
572 }
573
574 @Test
575 public void testSetFileValueByFile() throws IOException {
576 final String filename = getPath("InputCheckstyleAntTaskCheckstyleAntTest.properties");
577 final CheckstyleAntTask.Property property = new CheckstyleAntTask.Property();
578 property.setFile(new File(filename));
579 assertEquals("File path is unexpected",
580 property.getValue(), new File(filename).getAbsolutePath());
581 }
582
583 @Test
584 public void testDefaultLoggerListener() throws IOException {
585 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
586 formatter.setUseFile(false);
587 assertTrue("Listener instance has unexpected type",
588 formatter.createListener(null) instanceof DefaultLogger);
589 }
590
591 @Test
592 public void testDefaultLoggerListenerWithToFile() throws IOException {
593 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
594 formatter.setUseFile(false);
595 formatter.setTofile(new File("target/"));
596 assertTrue("Listener instance has unexpected type",
597 formatter.createListener(null) instanceof DefaultLogger);
598 }
599
600 @Test
601 public void testXmlLoggerListener() throws IOException {
602 final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
603 formatterType.setValue("xml");
604 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
605 formatter.setType(formatterType);
606 formatter.setUseFile(false);
607 assertTrue("Listener instance has unexpected type",
608 formatter.createListener(null) instanceof XMLLogger);
609 }
610
611 @Test
612 public void testXmlLoggerListenerWithToFile() throws IOException {
613 final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
614 formatterType.setValue("xml");
615 final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
616 formatter.setType(formatterType);
617 formatter.setUseFile(false);
618 formatter.setTofile(new File("target/"));
619 assertTrue("Listener instance has unexpected type",
620 formatter.createListener(null) instanceof XMLLogger);
621 }
622
623 @Test
624 public void testSetClasspath() {
625 final CheckstyleAntTask antTask = new CheckstyleAntTask();
626 final Project project = new Project();
627 final String path1 = "firstPath";
628 final String path2 = "secondPath";
629 antTask.setClasspath(new Path(project, path1));
630 antTask.setClasspath(new Path(project, path2));
631
632 final Path classpath = Whitebox.getInternalState(antTask, "classpath");
633 assertNotNull("Classpath should not be null", classpath);
634 assertTrue("Classpath contain provided path", classpath.toString().contains(path1));
635 assertTrue("Classpath contain provided path", classpath.toString().contains(path2));
636 }
637
638 @Test
639 public void testSetClasspathRef() {
640 final CheckstyleAntTask antTask = new CheckstyleAntTask();
641 antTask.setClasspathRef(new Reference(new Project(), "id"));
642
643 assertNotNull("Classpath should not be null",
644 Whitebox.getInternalState(antTask, "classpath"));
645 }
646
647
648 @Test
649 public void testSetClasspathRef1() {
650 final CheckstyleAntTask antTask = new CheckstyleAntTask();
651 final Project project = new Project();
652 antTask.setClasspath(new Path(project, "firstPath"));
653 antTask.setClasspathRef(new Reference(project, "idXX"));
654
655 try {
656 assertNotNull("Classpath should not be null",
657 Whitebox.getInternalState(antTask, "classpath"));
658 final Path classpath = Whitebox.getInternalState(antTask, "classpath");
659 classpath.list();
660 fail("Exception is expected");
661 }
662 catch (BuildException ex) {
663 assertEquals("unexpected exception message",
664 "Reference idXX not found.", ex.getMessage());
665 }
666 }
667
668 @Test
669 public void testCreateClasspath() {
670 final CheckstyleAntTask antTask = new CheckstyleAntTask();
671
672 assertEquals("Invalid classpath", "", antTask.createClasspath().toString());
673
674 antTask.setClasspath(new Path(new Project(), "/path"));
675
676 assertEquals("Invalid classpath", "", antTask.createClasspath().toString());
677 }
678
679 @Test
680 public void testDestroyed() throws IOException {
681 TestRootModuleChecker.reset();
682
683 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
684 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
685 antTask.setMaxWarnings(0);
686 antTask.execute();
687
688 assertTrue("Checker is not destroyed",
689 TestRootModuleChecker.isDestroyed());
690 }
691
692 @Test
693 public void testMaxWarnings() throws IOException {
694 TestRootModuleChecker.reset();
695
696 final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
697 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
698 antTask.setMaxWarnings(0);
699 antTask.execute();
700
701 assertTrue("Checker is not processed",
702 TestRootModuleChecker.isProcessed());
703 }
704
705 @Test
706 public void testClassloaderInRootModule() throws IOException {
707 TestRootModuleChecker.reset();
708 CheckerStub.reset();
709
710 final CheckstyleAntTask antTask =
711 getCheckstyleAntTask(
712 "InputCheckstyleAntTaskConfigCustomCheckerRootModule.xml");
713 antTask.setFile(new File(getPath(VIOLATED_INPUT)));
714
715 antTask.execute();
716
717 final ClassLoader classLoader = CheckerStub.getClassLoader();
718 assertTrue("Classloader is not set or has invalid type",
719 classLoader instanceof AntClassLoader);
720 }
721
722 private static class CheckstyleAntTaskLogStub extends CheckstyleAntTask {
723
724 private final List<String> loggedMessages = new ArrayList<>();
725
726 @Override
727 public void log(String msg, int msgLevel) {
728 loggedMessages.add(msg);
729 }
730
731 @Override
732 public void log(String msg, Throwable t, int msgLevel) {
733 loggedMessages.add(msg);
734 }
735
736 public List<String> getLoggedMessages() {
737 return Collections.unmodifiableList(loggedMessages);
738 }
739
740 }
741
742 }