View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * The annotation names to scan for
37       */
38      private Set<String> annotationNames = null;
39  
40      /**
41       * The tested class
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       * Test that filter returns <code>false</code> for beans without annotations 
65       */
66      public void testBeanWithoutAnnotations() throws IOException
67      {
68          DataInputStream byteCode = getDataInputStreamForClass(ClassWithoutAnnotations.class);
69          assertFalse(filter.couldContainAnnotationsOnClassDef(byteCode, annotationNames));
70      }
71  
72      /**
73       * Test that filter returns <code>true</code> for a bean with a {@link ManagedBean} annotation 
74       */
75      public void testBeanWithManagedBeanAnnotation() throws IOException
76      {
77          DataInputStream byteCode = getDataInputStreamForClass(ClassWithManagedBeanAnnotation.class);
78          assertTrue(filter.couldContainAnnotationsOnClassDef(byteCode, annotationNames));
79      }
80  
81      /**
82       * Test that filter returns <code>false</code> for a bean with no annotations and
83       * a long constant in the constants pool. 
84       */
85      public void testBeanWithLongConstant() throws IOException
86      {
87          DataInputStream byteCode = getDataInputStreamForClass(ClassWithLongConstant.class);
88          assertFalse(filter.couldContainAnnotationsOnClassDef(byteCode, annotationNames));
89      }
90  
91      /**
92       * Helper method to load a .class file from the classpath
93       * @param clazz the {@link Class} object for the class to load
94       * @return The {@link DataInputStream} of the .class file
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      * A class without any annotations
107      */
108     public static class ClassWithoutAnnotations
109     {
110         // nothing
111     }
112 
113     /**
114      * A class with a {@link ManagedBean} annotation
115      */
116     @ManagedBean
117     public static class ClassWithManagedBeanAnnotation
118     {
119 
120     }
121 
122     /**
123      * A class without a long constants in the constant pool
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 }