Search in sources :

Example 6 with Pointcut

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut in project newrelic-java-agent by newrelic.

the class ExtensionDomParserTest method testDomOnePointCut.

@Test
public void testDomOnePointCut() throws Exception {
    Extension ext = ExtensionDomParser.readFile(getFile(FILE_PATH_1));
    // ext attributes
    Assert.assertEquals("test1", ext.getName());
    Assert.assertEquals(1.0, ext.getVersion(), .001);
    Assert.assertTrue(ext.isEnabled());
    Instrumentation inst = ext.getInstrumentation();
    Assert.assertEquals("PREFIX", inst.getMetricPrefix());
    List<Pointcut> thePcs = inst.getPointcut();
    Assert.assertEquals(1, thePcs.size());
    Pointcut pc = thePcs.get(0);
    Assert.assertTrue(pc.isTransactionStartPoint());
    Assert.assertFalse(pc.isIgnoreTransaction());
    Assert.assertFalse(pc.isExcludeFromTransactionTrace());
    Assert.assertFalse(pc.isLeaf());
    Assert.assertNull(pc.getMetricNameFormat());
    Assert.assertEquals("test.CustomExampleTest", pc.getClassName().getValue());
    List<Method> methods = pc.getMethod();
    Assert.assertEquals(2, methods.size());
    Assert.assertEquals("run", methods.get(0).getName());
    Assert.assertEquals("finish", methods.get(1).getName());
    Assert.assertEquals("(Ljava/lang/String;Ljava/lang/String;)", MethodParameters.getDescriptor(methods.get(0).getParameters()));
    Assert.assertEquals("(F)", MethodParameters.getDescriptor(methods.get(1).getParameters()));
    List<ExtensionClassAndMethodMatcher> pcs = ExtensionConversionUtility.convertToPointCutsForValidation(ext);
    Assert.assertNotNull(pcs);
    Assert.assertEquals(1, pcs.size());
    ExtensionClassAndMethodMatcher actual = pcs.get(0);
    // test method matching
    Assert.assertTrue(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "run", "(Ljava/lang/String;Ljava/lang/String;)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(Ljava/lang/String;Ljava/lang/String;)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(F)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "run", "(F)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(B)V", com.google.common.collect.ImmutableSet.<String>of()));
    // test class matching
    Assert.assertEquals(1, actual.getClassMatcher().getClassNames().size());
    Assert.assertEquals("test/CustomExampleTest", actual.getClassMatcher().getClassNames().toArray()[0]);
}
Also used : Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Test(org.junit.Test)

Example 7 with Pointcut

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut in project newrelic-java-agent by newrelic.

the class ExtensionConversionUtilityTest method createClassMatcher.

@Test
public void createClassMatcher() throws XmlException {
    Pointcut pointcut = new Pointcut();
    ClassName name = new ClassName();
    name.setValue(ArrayList.class.getName());
    pointcut.setClassName(name);
    ClassMatcher classMatcher = ExtensionConversionUtility.createClassMatcher(pointcut, "test");
    Assert.assertTrue(classMatcher instanceof ExactClassMatcher);
    Assert.assertTrue(classMatcher.isMatch(ArrayList.class));
}
Also used : Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) ExactClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher) ClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ClassMatcher) ExactClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher) ClassName(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.ClassName) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with Pointcut

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut in project newrelic-java-agent by newrelic.

the class ExtensionConversionUtilityTest method createClassMatcherReturnType.

@Test(expected = XmlException.class)
public void createClassMatcherReturnType() throws XmlException {
    Pointcut pointcut = new Pointcut();
    Method m = new Method();
    m.setReturnType(Action.class.getName());
    pointcut.getMethod().add(m);
    ExtensionConversionUtility.createClassMatcher(pointcut, "test");
}
Also used : Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Test(org.junit.Test)

Example 9 with Pointcut

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut in project newrelic-java-agent by newrelic.

the class ReinstrumentUtilsTest method checkInputNotMatchingClasses.

