use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class AgentBuilderRedefinitionStrategyResubmissionStrategyTest method testRedefinitionNonModifiable.
@Test
@SuppressWarnings("unchecked")
public void testRedefinitionNonModifiable() throws Exception {
when(instrumentation.isModifiableClass(Foo.class)).thenReturn(false);
when(redefinitionBatchAllocator.batch(Mockito.any(List.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
return Collections.singleton(invocationOnMock.getArgumentAt(0, List.class));
}
});
when(rawMatcher.matches(new TypeDescription.ForLoadedType(Foo.class), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), Foo.class, Foo.class.getProtectionDomain())).thenReturn(true);
when(matcher.matches(error)).thenReturn(true);
when(resubmissionScheduler.isAlive()).thenReturn(true);
ClassFileLocator classFileLocator = mock(ClassFileLocator.class);
when(locationStrategy.classFileLocator(Foo.class.getClassLoader(), JavaModule.ofType(Foo.class))).thenReturn(classFileLocator);
when(classFileLocator.locate(Foo.class.getName())).thenReturn(new ClassFileLocator.Resolution.Explicit(new byte[] { 1, 2, 3 }));
AgentBuilder.RedefinitionStrategy.ResubmissionStrategy.Installation installation = new AgentBuilder.RedefinitionStrategy.ResubmissionStrategy.Enabled(resubmissionScheduler, matcher).install(instrumentation, locationStrategy, listener, installationListener, circularityLock, rawMatcher, AgentBuilder.RedefinitionStrategy.REDEFINITION, redefinitionBatchAllocator, redefinitionListener);
installation.getInstallationListener().onInstall(instrumentation, classFileTransformer);
installation.getListener().onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), false, error);
ArgumentCaptor<Runnable> argumentCaptor = ArgumentCaptor.forClass(Runnable.class);
verify(resubmissionScheduler).isAlive();
verify(resubmissionScheduler).schedule(argumentCaptor.capture());
argumentCaptor.getValue().run();
verifyNoMoreInteractions(resubmissionScheduler);
verify(instrumentation).isModifiableClass(Foo.class);
verifyNoMoreInteractions(instrumentation);
verifyZeroInteractions(rawMatcher);
verifyZeroInteractions(redefinitionBatchAllocator);
verify(listener).onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), false, error);
verifyNoMoreInteractions(listener);
verify(matcher).matches(error);
verifyNoMoreInteractions(matcher);
}
use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class AgentBuilderRedefinitionStrategyResubmissionStrategyTest method testRedefinitionError.
@Test
@SuppressWarnings("unchecked")
public void testRedefinitionError() throws Exception {
when(instrumentation.isModifiableClass(Foo.class)).thenReturn(true);
when(redefinitionBatchAllocator.batch(Mockito.any(List.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
return Collections.singleton(invocationOnMock.getArgumentAt(0, List.class));
}
});
RuntimeException runtimeException = new RuntimeException();
when(rawMatcher.matches(new TypeDescription.ForLoadedType(Foo.class), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), Foo.class, Foo.class.getProtectionDomain())).thenThrow(runtimeException);
when(matcher.matches(error)).thenReturn(true);
when(resubmissionScheduler.isAlive()).thenReturn(true);
ClassFileLocator classFileLocator = mock(ClassFileLocator.class);
when(locationStrategy.classFileLocator(Foo.class.getClassLoader(), JavaModule.ofType(Foo.class))).thenReturn(classFileLocator);
when(classFileLocator.locate(Foo.class.getName())).thenReturn(new ClassFileLocator.Resolution.Explicit(new byte[] { 1, 2, 3 }));
AgentBuilder.RedefinitionStrategy.ResubmissionStrategy.Installation installation = new AgentBuilder.RedefinitionStrategy.ResubmissionStrategy.Enabled(resubmissionScheduler, matcher).install(instrumentation, locationStrategy, listener, installationListener, circularityLock, rawMatcher, AgentBuilder.RedefinitionStrategy.REDEFINITION, redefinitionBatchAllocator, redefinitionListener);
installation.getInstallationListener().onInstall(instrumentation, classFileTransformer);
installation.getListener().onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), false, error);
ArgumentCaptor<Runnable> argumentCaptor = ArgumentCaptor.forClass(Runnable.class);
verify(resubmissionScheduler).isAlive();
verify(resubmissionScheduler).schedule(argumentCaptor.capture());
argumentCaptor.getValue().run();
verifyNoMoreInteractions(resubmissionScheduler);
verify(instrumentation).isModifiableClass(Foo.class);
verifyNoMoreInteractions(instrumentation);
verify(rawMatcher).matches(new TypeDescription.ForLoadedType(Foo.class), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), Foo.class, Foo.class.getProtectionDomain());
verifyNoMoreInteractions(rawMatcher);
verifyZeroInteractions(redefinitionBatchAllocator);
verify(listener).onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), false, error);
verify(listener).onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), true, runtimeException);
verify(listener).onComplete(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), true);
verifyNoMoreInteractions(listener);
verify(matcher).matches(error);
verifyNoMoreInteractions(matcher);
}
use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class TypePoolDefaultTest method testTypeIsCached.
@Test
public void testTypeIsCached() throws Exception {
ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
TypePool typePool = TypePool.Default.of(classFileLocator);
TypePool.Resolution resolution = typePool.describe(Object.class.getName());
assertThat(typePool.describe(Object.class.getName()).resolve(), CoreMatchers.is(resolution.resolve()));
verify(classFileLocator).locate(Object.class.getName());
verifyNoMoreInteractions(classFileLocator);
}
use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class TypePoolDefaultTest method testReferencedTypeIsCached.
@Test
public void testReferencedTypeIsCached() throws Exception {
ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
TypePool typePool = TypePool.Default.of(classFileLocator);
TypePool.Resolution resolution = typePool.describe(String.class.getName());
assertThat(typePool.describe(String.class.getName()).resolve(), CoreMatchers.is(resolution.resolve()));
assertThat(typePool.describe(String.class.getName()).resolve().getSuperClass().asErasure(), CoreMatchers.is(TypeDescription.OBJECT));
verify(classFileLocator).locate(String.class.getName());
verify(classFileLocator).locate(Object.class.getName());
verifyNoMoreInteractions(classFileLocator);
}
use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.
the class TypePoolDefaultWithLazyResolutionTypeDescriptionTest method testNonGenericSuperInterfaceHierarchyResolutionIsLazy.
@Test
public void testNonGenericSuperInterfaceHierarchyResolutionIsLazy() throws Exception {
ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
assertThat(describe(NonGenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()).getInterfaces().getOnly().asErasure(), CoreMatchers.is((TypeDescription) new TypeDescription.ForLoadedType(SampleInterface.class)));
verify(classFileLocator).locate(NonGenericType.class.getName());
verifyNoMoreInteractions(classFileLocator);
}
Aggregations