Some more attempts to UT glassfish classloader

This commit is contained in:
Luca Abbati 2019-05-28 19:39:11 +02:00
parent f62e1dedeb
commit 37b734dd50
No known key found for this signature in database
GPG Key ID: C901DDA2FFE14529
5 changed files with 69 additions and 36 deletions

@ -1 +1 @@
Subproject commit 3e38b4e75edcee3ca84f022ea50240b0fc0537f2 Subproject commit e6a01f9e885ac9b71c0ffec8c28dc75668570b15

View File

@ -28,7 +28,7 @@ dependencies {
implementation deps.autoservice implementation deps.autoservice
testCompile project(':dd-java-agent:testing') testCompile project(':dd-java-agent:testing')
testCompile group: 'org.glassfish.main.extras', name: 'glassfish-embedded-all', version: '4.1.2' // testCompile group: 'org.glassfish.main.extras', name: 'glassfish-embedded-all', version: '4.1.2'
latestDepTestCompile sourceSets.test.output latestDepTestCompile sourceSets.test.output
} }

View File

@ -0,0 +1,33 @@
package datadog.trace.instrumentation.glassfish4
import com.sun.enterprise.v3.server.APIClassLoaderServiceImpl
import datadog.trace.agent.test.AgentTestRunner
class GlassfishInstrumentationTest extends AgentTestRunner {
def "classes not related to datadog can be black-listed"() {
setup:
def cli = new APIClassLoaderServiceImpl()
def cl = cli.getApiClassLoader()
cl.triggerAddToBlackList('com.some.Entity')
// def rf = ReflectionFactory.reflectionFactory
// def clazz = Class.forName('com.sun.enterprise.v3.server.APIClassLoaderServiceImpl$APIClassLoader')
// def objDef = clazz.getDeclaredConstructor(ClassLoader.class, ClassLoader.class)
// def intConstr = rf.newConstructorForSerialization(clazz, objDef)
// def instance = clazz.cast(intConstr.newInstance())
expect:
'com.some.Entity' in cl.blacklist
}
def "classes related to datadog are not black-listed"() {
setup:
def cli = new APIClassLoaderServiceImpl()
def cl = cli.getApiClassLoader()
cl.triggerAddToBlackList('io.opentracing.some.Entity')
expect:
!('io.opentracing.some.Entity' in cl.blacklist)
'__datadog_no_blacklist.io.opentracing.some.Entity' in cl.blacklist
}
}

View File

@ -0,0 +1,34 @@
package com.sun.enterprise.v3.server;
import java.util.HashSet;
import java.util.Set;
public class APIClassLoaderServiceImpl {
private APIClassLoader instance;
public APIClassLoaderServiceImpl() {
this.instance = new APIClassLoader();
}
public APIClassLoader getApiClassLoader() {
return instance;
}
private class APIClassLoader {
private Set<String> blacklist = new HashSet<String>();
public void triggerAddToBlackList(String name) {
addToBlackList(name);
}
private synchronized void addToBlackList(String name) {
blacklist.add(name);
}
public Set<String> getBlacklist() {
return blacklist;
}
}
}

View File

@ -1,34 +0,0 @@
package datadog.trace.instrumentation.glassfish4
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.agent.tooling.Constants
import net.bytebuddy.matcher.NameMatcher
class GlassfishInstrumentationTest extends AgentTestRunner {
def "test type matches the correct class loader"() {
setup:
def matchingType = new GlassfishInstrumentation().typeMatcher()
expect:
matchingType instanceof NameMatcher
matchingType.toString() == 'name(equals(com.sun.enterprise.v3.server.APIClassLoaderServiceImpl$APIClassLoader))'
}
def "test correct classes are added to the helpers"() {
setup:
def helpers = new GlassfishInstrumentation().helperClassNames()
expect:
Constants.class.getName() in helpers
}
def "test the advice is registered for the 'addToBlackList' method"() {
setup:
def transformers = new GlassfishInstrumentation().transformers()
def transformer = transformers.entrySet()[0]
expect:
transformer.key.toString() == '((isMethod() and name(equals(addToBlackList))) and hasParameter(ofSize(1)))'
}
}