Skip to content

Commit

Permalink
Fixed more specific tests for visibility bridges.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Jul 24, 2015
1 parent 61e33f5 commit bb7f972
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 62 deletions.
Expand Up @@ -105,13 +105,13 @@ protected boolean isSort(MethodDescription target) {
if (target.getDeclaringType().asRawType().isInterface()) { if (target.getDeclaringType().asRawType().isInterface()) {
return false; return false;
} }
for (GenericTypeDescription superType : target.getDeclaringType().asRawType().getSuperType()) { for (GenericTypeDescription currentType : target.getDeclaringType().getSuperType()) {
for (MethodDescription methodDescription : superType.getDeclaredMethods()) { for (MethodDescription methodDescription : currentType.getDeclaredMethods()) {
if (methodDescription.asDefined().asToken().accept(GenericTypeDescription.Visitor.TypeErasing.INSTANCE).equals(target.asToken())) { if (methodDescription.asDefined().asToken().equals(target.asToken())) {
return !methodDescription.isBridge() && target.getDeclaringType().asRawType().getDeclaredMethods() return !methodDescription.isBridge() && target.getDeclaringType().asRawType().getDeclaredMethods()
.filter(not(isBridge()) .filter(not(isBridge())
.and(hasMethodName(target.getInternalName())) .and(hasMethodName(methodDescription.getInternalName()))
.and(takesArguments(target.getParameters().asTypeList().asRawTypes()))) .and(takesArguments(methodDescription.getParameters().asTypeList().asRawTypes())))
.isEmpty(); .isEmpty();
} }
} }
Expand All @@ -121,13 +121,6 @@ protected boolean isSort(MethodDescription target) {
} }
}, },


TYPE_VARIABLE_BRIDGE("isTypeVariableBridge()") {
@Override
protected boolean isSort(MethodDescription target) {
return false;
}
},

