8000 Include JaCoCo version in instrumentation/analysis exception messages… · jacoco/jacoco@c54fa8a · GitHub
[go: up one dir, main page]

Skip to content

Commit c54fa8a

Browse files
authored
Include JaCoCo version in instrumentation/analysis exception messages (#1217)
This will hopefully help people to understand which JaCoCo version they are actually using in their build setups.
1 parent 046ba52 commit c54fa8a

File tree

9 files changed

+76
-41
lines changed

9 files changed

+76
-41
lines changed

org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/CoverageTransformerTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,15 @@ public void testTransformFailure() {
215215
protectionDomain, null);
216216
fail("IllegalClassFormatException expected.");
217217
} catch (IllegalClassFormatException e) {
218-
assertEquals("Error while instrumenting org.jacoco.Sample.",
218+
assertEquals(String.format(
219+
"Error while instrumenting %s with JaCoCo %s/%s.",
220+
"org.jacoco.Sample", JaCoCo.VERSION, JaCoCo.COMMITID_SHORT),
219221
e.getMessage());
220222
}
221223
recorder.assertException(IllegalClassFormatException.class,
222-
"Error while instrumenting org.jacoco.Sample.",
224+
String.format("Error while instrumenting %s with JaCoCo %s/%s.",
225+
"org.jacoco.Sample", JaCoCo.VERSION,
226+
JaCoCo.COMMITID_SHORT),
223227
IOException.class);
224228
recorder.clear();
225229
}

org.jacoco.core.test/src/org/jacoco/core/JaCoCoTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
6D4E *******************************************************************************/
1313
package org.jacoco.core;
1414

15+
import static org.junit.Assert.assertEquals;
1516
import static org.junit.Assert.assertNotNull;
1617

1718
import org.junit.Test;
@@ -26,6 +27,16 @@ public void testVERSION() {
2627
assertNotNull(JaCoCo.VERSION);
2728
}
2829

30+
@Test
31+
public void testCOMMITID() {
32+
assertNotNull(JaCoCo.COMMITID);
33+
}
34+
35+
@Test
36+
public void testCOMMITID_SHORT() {
37+
assertEquals(7, JaCoCo.COMMITID_SHORT.length());
38+
}
39+
2940
@Test
3041
public void testHOMEURL() {
3142
assertNotNull(JaCoCo.HOMEURL);

org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.zip.ZipInputStream;
3737
import java.util.zip.ZipOutputStream;
3838

39+
import org.jacoco.core.JaCoCo;
3940
import org.jacoco.core.data.ExecutionDataStore;
4041
import org.jacoco.core.internal.Pack200Streams;
4142
import org.jacoco.core.internal.data.CRC64;
@@ -136,8 +137,7 @@ public void analyzeClass_should_throw_exception_for_unsupported_class_file_versi
136137
analyzer.analyzeClass(bytes, "UnsupportedVersion");
137138
fail("exception expected");
138139
} catch (IOException e) {
139-
assertEquals("Error while analyzing UnsupportedVersion.",
140-
e.getMessage());
140+
assertExceptionMessage("UnsupportedVersion", e);
141141
assertEquals("Unsupported class file major version 64",
142142
e.getCause().getMessage());
143143
}
@@ -164,7 +164,7 @@ public void testAnalyzeClassIdMatch() throws IOException {
164164
final byte[] bytes = TargetLoader
165165
.getClassDataAsBytes(AnalyzerTest.class);
166166
executionData.get(Long.valueOf(CRC64.classId(bytes)),
167-
"org/jacoco/core/analysis/AnalyzerTest", 200);
167+
"org/jacoco/core/analysis/AnalyzerTest", 400);
168168
analyzer.analyzeClass(bytes, "Test");
169169
assertFalse(classes.get("org/jacoco/core/analysis/AnalyzerTest")
170170
.isNoMatch());
@@ -173,7 +173,7 @@ public void testAnalyzeClassIdMatch() throws IOException {
173173
@Test
174174
public void testAnalyzeClassNoIdMatch() throws IOException {
175175
executionData.get(Long.valueOf(0),
176-
"org/jacoco/core/analysis/AnalyzerTest", 200);
176+
"org/jacoco/core/analysis/AnalyzerTest", 400);
177177
analyzer.analyzeClass(
178178
TargetLoader.getClassDataAsBytes(AnalyzerTest.class), "Test");
179179
assertTrue(classes.get("org/jacoco/core/analysis/AnalyzerTest")
@@ -189,7 +189,7 @@ public void testAnalyzeClass_Broken() throws IOException {
189189
analyzer.analyzeClass(brokenclass, "Broken.class");
190190
fail("expected exception");
191191
} catch (IOException e) {
192-
assertEquals("Error while analyzing Broken.class.", e.getMessage());
192+
assertExceptionMessage("Broken.class", e);
193193
}
194194
}
195195

@@ -209,7 +209,7 @@ public void testAnalyzeClass_BrokenStream() throws IOException {
209209
analyzer.analyzeClass(new BrokenInputStream(), "BrokenStream");
210210
fail("exception expected");
211211
} catch (IOException e) {
212-
assertEquals("Error while analyzing BrokenStream.", e.getMessage());
212+
assertExceptionMessage("BrokenStream", e);
213213
}
214214
}
215215

