use of com.newrelic.agent.instrumentation.RequireMethodsAdapter in project newrelic-java-agent by newrelic.
the class ApiImplementationUpdateTest method testMatcherMissingMethods.
@Test
public void testMatcherMissingMethods() throws Exception {
ArgumentCaptor<ClassMatchVisitorFactory> arg = ArgumentCaptor.forClass(ClassMatchVisitorFactory.class);
InstrumentationContext iInstrumentationContext = Mockito.mock(InstrumentationContext.class);
// remove required methods from Request implementation class
Class<?> clazz = RequestImpl.class;
Set<Method> methodsToRemove = new HashSet<>();
methodsToRemove.add(new Method("getHeaderType", "()Lcom/newrelic/api/agent/HeaderType;"));
byte[] classBytes = removeMethods(clazz, methodsToRemove);
// verify missing methods
RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
expectMissingMethods(adapter, classBytes);
ApiImplementationUpdate transformer = new ApiImplementationUpdate();
ClassMatchVisitorFactory matcher = transformer.getMatcher();
ClassReader reader = new ClassReader(new ByteArrayInputStream(classBytes));
ClassVisitor visitor = matcher.newClassMatchVisitor(clazz.getClassLoader(), null, reader, null, iInstrumentationContext);
reader.accept(visitor, ClassReader.SKIP_CODE);
Mockito.verify(iInstrumentationContext, Mockito.only()).putMatch(arg.capture(), Mockito.<Match>anyObject());
Assert.assertSame(matcher, arg.getValue());
}
use of com.newrelic.agent.instrumentation.RequireMethodsAdapter in project newrelic-java-agent by newrelic.
the class ApiImplementationUpdateTest method testTransform.
@Test
public void testTransform() throws Exception {
// remove required methods from Request implementation class
Class<?> clazz = RequestImpl.class;
Set<Method> methodsToRemove = new HashSet<>();
methodsToRemove.add(new Method("getHeaderType", "()Lcom/newrelic/api/agent/HeaderType;"));
byte[] classBytes = removeMethods(clazz, methodsToRemove);
// verify missing methods
RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
expectMissingMethods(adapter, classBytes);
// should add default implementations for missing methods
ApiImplementationUpdate transformer = new ApiImplementationUpdate();
classBytes = transformer.transform(clazz.getClassLoader(), null, null, null, classBytes, null, null);
// verify no missing methods
adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
expectNoMissingMethods(adapter, classBytes);
}
use of com.newrelic.agent.instrumentation.RequireMethodsAdapter in project newrelic-java-agent by newrelic.
the class ApiImplementationUpdateTest method testMatcher.
@Test
public void testMatcher() throws Exception {
InstrumentationContext iInstrumentationContext = Mockito.mock(InstrumentationContext.class);
Class<?> clazz = RequestImpl.class;
Set<Method> methodsToRemove = new HashSet<>();
byte[] classBytes = removeMethods(clazz, methodsToRemove);
// verify no missing methods
RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
expectNoMissingMethods(adapter, classBytes);
ApiImplementationUpdate transformer = new ApiImplementationUpdate();
ClassMatchVisitorFactory matcher = transformer.getMatcher();
ClassReader reader = new ClassReader(new ByteArrayInputStream(classBytes));
ClassVisitor visitor = matcher.newClassMatchVisitor(clazz.getClassLoader(), null, reader, null, iInstrumentationContext);
reader.accept(visitor, ClassReader.SKIP_CODE);
Mockito.verify(iInstrumentationContext, Mockito.never()).putMatch(Mockito.<ClassMatchVisitorFactory>anyObject(), Mockito.<Match>anyObject());
}
Aggregations