Skip to content

Commit d0a73ed

Browse files
marchofGodin
authored andcommitted
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.
1 parent 02682c9 commit d0a73ed

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

org.jacoco.doc/docroot/doc/changes.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ <h3>Fixed bugs</h3>
4444
<li><code>synthetic</code> methods that contain bodies of anonymous functions
4545
in Scala should not be ignored
4646
(GitHub <a href="https://github.com/jacoco/jacoco/issues/912">#912</a>).</li>
47+
<li>To avoid failures with invalid class files report generation now checks
48+
that source references are actually files
49+
(GitHub <a href="https://github.com/jacoco/jacoco/issues/941">#941</a>).</li>
4750
</ul>
4851

4952
<h3>Non-functional Changes</h3>

org.jacoco.report.test/src/org/jacoco/report/DirectorySourceFileLocatorTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,24 @@ public void setup() {
4545
}
4646

4747
@Test
48-
public void testGetSourceFileNegative() throws IOException {
48+
public void getSourceFile_should_return_null_when_source_does_not_exist()
49+
throws IOException {
4950
assertNull(locator.getSourceFile("org/jacoco/example",
5051
"DoesNotExist.java"));
5152
}
5253

5354
@Test
54-
public void testGetSourceFile() throws IOException {
55+
public void getSourceFile_should_return_null_when_source_is_folder()
56+
throws IOException {
57+
final File file = new File(sourceFolder.getRoot(),
58+
"org/jacoco/example");
59+
file.mkdirs();
60+
assertNull(locator.getSourceFile("org/jacoco", "example"));
61+
}
62+
63+
@Test
64+
public void getSourceFile_should_return_content_when_file_exists()
65+
throws IOException {
5566
createFile("org/jacoco/example/Test.java");
5667
final Reader source = locator.getSourceFile("org/jacoco/example",
5768
"Test.java");
@@ -61,8 +72,8 @@ public void testGetSourceFile() throws IOException {
6172
private void createFile(String path) throws IOException {
6273
final File file = new File(sourceFolder.getRoot(), path);
6374
file.getParentFile().mkdirs();
64-
final Writer writer = new OutputStreamWriter(
65-
new FileOutputStream(file), "UTF-8");
75+
final Writer writer = new OutputStreamWriter(new FileOutputStream(file),
76+
"UTF-8");
6677
writer.write("Source");
6778
writer.close();
6879
}

org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public class DirectorySourceFileLocator extends InputStreamSourceFileLocator {
3535
* default encoding
3636
* @param tabWidth
3737
* tab width in source files as number of blanks
38-
*
3938
*/
4039
public DirectorySourceFileLocator(final File directory,
4140
final String encoding, final int tabWidth) {
@@ -44,9 +43,10 @@ public DirectorySourceFileLocator(final File directory,
4443
}
4544

4645
@Override
47-
protected InputStream getSourceStream(final String path) throws IOException {
46+
protected InputStream getSourceStream(final String path)
47+
throws IOException {
4848
final File file = new File(directory, path);
49-
if (file.exists()) {
49+
if (file.isFile()) {
5050
return new FileInputStream(file);
5151
} else {
5252
return null;

0 commit comments

Comments
 (0)