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

Method Analyzer.analyzeClass(InputStream, String) as others should provide context information when unable to read input during analysis #541

Merged
merged 1 commit into from
May 29, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ public void testAnalyzeClass_Broken() throws IOException {
}
}

private static class BrokenInputStream extends InputStream {
@Override
public int read() throws IOException {
throw new IOException();
}
}

/**
* Triggers exception in
* {@link Analyzer#analyzeClass(InputStream, String)}.
*/
@Test
public void testAnalyzeClass_BrokenStream() throws IOException {
try {
analyzer.analyzeClass(new BrokenInputStream(), "BrokenStream");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while analyzing BrokenStream.", e.getMessage());
}
}

@Test
public void testAnalyzeAll_Class() throws IOException {
final int count = analyzer.analyzeAll(
Expand Down Expand Up @@ -165,12 +186,7 @@ public void testAnalyzeAll_EmptyZipEntry() throws IOException {
@Test
public void testAnalyzeAll_Broken() throws IOException {
try {
analyzer.analyzeAll(new InputStream() {
@Override
public int read() throws IOException {
throw new IOException();
}
}, "Test");
analyzer.analyzeAll(new BrokenInputStream(), "Test");
fail("expected exception");
} catch (IOException e) {
assertEquals("Error while analyzing Test.", e.getMessage());
Expand Down
6 changes: 4 additions & 2 deletions org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ public void analyzeClass(final byte[] buffer, final String location)
*/
public void analyzeClass(final InputStream input, final String location)
throws IOException {
final byte[] buffer;
try {
analyzeClass(Java9Support.readFully(input), location);
} catch (final RuntimeException e) {
buffer = Java9Support.readFully(input);
} catch (final IOException e) {
throw analyzerError(location, e);
}
analyzeClass(buffer, location);
}

private IOException analyzerError(final String location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ private Java9Support() {
*/
public static byte[] readFully(final InputStream is)
throws IOException {
if (is == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Godin Why do we need this check specifically here? I think all other APIs like analyzeAll() will result in a NPE for a null InputStream. Which looks ok for me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marchof I guess that you misread change: it doesn't add check, but removes it exactly because we don't need it - indeed in all places for a null InputStream we throw NullPointerException instead of IllegalArgumentException, also this condition wasn't covered by tests, so I took opportunity to remove it in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Godin Sure, my bad. You actually fixed the issue - thanks!!!

throw new IllegalArgumentException();
}
final byte[] buf = new byte[1024];
final ByteArrayOutputStream out = new ByteArrayOutputStream();
while (true) {
Expand Down
3 changes: 3 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ <h3>Fixed Bugs</h3>
<li><code>dump</code> commands now report error when server unexpectedly
closes connection without sending response
(GitHub <a href="https://github.com/jacoco/jacoco/issues/538">#538</a>).</li>
<li>More information about context is provided when unable to read stream during
analysis
(GitHub <a href="https://github.com/jacoco/jacoco/issues/541">#541</a>).</li>
</ul>

<h3>Non-functional Changes</h3>
Expand Down