Skip to content

Commit

Permalink
Added further tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jan 13, 2016
1 parent 20aa28e commit 0140c0a
Showing 1 changed file with 105 additions and 5 deletions.
Expand Up @@ -3,20 +3,20 @@
import net.bytebuddy.ClassFileVersion; import net.bytebuddy.ClassFileVersion;
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.TypeDescription;
import net.bytebuddy.dynamic.loading.ByteArrayClassLoader; import net.bytebuddy.dynamic.loading.ByteArrayClassLoader;
import net.bytebuddy.dynamic.loading.PackageDefinitionStrategy; import net.bytebuddy.dynamic.loading.PackageDefinitionStrategy;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.test.utility.MockitoRule; import net.bytebuddy.test.utility.MockitoRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion; import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.hamcrest.CoreMatchers; import org.hamcrest.CoreMatchers;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.mockito.Mock; import org.mockito.Mock;
import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.*;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;


import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
Expand All @@ -42,6 +42,9 @@ public class AnnotationAppenderDefaultTest {
@Rule @Rule
public TestRule mockitoRule = new MockitoRule(this); public TestRule mockitoRule = new MockitoRule(this);


@Rule
public MethodRule javaVersionRule = new JavaVersionRule();

@Mock @Mock
private AnnotationAppender.Target target; private AnnotationAppender.Target target;


Expand Down Expand Up @@ -104,6 +107,64 @@ public void testMultipleArgumentAnnotation() throws Exception {
assertThat(bar.getAnnotation(Baz.class).type(), CoreMatchers.<Class<?>>is(Void.class)); assertThat(bar.getAnnotation(Baz.class).type(), CoreMatchers.<Class<?>>is(Void.class));
} }


@Test
@JavaVersionRule.Enforce(8)
public void testNoArgumentTypeAnnotation() throws Exception {
Class<?> bar = makeTypeWithSuperTypeAnnotation(new Foo.Instance());
TypeDescription.Generic.AnnotationReader annotationReader = TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveSuperClass(bar);
assertThat(annotationReader.asList().size(), is(1));
assertThat(annotationReader.asList().isAnnotationPresent(Foo.class), is(true));
}

@Test
@JavaVersionRule.Enforce(8)
public void testNoArgumentTypeAnnotationSourceCodeRetention() throws Exception {
Class<?> bar = makeTypeWithSuperTypeAnnotation(new FooSourceCodeRetention.Instance());
TypeDescription.Generic.AnnotationReader annotationReader = TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveSuperClass(bar);
assertThat(annotationReader.asList().size(), is(0));
}

@Test
@JavaVersionRule.Enforce(8)
public void testNoArgumentTypeAnnotationByteCodeRetention() throws Exception {
Class<?> bar = makeTypeWithSuperTypeAnnotation(new FooByteCodeRetention.Instance());
TypeDescription.Generic.AnnotationReader annotationReader = TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveSuperClass(bar);
assertThat(annotationReader.asList().size(), is(0));
}

@Test
@JavaVersionRule.Enforce(8)
public void testNoArgumentTypeAnnotationNoRetention() throws Exception {
Class<?> bar = makeTypeWithSuperTypeAnnotation(new FooNoRetention.Instance());
TypeDescription.Generic.AnnotationReader annotationReader = TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveSuperClass(bar);
assertThat(annotationReader.asList().size(), is(0));
}

@Test
@JavaVersionRule.Enforce(8)
public void testSingleTypeArgumentAnnotation() throws Exception {
Class<?> bar = makeTypeWithSuperTypeAnnotation(new Qux.Instance(FOOBAR));
TypeDescription.Generic.AnnotationReader annotationReader = TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveSuperClass(bar);
assertThat(annotationReader.asList().size(), is(1));
assertThat(annotationReader.asList().isAnnotationPresent(Qux.class), is(true));
assertThat(annotationReader.asList().ofType(Qux.class).load().value(), is(FOOBAR));
}

