Search in sources :

Example 1 with ClassFileLocator

use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.

the class AgentBuilderRedefinitionStrategyResubmissionStrategyTest method testRedefinition.

@Test
@SuppressWarnings("unchecked")
public void testRedefinition() 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));
        }
    });
    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);
    verify(instrumentation).redefineClasses(Mockito.argThat(new BaseMatcher<ClassDefinition[]>() {

        @Override
        public boolean matches(Object o) {
            return ((ClassDefinition) o).getDefinitionClass() == Foo.class && Arrays.equals(((ClassDefinition) o).getDefinitionClassFile(), new byte[] { 1, 2, 3 });
        }

        @Override
        public void describeTo(Description description) {
        }
    }));
    verifyNoMoreInteractions(instrumentation);
    verify(rawMatcher).matches(new TypeDescription.ForLoadedType(Foo.class), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), Foo.class, Foo.class.getProtectionDomain());
    verifyNoMoreInteractions(rawMatcher);
    verify(redefinitionBatchAllocator).batch(Collections.<Class<?>>singletonList(Foo.class));
    verifyNoMoreInteractions(redefinitionBatchAllocator);
    verify(listener).onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), false, error);
    verifyNoMoreInteractions(listener);
    verify(matcher).matches(error);
    verifyNoMoreInteractions(matcher);
}
Also used : Description(org.hamcrest.Description) TypeDescription(net.bytebuddy.description.type.TypeDescription) ClassDefinition(java.lang.instrument.ClassDefinition) ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) BaseMatcher(org.hamcrest.BaseMatcher) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TypeDescription(net.bytebuddy.description.type.TypeDescription) List(java.util.List) Test(org.junit.Test)

Example 2 with ClassFileLocator

use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.

the class AgentBuilderRedefinitionStrategyResubmissionStrategyTest method testRedefinitionNonMatchedError.

@Test
@SuppressWarnings("unchecked")
public void testRedefinitionNonMatchedError() 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));
        }
    });
    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(false);
    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);
    verifyZeroInteractions(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);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TypeDescription(net.bytebuddy.description.type.TypeDescription) List(java.util.List) Test(org.junit.Test)

Example 3 with ClassFileLocator

use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.

the class AgentBuilderRedefinitionStrategyResubmissionStrategyTest method testRedefinitionAlreadyLoaded.

@Test
@SuppressWarnings("unchecked")
public void testRedefinitionAlreadyLoaded() 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));
        }
    });
    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(false);
    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), true, error);
    ArgumentCaptor<Runnable> argumentCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(resubmissionScheduler).isAlive();
    verify(resubmissionScheduler).schedule(argumentCaptor.capture());
    argumentCaptor.getValue().run();
    verifyNoMoreInteractions(resubmissionScheduler);
    verifyZeroInteractions(instrumentation);
    verifyZeroInteractions(rawMatcher);
    verifyZeroInteractions(redefinitionBatchAllocator);
    verify(listener).onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), true, error);
    verifyNoMoreInteractions(listener);
    verifyZeroInteractions(matcher);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TypeDescription(net.bytebuddy.description.type.TypeDescription) List(java.util.List) Test(org.junit.Test)

Example 4 with ClassFileLocator

use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.

the class AgentBuilderRedefinitionStrategyResubmissionStrategyTest method testRedefinitionNonAlive.

@Test
@SuppressWarnings("unchecked")
public void testRedefinitionNonAlive() 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));
        }
    });
    when(rawMatcher.matches(new TypeDescription.ForLoadedType(Foo.class), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), Foo.class, Foo.class.getProtectionDomain())).thenReturn(false);
    when(matcher.matches(error)).thenReturn(true);
    when(resubmissionScheduler.isAlive()).thenReturn(false);
    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);
    verify(resubmissionScheduler).isAlive();
    verifyNoMoreInteractions(resubmissionScheduler);
    verifyZeroInteractions(instrumentation);
    verifyZeroInteractions(rawMatcher);
    verifyZeroInteractions(redefinitionBatchAllocator);
    verify(listener).onError(Foo.class.getName(), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), false, error);
    verifyNoMoreInteractions(listener);
    verifyZeroInteractions(matcher);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TypeDescription(net.bytebuddy.description.type.TypeDescription) List(java.util.List) Test(org.junit.Test)

Example 5 with ClassFileLocator

use of net.bytebuddy.dynamic.ClassFileLocator in project byte-buddy by raphw.

the class AgentBuilderRedefinitionStrategyResubmissionStrategyTest method testRedefinitionNonMatched.

@Test
@SuppressWarnings("unchecked")
public void testRedefinitionNonMatched() 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));
        }
    });
    when(rawMatcher.matches(new TypeDescription.ForLoadedType(Foo.class), Foo.class.getClassLoader(), JavaModule.ofType(Foo.class), Foo.class, Foo.class.getProtectionDomain())).thenReturn(false);
    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);
    verifyNoMoreInteractions(listener);
    verify(matcher).matches(error);
    verifyNoMoreInteractions(matcher);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TypeDescription(net.bytebuddy.description.type.TypeDescription) List(java.util.List) Test(org.junit.Test)

Aggregations

ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)31 Test (org.junit.Test)29 TypeDescription (net.bytebuddy.description.type.TypeDescription)17 AbstractTypeDescriptionTest (net.bytebuddy.description.type.AbstractTypeDescriptionTest)15 List (java.util.List)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 ByteBuddy (net.bytebuddy.ByteBuddy)6 File (java.io.File)2 Instrumentation (java.lang.instrument.Instrumentation)2 ArrayList (java.util.ArrayList)2 TypePool (net.bytebuddy.pool.TypePool)2 IOException (java.io.IOException)1 ClassDefinition (java.lang.instrument.ClassDefinition)1 Callable (java.util.concurrent.Callable)1 RandomString (net.bytebuddy.utility.RandomString)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 GradleException (org.gradle.api.GradleException)1 BaseMatcher (org.hamcrest.BaseMatcher)1 Description (org.hamcrest.Description)1