Skip to content

Commit

Permalink
Check whether source reference actually is a file (#941)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marchof authored and Godin committed Sep 21, 2019
1 parent 02682c9 commit d0a73ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Expand Up @@ -44,6 +44,9 @@ <h3>Fixed bugs</h3>
<li><code>synthetic</code> methods that contain bodies of anonymous functions
in Scala should not be ignored
(GitHub <a href="https://github.com/jacoco/jacoco/issues/912">#912</a>).</li>
<li>To avoid failures with invalid class files report generation now checks
that source references are actually files
(GitHub <a href="https://github.com/jacoco/jacoco/issues/941">#941</a>).</li>
</ul>

<h3>Non-functional Changes</h3>
Expand Down
Expand Up @@ -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");
Expand All @@ -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();
}
Expand Down
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit d0a73ed

Please sign in to comment.