Search in sources :

Example 11 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.

the class ManifestAppenderTest method appendsValues.

@Test
public void appendsValues() throws IOException {
    WeavePackage mockPackage = mock(WeavePackage.class);
    Map<Attributes.Name, String> expectedValues = new HashMap<>();
    when(mockPackage.getAllRequiredAnnotationClasses()).thenReturn(new HashSet<>(Arrays.asList("reqann1", "reqann2")));
    expectedValues.put(new Attributes.Name(CachedWeavePackage.CLASS_REQUIRED_ANNOTATIONS_MANIFEST_ATTRIBUTE_NAME), "reqann1,reqann2");
    when(mockPackage.getAllRequiredMethodAnnotationClasses()).thenReturn(new HashSet<>(Arrays.asList("reqmeth1", "reqmeth2")));
    expectedValues.put(new Attributes.Name(CachedWeavePackage.METHOD_REQUIRED_ANNOTATIONS_MANIFEST_ATTRIBUTE_NAME), "reqmeth1,reqmeth2");
    when(mockPackage.getIllegalClasses()).thenReturn(Collections.singleton("skipme"));
    expectedValues.put(new Attributes.Name(CachedWeavePackage.ILLEGAL_CLASSES_MANIFEST_ATTRIBUTE_NAME), "skipme");
    when(mockPackage.getMatchTypes()).thenReturn(Collections.singletonMap("matchtypek", MatchType.BaseClass));
    expectedValues.put(new Attributes.Name(CachedWeavePackage.WEAVE_CLASSES_MANIFEST_ATTRIBUTE_NAME), "matchtypek");
    when(mockPackage.getMethodSignatures()).thenReturn(new HashSet<>(Arrays.asList("zzzsignature", "aaasignature")));
    expectedValues.put(new Attributes.Name(CachedWeavePackage.WEAVE_METHODS_MANIFEST_ATTRIBUTE_NAME), "\"aaasignature\",\"zzzsignature\"");
    when(mockPackage.getReferencedClassNames()).thenReturn(Collections.emptySet());
    expectedValues.put(new Attributes.Name(CachedWeavePackage.REFERENCE_CLASSES_MANIFEST_ATTRIBUTE_NAME), "");
    expectedValues.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    ManifestAppender target = new ManifestAppender();
    target.copyAttributesToManifest(mockPackage);
    assertEquals(expectedValues, target.getManifest().getMainAttributes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    target.getManifest().write(baos);
    String manifest = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    for (Map.Entry<Attributes.Name, String> expectedEntry : expectedValues.entrySet()) {
        assertTrue(manifest.contains(expectedEntry.getKey().toString() + ": " + expectedEntry.getValue() + "\r\n"));
    }
}
Also used : HashMap(java.util.HashMap) Attributes(java.util.jar.Attributes) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 12 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.

the class RegisterInstrumentationCloseableTest method init.

@BeforeClass
public static void init() throws IOException {
    List<byte[]> weaveBytes = new ArrayList<>();
    WeavePackageConfig config = WeavePackageConfig.builder().name("agent_unittest").source("newrelic_agent.unit_test").build();
    testPackage = new WeavePackage(config, weaveBytes);
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ArrayList(java.util.ArrayList) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) BeforeClass(org.junit.BeforeClass)

Example 13 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.

the class WeavePackageVerifier method verify.

/**
 * Run validation using the weave package and classpath and return whether or not the weave package verified.
 *
 * @return whether or not the weave package verified
 */
private boolean verify() throws Exception {
    WeavePackage weavePackage = getWeavePackage(instrumentationJar);
    ClassLoader loader = createClassloaderForVerification(userJars);
    ClassCache cache = new ClassCache(new ClassLoaderFinder(loader));
    PackageValidationResult result = weavePackage.validate(cache);
    if (!result.succeeded()) {
        for (WeaveViolation violation : result.getViolations()) {
            out.println(violation.toString());
        }
    }
    return result.succeeded();
}
Also used : ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) CachedWeavePackage(com.newrelic.weave.weavepackage.CachedWeavePackage) WeaveViolation(com.newrelic.weave.violation.WeaveViolation) ClassCache(com.newrelic.weave.utils.ClassCache) PackageValidationResult(com.newrelic.weave.weavepackage.PackageValidationResult)

