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()) {
return false;
}
for (GenericTypeDescription superType : target.getDeclaringType().asRawType().getSuperType()) {
for (MethodDescription methodDescription : superType.getDeclaredMethods()) {
if (methodDescription.asDefined().asToken().accept(GenericTypeDescription.Visitor.TypeErasing.INSTANCE).equals(target.asToken())) {
for (GenericTypeDescription currentType : target.getDeclaringType().getSuperType()) {
for (MethodDescription methodDescription : currentType.getDeclaredMethods()) {
if (methodDescription.asDefined().asToken().equals(target.asToken())) {
return !methodDescription.isBridge() && target.getDeclaringType().asRawType().getDeclaredMethods()
.filter(not(isBridge())
.and(hasMethodName(target.getInternalName()))
.and(takesArguments(target.getParameters().asTypeList().asRawTypes())))
.and(hasMethodName(methodDescription.getInternalName()))
.and(takesArguments(methodDescription.getParameters().asTypeList().asRawTypes())))
.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()") {
@Override
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)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(true));
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)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true));
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)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Number.class))).getOnly()), is(false));
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)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(false));
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)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true));
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)
.getDeclaredMethods().filter(named(FOO).and(ElementMatchers.returns(Object.class))).getOnly()), is(true));
assertThat(ElementMatchers.isBridge()
Expand Down
Expand Up @@ -10,21 +10,20 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mock;
import org.mockito.Mockito;

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

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

@RunWith(Parameterized.class)
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;

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

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

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

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

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

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

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

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