Skip to content

Commit

Permalink
Add JaCoCo version to instrumentation and analysis errors
Browse files Browse the repository at this point in the history
This will hopefully help people to understand which JaCoCo version they
are actually using in their build setups.
  • Loading branch information
marchof committed Sep 5, 2021
1 parent 14e4156 commit 140b97a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
34 changes: 19 additions & 15 deletions org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java
Expand Up @@ -36,6 +36,7 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.JaCoCo;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.internal.Pack200Streams;
import org.jacoco.core.internal.data.CRC64;
Expand Down Expand Up @@ -136,8 +137,7 @@ public void analyzeClass_should_throw_exception_for_unsupported_class_file_versi
analyzer.analyzeClass(bytes, "UnsupportedVersion");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while analyzing UnsupportedVersion.",
e.getMessage());
assertExceptionMessage("UnsupportedVersion", e);
assertEquals("Unsupported class file major version 63",
e.getCause().getMessage());
}
Expand All @@ -164,7 +164,7 @@ public void testAnalyzeClassIdMatch() throws IOException {
final byte[] bytes = TargetLoader
.getClassDataAsBytes(AnalyzerTest.class);
executionData.get(Long.valueOf(CRC64.classId(bytes)),
"org/jacoco/core/analysis/AnalyzerTest", 200);
"org/jacoco/core/analysis/AnalyzerTest", 400);
analyzer.analyzeClass(bytes, "Test");
assertFalse(classes.get("org/jacoco/core/analysis/AnalyzerTest")
.isNoMatch());
Expand All @@ -173,7 +173,7 @@ public void testAnalyzeClassIdMatch() throws IOException {
@Test
public void testAnalyzeClassNoIdMatch() throws IOException {
executionData.get(Long.valueOf(0),
"org/jacoco/core/analysis/AnalyzerTest", 200);
"org/jacoco/core/analysis/AnalyzerTest", 400);
analyzer.analyzeClass(
TargetLoader.getClassDataAsBytes(AnalyzerTest.class), "Test");
assertTrue(classes.get("org/jacoco/core/analysis/AnalyzerTest")
Expand All @@ -189,7 +189,7 @@ public void testAnalyzeClass_Broken() throws IOException {
analyzer.analyzeClass(brokenclass, "Broken.class");
fail("expected exception");
} catch (IOException e) {
assertEquals("Error while analyzing Broken.class.", e.getMessage());
assertExceptionMessage("Broken.class", e);
}
}

Expand All @@ -209,7 +209,7 @@ public void testAnalyzeClass_BrokenStream() throws IOException {
analyzer.analyzeClass(new BrokenInputStream(), "BrokenStream");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while analyzing BrokenStream.", e.getMessage());
assertExceptionMessage("BrokenStream", e);
}
}

Expand All @@ -224,8 +224,7 @@ public void analyzeAll_should_throw_exception_for_unsupported_class_file_version
"UnsupportedVersion");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while analyzing UnsupportedVersion.",
e.getMessage());
assertExceptionMessage("UnsupportedVersion", e);
assertEquals("Unsupported class file major version 63",
e.getCause().getMessage());
}
Expand Down Expand Up @@ -274,7 +273,7 @@ public void testAnalyzeAll_Broken() throws IOException {
analyzer.analyzeAll(new BrokenInputStream(), "Test");
fail("expected exception");
} catch (IOException e) {
assertEquals("Error while analyzing Test.", e.getMessage());
assertExceptionMessage("Test", e);
}
}

Expand All @@ -289,7 +288,7 @@ public void testAnalyzeAll_BrokenGZ() {
analyzer.analyzeAll(new ByteArrayInputStream(buffer), "Test.gz");
fail("expected exception");
} catch (IOException e) {
assertEquals("Error while analyzing Test.gz.", e.getMessage());
assertExceptionMessage("Test.gz", e);
}
}

