Skip to content

Commit

Permalink
Issue #2109: CLI should print a flie name where exception is happen
Browse files Browse the repository at this point in the history
  • Loading branch information
romani committed Oct 30, 2015
1 parent 8cdcc50 commit d68a973
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 33 deletions.
6 changes: 6 additions & 0 deletions config/pmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@
<property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration[@Image='PackageObjectFactory']"/>
</properties>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingGenericException">
<properties>
<!-- There is no other way to deliver filename that was under processing -->
<property name="violationSuppressXPath" value="//MethodDeclaration[@Name='process' and ../../..[@Image='Checker']]"/>
</properties>
</rule>

<rule ref="rulesets/java/strings.xml"/>
<rule ref="rulesets/java/sunsecure.xml/MethodReturnsInternalArray">
Expand Down
5 changes: 5 additions & 0 deletions config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
files="TokenTypes.java"
lines="1"/>

<!-- There is no other way to deliver filename that was under processing -->
<suppress checks="IllegalCatch"
files="Checker.java"
lines="279"/>

<!-- we can not change it as, Check name is part of API (used in configurations) -->
<suppress checks="AbbreviationAsWordInName"
files="JavaNCSSCheck.java"
Expand Down
42 changes: 24 additions & 18 deletions src/main/java/com/puppycrawl/tools/checkstyle/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,28 +252,34 @@ public int process(List<File> files) throws CheckstyleException {

// Process each file
for (final File file : files) {
if (!CommonUtils.matchesFileExtension(file, fileExtensions)) {
continue;
}
final String fileName = file.getAbsolutePath();
fireFileStarted(fileName);
final SortedSet<LocalizedMessage> fileMessages = Sets.newTreeSet();
try {
final FileText theText = new FileText(file.getAbsoluteFile(),
charset);
for (final FileSetCheck fsc : fileSetChecks) {
fileMessages.addAll(fsc.process(file, theText));
if (!CommonUtils.matchesFileExtension(file, fileExtensions)) {
continue;
}
final String fileName = file.getAbsolutePath();
fireFileStarted(fileName);
final SortedSet<LocalizedMessage> fileMessages = Sets.newTreeSet();
try {
final FileText theText = new FileText(file.getAbsoluteFile(),
charset);
for (final FileSetCheck fsc : fileSetChecks) {
fileMessages.addAll(fsc.process(file, theText));
}
}
catch (final IOException ioe) {
LOG.debug("IOException occurred.", ioe);
fileMessages.add(new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, "general.exception",
new String[] {ioe.getMessage()}, null, getClass(),
null));
}
fireErrors(fileName, fileMessages);
fireFileFinished(fileName);
}
catch (final IOException ioe) {
LOG.debug("IOException occurred.", ioe);
fileMessages.add(new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, "general.exception",
new String[] {ioe.getMessage()}, null, getClass(),
null));
catch (Exception ex) {
throw new CheckstyleException("Exception happens during processing of "
+ file.getPath(), ex);
}
fireErrors(fileName, fileMessages);
fireFileFinished(fileName);
}

// Finish up
Expand Down
39 changes: 35 additions & 4 deletions src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ private static String getPath(String filename) {
return "src/test/resources/com/puppycrawl/tools/checkstyle/" + filename;
}

private static String getNonCompilablePath(String filename) {
return "src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/" + filename;
}

private static String getFilePath(String filename) throws IOException {
return new File(getPath(filename)).getCanonicalPath();
}
Expand Down Expand Up @@ -439,10 +443,10 @@ public void checkAssertion() {
+ " - Content is not allowed in prolog.:7:1%n"
+ "Cause: org.xml.sax.SAXParseException; systemId: file:")));
assertTrue(systemOut.getLog().endsWith(String.format(Locale.ROOT,
"com/puppycrawl/tools/checkstyle/config-Incorrect.xml;"
+ " lineNumber: 7; columnNumber: 1; "
+ "Content is not allowed in prolog.%n"
+ "Checkstyle ends with 1 errors.%n")));
"com/puppycrawl/tools/checkstyle/config-Incorrect.xml;"
+ " lineNumber: 7; columnNumber: 1; "
+ "Content is not allowed in prolog.%n"
+ "Checkstyle ends with 1 errors.%n")));
assertEquals("", systemErr.getLog());
}
});
Expand Down Expand Up @@ -577,4 +581,31 @@ public void testListFilesDirectoryWithNull() throws Exception {
final List<File> result = (List<File>) method.invoke(null, fileMock);
assertEquals(0, result.size());
}

