From 8e18ba0d0442f7bee362a0e37566070afaf12412 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Thu, 19 Feb 2015 09:01:25 +0100 Subject: [PATCH] Added additional test cases. --- .../type/auxiliary/TypeProxy.java | 2 +- .../member/MethodInvocationOtherTest.java | 4 +++ .../type/auxiliary/TypeProxyCreationTest.java | 28 ++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/instrumentation/type/auxiliary/TypeProxy.java b/byte-buddy-dep/src/main/java/net/bytebuddy/instrumentation/type/auxiliary/TypeProxy.java index 212358b0b6d..d31c5f908f2 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/instrumentation/type/auxiliary/TypeProxy.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/instrumentation/type/auxiliary/TypeProxy.java @@ -882,7 +882,7 @@ protected AccessorMethodInvocation(MethodDescription instrumentedMethod, @Override public boolean isValid() { - return true; + return specialMethodInvocation.isValid(); } @Override diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/instrumentation/method/bytecode/stack/member/MethodInvocationOtherTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/instrumentation/method/bytecode/stack/member/MethodInvocationOtherTest.java index 51635decfc9..b75243b3756 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/instrumentation/method/bytecode/stack/member/MethodInvocationOtherTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/instrumentation/method/bytecode/stack/member/MethodInvocationOtherTest.java @@ -10,6 +10,8 @@ import org.junit.Test; import org.objectweb.asm.MethodVisitor; +import java.util.List; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.mock; @@ -42,6 +44,8 @@ public void testIllegal() throws Exception { is((StackManipulation) StackManipulation.Illegal.INSTANCE)); assertThat(MethodInvocation.IllegalInvocation.INSTANCE.virtual(mock(TypeDescription.class)), is((StackManipulation) StackManipulation.Illegal.INSTANCE)); + assertThat(MethodInvocation.IllegalInvocation.INSTANCE.dynamic(FOO, mock(TypeDescription.class), mock(TypeList.class), mock(List.class)), + is((StackManipulation) StackManipulation.Illegal.INSTANCE)); MethodInvocation.IllegalInvocation.INSTANCE.apply(mock(MethodVisitor.class), mock(Instrumentation.Context.class)); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/instrumentation/type/auxiliary/TypeProxyCreationTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/instrumentation/type/auxiliary/TypeProxyCreationTest.java index d28d8cc03b1..e4bd509d38f 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/instrumentation/type/auxiliary/TypeProxyCreationTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/instrumentation/type/auxiliary/TypeProxyCreationTest.java @@ -3,12 +3,15 @@ import net.bytebuddy.ClassFileVersion; import net.bytebuddy.instrumentation.Instrumentation; import net.bytebuddy.instrumentation.ModifierContributor; +import net.bytebuddy.instrumentation.field.FieldDescription; +import net.bytebuddy.instrumentation.field.FieldList; import net.bytebuddy.instrumentation.method.MethodDescription; import net.bytebuddy.instrumentation.method.MethodList; import net.bytebuddy.instrumentation.method.MethodLookupEngine; import net.bytebuddy.instrumentation.method.bytecode.stack.StackManipulation; import net.bytebuddy.instrumentation.type.TypeDescription; import net.bytebuddy.instrumentation.type.TypeList; +import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.test.utility.MockitoRule; import net.bytebuddy.test.utility.MoreOpcodes; import org.junit.Before; @@ -311,10 +314,33 @@ public void testForReflectionFactoryConstruction() throws Exception { } @Test - public void testInstrumentationsValid() throws Exception { + public void testInstrumentationIsValid() throws Exception { assertThat(TypeProxy.AbstractMethodErrorThrow.INSTANCE.isValid(), is(true)); } + @Test + @SuppressWarnings("unchecked") + public void testAccessorIsValid() throws Exception { + TypeProxy typeProxy = new TypeProxy(mock(TypeDescription.class), + mock(Instrumentation.Target.class), + mock(TypeProxy.InvocationFactory.class), + false, + false); + TypeProxy.MethodCall methodCall = typeProxy.new MethodCall(mock(AuxiliaryType.MethodAccessorFactory.class)); + TypeDescription instrumentedType = mock(TypeDescription.class); + FieldList fieldList = mock(FieldList.class); + when(fieldList.filter(any(ElementMatcher.class))).thenReturn(fieldList); + when(fieldList.getOnly()).thenReturn(mock(FieldDescription.class)); + when(instrumentedType.getDeclaredFields()).thenReturn(fieldList); + TypeProxy.MethodCall.Appender appender = methodCall.new Appender(instrumentedType); + Instrumentation.SpecialMethodInvocation specialMethodInvocation = mock(Instrumentation.SpecialMethodInvocation.class); + when(specialMethodInvocation.isValid()).thenReturn(true); + StackManipulation stackManipulation = appender.new AccessorMethodInvocation(mock(MethodDescription.class), specialMethodInvocation); + assertThat(stackManipulation.isValid(), is(true)); + verify(specialMethodInvocation).isValid(); + verifyNoMoreInteractions(specialMethodInvocation); + } + public static class Foo { private Void target;