Expand Down Expand Up @@ -333,7 +332,7 @@ public void testAnalyzeAll_BrokenPack200() {
"Test.pack200");
fail("expected exception");
} catch (IOException e) {
assertEquals("Error while analyzing Test.pack200.", e.getMessage());
assertExceptionMessage("Test.pack200", e);
}
}

Expand Down Expand Up @@ -380,7 +379,7 @@ public void testAnalyzeAll_BrokenZip() {
analyzer.analyzeAll(new ByteArrayInputStream(buffer), "Test.zip");
fail("expected exception");
} catch (IOException e) {
assertEquals("Error while analyzing Test.zip.", e.getMessage());
assertExceptionMessage("Test.zip", e);
}
}

Expand Down Expand Up @@ -431,9 +430,8 @@ public void testAnalyzeAll_BrokenClassFileInZip() throws IOException {
"test.zip");
fail("expected exception");
} catch (IOException e) {
assertEquals(
"Error while analyzing test.zip@org/jacoco/core/analysis/AnalyzerTest.class.",
e.getMessage());
assertExceptionMessage(
"test.zip@org/jacoco/core/analysis/AnalyzerTest.class", e);
}
}

Expand All @@ -452,4 +450,10 @@ private void assertClasses(String... classNames) {
classes.keySet());
}

private void assertExceptionMessage(String name, Exception ex) {
String expected = "Error while analyzing " + name + " with JaCoCo "
+ JaCoCo.VERSION + "/" + JaCoCo.COMMITID_SHORT + ".";
assertEquals(expected, ex.getMessage());
}

}
Expand Up @@ -33,6 +33,7 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.JaCoCo;
import org.jacoco.core.analysis.AnalyzerTest;
import org.jacoco.core.internal.Pack200Streams;
import org.jacoco.core.internal.data.CRC64;
Expand Down Expand Up @@ -127,8 +128,7 @@ public void instrument_should_throw_exception_for_unsupported_class_file_version
instrumenter.instrument(bytes, "UnsupportedVersion");
fail("exception expected");
} catch (final IOException e) {
assertEquals("Error while instrumenting UnsupportedVersion.",
e.getMessage());
assertExceptionMessage("UnsupportedVersion", e);
assertEquals("Unsupported class file major version 63",
e.getCause().getMessage());
}
Expand Down Expand Up @@ -156,8 +156,7 @@ public void testInstrumentBrokenClass1() throws IOException {
instrumenter.instrument(brokenclass, "Broken.class");
fail();
} catch (IOException e) {
assertEquals("Error while instrumenting Broken.class.",
e.getMessage());
assertExceptionMessage("Broken.class", e);
}
}

Expand All @@ -178,8 +177,7 @@ public void testInstrumentBrokenStream() {
instrumenter.instrument(new BrokenInputStream(), "BrokenStream");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while instrumenting BrokenStream.",
e.getMessage());
assertExceptionMessage("BrokenStream", e);
}
}

Expand All @@ -194,8 +192,7 @@ public void testInstrumentBrokenStream2() {
new ByteArrayOutputStream(), "BrokenStream");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while instrumenting BrokenStream.",
e.getMessage());
assertExceptionMessage("BrokenStream", e);
}
}

Expand Down Expand Up @@ -230,8 +227,7 @@ public void instrumentAll_should_throw_exception_for_unsupported_class_file_vers
new ByteArrayOutputStream(), "UnsupportedVersion");
fail("exception expected");
} catch (final IOException e) {
assertEquals("Error while instrumenting UnsupportedVersion.",
e.getMessage());
assertExceptionMessage("UnsupportedVersion", e);
assertEquals("Unsupported class file major version 63",
e.getCause().getMessage());
}
Expand Down Expand Up @@ -297,7 +293,7 @@ public void testInstrumentAll_Broken() {
new ByteArrayOutputStream(), "Broken");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while instrumenting Broken.", e.getMessage());
assertExceptionMessage("Broken", e);
}
}

