Skip to content

Commit

Permalink
Added additional tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jan 13, 2016
1 parent b3ee51e commit 698d31b
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 31 deletions.
Expand Up @@ -489,7 +489,7 @@ public static AnnotationAppender ofTypeVariable(AnnotationAppender annotationApp
for (AnnotationDescription annotationDescription : typeVariable.getDeclaredAnnotations()) { for (AnnotationDescription annotationDescription : typeVariable.getDeclaredAnnotations()) {
annotationAppender = annotationAppender.append(annotationDescription, annotationValueFilter, typeReference, EMPTY_TYPE_PATH); annotationAppender = annotationAppender.append(annotationDescription, annotationValueFilter, typeReference, EMPTY_TYPE_PATH);
} }
int boundIndex = typeVariable.getUpperBounds().get(0).getSort().isTypeVariable() || typeVariable.getUpperBounds().get(0).asErasure().isInterface() int boundIndex = !typeVariable.getUpperBounds().get(0).getSort().isTypeVariable() && typeVariable.getUpperBounds().get(0).asErasure().isInterface()
? 1 ? 1
: 0; : 0;
for (TypeDescription.Generic typeBound : typeVariable.getUpperBounds()) { for (TypeDescription.Generic typeBound : typeVariable.getUpperBounds()) {
Expand Down
Expand Up @@ -130,10 +130,10 @@ enum ForInstrumentedField implements FieldAttributeAppender, Factory {


@Override @Override
public void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter) { public void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter) {
AnnotationAppender appender = new AnnotationAppender.Default(new AnnotationAppender.Target.OnField(fieldVisitor)); AnnotationAppender annotationAppender = new AnnotationAppender.Default(new AnnotationAppender.Target.OnField(fieldVisitor));
appender = fieldDescription.getType().accept(AnnotationAppender.ForTypeAnnotations.ofFieldType(appender, annotationValueFilter)); annotationAppender = fieldDescription.getType().accept(AnnotationAppender.ForTypeAnnotations.ofFieldType(annotationAppender, annotationValueFilter));
for (AnnotationDescription annotation : fieldDescription.getDeclaredAnnotations()) { for (AnnotationDescription annotation : fieldDescription.getDeclaredAnnotations()) {
appender = appender.append(annotation, annotationValueFilter); annotationAppender = annotationAppender.append(annotation, annotationValueFilter);
} }
} }


Expand Down
Expand Up @@ -130,8 +130,9 @@ public boolean equals(Object other) {
if (this == other) return true; if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false; if (other == null || getClass() != other.getClass()) return false;
Differentiating differentiating = (Differentiating) other; Differentiating differentiating = (Differentiating) other;
if (annotationIndex != differentiating.annotationIndex) return false; return annotationIndex == differentiating.annotationIndex
return typeVariableIndex == differentiating.typeVariableIndex && interfaceTypeIndex == differentiating.interfaceTypeIndex; && typeVariableIndex == differentiating.typeVariableIndex
&& interfaceTypeIndex == differentiating.interfaceTypeIndex;
} }


@Override @Override
Expand All @@ -144,7 +145,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "TypeAttributeAppender.ForInstrumentedType.Excluding{" + return "TypeAttributeAppender.ForInstrumentedType.Differentiating{" +
"annotationIndex=" + annotationIndex + "annotationIndex=" + annotationIndex +
", typeVariableIndex=" + typeVariableIndex + ", typeVariableIndex=" + typeVariableIndex +
", interfaceTypeIndex=" + interfaceTypeIndex + ", interfaceTypeIndex=" + interfaceTypeIndex +
Expand Down
Expand Up @@ -150,6 +150,7 @@ public List<?> create() {
public TypeDescription create() { public TypeDescription create() {
TypeDescription rawTypeDescription = mock(TypeDescription.class); TypeDescription rawTypeDescription = mock(TypeDescription.class);
when(rawTypeDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty()); when(rawTypeDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
when(rawTypeDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
TypeDescription.Generic typeDescription = mock(TypeDescription.Generic.class); TypeDescription.Generic typeDescription = mock(TypeDescription.Generic.class);
when(typeDescription.asErasure()).thenReturn(rawTypeDescription); when(typeDescription.asErasure()).thenReturn(rawTypeDescription);
when(typeDescription.asGenericType()).thenReturn(typeDescription); when(typeDescription.asGenericType()).thenReturn(typeDescription);
Expand Down
Expand Up @@ -2,7 +2,9 @@


import net.bytebuddy.description.annotation.AnnotationDescription; import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.test.utility.MockitoRule; import net.bytebuddy.test.utility.MockitoRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
Expand All @@ -27,9 +29,27 @@ public abstract class AbstractAttributeAppenderTest {
@Mock @Mock
protected AnnotationValueFilter annotationValueFilter; protected AnnotationValueFilter annotationValueFilter;


@Mock
protected TypeDescription.Generic.OfNonGenericType simpleAnnotatedType;

@Mock
protected TypeDescription.Generic.OfTypeVariable annotatedTypeVariable;

@Mock
protected TypeDescription.Generic.OfNonGenericType annotatedTypeVariableBound;

@Before @Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception { public void setUp() throws Exception {
when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true); when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
when(annotatedTypeVariable.accept(any(TypeDescription.Generic.Visitor.class))).thenCallRealMethod();
when(annotatedTypeVariable.getUpperBounds()).thenReturn(new TypeList.Generic.Explicit(annotatedTypeVariableBound));
when(annotatedTypeVariable.asGenericType()).thenReturn(annotatedTypeVariable);
when(annotatedTypeVariableBound.accept(any(TypeDescription.Generic.Visitor.class))).thenCallRealMethod();
when(annotatedTypeVariableBound.getSort()).thenReturn(TypeDefinition.Sort.VARIABLE);
when(annotatedTypeVariableBound.asGenericType()).thenReturn(annotatedTypeVariableBound);
when(simpleAnnotatedType.accept(any(TypeDescription.Generic.Visitor.class))).thenCallRealMethod();
when(simpleAnnotatedType.asGenericType()).thenReturn(simpleAnnotatedType);
} }


@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
Expand Down
@@ -1,8 +1,10 @@
package net.bytebuddy.implementation.attribute; package net.bytebuddy.implementation.attribute;


import net.bytebuddy.description.annotation.AnnotationList; import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.type.TypeDescription;
import org.junit.Test; import org.junit.Test;
import org.mockito.asm.Type; import org.mockito.asm.Type;
import org.objectweb.asm.TypeReference;


import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
Expand All @@ -18,30 +20,80 @@ public void testFactory() throws Exception {


@Test @Test
public void testAnnotationAppenderNoRetention() throws Exception { public void testAnnotationAppenderNoRetention() throws Exception {
when(fieldDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Qux.Instance())); when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Qux.Instance()));
FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter); FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter);
verifyZeroInteractions(fieldVisitor); verifyZeroInteractions(fieldVisitor);
verify(fieldDescription).getDeclaredAnnotations(); verify(fieldDescription).getDeclaredAnnotations();
verify(fieldDescription).getType();
verifyNoMoreInteractions(fieldDescription); verifyNoMoreInteractions(fieldDescription);
} }


@Test @Test
public void testAnnotationAppenderRuntimeRetention() throws Exception { public void testAnnotationAppenderRuntimeRetention() throws Exception {
when(fieldDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance())); when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter); FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter);
verify(fieldVisitor).visitAnnotation(Type.getDescriptor(Baz.class), true); verify(fieldVisitor).visitAnnotation(Type.getDescriptor(Baz.class), true);
verifyNoMoreInteractions(fieldVisitor); verifyNoMoreInteractions(fieldVisitor);
verify(fieldDescription).getDeclaredAnnotations(); verify(fieldDescription).getDeclaredAnnotations();
verify(fieldDescription).getType();
verifyNoMoreInteractions(fieldDescription); verifyNoMoreInteractions(fieldDescription);
} }


