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"));
}
}
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);
}
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();
}
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);
}
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);
}
Aggregations