use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.
the class CyclomaticComplexityTest method instrument.
private void instrument(final Class<? extends Target> clazz) throws Exception {
bytes = TargetLoader.getClassDataAsBytes(clazz);
final byte[] instrumented = new Instrumenter(runtime).instrument(bytes, "TestTarget");
final TargetLoader loader = new TargetLoader();
target = (Target) loader.add(clazz, instrumented).newInstance();
}
use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.
the class InstrumenterTest method testInstrumentClass.
@Test
public void testInstrumentClass() throws Exception {
byte[] bytes = instrumenter.instrument(TargetLoader.getClassDataAsBytes(InstrumenterTest.class), "Test");
TargetLoader loader = new TargetLoader();
Class<?> clazz = loader.add(InstrumenterTest.class, bytes);
assertEquals("org.jacoco.core.instr.InstrumenterTest", clazz.getName());
}
use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.
the class InstrumenterTest method testSerialization.
@Test
public void testSerialization() throws Exception {
// Create instrumented instance:
byte[] bytes = instrumenter.instrument(TargetLoader.getClassData(SerializationTarget.class), "Test");
TargetLoader loader = new TargetLoader();
Object obj1 = loader.add(SerializationTarget.class, bytes).getConstructor(String.class, Integer.TYPE).newInstance("Hello", Integer.valueOf(42));
// Serialize instrumented instance:
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
new ObjectOutputStream(buffer).writeObject(obj1);
// Deserialize with original class definition:
Object obj2 = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())).readObject();
assertEquals("Hello42", obj2.toString());
}
use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.
the class ModifiedSystemClassRuntimeTest method verifyInstrumentedClass.
private static void verifyInstrumentedClass(String name, byte[] source) throws Exception {
name = name.replace('/', '.');
final Class<?> targetClass = new TargetLoader().add(name, source);
// Check added field:
final Field f = targetClass.getField("$jacocoAccess");
assertTrue(Modifier.isPublic(f.getModifiers()));
assertTrue(Modifier.isStatic(f.getModifiers()));
assertTrue(Modifier.isTransient(f.getModifiers()));
assertEquals(Object.class, f.getType());
}
use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.
the class ExecuteInstrumentedCodeScenario method getInstrumentedCallable.
@Override
@SuppressWarnings("unchecked")
protected Callable<Void> getInstrumentedCallable() throws Exception {
IRuntime runtime = new LoggerRuntime();
runtime.startup(new RuntimeData());
final Instrumenter instr = new Instrumenter(runtime);
final byte[] original = TargetLoader.getClassDataAsBytes(target);
final byte[] instrumentedBuffer = instr.instrument(original, "");
final TargetLoader loader = new TargetLoader();
return (Callable<Void>) loader.add(target, instrumentedBuffer).newInstance();
}
Aggregations