@Test
public void checkInputNotMatchingClasses() {
    SampleObject obj = new SampleObject();
    ReinstrumentResult result = new ReinstrumentResult();
    obj.getClass().getClassLoader();
    Set<ClassLoader> loaders = new HashSet<>();
    loaders.add(this.getClass().getClassLoader());
    Extension ext = new Extension();
    Instrumentation inst = new Instrumentation();
    ext.setInstrumentation(inst);
    Pointcut pc = new Pointcut();
    inst.getPointcut().add(pc);
    setClassName(pc, SampleObject.class);
    // this should match
    Method m1 = new Method();
    m1.setName("falala");
    MethodParameters params1 = new MethodParameters(new ArrayList<String>());
    m1.setParameters(params1);
    // this should not match
    Method m2 = new Method();
    m2.setName("notAMatch");
    MethodParameters params2 = new MethodParameters(Arrays.asList("boolean"));
    m2.setParameters(params2);
    // this should not match
    Method m3 = new Method();
    m3.setName("getHello");
    MethodParameters params3 = new MethodParameters(Arrays.asList("java.lang.String", "int"));
    m3.setParameters(params3);
    pc.getMethod().addAll(Arrays.asList(m1, m2, m3));
    Map<String, Class<?>> daClasses = new HashMap<>();
    daClasses.put(SampleObject.class.getName(), SampleObject.class);
    ReinstrumentUtils.checkInputClasses(result, loaders, ext, daClasses);
    Object actual = result.getStatusMap().get(ReinstrumentResult.ERROR_KEY);
    Assert.assertNotNull("There should not have been an error. Errors: " + actual, actual);
    Assert.assertFalse("The method " + m1.getName() + " should  NOT be in the error message", ((String) actual).contains(m1.getName()));
    Assert.assertTrue("The method " + m2.getName() + " should be in the error message", ((String) actual).contains(m2.getName()));
    Assert.assertTrue("The method " + m3.getName() + " should be in the error message", ((String) actual).contains(m3.getName()));
}
Also used : HashMap(java.util.HashMap) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) MethodParameters(com.newrelic.agent.extension.beans.MethodParameters) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with Pointcut

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut in project newrelic-java-agent by newrelic.

the class ReinstrumentUtilsTest method checkInputClassesNullPointers3.

@Test
public void checkInputClassesNullPointers3() {
    SampleObject obj = new SampleObject();
    ReinstrumentResult result = new ReinstrumentResult();
    obj.getClass().getClassLoader();
    Set<ClassLoader> loaders = new HashSet<>();
    loaders.add(this.getClass().getClassLoader());
    Extension ext = new Extension();
    Instrumentation inst = new Instrumentation();
    ext.setInstrumentation(inst);
    Pointcut pc = new Pointcut();
    inst.getPointcut().add(pc);
    // no class name set
    Method m1 = new Method();
    m1.setName("falala");
    MethodParameters params1 = new MethodParameters(new ArrayList<String>());
    m1.setParameters(params1);
    Map<String, Class<?>> daClasses = new HashMap<>();
    daClasses.put(SampleObject.class.getName(), SampleObject.class);
    ReinstrumentUtils.checkInputClasses(result, loaders, ext, daClasses);
    Object actual = result.getStatusMap().get(ReinstrumentResult.ERROR_KEY);
    Assert.assertNull("There should not have been an error. Errors: " + actual, actual);
}
Also used : HashMap(java.util.HashMap) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) MethodParameters(com.newrelic.agent.extension.beans.MethodParameters) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Pointcut (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut)22 Test (org.junit.Test)19 Instrumentation (com.newrelic.agent.extension.beans.Extension.Instrumentation)17 Extension (com.newrelic.agent.extension.beans.Extension)16 Method (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method)12 ExtensionClassAndMethodMatcher (com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher)9 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)6 MethodParameters (com.newrelic.agent.extension.beans.MethodParameters)4 ArrayList (java.util.ArrayList)4 ClassName (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.ClassName)2 AgentHelper.getFile (com.newrelic.agent.AgentHelper.getFile)1 XmlException (com.newrelic.agent.extension.util.XmlException)1 ChildClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ChildClassMatcher)1 ClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ClassMatcher)1 ExactClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher)1 InterfaceMatcher (com.newrelic.agent.instrumentation.classmatchers.InterfaceMatcher)1 ExactReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher)1 File (java.io.File)1