@@ -224,8 +224,7 @@ public void analyzeAll_should_throw_exception_for_unsupported_class_file_version
224224
"UnsupportedVersion");
225225
fail("exception expected");
226226
} catch (IOException e) {
227-
assertEquals("Error while analyzing UnsupportedVersion.",
228-
e.getMessage());
227+
assertExceptionMessage("UnsupportedVersion", e);
229228
assertEquals("Unsupported class file major version 64",
230229
e.getCause().getMessage());
231230
}
@@ -274,7 +273,7 @@ public void testAnalyzeAll_Broken() throws IOException {
274273
analyzer.analyzeAll(new BrokenInputStream(), "Test");
275274
fail("expected exception");
276275
} catch (IOException e) {
277-
assertEquals("Error while analyzing Test.", e.getMessage());
276+
assertExceptionMessage("Test", e);
278277
}
279278
}
280279

@@ -289,7 +288,7 @@ public void testAnalyzeAll_BrokenGZ() {
289288
analyzer.analyzeAll(new ByteArrayInputStream(buffer), "Test.gz");
290289
fail("expected exception");
291290
} catch (IOException e) {
292-
assertEquals("Error while analyzing Test.gz.", e.getMessage());
291+
assertExceptionMessage("Test.gz", e);
293292
}
294293
}
295294

@@ -333,7 +332,7 @@ public void testAnalyzeAll_BrokenPack200() {
333332
"Test.pack200");
334333
fail("expected exception");
335334
} catch (IOException e) {
336-
assertEquals("Error while analyzing Test.pack200.", e.getMessage());
335+
assertExceptionMessage("Test.pack200", e);
337336
}
338337
}
339338

@@ -380,7 +379,7 @@ public void testAnalyzeAll_BrokenZip() {
380379
analyzer.analyzeAll(new ByteArrayInputStream(buffer), "Test.zip");
381380
fail("expected exception");
382381
} catch (IOException e) {
383-
assertEquals("Error while analyzing Test.zip.", e.getMessage());
382+
assertExceptionMessage("Test.zip", e);
384383
}
385384
}
386385

@@ -431,9 +430,8 @@ public void testAnalyzeAll_BrokenClassFileInZip() throws IOException {
431430
"test.zip");
432431
fail("expected exception");
433432
} catch (IOException e) {
434-
assertEquals(
435-
"Error while analyzing test.zip@org/jacoco/core/analysis/AnalyzerTest.class.",
436-
e.getMessage());
433+
assertExceptionMessage(
434+
"test.zip@org/jacoco/core/analysis/AnalyzerTest.class", e);
437435
}
438436
}
439437

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

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

org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.zip.ZipInputStream;
3434
import java.util.zip.ZipOutputStream;
3535

