1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.config.annotation;
20
21 import java.io.DataInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Arrays;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import javax.faces.bean.ManagedBean;
29
30 import junit.framework.TestCase;
31
32 public class ClassByteCodeAnnotationFilterTest extends TestCase
33 {
34
35
36
37
38 private Set<String> annotationNames = null;
39
40
41
42
43 private _ClassByteCodeAnnotationFilter filter = null;
44
45 @Override
46 protected void setUp() throws Exception
47 {
48 super.setUp();
49 filter = new _ClassByteCodeAnnotationFilter();
50 annotationNames = new HashSet<String>(
51 Arrays.asList("Ljavax/faces/bean/ManagedBean;")
52 );
53 }
54
55 @Override
56 protected void tearDown() throws Exception
57 {
58 super.tearDown();
59 filter = null;
60 annotationNames = null;
61 }
62
63
64
65
66 public void testBeanWithoutAnnotations() throws IOException
67 {
68 DataInputStream byteCode = getDataInputStreamForClass(ClassWithoutAnnotations.class);
69 assertFalse(filter.couldContainAnnotationsOnClassDef(byteCode, annotationNames));
70 }
71
72
73
74
75 public void testBeanWithManagedBeanAnnotation() throws IOException
76 {
77 DataInputStream byteCode = getDataInputStreamForClass(ClassWithManagedBeanAnnotation.class);
78 assertTrue(filter.couldContainAnnotationsOnClassDef(byteCode, annotationNames));
79 }
80
81
82
83
84
85 public void testBeanWithLongConstant() throws IOException
86 {
87 DataInputStream byteCode = getDataInputStreamForClass(ClassWithLongConstant.class);
88 assertFalse(filter.couldContainAnnotationsOnClassDef(byteCode, annotationNames));
89 }
90
91
92
93
94
95
96 private DataInputStream getDataInputStreamForClass(Class<?> clazz)
97 {
98 ClassLoader cl = Thread.currentThread().getContextClassLoader();
99 String resourceName = clazz.getName().replace('.', '/') + ".class";
100 InputStream stream = cl.getResourceAsStream(resourceName);
101 assertNotNull("Cannot find class: " + clazz.getName(), stream);
102 return new DataInputStream(stream);
103 }
104
105
106
107
108 public static class ClassWithoutAnnotations
109 {
110
111 }
112
113
114
115
116 @ManagedBean
117 public static class ClassWithManagedBeanAnnotation
118 {
119
120 }
121
122
123
124
125 public static class ClassWithLongConstant
126 {
127 public final static long value1 = 9223362036854775807l;
128 public final static long value2 = 9223362036854775808l;
129 public final static long value3 = 9223362036854775809l;
130 public final static long value4 = 9223362036854775801l;
131 }
132
133 }