Expand All @@ -324,7 +320,7 @@ public int read() throws IOException {
instrumenter.instrumentAll(inputStream, new ByteArrayOutputStream(),
"Broken");
} catch (IOException e) {
assertEquals("Error while instrumenting Broken.", e.getMessage());
assertExceptionMessage("Broken", e);
}
}

Expand All @@ -346,7 +342,7 @@ public void testInstrumentAll_BrokenZip() {
new ByteArrayOutputStream(), "Test.zip");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while instrumenting Test.zip.", e.getMessage());
assertExceptionMessage("Test.zip", e);
}
}

Expand All @@ -371,9 +367,7 @@ public void testInstrumentAll_BrokenZipEntry() throws IOException {
new ByteArrayOutputStream(), "broken.zip");
fail("exception expected");
} catch (IOException e) {
assertEquals(
"Error while instrumenting broken.zip@brokenentry.txt.",
e.getMessage());
assertExceptionMessage("broken.zip@brokenentry.txt", e);
}
}

Expand All @@ -394,8 +388,7 @@ public void testInstrumentAll_BrokenClassFileInZip() throws IOException {
"test.zip");
fail();
} catch (IOException e) {
assertEquals("Error while instrumenting test.zip@Test.class.",
e.getMessage());
assertExceptionMessage("test.zip@Test.class", e);
}
}

Expand All @@ -412,7 +405,7 @@ public void testInstrumentAll_BrokenGZ() {
new ByteArrayOutputStream(), "Test.gz");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while instrumenting Test.gz.", e.getMessage());
assertExceptionMessage("Test.gz", e);
}
}

Expand Down Expand Up @@ -462,8 +455,7 @@ public void testInstrumentAll_BrokenPack200() {
instrumenter.instrumentAll(new ByteArrayInputStream(buffer),
new ByteArrayOutputStream(), "Test.pack200");
} catch (IOException e) {
assertEquals("Error while instrumenting Test.pack200.",
e.getMessage());
assertExceptionMessage("Test.pack200", e);
}
}

Expand Down Expand Up @@ -516,4 +508,10 @@ public void testInstrumentAll_KeepSignatures() throws IOException {
assertNull(zipin.getNextEntry());
}

private void assertExceptionMessage(String name, Exception ex) {
String expected = "Error while instrumenting " + name + " with JaCoCo "
+ JaCoCo.VERSION + "/" + JaCoCo.COMMITID_SHORT + ".";
assertEquals(expected, ex.getMessage());
}

}
4 changes: 3 additions & 1 deletion org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java
Expand Up @@ -21,6 +21,7 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.jacoco.core.JaCoCo;
import org.jacoco.core.data.ExecutionData;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.internal.ContentTypeDetector;
Expand Down Expand Up @@ -160,7 +161,8 @@ public void analyzeClass(final InputStream input, final String location)
private IOException analyzerError(final String location,
final Exception cause) {
final IOException ex = new IOException(
String.format("Error while analyzing %s.", location));
String.format("Error while analyzing %s with JaCoCo %s/%s.",
location, JaCoCo.VERSION, JaCoCo.COMMITID_SHORT));
ex.initCause(cause);
return ex;
}
Expand Down
4 changes: 3 additions & 1 deletion org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java
Expand Up @@ -23,6 +23,7 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.JaCoCo;
import org.jacoco.core.internal.ContentTypeDetector;
import org.jacoco.core.internal.InputStreams;
import org.jacoco.core.internal.Pack200Streams;
Expand Down Expand Up @@ -158,7 +159,8 @@ public void instrument(final InputStream input, final OutputStream output,
private IOException instrumentError(final String name,
final Exception cause) {
final IOException ex = new IOException(
String.format("Error while instrumenting %s.", name));
String.format("Error while instrumenting %s with JaCoCo %s/%s.",
name, JaCoCo.VERSION, JaCoCo.COMMITID_SHORT));
ex.initCause(cause);
return ex;
}
Expand Down

0 comments on commit 140b97a

Please sign in to comment.