@@ -59,6 +59,10 @@ private static String getPath(String filename) {
5959 return "src/test/resources/com/puppycrawl/tools/checkstyle/" + filename ;
6060 }
6161
62+ private static String getNonCompilablePath (String filename ) {
63+ return "src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/" + filename ;
64+ }
65+
6266 private static String getFilePath (String filename ) throws IOException {
6367 return new File (getPath (filename )).getCanonicalPath ();
6468 }
@@ -143,11 +147,11 @@ public void testNonExistingConfigFile()
143147 exit .checkAssertionAfterwards (new Assertion () {
144148 @ Override
145149 public void checkAssertion () {
146- assertEquals (String .format (Locale .ROOT ,
147- "Unable to find: src/main/resources/non_existing_config.xml%n"
148- + "Checkstyle ends with 1 errors.%n" ),
150+ assertEquals (String .format (Locale .ROOT , "Checkstyle ends with 1 errors.%n" ),
149151 systemOut .getLog ());
150- assertEquals ("" , systemErr .getLog ());
152+ final String cause = "com.puppycrawl.tools.checkstyle.api.CheckstyleException:"
153+ + " Unable to find: src/main/resources/non_existing_config.xml" ;
154+ assertTrue (systemErr .getLog ().startsWith (cause ));
151155 }
152156 });
153157 Main .main ("-c" , "src/main/resources/non_existing_config.xml" ,
@@ -172,38 +176,18 @@ public void checkAssertion() {
172176 @ Test
173177 public void testNonExistingClass () throws Exception {
174178 exit .expectSystemExitWithStatus (-2 );
175- final String cause = "Unable to instantiate 'NonExistingClass' class,"
176- + " it is also not possible to instantiate it as"
177- + " com.puppycrawl.tools.checkstyle.checks.annotation.NonExistingClass,"
178- + " com.puppycrawl.tools.checkstyle.checks.blocks.NonExistingClass,"
179- + " com.puppycrawl.tools.checkstyle.checks.coding.NonExistingClass,"
180- + " com.puppycrawl.tools.checkstyle.checks.design.NonExistingClass,"
181- + " com.puppycrawl.tools.checkstyle.checks.header.NonExistingClass,"
182- + " com.puppycrawl.tools.checkstyle.checks.imports.NonExistingClass,"
183- + " com.puppycrawl.tools.checkstyle.checks.indentation.NonExistingClass,"
184- + " com.puppycrawl.tools.checkstyle.checks.javadoc.NonExistingClass,"
185- + " com.puppycrawl.tools.checkstyle.checks.metrics.NonExistingClass,"
186- + " com.puppycrawl.tools.checkstyle.checks.modifier.NonExistingClass,"
187- + " com.puppycrawl.tools.checkstyle.checks.naming.NonExistingClass,"
188- + " com.puppycrawl.tools.checkstyle.checks.regexp.NonExistingClass,"
189- + " com.puppycrawl.tools.checkstyle.checks.sizes.NonExistingClass,"
190- + " com.puppycrawl.tools.checkstyle.checks.whitespace.NonExistingClass,"
191- + " com.puppycrawl.tools.checkstyle.checks.NonExistingClass,"
192- + " com.puppycrawl.tools.checkstyle.filters.NonExistingClass,"
193- + " com.puppycrawl.tools.checkstyle.NonExistingClass."
194- + " Please recheck that class name is specified as canonical name or read"
195- + " how to configure short name usage"
196- + " http://checkstyle.sourceforge.net/config.html#Packages."
197- + " Please also recheck that provided ClassLoader to Checker is configured correctly." ;
198- final String expectedExceptionMessage =
199- String .format (Locale .ROOT , "cannot initialize module TreeWalker - %1$s%n"
200- + "Cause: com.puppycrawl.tools.checkstyle.api.CheckstyleException: %1$s%n"
201- + "Checkstyle ends with 1 errors.%n" , cause );
202179 exit .checkAssertionAfterwards (new Assertion () {
203180 @ Override
204181 public void checkAssertion () {
182+ final String expectedExceptionMessage =
183+ String .format (Locale .ROOT , "Checkstyle ends with 1 errors.%n" );
205184 assertEquals (expectedExceptionMessage , systemOut .getLog ());
206- assertEquals ("" , systemErr .getLog ());
185+
186+ final String cause = "com.puppycrawl.tools.checkstyle.api.CheckstyleException:"
187+ + " cannot initialize module TreeWalker - "
188+ + "Unable to instantiate 'NonExistingClass' class, "
189+ + "it is also not possible to instantiate it as " ;
190+ assertTrue (systemErr .getLog ().startsWith (cause ));
207191 }
208192 });
209193
@@ -434,16 +418,14 @@ public void testExistingIncorrectConfigFile()
434418 exit .checkAssertionAfterwards (new Assertion () {
435419 @ Override
436420 public void checkAssertion () {
437- assertTrue (systemOut .getLog ().startsWith (String .format (Locale .ROOT ,
438- "unable to parse configuration stream"
439- + " - Content is not allowed in prolog.:7:1%n"
440- + "Cause: org.xml.sax.SAXParseException; systemId: file:" )));
441- assertTrue (systemOut .getLog ().endsWith (String .format (Locale .ROOT ,
442- "com/puppycrawl/tools/checkstyle/config-Incorrect.xml;"
443- + " lineNumber: 7; columnNumber: 1; "
444- + "Content is not allowed in prolog.%n"
445- + "Checkstyle ends with 1 errors.%n" )));
446- assertEquals ("" , systemErr .getLog ());
421+ final String output = String .format (Locale .ROOT ,
422+ "Checkstyle ends with 1 errors.%n" );
423+ assertEquals (output , systemOut .getLog ());
424+ final String errorOuput = String .format (Locale .ROOT ,
425+ "com.puppycrawl.tools.checkstyle.api."
426+ + "CheckstyleException: unable to parse configuration stream"
427+ + " - Content is not allowed in prolog.:7:1%n" );
428+ assertTrue (systemErr .getLog ().startsWith (errorOuput ));
447429 }
448430 });
449431 Main .main ("-c" , getPath ("config-Incorrect.xml" ),
@@ -545,7 +527,7 @@ public void testExistingDirectoryWithViolations() throws Exception {
545527 });
546528
547529 Main .main ("-c" , getPath ("config-filelength.xml" ),
548- getPath ("checks/metrics" ));
530+ getPath ("checks/metrics" ));
549531 }
550532
551533 @ Test
@@ -577,4 +559,30 @@ public void testListFilesDirectoryWithNull() throws Exception {
577559 final List <File > result = (List <File >) method .invoke (null , fileMock );
578560 assertEquals (0 , result .size ());
579561 }
562+
563+ @ Test
564+ public void testFileReferenceDuringException () throws Exception {
565+ exit .expectSystemExitWithStatus (-2 );
566+ exit .checkAssertionAfterwards (new Assertion () {
567+ @ Override
568+ public void checkAssertion () {
569+ final String expectedExceptionMessage =
570+ String .format (Locale .ROOT , "Starting audit...%n"
571+ + "Checkstyle ends with 1 errors.%n" );
572+ assertEquals (expectedExceptionMessage , systemOut .getLog ());
573+
574+ final String exceptionFirstLine = String .format (Locale .ROOT ,
575+ "com.puppycrawl.tools.checkstyle.api."
576+ + "CheckstyleException: Exception was thrown while processing "
577+ + new File (getNonCompilablePath ("InputIncorrectClass.java" )).getPath ()
578+ + "%n" );
579+ assertTrue (systemErr .getLog ().startsWith (exceptionFirstLine ));
580+ }
581+ });
582+
583+ // We put xml as source to cause parse excepion
584+ Main .main ("-c" , getPath ("config-classname.xml" ),
585+ getNonCompilablePath ("InputIncorrectClass.java" ));
586+ }
587+
580588}
0 commit comments