@Test
public void testFileReferenceDuringException() throws Exception {
exit.expectSystemExitWithStatus(-2);
final String cause = "Cause: com.puppycrawl.tools.checkstyle.api.CheckstyleException:"
+ " NoViableAltException occurred during the analysis of file "
+ getNonCompilablePath("InputIncorrectClass.java.");

final String expectedExceptionMessage =
String.format(Locale.ROOT, "Starting audit...%n"
+ "Exception happens during processing of %1$s%n"
+ "%2$s%n"
+ "Checkstyle ends with 1 errors.%n",
getNonCompilablePath("InputIncorrectClass.java"), cause);
exit.checkAssertionAfterwards(new Assertion() {
@Override
public void checkAssertion() {
assertEquals(expectedExceptionMessage, systemOut.getLog());
assertEquals("", systemErr.getLog());
}
});

// We put xml as source to cause parse excepion
Main.main("-c", getPath("config-classname.xml"),
getNonCompilablePath("InputIncorrectClass.java"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck;

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -164,10 +165,11 @@ public void testWithoutLogErrors() throws Exception {
try {
verify(config, getPath("InputLoadErrors.java"), expected);
}
catch (IllegalStateException ex) {
catch (CheckstyleException ex) {
final IllegalStateException cause = (IllegalStateException) ex.getCause();
assertEquals("Unable to get"
+ " class information for @throws tag 'InvalidExceptionName'.",
ex.getMessage());
+ " class information for @throws tag 'InvalidExceptionName'.",
cause.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,39 @@ private static String[] removeSuppressed(String[] from, String... remove) {
return coll.toArray(new String[coll.size()]);
}

@Test(expected = ConversionException.class)
@Test
public void testInvalidInfluenceFormat() throws Exception {
final DefaultConfiguration filterConfig =
createFilterConfig(SuppressWithNearbyCommentFilter.class);
filterConfig.addAttribute("influenceFormat", "a");
final String[] suppressed = ArrayUtils.EMPTY_STRING_ARRAY;
verifySuppressed(filterConfig, suppressed);

try {
verifySuppressed(filterConfig, suppressed);
}
catch (CheckstyleException ex) {
final ConversionException cause = (ConversionException) ex.getCause();
assertEquals("unable to parse influence"
+ " from 'SUPPRESS CHECKSTYLE MemberNameCheck' using a",
cause.getMessage());
}
}

@Test(expected = ConversionException.class)
@Test
public void testInvalidCheckFormat() throws Exception {
final DefaultConfiguration filterConfig =
createFilterConfig(SuppressWithNearbyCommentFilter.class);
filterConfig.addAttribute("checkFormat", "a[l");
final String[] suppressed = ArrayUtils.EMPTY_STRING_ARRAY;
verifySuppressed(filterConfig, suppressed);

try {
verifySuppressed(filterConfig, suppressed);
}
catch (CheckstyleException ex) {
final ConversionException cause = (ConversionException) ex.getCause();
assertEquals("unable to parse expanded comment a[l",
cause.getMessage());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,39 @@ public void testToStringOfTagClass() {
assertEquals("Tag[line=0; col=1; on=false; text='text']", tag.toString());
}

@Test(expected = ConversionException.class)
@Test
public void testInvalidCheckFormat() throws Exception {
final DefaultConfiguration filterConfig =
createFilterConfig(SuppressionCommentFilter.class);
filterConfig.addAttribute("checkFormat", "e[l");
final String[] suppressed = ArrayUtils.EMPTY_STRING_ARRAY;
verifySuppressed(filterConfig, suppressed);

try {
verifySuppressed(filterConfig, suppressed);
}
catch (CheckstyleException ex) {
final ConversionException cause = (ConversionException) ex.getCause();
assertEquals("unable to parse expanded comment e[l",
cause.getMessage());
}
}

@Test(expected = ConversionException.class)
@Test
public void testInvalidMessageFormat() throws Exception {
final DefaultConfiguration filterConfig =
createFilterConfig(SuppressionCommentFilter.class);
filterConfig.addAttribute("messageFormat", "e[l");
final String[] suppressed = ArrayUtils.EMPTY_STRING_ARRAY;
verifySuppressed(filterConfig, suppressed);

try {
verifySuppressed(filterConfig, suppressed);
}
catch (CheckstyleException ex) {
final ConversionException cause = (ConversionException) ex.getCause();
assertEquals("unable to parse expanded comment e[l",
cause.getMessage());
}

}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!@#$^$^&%5

0 comments on commit d68a973

Please sign in to comment.