Example 14 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.

the class EnumAccessTest method makeWeavePackage.

public static WeavePackage makeWeavePackage() throws IOException {
    List<byte[]> weaveBytes = new ArrayList<>();
    weaveBytes.add(WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumAccessTest$State_Instrumentation"));
    weaveBytes.add(WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumAccessTest$Foo_Instrumentation"));
    WeavePackageConfig config = WeavePackageConfig.builder().name("weave_unittest").source("com.newrelic.weave.weavepackage.testclasses").build();
    return new WeavePackage(config, weaveBytes);
}
Also used : WeavePackageConfig(com.newrelic.weave.weavepackage.WeavePackageConfig) ArrayList(java.util.ArrayList) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage)

Example 15 with WeavePackage

use of com.newrelic.weave.weavepackage.WeavePackage in project newrelic-java-agent by newrelic.

the class EnumAccessTest method setup.

@Before
public void setup() throws IOException {
    WeavePackage weavePackage = makeWeavePackage();
    ClassCache cache = new ClassCache(new ClassLoaderFinder(EnumAccessTest.class.getClassLoader()));
    PackageValidationResult result = weavePackage.validate(cache);
    WeaveTestUtils.expectViolations(result);
    WeaveTestUtils.loadUtilityClasses(EnumAccessTest.class.getClassLoader(), result.computeUtilityClassBytes(cache));
    byte[] stateCompositeBytes = result.weave("com/newrelic/weave/EnumAccessTest$Foo$State", new String[0], new String[0], WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumAccessTest$Foo$State"), cache, Collections.emptyMap()).getCompositeBytes(cache);
    Assert.assertNotNull(stateCompositeBytes);
    byte[] aCompositeBytes = result.weave("com/newrelic/weave/EnumAccessTest$Foo", new String[0], new String[0], WeaveTestUtils.getClassBytes("com.newrelic.weave.EnumAccessTest$Foo"), cache, Collections.emptyMap()).getCompositeBytes(cache);
    Assert.assertNotNull(aCompositeBytes);
    WeaveTestUtils.addToContextClassloader("com.newrelic.weave.EnumAccessTest$Foo$State", stateCompositeBytes);
    WeaveTestUtils.addToContextClassloader("com.newrelic.weave.EnumAccessTest$Foo", aCompositeBytes);
}
Also used : ClassLoaderFinder(com.newrelic.weave.utils.ClassLoaderFinder) WeavePackage(com.newrelic.weave.weavepackage.WeavePackage) ClassCache(com.newrelic.weave.utils.ClassCache) PackageValidationResult(com.newrelic.weave.weavepackage.PackageValidationResult) Before(org.junit.Before)

Aggregations

WeavePackage (com.newrelic.weave.weavepackage.WeavePackage)18 WeavePackageConfig (com.newrelic.weave.weavepackage.WeavePackageConfig)9 CachedWeavePackage (com.newrelic.weave.weavepackage.CachedWeavePackage)8 ArrayList (java.util.ArrayList)7 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)4 IOException (java.io.IOException)4 ClassTransformerConfig (com.newrelic.agent.config.ClassTransformerConfig)3 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)3 ClassCache (com.newrelic.weave.utils.ClassCache)3 ClassLoaderFinder (com.newrelic.weave.utils.ClassLoaderFinder)3 PackageValidationResult (com.newrelic.weave.weavepackage.PackageValidationResult)3 URL (java.net.URL)3 HashSet (java.util.HashSet)3 JarInputStream (java.util.jar.JarInputStream)3 AgentConfig (com.newrelic.agent.config.AgentConfig)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Map (java.util.Map)2 Test (org.junit.Test)2 AgentClassLoaderBaseInstrumentation (com.newrelic.agent.instrumentation.builtin.AgentClassLoaderBaseInstrumentation)1