@Test @Test
public void testAnnotationAppenderByteCodeRetention() throws Exception { public void testAnnotationAppenderByteCodeRetention() throws Exception {
when(fieldDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance())); when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter); FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter);
verify(fieldVisitor).visitAnnotation(Type.getDescriptor(QuxBaz.class), false); verify(fieldVisitor).visitAnnotation(Type.getDescriptor(QuxBaz.class), false);
verifyNoMoreInteractions(fieldVisitor); verifyNoMoreInteractions(fieldVisitor);
verify(fieldDescription).getDeclaredAnnotations(); verify(fieldDescription).getDeclaredAnnotations();
verify(fieldDescription).getType();
verifyNoMoreInteractions(fieldDescription);
}

@Test
public void testFieldTypeTypeAnnotationNoRetention() throws Exception {
when(fieldDescription.getType()).thenReturn(simpleAnnotatedType);
when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Qux.Instance()));
when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter);
verifyZeroInteractions(fieldVisitor);
verify(fieldDescription).getDeclaredAnnotations();
verify(fieldDescription).getType();
verifyNoMoreInteractions(fieldDescription);
}

@Test
public void testFieldTypeTypeAnnotationRuntimeRetention() throws Exception {
when(fieldDescription.getType()).thenReturn(simpleAnnotatedType);
when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter);
verify(fieldVisitor).visitTypeAnnotation(TypeReference.newTypeReference(TypeReference.FIELD).getValue(),
null,
Type.getDescriptor(Baz.class),
true);
verifyNoMoreInteractions(fieldVisitor);
verify(fieldDescription).getDeclaredAnnotations();
verify(fieldDescription).getType();
verifyNoMoreInteractions(fieldDescription);
}

@Test
public void testFieldTypeTypeAnnotationByteCodeRetention() throws Exception {
when(fieldDescription.getType()).thenReturn(simpleAnnotatedType);
when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter);
verify(fieldVisitor).visitTypeAnnotation(TypeReference.newTypeReference(TypeReference.FIELD).getValue(),
null,
Type.getDescriptor(QuxBaz.class),
false);
verifyNoMoreInteractions(fieldVisitor);
verify(fieldDescription).getDeclaredAnnotations();
verify(fieldDescription).getType();
verifyNoMoreInteractions(fieldDescription); verifyNoMoreInteractions(fieldDescription);
} }
} }

0 comments on commit 698d31b

Please sign in to comment.