TYPE_BRIDGE("isTypeBridge()") { TYPE_BRIDGE("isTypeBridge()") {
@Override @Override
protected boolean isSort(MethodDescription target) { protected boolean isSort(MethodDescription target) {
Expand Down
Expand Up @@ -697,7 +697,7 @@ public void testSortIsTypeBridge() throws Exception {
assertThat(ElementMatchers.isTypeBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeOverrideBridge.Inner.class) assertThat(ElementMatchers.isTypeBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeOverrideBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(true)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(true));
assertThat(ElementMatchers.isTypeBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeCombinationBridge.Inner.class) assertThat(ElementMatchers.isTypeBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeCombinationBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(true)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true));
assertThat(ElementMatchers.isTypeBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeSpecializationBridge.Inner.class) assertThat(ElementMatchers.isTypeBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeSpecializationBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true));
assertThat(ElementMatchers.isTypeBridge() assertThat(ElementMatchers.isTypeBridge()
Expand Down Expand Up @@ -737,7 +737,7 @@ public void testSortIsVisibilityBridge() throws Exception {
assertThat(ElementMatchers.isVisibilityBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeOverrideBridge.Inner.class) assertThat(ElementMatchers.isVisibilityBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeOverrideBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(false)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(false));
assertThat(ElementMatchers.isVisibilityBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeCombinationBridge.Inner.class) assertThat(ElementMatchers.isVisibilityBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeCombinationBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(false)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(false));
assertThat(ElementMatchers.isVisibilityBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeSpecializationBridge.Inner.class) assertThat(ElementMatchers.isVisibilityBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeSpecializationBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(false)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(false));
assertThat(ElementMatchers.isVisibilityBridge() assertThat(ElementMatchers.isVisibilityBridge()
Expand Down Expand Up @@ -790,7 +790,7 @@ public void testSortIsBridge() throws Exception {
assertThat(ElementMatchers.isBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeOverrideBridge.Inner.class) assertThat(ElementMatchers.isBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeOverrideBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true));
assertThat(ElementMatchers.isBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeCombinationBridge.Inner.class) assertThat(ElementMatchers.isBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeCombinationBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(true)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true));
assertThat(ElementMatchers.isBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeSpecializationBridge.Inner.class) assertThat(ElementMatchers.isBridge().matches(new TypeDescription.ForLoadedType(TypeVariableReturnTypeSpecializationBridge.Inner.class)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true)); .getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true));
assertThat(ElementMatchers.isBridge() assertThat(ElementMatchers.isBridge()
Expand Down
Expand Up @@ -10,21 +10,20 @@
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;


import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;


import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;


@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class MethodSortMatcherTest extends AbstractElementMatcherTest<MethodSortMatcher<?>> { public class MethodSortMatcherTest extends AbstractElementMatcherTest<MethodSortMatcher<?>> {


private static final String FOO = "foo"; private static final String FOO = "foo", BAR = "bar";


private final MethodSortMatcher.Sort sort; private final MethodSortMatcher.Sort sort;


Expand Down Expand Up @@ -78,88 +77,97 @@ private enum MockEngine {


CONSTRUCTOR { CONSTRUCTOR {
@Override @Override
protected void prepare(MethodDescription mock) { protected void prepare(MethodDescription target) {
when(mock.isConstructor()).thenReturn(true); when(target.isConstructor()).thenReturn(true);
} }
}, },


DEFAULT_METHOD { DEFAULT_METHOD {
@Override @Override
protected void prepare(MethodDescription mock) { protected void prepare(MethodDescription target) {
when(mock.isDefaultMethod()).thenReturn(true); when(target.isDefaultMethod()).thenReturn(true);
} }
}, },


METHOD { METHOD {
@Override @Override
protected void prepare(MethodDescription mock) { protected void prepare(MethodDescription target) {
when(mock.isMethod()).thenReturn(true); when(target.isMethod()).thenReturn(true);
} }
}, },


OVERRIDABLE { OVERRIDABLE {
@Override @Override
protected void prepare(MethodDescription mock) { protected void prepare(MethodDescription target) {
when(mock.isOverridable()).thenReturn(true); when(target.isOverridable()).thenReturn(true);
} }
}, },


TYPE_INITIALIZER { TYPE_INITIALIZER {
@Override @Override
protected void prepare(MethodDescription mock) { protected void prepare(MethodDescription target) {
when(mock.isTypeInitializer()).thenReturn(true); when(target.isTypeInitializer()).thenReturn(true);
} }
}, },


TYPE_BRIDGE { TYPE_BRIDGE {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void prepare(MethodDescription mock) { protected void prepare(MethodDescription target) {
when(mock.isBridge()).thenReturn(true); when(target.isBridge()).thenReturn(true);
when(mock.getInternalName()).thenReturn(FOO); TypeDescription declaringType = mock(TypeDescription.class);
when(mock.getParameters()).thenReturn((ParameterList) new ParameterList.Empty()); when(declaringType.asRawType()).thenReturn(declaringType);
TypeDescription typeDescription = Mockito.mock(TypeDescription.class); when(target.getDeclaringType()).thenReturn(declaringType);
when(typeDescription.asRawType()).thenReturn(typeDescription); TypeDescription superType = mock(TypeDescription.class);
when(typeDescription.getDeclaredMethods()).thenReturn(new MethodList.Empty()); when(superType.asRawType()).thenReturn(superType);
when(mock.getDeclaringType()).thenReturn(typeDescription);
TypeDescription superType = Mockito.mock(TypeDescription.class);
when(typeDescription.getSuperType()).thenReturn(superType);
when(superType.iterator()).thenReturn(new GenericTypeDescription.SuperTypeIterator(superType)); when(superType.iterator()).thenReturn(new GenericTypeDescription.SuperTypeIterator(superType));
MethodDescription bridgeTarget = Mockito.mock(MethodDescription.class); when(declaringType.getSuperType()).thenReturn(superType);
MethodDescription.InDefinedShape definedBridgeTarget = Mockito.mock(MethodDescription.InDefinedShape.class); MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
when(bridgeTarget.asDefined()).thenReturn(definedBridgeTarget); when(methodDescription.asDefined()).thenReturn(methodDescription);
MethodDescription.Token methodToken = Mockito.mock(MethodDescription.Token.class); MethodDescription.Token methodToken = mock(MethodDescription.Token.class);
when(definedBridgeTarget.asToken()).thenReturn(methodToken); when(methodDescription.asToken()).thenReturn(methodToken);
when(mock.asToken()).thenReturn(methodToken); when(target.asToken()).thenReturn(methodToken);
when(methodDescription.getInternalName()).thenReturn(FOO);
when(methodDescription.getParameters()).thenReturn(new ParameterList.Empty());
MethodDescription.InDefinedShape bridgeTarget = mock(MethodDescription.InDefinedShape.class);
when(bridgeTarget.getSourceCodeName()).thenReturn(FOO);
when(bridgeTarget.getParameters()).thenReturn(new ParameterList.Empty());
when(declaringType.getDeclaredMethods())
.thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(Collections.singletonList(bridgeTarget)));
when(superType.getDeclaredMethods()) when(superType.getDeclaredMethods())
.thenReturn((MethodList) new MethodList.Explicit<MethodDescription>(Collections.singletonList(bridgeTarget))); .thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(Collections.singletonList(methodDescription)));
} }
}, },


VISIBILITY_BRIDGE { VISIBILITY_BRIDGE {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void prepare(MethodDescription mock) { protected void prepare(MethodDescription target) {
when(mock.isBridge()).thenReturn(true); when(target.isBridge()).thenReturn(true);
when(mock.getInternalName()).thenReturn(FOO); TypeDescription declaringType = mock(TypeDescription.class);
when(mock.getParameters()).thenReturn((ParameterList) new ParameterList.Empty()); when(declaringType.asRawType()).thenReturn(declaringType);
TypeDescription typeDescription = Mockito.mock(TypeDescription.class); when(target.getDeclaringType()).thenReturn(declaringType);
when(typeDescription.asRawType()).thenReturn(typeDescription); TypeDescription superType = mock(TypeDescription.class);
when(typeDescription.getDeclaredMethods()).thenReturn(new MethodList.Empty()); when(superType.asRawType()).thenReturn(superType);
when(mock.getDeclaringType()).thenReturn(typeDescription);
TypeDescription superType = Mockito.mock(TypeDescription.class);
when(typeDescription.getSuperType()).thenReturn(superType);
when(superType.iterator()).thenReturn(new GenericTypeDescription.SuperTypeIterator(superType)); when(superType.iterator()).thenReturn(new GenericTypeDescription.SuperTypeIterator(superType));
MethodDescription.InDefinedShape bridgeTarget = Mockito.mock(MethodDescription.InDefinedShape.class); when(declaringType.getSuperType()).thenReturn(superType);
when(bridgeTarget.asDefined()).thenReturn(bridgeTarget); MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
MethodDescription.Token methodToken = Mockito.mock(MethodDescription.Token.class); when(methodDescription.asDefined()).thenReturn(methodDescription);
when(bridgeTarget.asToken()).thenReturn(methodToken); MethodDescription.Token methodToken = mock(MethodDescription.Token.class);
when(mock.asToken()).thenReturn(methodToken); when(methodDescription.asToken()).thenReturn(methodToken);
when(target.asToken()).thenReturn(methodToken);
when(methodDescription.getInternalName()).thenReturn(FOO);
when(methodDescription.getParameters()).thenReturn(new ParameterList.Empty());
MethodDescription.InDefinedShape bridgeTarget = mock(MethodDescription.InDefinedShape.class);
when(bridgeTarget.getSourceCodeName()).thenReturn(BAR);
when(bridgeTarget.getParameters()).thenReturn(new ParameterList.Empty());
when(declaringType.getDeclaredMethods())
.thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(Collections.singletonList(bridgeTarget)));
when(superType.getDeclaredMethods()) when(superType.getDeclaredMethods())
.thenReturn((MethodList) new MethodList.Explicit<MethodDescription>(Collections.singletonList(bridgeTarget))); .thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(Collections.singletonList(methodDescription)));
} }
}; };


protected abstract void prepare(MethodDescription mock); protected abstract void prepare(MethodDescription target);
} }
} }

0 comments on commit bb7f972

Please sign in to comment.