Skip to content
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
20 changes: 10 additions & 10 deletions jacoco-maven-plugin/src/org/jacoco/maven/InstrumentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
*******************************************************************************/
package org.jacoco.maven;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand All @@ -26,6 +19,13 @@
import java.io.OutputStream;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator;

/**
* Performs offline instrumentation. Note that after execution of test you must
* restore original classes with help of "restore-instrumented-classes" goal.
Expand Down Expand Up @@ -59,8 +59,8 @@ public void executeMojo() throws MojoExecutionException,

final List<String> fileNames;
try {
fileNames = new FileFilter(this.getIncludes(),
this.getExcludes()).getFileNames(classesDir);
fileNames = new FileFilter(this.getIncludes(), this.getExcludes())
.getFileNames(classesDir);
} catch (final IOException e1) {
throw new MojoExecutionException(
"Unable to get list of files to instrument.", e1);
Expand All @@ -78,7 +78,7 @@ public void executeMojo() throws MojoExecutionException,
FileUtils.copyFile(source, backup);
input = new FileInputStream(backup);
output = new FileOutputStream(source);
instrumenter.instrument(input, output);
instrumenter.instrument(input, output, source.getPath());
} catch (final IOException e2) {
throw new MojoExecutionException(
"Unable to instrument file.", e2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void testTransformFailure() {
}
recorder.assertException(IllegalClassFormatException.class,
"Error while instrumenting class org.jacoco.Sample.",
NullPointerException.class);
IOException.class);
recorder.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
*******************************************************************************/
package org.jacoco.agent.rt.internal;

