Search in sources :

Example 6 with InstrumentationContext

use of com.newrelic.agent.instrumentation.context.InstrumentationContext in project newrelic-java-agent by newrelic.

the class TraceByReturnTypeMatchVisitorTest method testStringReturnType.

@Test
public void testStringReturnType() throws IOException {
    InstrumentationContext context = instrumentClass(StringInstrumented.class);
    Set<Method> tracedMethods = context.getTraceInformation().getTraceAnnotations().keySet();
    Set<String> tracedMethodNames = methodNames(tracedMethods);
    Assert.assertTrue(allMatchTypeDescriptor(tracedMethods, String.class));
    Assert.assertThat(tracedMethodNames, containsInAnyOrder("returnString"));
    Assert.assertThat(tracedMethodNames, not(contains("returnInteger")));
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) Method(org.objectweb.asm.commons.Method) Test(org.junit.Test)

Example 7 with InstrumentationContext

use of com.newrelic.agent.instrumentation.context.InstrumentationContext in project newrelic-java-agent by newrelic.

the class TraceByReturnTypeMatchVisitorTest method testFutureReturnType.

@Test
public void testFutureReturnType() throws IOException {
    InstrumentationContext context = instrumentClass(JavaFutureInstrumented.class);
    Set<Method> tracedMethods = context.getTraceInformation().getTraceAnnotations().keySet();
    Set<String> tracedMethodNames = methodNames(tracedMethods);
    Assert.assertTrue(allMatchTypeDescriptor(tracedMethods, Future.class));
    Assert.assertThat(tracedMethodNames, containsInAnyOrder("returnFuture", "submitFuture", "access$000"));
    Assert.assertThat(tracedMethodNames, not(contains("returnString", "returnInteger")));
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) Future(java.util.concurrent.Future) Method(org.objectweb.asm.commons.Method) Test(org.junit.Test)

Example 8 with InstrumentationContext

use of com.newrelic.agent.instrumentation.context.InstrumentationContext in project newrelic-java-agent by newrelic.

the class ClassNoticingFactoryTest method noticingJunit.

@Test
public void noticingJunit() throws IOException {
    ExecutorService executorService = mock(ExecutorService.class);
    JarAnalystFactory factory = mock(JarAnalystFactory.class);
    when(factory.createURLAnalyzer(any(URL.class))).thenReturn(mock(Runnable.class));
    ClassNoticingFactory target = new ClassNoticingFactory(factory, executorService, new TestLogger());
    target.newClassMatchVisitor(Test.class.getClassLoader(), Test.class, new ClassReader("org.junit.Test"), new ClassWriter(0), new InstrumentationContext(null, Test.class, Test.class.getProtectionDomain()));
    ArgumentCaptor<URL> captor = ArgumentCaptor.forClass(URL.class);
    verify(factory, times(1)).createURLAnalyzer(captor.capture());
    // Yes, this test will fail if you upgrade junit. Should be an easy fix, but sorry!
    assertTrue(captor.getValue().toString(), captor.getValue().toString().endsWith("junit-4.12.jar"));
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) Test(org.junit.Test) ExecutorService(java.util.concurrent.ExecutorService) ClassReader(org.objectweb.asm.ClassReader) TestLogger(com.newrelic.agent.bridge.TestLogger) URL(java.net.URL) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Example 9 with InstrumentationContext

use of com.newrelic.agent.instrumentation.context.InstrumentationContext in project newrelic-java-agent by newrelic.

the class ClassNoticingFactoryTest method noticingTestClass.

@Test
public void noticingTestClass() throws IOException {
    ExecutorService executorService = mock(ExecutorService.class);
    JarAnalystFactory factory = mock(JarAnalystFactory.class);
    when(factory.createURLAnalyzer(any(URL.class))).thenReturn(mock(Runnable.class));
    ClassNoticingFactory target = new ClassNoticingFactory(factory, executorService, new TestLogger());
    target.newClassMatchVisitor(this.getClass().getClassLoader(), this.getClass(), new ClassReader(this.getClass().getName()), new ClassWriter(0), new InstrumentationContext(null, this.getClass(), this.getClass().getProtectionDomain()));
    String codeSourceLocation = this.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
    if (codeSourceLocation.endsWith("/")) {
        // we expect the test classes to be staged in a gradle directory, not in a jar.
        verifyZeroInteractions(factory, executorService);
    } else {
        fail("Unexpected jar for test class?? " + codeSourceLocation);
    }
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) ExecutorService(java.util.concurrent.ExecutorService) ClassReader(org.objectweb.asm.ClassReader) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TestLogger(com.newrelic.agent.bridge.TestLogger) URL(java.net.URL) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Example 10 with InstrumentationContext

use of com.newrelic.agent.instrumentation.context.InstrumentationContext in project newrelic-java-agent by newrelic.

the class OptimizedClassMatcherTest method testGenericsMatch.

@Test
public void testGenericsMatch() throws IOException {
    InstrumentationContext instrumentationContext = getInstrumentationContext(matcher, ArgGenerics.class);
    Match match = instrumentationContext.getMatches().values().iterator().next();
    Assert.assertNotNull(match);
    Assert.assertEquals(3, match.getMethods().size());
    Assert.assertTrue(match.getMethods().contains(new Method("add", "(Ljava/lang/Object;)Z")));
    Assert.assertEquals(match.getMethods().size(), match.getClassMatches().size());
    Assert.assertTrue(match.getClassMatches().containsKey(match7));
    Assert.assertEquals(2, instrumentationContext.getBridgeMethods().size());
    Assert.assertEquals(new Method("add", "(Ljava/lang/String;)Z"), instrumentationContext.getBridgeMethods().get(new Method("add", "(Ljava/lang/Object;)Z")));
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) Method(org.objectweb.asm.commons.Method) Match(com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match) Test(org.junit.Test)

Aggregations

InstrumentationContext (com.newrelic.agent.instrumentation.context.InstrumentationContext)15 Test (org.junit.Test)10 Method (org.objectweb.asm.commons.Method)9 ClassReader (org.objectweb.asm.ClassReader)7 ClassVisitor (org.objectweb.asm.ClassVisitor)4 ClassWriter (org.objectweb.asm.ClassWriter)4 Match (com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match)3 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)3 TestLogger (com.newrelic.agent.bridge.TestLogger)2 RequireMethodsAdapter (com.newrelic.agent.instrumentation.RequireMethodsAdapter)2 ClassMatcherTest (com.newrelic.agent.instrumentation.classmatchers.ClassMatcherTest)2 TraceClassVisitor (com.newrelic.agent.instrumentation.tracing.TraceClassVisitor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 ClassReader (com.newrelic.agent.deps.org.objectweb.asm.ClassReader)1 ClassVisitor (com.newrelic.agent.deps.org.objectweb.asm.ClassVisitor)1 ClassWriter (com.newrelic.agent.deps.org.objectweb.asm.ClassWriter)1