36+
import org.jacoco.core.JaCoCo;
3637
import org.jacoco.core.analysis.AnalyzerTest;
3738
import org.jacoco.core.internal.Pack200Streams;
3839
import org.jacoco.core.internal.data.CRC64;
@@ -127,8 +128,7 @@ public void instrument_should_throw_exception_for_unsupported_class_file_version
127128
instrumenter.instrument(bytes, "UnsupportedVersion");
128129
fail("exception expected");
129130
} catch (final IOException e) {
130-
assertEquals("Error while instrumenting UnsupportedVersion.",
131-
e.getMessage());
131+
assertExceptionMessage("UnsupportedVersion", e);
132132
assertEquals("Unsupported class file major version 64",
133133
e.getCause().getMessage());
134134
}
@@ -156,8 +156,7 @@ public void testInstrumentBrokenClass1() throws IOException {
156156
instrumenter.instrument(brokenclass, "Broken.class");
157157
fail();
158158
} catch (IOException e) {
159-
assertEquals("Error while instrumenting Broken.class.",
160-
e.getMessage());
159+
assertExceptionMessage("Broken.class", e);
161160
}
162161
}
163162

@@ -178,8 +177,7 @@ public void testInstrumentBrokenStream() {
178177
instrumenter.instrument(new BrokenInputStream(), "BrokenStream");
179178
fail("exception expected");
180179
} catch (IOException e) {
181-
assertEquals("Error while instrumenting BrokenStream.",
182-
e.getMessage());
180+
assertExceptionMessage("BrokenStream", e);
183181
}
184182
}
185183

@@ -194,8 +192,7 @@ public void testInstrumentBrokenStream2() {
194192
new ByteArrayOutputStream(), "BrokenStream");
195193
fail("exception expected");
196194
} catch (IOException e) {
197-
assertEquals("Error while instrumenting BrokenStream.",
198-
e.getMessage());
195+
assertExceptionMessage("BrokenStream", e);
199196
}
200197
}
201198

@@ -230,8 +227,7 @@ public void instrumentAll_should_throw_exception_for_unsupported_class_file_vers
230227
new ByteArrayOutputStream(), "UnsupportedVersion");
231228
fail("exception expected");
232229
} catch (final IOException e) {
233-
assertEquals("Error while instrumenting UnsupportedVersion.",
234-
e.getMessage());
230+
assertExceptionMessage("UnsupportedVersion", e);
235231
assertEquals("Unsupported class file major version 64",
236232
e.getCause().getMessage());
237233
}
@@ -297,7 +293,7 @@ public void testInstrumentAll_Broken() {
297293
new ByteArrayOutputStream(), "Broken");
298294
fail("exception expected");
299295
} catch (IOException e) {
300-
assertEquals("Error while instrumenting Broken.", e.getMessage());
296+
assertExceptionMessage("Broken", e);
301297
}
302298
}
303299

@@ -324,7 +320,7 @@ public int read() throws IOException {
324320
instrumenter.instrumentAll(inputStream, new ByteArrayOutputStream(),
325321
"Broken");
326322
} catch (IOException e) {
327-
assertEquals("Error while instrumenting Broken.", e.getMessage());
323+
assertExceptionMessage("Broken", e);
328324
}
329325
}
330326

@@ -346,7 +342,7 @@ public void testInstrumentAll_BrokenZip() {
346342
new ByteArrayOutputStream(), "Test.zip");
347343
fail("exception expected");
348344
} catch (IOException e) {
349-
assertEquals("Error while instrumenting Test.zip.", e.getMessage());
345+
assertExceptionMessage("Test.zip", e);
350346
}
351347
}
352348

@@ -371,9 +367,7 @@ public void testInstrumentAll_BrokenZipEntry() throws IOException {
371367
new ByteArrayOutputStream(), "broken.zip");
372368
fail("exception expected");
373369
} catch (IOException e) {
374-
assertEquals(
375-
"Error while instrumenting broken.zip@brokenentry.txt.",
376-
e.getMessage());
370+
assertExceptionMessage("broken.zip@brokenentry.txt", e);
377371
}
378372
}
379373

@@ -394,8 +388,7 @@ public void testInstrumentAll_BrokenClassFileInZip() throws IOException {
394388
"test.zip");
395389
fail();
396390
} catch (IOException e) {
397-
assertEquals("Error while instrumenting test.zip@Test.class.",
398-
e.getMessage());
391+
assertExceptionMessage("test.zip@Test.class", e);
399392
}
400393
}
401394

@@ -412,7 +405,7 @@ public void testInstrumentAll_BrokenGZ() {
412405
new ByteArrayOutputStream(), "Test.gz");
413406
fail("exception expected");
414407
} catch (IOException e) {
415-
assertEquals("Error while instrumenting Test.gz.", e.getMessage());
408+
assertExceptionMessage("Test.gz", e);
416409
}
417410
}
418411