import static java.lang.String.format;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
Expand Down Expand Up @@ -86,10 +84,10 @@ public byte[] transform(final ClassLoader loader, final String classname,
// reference as probes might have changed.
runtime.disconnect(classBeingRedefined);
}
return instrumenter.instrument(classfileBuffer);
return instrumenter.instrument(classfileBuffer, classname);
} catch (final Exception ex) {
final IllegalClassFormatException wrapper = new IllegalClassFormatException(
format("Error while instrumenting class %s.", classname));
ex.getMessage());
wrapper.initCause(ex);
// Report this, as the exception is ignored by the JVM:
logger.logExeption(wrapper);
Expand Down
3 changes: 2 additions & 1 deletion org.jacoco.ant/src/org/jacoco/ant/InstrumentTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private int instrument(final Instrumenter instrumenter,
try {
input = resource.getInputStream();
output = new FileOutputStream(file);
return instrumenter.instrumentAll(input, output);
return instrumenter.instrumentAll(input, output,
resource.getName());
} finally {
FileUtils.close(input);
FileUtils.close(output);
Expand Down
2 changes: 1 addition & 1 deletion org.jacoco.ant/src/org/jacoco/ant/ReportTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ private IBundleCoverage createBundle(final GroupElement group)
analyzer.analyzeAll(((FileResource) resource).getFile());
} else {
final InputStream in = resource.getInputStream();
analyzer.analyzeAll(in);
analyzer.analyzeAll(in, resource.getName());
in.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -65,25 +66,39 @@ public void setup() {

@Test
public void testAnalyzeClass1() throws IOException {
analyzer.analyzeClass(TargetLoader.getClassData(AnalyzerTest.class));
analyzer.analyzeClass(TargetLoader.getClassData(AnalyzerTest.class),
"Test");
assertEquals(
Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
classes);
}

@Test
public void testAnalyzeClass2() throws IOException {
analyzer.analyzeClass(TargetLoader
.getClassDataAsBytes(AnalyzerTest.class));
analyzer.analyzeClass(
TargetLoader.getClassDataAsBytes(AnalyzerTest.class), "Test");
assertEquals(
Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
classes);
}

@Test
public void testAnalyzeClass_Broken() throws IOException {
final byte[] brokenclass = TargetLoader
.getClassDataAsBytes(AnalyzerTest.class);
brokenclass[10] = 0x23;
try {
analyzer.analyzeClass(brokenclass, "Broken");
fail();
} catch (IOException e) {
assertEquals("Error while analyzing class Broken.", e.getMessage());
}
}

@Test
public void testAnalyzeAll_Class() throws IOException {
final int count = analyzer.analyzeAll(TargetLoader
.getClassData(AnalyzerTest.class));
final int count = analyzer.analyzeAll(
TargetLoader.getClassData(AnalyzerTest.class), "Test");
assertEquals(1, count);
assertEquals(
Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
Expand All @@ -98,8 +113,8 @@ public void testAnalyzeAll_Zip() throws IOException {
"org/jacoco/core/analysis/AnalyzerTest.class"));
zip.write(TargetLoader.getClassDataAsBytes(AnalyzerTest.class));
zip.finish();
final int count = analyzer.analyzeAll(new ByteArrayInputStream(buffer
.toByteArray()));
final int count = analyzer.analyzeAll(
new ByteArrayInputStream(buffer.toByteArray()), "Test");
assertEquals(1, count);
assertEquals(
Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
Expand All @@ -123,7 +138,7 @@ public void testAnalyzeAll_Pack200() throws IOException {
gzipOutput.finish();

final int count = analyzer.analyzeAll(new ByteArrayInputStream(
pack200buffer.toByteArray()));
pack200buffer.toByteArray()), "Test");
assertEquals(1, count);
assertEquals(
Collections.singleton("org/jacoco/core/analysis/AnalyzerTest"),
Expand All @@ -133,7 +148,7 @@ public void testAnalyzeAll_Pack200() throws IOException {
@Test
public void testAnalyzeAll_Empty() throws IOException {
final int count = analyzer.analyzeAll(new ByteArrayInputStream(
new byte[0]));
new byte[0]), "Test");
assertEquals(0, count);
assertEquals(Collections.emptySet(), classes);
}
Expand Down Expand Up @@ -172,6 +187,29 @@ public void testAnalyzeAll_BrokenZip() throws IOException {
analyzer.analyzeAll(file);
}

@Test
public void testAnalyzeAll_BrokenClassFileInZip() throws IOException {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ZipOutputStream zip = new ZipOutputStream(buffer);
zip.putNextEntry(new ZipEntry(
"org/jacoco/core/analysis/AnalyzerTest.class"));
final byte[] brokenclass = TargetLoader
.getClassDataAsBytes(AnalyzerTest.class);
brokenclass[10] = 0x23;
zip.write(brokenclass);
zip.finish();

try {
analyzer.analyzeAll(new ByteArrayInputStream(buffer.toByteArray()),
"test.zip");
fail();
} catch (IOException e) {
assertEquals(
"Error while analyzing class test.zip@org/jacoco/core/analysis/AnalyzerTest.class.",
e.getMessage());
}
}

private void createClassfile(final String dir, final Class<?> source)
throws IOException {
File file = new File(folder.getRoot(), dir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -31,6 +32,7 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.analysis.AnalyzerTest;
import org.jacoco.core.runtime.RuntimeData;
import org.jacoco.core.runtime.SystemPropertiesRuntime;
import org.jacoco.core.test.TargetLoader;
Expand Down Expand Up @@ -79,11 +81,50 @@ public void teardown() {
runtime.shutdown();
}

@Test
public void testInstrumentClass() throws Exception {
byte[] bytes = instrumenter.instrument(
TargetLoader.getClassDataAsBytes(InstrumenterTest.class),
"Test");
TargetLoader loader = new TargetLoader(InstrumenterTest.class, bytes);
Class<?> clazz = loader.getTargetClass();
assertEquals("org.jacoco.core.instr.InstrumenterTest", clazz.getName());
}

@Test
public void testInstrumentBrokenClass1() throws IOException {
final byte[] brokenclass = TargetLoader
.getClassDataAsBytes(AnalyzerTest.class);
brokenclass[10] = 0x23;
try {
instrumenter.instrument(brokenclass, "Broken");
fail();
} catch (IOException e) {
assertEquals("Error while instrumenting class Broken.",
e.getMessage());
}
}

@Test
public void testInstrumentBrokenClass2() throws IOException {
final byte[] brokenclass = TargetLoader
.getClassDataAsBytes(AnalyzerTest.class);
brokenclass[10] = 0x23;
try {
instrumenter.instrument(new ByteArrayInputStream(brokenclass),
"Broken");
fail();
} catch (IOException e) {
assertEquals("Error while instrumenting class Broken.",
e.getMessage());
}
}

@Test
public void testSerialization() throws Exception {
// Create instrumented instance:
byte[] bytes = instrumenter.instrument(TargetLoader
.getClassData(SerializationTarget.class));
byte[] bytes = instrumenter.instrument(
TargetLoader.getClassData(SerializationTarget.class), "Test");
TargetLoader loader = new TargetLoader(SerializationTarget.class, bytes);
Object obj1 = loader.getTargetClass()
.getConstructor(String.class, Integer.TYPE)
Expand All @@ -104,7 +145,7 @@ public void testInstrumentAll_Class() throws IOException {
InputStream in = TargetLoader.getClassData(getClass());
OutputStream out = new ByteArrayOutputStream();

int count = instrumenter.instrumentAll(in, out);
int count = instrumenter.instrumentAll(in, out, "Test");

assertEquals(1, count);
}
Expand All @@ -119,7 +160,7 @@ public void testInstrumentAll_Zip() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();

int count = instrumenter.instrumentAll(
new ByteArrayInputStream(buffer.toByteArray()), out);
new ByteArrayInputStream(buffer.toByteArray()), out, "Test");

assertEquals(1, count);
ZipInputStream zipin = new ZipInputStream(new ByteArrayInputStream(
Expand All @@ -128,6 +169,29 @@ public void testInstrumentAll_Zip() throws IOException {
assertNull(zipin.getNextEntry());
}

@Test
public void testInstrumentAll_BrokenClassFileInZip() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ZipOutputStream zipout = new ZipOutputStream(buffer);
zipout.putNextEntry(new ZipEntry("Test.class"));
final byte[] brokenclass = TargetLoader.getClassDataAsBytes(getClass());
brokenclass[10] = 0x23;
zipout.write(brokenclass);
zipout.finish();
ByteArrayOutputStream out = new ByteArrayOutputStream();

try {
instrumenter.instrumentAll(
new ByteArrayInputStream(buffer.toByteArray()), out,
"test.zip");
fail();
} catch (IOException e) {
assertEquals(
"Error while instrumenting class [email protected].",
e.getMessage());
}
}

@Test
public void testInstrumentAll_Pack200() throws IOException {
ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
Expand All @@ -145,7 +209,7 @@ public void testInstrumentAll_Pack200() throws IOException {

ByteArrayOutputStream out = new ByteArrayOutputStream();
int count = instrumenter.instrumentAll(new ByteArrayInputStream(
pack200buffer.toByteArray()), out);
pack200buffer.toByteArray()), out, "Test");

jarbuffer.reset();
Pack200.newUnpacker()
Expand All @@ -164,7 +228,7 @@ public void testInstrumentAll_Other() throws IOException {
InputStream in = new ByteArrayInputStream("text".getBytes());
ByteArrayOutputStream out = new ByteArrayOutputStream();

int count = instrumenter.instrumentAll(in, out);
int count = instrumenter.instrumentAll(in, out, "Test");

assertEquals(0, count);
assertEquals("text", new String(out.toByteArray()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*******************************************************************************/
package org.jacoco.core.test.perf;

import java.util.concurrent.Callable;

import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.ICoverageVisitor;
Expand All @@ -33,19 +35,20 @@ protected AnalysisTimeScenario(Class<?> target, int count) {
}

@Override
protected Runnable getInstrumentedRunnable() throws Exception {
protected Callable<Void> getInstrumentedCallable() throws Exception {
final byte[] bytes = TargetLoader.getClassDataAsBytes(target);
final ExecutionDataStore executionData = new ExecutionDataStore();
ICoverageVisitor visitor = new ICoverageVisitor() {
public void visitCoverage(IClassCoverage coverage) {
}
};
final Analyzer analyzer = new Analyzer(executionData, visitor);
return new Runnable() {
public void run() {
return new Callable<Void>() {
public Void call() throws Exception {
for (int i = 0; i < count; i++) {
analyzer.analyzeClass(bytes);
analyzer.analyzeClass(bytes, target.getName());
}
return null;
}
};
}
Expand Down
Loading