From d0a73edd41a29f4c0ffabae15d7c023a37df976f Mon Sep 17 00:00:00 2001 From: "Marc R. Hoffmann" Date: Sun, 22 Sep 2019 00:57:31 +0200 Subject: [PATCH] Check whether source reference actually is a file (#941) A corner case has been reported where `SourceFile` class file attribute is empty, what causes `FileNotFoundException` with message `Is a directory` during report generation. Instead of throwing exception such cases should be handled as missing file. --- org.jacoco.doc/docroot/doc/changes.html | 3 +++ .../DirectorySourceFileLocatorTest.java | 19 +++++++++++++++---- .../report/DirectorySourceFileLocator.java | 6 +++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/org.jacoco.doc/docroot/doc/changes.html b/org.jacoco.doc/docroot/doc/changes.html index c970039f8e..82042ad001 100644 --- a/org.jacoco.doc/docroot/doc/changes.html +++ b/org.jacoco.doc/docroot/doc/changes.html @@ -44,6 +44,9 @@

Fixed bugs

  • synthetic methods that contain bodies of anonymous functions in Scala should not be ignored (GitHub #912).
  • +
  • To avoid failures with invalid class files report generation now checks + that source references are actually files + (GitHub #941).
  • Non-functional Changes

    diff --git a/org.jacoco.report.test/src/org/jacoco/report/DirectorySourceFileLocatorTest.java b/org.jacoco.report.test/src/org/jacoco/report/DirectorySourceFileLocatorTest.java index a93e5c3a5f..c3fbc82a7f 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/DirectorySourceFileLocatorTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/DirectorySourceFileLocatorTest.java @@ -45,13 +45,24 @@ public void setup() { } @Test - public void testGetSourceFileNegative() throws IOException { + public void getSourceFile_should_return_null_when_source_does_not_exist() + throws IOException { assertNull(locator.getSourceFile("org/jacoco/example", "DoesNotExist.java")); } @Test - public void testGetSourceFile() throws IOException { + public void getSourceFile_should_return_null_when_source_is_folder() + throws IOException { + final File file = new File(sourceFolder.getRoot(), + "org/jacoco/example"); + file.mkdirs(); + assertNull(locator.getSourceFile("org/jacoco", "example")); + } + + @Test + public void getSourceFile_should_return_content_when_file_exists() + throws IOException { createFile("org/jacoco/example/Test.java"); final Reader source = locator.getSourceFile("org/jacoco/example", "Test.java"); @@ -61,8 +72,8 @@ public void testGetSourceFile() throws IOException { private void createFile(String path) throws IOException { final File file = new File(sourceFolder.getRoot(), path); file.getParentFile().mkdirs(); - final Writer writer = new OutputStreamWriter( - new FileOutputStream(file), "UTF-8"); + final Writer writer = new OutputStreamWriter(new FileOutputStream(file), + "UTF-8"); writer.write("Source"); writer.close(); } diff --git a/org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java b/org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java index 79881a72bc..dc6c870d88 100644 --- a/org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java +++ b/org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java @@ -35,7 +35,6 @@ public class DirectorySourceFileLocator extends InputStreamSourceFileLocator { * default encoding * @param tabWidth * tab width in source files as number of blanks - * */ public DirectorySourceFileLocator(final File directory, final String encoding, final int tabWidth) { @@ -44,9 +43,10 @@ public DirectorySourceFileLocator(final File directory, } @Override - protected InputStream getSourceStream(final String path) throws IOException { + protected InputStream getSourceStream(final String path) + throws IOException { final File file = new File(directory, path); - if (file.exists()) { + if (file.isFile()) { return new FileInputStream(file); } else { return null;