Search in sources :

Example 6 with TargetLoader

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();
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) Instrumenter(org.jacoco.core.instr.Instrumenter)

Example 7 with TargetLoader

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());
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) Test(org.junit.Test) AnalyzerTest(org.jacoco.core.analysis.AnalyzerTest)

Example 8 with TargetLoader

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TargetLoader(org.jacoco.core.test.TargetLoader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) AnalyzerTest(org.jacoco.core.analysis.AnalyzerTest)

Example 9 with TargetLoader

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());
}
Also used : Field(java.lang.reflect.Field) TargetLoader(org.jacoco.core.test.TargetLoader)

Example 10 with TargetLoader

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();
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) LoggerRuntime(org.jacoco.core.runtime.LoggerRuntime) TargetLoader(org.jacoco.core.test.TargetLoader) Instrumenter(org.jacoco.core.instr.Instrumenter) Callable(java.util.concurrent.Callable) IRuntime(org.jacoco.core.runtime.IRuntime)

Aggregations

TargetLoader (org.jacoco.core.test.TargetLoader)14 Test (org.junit.Test)8 ClassWriter (org.objectweb.asm.ClassWriter)8 MethodVisitor (org.objectweb.asm.MethodVisitor)6 Callable (java.util.concurrent.Callable)3 Instrumenter (org.jacoco.core.instr.Instrumenter)3 AnalyzerTest (org.jacoco.core.analysis.AnalyzerTest)2 ClassReader (org.objectweb.asm.ClassReader)2 ClassVisitor (org.objectweb.asm.ClassVisitor)2 Type (org.objectweb.asm.Type)2 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)2 Method (org.objectweb.asm.commons.Method)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Field (java.lang.reflect.Field)1 IRuntime (org.jacoco.core.runtime.IRuntime)1 LoggerRuntime (org.jacoco.core.runtime.LoggerRuntime)1 RuntimeData (org.jacoco.core.runtime.RuntimeData)1