Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check whether source reference actually is a file #941

Merged
merged 2 commits into from Sep 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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