Skip to content

Commit

Permalink
Refactored more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Dec 21, 2015
1 parent c255582 commit c0fe11a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
Expand Up @@ -1233,7 +1233,7 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> declaresE
* @return A matcher that matches any method that exactly matches the provided exception. * @return A matcher that matches any method that exactly matches the provided exception.
*/ */
public static <T extends MethodDescription> ElementMatcher.Junction<T> declaresException(TypeDescription exceptionType) { public static <T extends MethodDescription> ElementMatcher.Junction<T> declaresException(TypeDescription exceptionType) {
return !exceptionType.isAssignableTo(Throwable.class) return exceptionType.isAssignableTo(Throwable.class)
? ElementMatchers.<T>declaresGenericException(new CollectionItemMatcher<GenericTypeDescription>(rawType(exceptionType))) ? ElementMatchers.<T>declaresGenericException(new CollectionItemMatcher<GenericTypeDescription>(rawType(exceptionType)))
: new BooleanMatcher<T>(false); : new BooleanMatcher<T>(false);
} }
Expand Down
Expand Up @@ -83,6 +83,8 @@ public S subList(int fromIndex, int toIndex) {
*/ */
abstract class AbstractBase<T, S extends FilterableList<T, S>> extends AbstractList<T> implements FilterableList<T, S> { abstract class AbstractBase<T, S extends FilterableList<T, S>> extends AbstractList<T> implements FilterableList<T, S> {


private static final int ONLY = 0;

@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public S filter(ElementMatcher<? super T> elementMatcher) { public S filter(ElementMatcher<? super T> elementMatcher) {
Expand All @@ -101,9 +103,8 @@ public S filter(ElementMatcher<? super T> elementMatcher) {
public T getOnly() { public T getOnly() {
if (size() != 1) { if (size() != 1) {
throw new IllegalStateException("size = " + size()); throw new IllegalStateException("size = " + size());
} else {
return get(0);
} }
return get(ONLY);
} }


@Override @Override
Expand Down
Expand Up @@ -74,6 +74,7 @@ public void setUp() throws Exception {
when(exceptionType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(exceptionType); when(exceptionType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(exceptionType);
when(typeVariable.getSymbol()).thenReturn(QUX); when(typeVariable.getSymbol()).thenReturn(QUX);
when(typeVariable.getSort()).thenReturn(TypeDefinition.Sort.VARIABLE); when(typeVariable.getSort()).thenReturn(TypeDefinition.Sort.VARIABLE);
when(typeVariable.asGenericType()).thenReturn(typeVariable);
when(methodDescription.asToken()).thenReturn(methodToken); when(methodDescription.asToken()).thenReturn(methodToken);
when(methodDescription.getDeclaringType()).thenReturn(declaringType); when(methodDescription.getDeclaringType()).thenReturn(declaringType);
when(methodDescription.asDefined()).thenReturn(definedMethod); when(methodDescription.asDefined()).thenReturn(definedMethod);
Expand All @@ -96,6 +97,7 @@ public void setUp() throws Exception {
when(declaringType.asErasure()).thenReturn(rawDeclaringType); when(declaringType.asErasure()).thenReturn(rawDeclaringType);
when(returnType.asErasure()).thenReturn(rawReturnType); when(returnType.asErasure()).thenReturn(rawReturnType);
when(parameterType.asErasure()).thenReturn(rawParameterType); when(parameterType.asErasure()).thenReturn(rawParameterType);
when(exceptionType.asGenericType()).thenReturn(exceptionType);
} }


@Test @Test
Expand Down
Expand Up @@ -43,7 +43,13 @@ public class MethodRebaseResolverResolutionForRebasedConstructorTest {
private GenericTypeDescription genericTypeDescription; private GenericTypeDescription genericTypeDescription;


@Mock @Mock
private TypeDescription typeDescription, returnType, parameterType, placeholderType, otherPlaceHolderType; private TypeDescription typeDescription, returnType, otherPlaceHolderType, rawPlaceholderType;

@Mock
private GenericTypeDescription parameterType, placeholderType;

@Mock
private TypeDescription rawParameterType;


@Mock @Mock
private GenericTypeDescription genericReturnType; private GenericTypeDescription genericReturnType;
Expand All @@ -61,21 +67,24 @@ public void setUp() throws Exception {
when(methodDescription.isConstructor()).thenReturn(true); when(methodDescription.isConstructor()).thenReturn(true);
when(methodDescription.getDeclaringType()).thenReturn(typeDescription); when(methodDescription.getDeclaringType()).thenReturn(typeDescription);
when(methodDescription.getReturnType()).thenReturn(genericReturnType); when(methodDescription.getReturnType()).thenReturn(genericReturnType);
when(parameterType.getStackSize()).thenReturn(StackSize.ZERO);
when(placeholderType.getStackSize()).thenReturn(StackSize.ZERO); when(placeholderType.getStackSize()).thenReturn(StackSize.ZERO);
when(placeholderType.asErasure()).thenReturn(rawPlaceholderType);
when(methodDescription.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(methodDescription, Collections.singletonList(parameterType))); when(methodDescription.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(methodDescription, Collections.singletonList(parameterType)));
when(parameterType.asGenericType()).thenReturn(parameterType);
when(parameterType.getStackSize()).thenReturn(StackSize.ZERO);
when(parameterType.asErasure()).thenReturn(rawParameterType);
when(rawParameterType.asGenericType()).thenReturn(parameterType); // TODO
when(methodDescription.getInternalName()).thenReturn(FOO); when(methodDescription.getInternalName()).thenReturn(FOO);
when(methodDescription.getDescriptor()).thenReturn(QUX); when(methodDescription.getDescriptor()).thenReturn(QUX);
when(typeDescription.getInternalName()).thenReturn(BAR); when(typeDescription.getInternalName()).thenReturn(BAR);
when(placeholderType.getDescriptor()).thenReturn(BAZ); when(rawPlaceholderType.getDescriptor()).thenReturn(BAZ);
when(otherPlaceHolderType.getDescriptor()).thenReturn(FOO); when(otherPlaceHolderType.getDescriptor()).thenReturn(FOO);
when(genericReturnType.asErasure()).thenReturn(returnType); // TODO when(genericReturnType.asErasure()).thenReturn(returnType); // TODO
when(parameterType.asErasure()).thenReturn(parameterType);
} }


@Test @Test
public void testPreservation() throws Exception { public void testPreservation() throws Exception {
MethodRebaseResolver.Resolution resolution = MethodRebaseResolver.Resolution.ForRebasedConstructor.of(methodDescription, placeholderType); MethodRebaseResolver.Resolution resolution = MethodRebaseResolver.Resolution.ForRebasedConstructor.of(methodDescription, rawPlaceholderType);
assertThat(resolution.isRebased(), is(true)); assertThat(resolution.isRebased(), is(true));
assertThat(resolution.getResolvedMethod().getDeclaringType(), is(typeDescription)); assertThat(resolution.getResolvedMethod().getDeclaringType(), is(typeDescription));
assertThat(resolution.getResolvedMethod().getInternalName(), is(MethodDescription.CONSTRUCTOR_INTERNAL_NAME)); assertThat(resolution.getResolvedMethod().getInternalName(), is(MethodDescription.CONSTRUCTOR_INTERNAL_NAME));
Expand Down
Expand Up @@ -124,6 +124,12 @@ public ImplementationContextDefaultTest(boolean interfaceType, int accessorMetho
@Mock @Mock
private TypeDescription firstRawSpecialParameterType, secondRawSpecialParameterType; private TypeDescription firstRawSpecialParameterType, secondRawSpecialParameterType;


@Mock
private GenericTypeDescription firstSpecialExceptionType, secondSpecialExceptionType;

@Mock
private TypeDescription firstRawSpecialExceptionType, secondRawSpecialExceptionType;

@Mock @Mock
private ByteCodeAppender injectedCodeAppender, terminationAppender; private ByteCodeAppender injectedCodeAppender, terminationAppender;


Expand All @@ -138,7 +144,6 @@ public ImplementationContextDefaultTest(boolean interfaceType, int accessorMetho


@Mock @Mock
private TypeDescription firstSpecialType, secondSpecialType, private TypeDescription firstSpecialType, secondSpecialType,
firstSpecialExceptionType, secondSpecialExceptionType,
firstDeclaringType, secondDeclaringType; firstDeclaringType, secondDeclaringType;


@Mock @Mock
Expand Down Expand Up @@ -196,10 +201,13 @@ public void setUp() throws Exception {
when(firstSpecialParameterType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC); when(firstSpecialParameterType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
when(firstRawSpecialReturnType.getDescriptor()).thenReturn(QUX); when(firstRawSpecialReturnType.getDescriptor()).thenReturn(QUX);
when(firstSpecialReturnType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC); when(firstSpecialReturnType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
when(firstSpecialExceptionType.getInternalName()).thenReturn(FOO); when(firstSpecialReturnType.getStackSize()).thenReturn(StackSize.ZERO);
when(firstSpecialReturnType.asRawType()).thenReturn(firstSpecialReturnType);
when(firstRawSpecialExceptionType.getInternalName()).thenReturn(FOO);
when(firstSpecialExceptionType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC); when(firstSpecialExceptionType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
when(firstSpecialParameterType.getStackSize()).thenReturn(StackSize.ZERO); when(firstSpecialParameterType.getStackSize()).thenReturn(StackSize.ZERO);
when(firstSpecialReturnType.getStackSize()).thenReturn(StackSize.ZERO); when(firstSpecialParameterType.asGenericType()).thenReturn(firstSpecialParameterType);
when(firstSpecialParameterType.asRawType()).thenReturn(firstSpecialParameterType);
when(firstSpecialInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0)); when(firstSpecialInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0));
when(firstSpecialMethod.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(firstSpecialMethod, when(firstSpecialMethod.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(firstSpecialMethod,
Collections.singletonList(firstSpecialParameterType))); Collections.singletonList(firstSpecialParameterType)));
Expand All @@ -210,12 +218,15 @@ public void setUp() throws Exception {
when(secondSpecialMethod.getExceptionTypes()).thenReturn(secondSpecialExceptionTypes); when(secondSpecialMethod.getExceptionTypes()).thenReturn(secondSpecialExceptionTypes);
when(secondRawSpecialParameterType.getDescriptor()).thenReturn(BAR); when(secondRawSpecialParameterType.getDescriptor()).thenReturn(BAR);
when(secondRawSpecialReturnType.getDescriptor()).thenReturn(FOO); when(secondRawSpecialReturnType.getDescriptor()).thenReturn(FOO);
when(secondSpecialExceptionType.getInternalName()).thenReturn(BAZ); when(secondRawSpecialExceptionType.getInternalName()).thenReturn(BAZ);
when(secondSpecialExceptionType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC); when(secondSpecialExceptionType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
when(secondSpecialParameterType.getStackSize()).thenReturn(StackSize.ZERO); when(secondSpecialParameterType.getStackSize()).thenReturn(StackSize.ZERO);
when(secondSpecialParameterType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC); when(secondSpecialParameterType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
when(secondSpecialParameterType.asGenericType()).thenReturn(secondSpecialParameterType);
when(secondSpecialParameterType.asRawType()).thenReturn(secondSpecialParameterType);
when(secondSpecialReturnType.getStackSize()).thenReturn(StackSize.ZERO); when(secondSpecialReturnType.getStackSize()).thenReturn(StackSize.ZERO);
when(secondSpecialReturnType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC); when(secondSpecialReturnType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
when(secondSpecialReturnType.asRawType()).thenReturn(secondSpecialReturnType);
when(secondSpecialInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0)); when(secondSpecialInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0));
when(secondSpecialMethod.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(secondSpecialMethod, when(secondSpecialMethod.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(secondSpecialMethod,
Collections.singletonList(secondSpecialParameterType))); Collections.singletonList(secondSpecialParameterType)));
Expand All @@ -235,8 +246,8 @@ public void setUp() throws Exception {
when(secondFieldDeclaringType.getInternalName()).thenReturn(BAZ); when(secondFieldDeclaringType.getInternalName()).thenReturn(BAZ);
when(firstSpecialReturnType.asErasure()).thenReturn(firstRawSpecialReturnType); when(firstSpecialReturnType.asErasure()).thenReturn(firstRawSpecialReturnType);
when(secondSpecialReturnType.asErasure()).thenReturn(secondRawSpecialReturnType); when(secondSpecialReturnType.asErasure()).thenReturn(secondRawSpecialReturnType);
when(firstSpecialExceptionType.asErasure()).thenReturn(firstSpecialExceptionType); when(firstSpecialExceptionType.asErasure()).thenReturn(firstRawSpecialExceptionType);
when(secondSpecialExceptionType.asErasure()).thenReturn(secondSpecialExceptionType); when(secondSpecialExceptionType.asErasure()).thenReturn(secondRawSpecialExceptionType);
when(firstSpecialParameterType.asErasure()).thenReturn(firstRawSpecialParameterType); when(firstSpecialParameterType.asErasure()).thenReturn(firstRawSpecialParameterType);
when(secondSpecialParameterType.asErasure()).thenReturn(secondRawSpecialParameterType); when(secondSpecialParameterType.asErasure()).thenReturn(secondRawSpecialParameterType);
when(firstSpecialParameterType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(firstSpecialParameterType); when(firstSpecialParameterType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(firstSpecialParameterType);
Expand Down
Expand Up @@ -35,13 +35,11 @@ public void testForAnnotationProperty() throws Exception {
when(methodDescription.getSourceCodeName()).thenReturn(FOO); when(methodDescription.getSourceCodeName()).thenReturn(FOO);
GenericTypeDescription returnType = mock(GenericTypeDescription.class); GenericTypeDescription returnType = mock(GenericTypeDescription.class);
TypeDescription rawReturnType = mock(TypeDescription.class); TypeDescription rawReturnType = mock(TypeDescription.class);
when(returnType.asErasure()).thenReturn(rawReturnType); // TODO when(returnType.asErasure()).thenReturn(rawReturnType);
when(methodDescription.getReturnType()).thenReturn(returnType); when(methodDescription.getReturnType()).thenReturn(returnType);
GenericTypeDescription componentType = mock(GenericTypeDescription.class);
TypeDescription rawComponentType = mock(TypeDescription.class); TypeDescription rawComponentType = mock(TypeDescription.class);
when(componentType.asErasure()).thenReturn(rawComponentType); // TODO when(rawReturnType.getComponentType()).thenReturn(rawComponentType);
when(rawComponentType.getName()).thenReturn(QUX); when(rawComponentType.getName()).thenReturn(QUX);
when(returnType.getComponentType()).thenReturn(componentType);
assertThat(new TypePool.Default.ComponentTypeLocator.ForAnnotationProperty(typePool, BAR_DESCRIPTOR).bind(FOO).lookup(), is(QUX)); assertThat(new TypePool.Default.ComponentTypeLocator.ForAnnotationProperty(typePool, BAR_DESCRIPTOR).bind(FOO).lookup(), is(QUX));
} }


Expand Down

0 comments on commit c0fe11a

Please sign in to comment.