From 140b97ac709cad12025b2a506a442f00944c4055 Mon Sep 17 00:00:00 2001 From: "Marc R. Hoffmann" Date: Sun, 5 Sep 2021 13:16:48 +0200 Subject: [PATCH] Add JaCoCo version to instrumentation and analysis errors This will hopefully help people to understand which JaCoCo version they are actually using in their build setups. --- .../jacoco/core/analysis/AnalyzerTest.java | 34 +++++++++------- .../jacoco/core/instr/InstrumenterTest.java | 40 +++++++++---------- .../org/jacoco/core/analysis/Analyzer.java | 4 +- .../org/jacoco/core/instr/Instrumenter.java | 4 +- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java b/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java index 00b0aebba5..6508465bb9 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java @@ -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; @@ -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()); } @@ -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()); @@ -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") @@ -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); } } @@ -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); } } @@ -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()); } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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()); + } + } diff --git a/org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java b/org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java index 44d16d36e7..006104c30a 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java @@ -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; @@ -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()); } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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()); } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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()); + } + } diff --git a/org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java b/org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java index 0cc06ada72..cf6a5fb751 100644 --- a/org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java +++ b/org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java @@ -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; @@ -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; } diff --git a/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java b/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java index 5225d2e4c0..4a8b76bcec 100644 --- a/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java +++ b/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java @@ -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; @@ -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; }