@@ -462,8 +455,7 @@ public void testInstrumentAll_BrokenPack200() {
462455
instrumenter.instrumentAll(new ByteArrayInputStream(buffer),
463456
new ByteArrayOutputStream(), "Test.pack200");
464457
} catch (IOException e) {
465-
assertEquals("Error while instrumenting Test.pack200.",
466-
e.getMessage());
458+
assertExceptionMessage("Test.pack200", e);
467459
}
468460
}
469461

@@ -516,4 +508,10 @@ public void testInstrumentAll_KeepSignatures() throws IOException {
516508
assertNull(zipin.getNextEntry());
517509
}
518510

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

org.jacoco.core/src/org/jacoco/core/JaCoCo.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@
1919
*/
2020
public final class JaCoCo {
2121

22-
/** Qualified build version of the JaCoCo core library. */
22+
/** Qualified version of JaCoCo core. */
2323
public static final String VERSION;
2424

25+
/** Commit ID of the source tree of JaCoCo core. */
26+
public static final String COMMITID;
27+
28+
/**
29+
* Shortened (7 digit) commit ID of the source tree of JaCoCo core.
30+
*/
31+
public static final String COMMITID_SHORT;
32+
2533
/** Absolute URL of the current JaCoCo home page */
2634
public static final String HOMEURL;
2735

@@ -32,6 +40,8 @@ public final class JaCoCo {
3240
final ResourceBundle bundle = ResourceBundle
3341
.getBundle("org.jacoco.core.jacoco");
3442
VERSION = bundle.getString("VERSION");
43+
COMMITID = bundle.getString("COMMITID");
44+
COMMITID_SHORT = COMMITID.substring(0, 7);
3545
HOMEURL = bundle.getString("HOMEURL");
3646
RUNTIMEPACKAGE = bundle.getString("RUNTIMEPACKAGE");
3747
}

org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.zip.ZipEntry;
2222
import java.util.zip.ZipInputStream;
2323

24+
import org.jacoco.core.JaCoCo;
2425
import org.jacoco.core.data.ExecutionData;
2526
import org.jacoco.core.data.ExecutionDataStore;
2627
import org.jacoco.core.internal.ContentTypeDetector;
@@ -160,7 +161,8 @@ public void analyzeClass(final InputStream input, final String location)
160161
private IOException analyzerError(final String location,
161162
final Exception cause) {
162163
final IOException ex = new IOException(
163-
String.format("Error while analyzing %s.", location));
164+
String.format("Error while analyzing %s with JaCoCo %s/%s.",
165+
location, JaCoCo.VERSION, JaCoCo.COMMITID_SHORT));
164166
ex.initCause(cause);
165167
return ex;
166168
}

org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.zip.ZipInputStream;
2424
import java.util.zip.ZipOutputStream;
2525

26+
import org.jacoco.core.JaCoCo;
2627
import org.jacoco.core.internal.ContentTypeDetector;
2728
import org.jacoco.core.internal.InputStreams;
2829
import org.jacoco.core.internal.Pack200Streams;
@@ -158,7 +159,8 @@ public void instrument(final InputStream input, final OutputStream output,
158159
private IOException instrumentError(final String name,
159160
final Exception cause) {
160161
final IOException ex = new IOException(
161-
String.format("Error while instrumenting %s.", name));
162+
String.format("Error while instrumenting %s with JaCoCo %s/%s.",
163+
name, JaCoCo.VERSION, JaCoCo.COMMITID_SHORT));
162164
ex.initCause(cause);
163165
return ex;
164166
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
VERSION=${qualified.bundle.version}
2+
COMMITID=${buildNumber}
23
HOMEURL=${jacoco.home.url}
34
RUNTIMEPACKAGE=${jacoco.runtime.package.name}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ <h3>Non-functional Changes</h3>
4646
<ul>
4747
<li>JaCoCo now depends on ASM 9.2
4848
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1206">#1206</a>).</li>
49+
<li>Messages of exceptions occurring during analysis or instrumentation now include
50+
JaCoCo version
51+
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1217">#1217</a>).</li>
4952
</ul>
5053

5154
<h2>Release 0.8.7 (2021/05/04)</h2>

0 commit comments

Comments
 (0)
0