@Test
@JavaVersionRule.Enforce(8)
public void testMultipleTypeArgumentAnnotation() throws Exception {
int[] array = {2, 3, 4};
Class<?> bar = makeTypeWithSuperTypeAnnotation(new Baz.Instance(FOOBAR, array, new Foo.Instance(), Baz.Enum.VALUE, Void.class));
TypeDescription.Generic.AnnotationReader annotationReader = TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveSuperClass(bar);
assertThat(annotationReader.asList().size(), is(1));
assertThat(annotationReader.asList().isAnnotationPresent(Baz.class), is(true));
assertThat(annotationReader.asList().ofType(Baz.class).load().value(), is(FOOBAR));
assertThat(annotationReader.asList().ofType(Baz.class).load().array(), is(array));
assertThat(annotationReader.asList().ofType(Baz.class).load().annotation(), is((Foo) new Foo.Instance()));
assertThat(annotationReader.asList().ofType(Baz.class).load().enumeration(), is(Baz.Enum.VALUE));
assertThat(annotationReader.asList().ofType(Baz.class).load().type(), CoreMatchers.<Class<?>>is(Void.class));
}

private Class<?> makeTypeWithAnnotation(Annotation annotation) throws Exception { private Class<?> makeTypeWithAnnotation(Annotation annotation) throws Exception {
when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true); when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
ClassWriter classWriter = new ClassWriter(ASM_MANUAL); ClassWriter classWriter = new ClassWriter(ASM_MANUAL);
Expand All @@ -129,8 +190,36 @@ private Class<?> makeTypeWithAnnotation(Annotation annotation) throws Exception
return bar; return bar;
} }


private Class<?> makeTypeWithSuperTypeAnnotation(Annotation annotation) throws Exception {
when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
ClassWriter classWriter = new ClassWriter(ASM_MANUAL);
classWriter.visit(ClassFileVersion.forCurrentJavaVersion().getMinorMajorVersion(),
Opcodes.ACC_PUBLIC,
BAR.replace('.', '/'),
null,
Type.getInternalName(Object.class),
null);
AnnotationVisitor annotationVisitor = classWriter.visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(),
null,
Type.getDescriptor(annotation.annotationType()),
true);
when(target.visit(any(String.class), anyBoolean())).thenReturn(annotationVisitor);
AnnotationDescription annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation);
annotationAppender.append(annotationDescription, valueFilter);
classWriter.visitEnd();
Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(),
Collections.singletonMap(BAR, classWriter.toByteArray()),
DEFAULT_PROTECTION_DOMAIN,
AccessController.getContext(),
ByteArrayClassLoader.PersistenceHandler.LATENT,
PackageDefinitionStrategy.NoOp.INSTANCE).loadClass(BAR);
assertThat(bar.getName(), is(BAR));
assertThat(bar.getSuperclass(), CoreMatchers.<Class<?>>is(Object.class));
return bar;
}

@Test @Test
public void testSourceAnnotation() throws Exception { public void testSourceRetentionAnnotation() throws Exception {
AnnotationVisitor annotationVisitor = mock(AnnotationVisitor.class); AnnotationVisitor annotationVisitor = mock(AnnotationVisitor.class);
when(target.visit(anyString(), anyBoolean())).thenReturn(annotationVisitor); when(target.visit(anyString(), anyBoolean())).thenReturn(annotationVisitor);
AnnotationDescription annotationDescription = mock(AnnotationDescription.class); AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
Expand All @@ -140,6 +229,17 @@ public void testSourceAnnotation() throws Exception {
verifyZeroInteractions(annotationVisitor); verifyZeroInteractions(annotationVisitor);
} }


@Test
public void testSourceRetentionTypeAnnotation() throws Exception {
AnnotationVisitor annotationVisitor = mock(AnnotationVisitor.class);
when(target.visit(anyString(), anyBoolean())).thenReturn(annotationVisitor);
AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.SOURCE);
annotationAppender.append(annotationDescription, valueFilter, 0, null);
verifyZeroInteractions(valueFilter);
verifyZeroInteractions(annotationVisitor);
}

@Test @Test
public void testObjectProperties() throws Exception { public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(AnnotationAppender.Default.class).apply(); ObjectPropertyAssertion.of(AnnotationAppender.Default.class).apply();
Expand Down

0 comments on commit 0140c0a

Please sign in to comment.