KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > AnnotationTest


1 /***
2  * ASM tests
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm;
31
32 import java.io.InputStream JavaDoc;
33 import java.lang.annotation.Annotation JavaDoc;
34
35 import org.objectweb.asm.ClassReader;
36 import org.objectweb.asm.tree.ClassNode;
37
38 import annotations.ValueAttrAnnotation;
39 import annotations.Values;
40 import annotations.ValuesAnnotation;
41 import annotations.ValuesDump;
42 import annotations.ValuesEnum;
43
44 import junit.framework.TestCase;
45
46 public class AnnotationTest extends TestCase {
47
48     private ValuesAnnotation a;
49
50     public AnnotationTest(String JavaDoc name) {
51         super(name);
52     }
53
54     protected void setUp() throws Exception JavaDoc {
55         TestClassLoader cl = new TestClassLoader("annotations.Values",
56                 getClass().getClassLoader());
57         Class JavaDoc c = cl.loadClass("annotations.Values");
58         Annotation JavaDoc[] annotations = c.getAnnotations();
59         for (int i = 0; i < annotations.length; ++i) {
60             if (annotations[i] instanceof ValuesAnnotation) {
61                 a = (ValuesAnnotation) annotations[i];
62             }
63         }
64         if (a == null) {
65             fail();
66         }
67     }
68
69     public void testByteValue() {
70         assertEquals(1, a.byteValue());
71     }
72
73     public void testCharValue() {
74         assertEquals('A', a.charValue());
75     }
76
77     public void testBooleanValue() {
78         assertEquals(true, a.booleanValue());
79     }
80
81     public void testIntValue() {
82         assertEquals(1, a.intValue());
83     }
84
85     public void testShortValue() {
86         assertEquals(1, a.shortValue());
87     }
88
89     public void testLongValue() {
90         assertEquals(1L, a.longValue());
91     }
92
93     public void testFloatValue() {
94         assertEquals(1.0f, a.floatValue(), 0.1f);
95     }
96
97     public void testDoubleValue() {
98         assertEquals(1.0d, a.doubleValue(), 0.1d);
99     }
100
101     public void testStringValue() {
102         assertEquals("A", a.stringValue());
103     }
104
105     public void testAnnotationValue() {
106         ValueAttrAnnotation ann = a.annotationValue();
107         assertEquals("annotation", ann.value());
108     }
109
110     public void testEnumValue() {
111         ValuesEnum en = a.enumValue();
112         assertEquals(ValuesEnum.ONE, en);
113     }
114
115     public void testClassValue() {
116         Class JavaDoc c = a.classValue();
117         assertEquals(Values.class.getName(), c.getName());
118     }
119
120     public void testByteArrayValue() {
121         byte[] bs = a.byteArrayValue();
122         assertEquals(1, bs[0]);
123         assertEquals(-1, bs[1]);
124     }
125
126     public void testCharArrayValue() {
127         char[] bs = a.charArrayValue();
128         assertEquals('c', bs[0]);
129         assertEquals('b', bs[1]);
130         assertEquals((char) -1, bs[2]);
131     }
132
133     public void testBooleanArrayValue() {
134         boolean[] bs = a.booleanArrayValue();
135         assertEquals(true, bs[0]);
136         assertEquals(false, bs[1]);
137     }
138
139     public void testIntArrayValue() {
140         int[] bs = a.intArrayValue();
141         assertEquals(1, bs[0]);
142         assertEquals(-1, bs[1]);
143     }
144
145     public void testShortArrayValue() {
146         short[] bs = a.shortArrayValue();
147         assertEquals(1, bs[0]);
148         assertEquals(-1, bs[1]);
149     }
150
151     public void testLongArrayValue() {
152         long[] bs = a.longArrayValue();
153         assertEquals(1L, bs[0]);
154         assertEquals(-1L, bs[1]);
155     }
156
157     public void testFloatArrayValue() {
158         float[] bs = a.floatArrayValue();
159         assertEquals(1.0f, bs[0], 0.1f);
160         assertEquals(-1.0f, bs[1], 0.1f);
161     }
162
163     public void testDoubleArrayValue() {
164         double[] bs = a.doubleArrayValue();
165         assertEquals(1.0d, bs[0], 0.1d);
166         assertEquals(-1.0d, bs[1], 0.1d);
167     }
168
169     public void testStringArrayValue() {
170         String JavaDoc[] s = a.stringArrayValue();
171         assertEquals("aa", s[0]);
172         assertEquals("bb", s[1]);
173     }
174
175     public void testAnnotationArrayValue() {
176         ValueAttrAnnotation[] ann = a.annotationArrayValue();
177         assertEquals("annotation1", ann[0].value());
178         assertEquals("annotation2", ann[1].value());
179     }
180
181     public void testEnumArrayValue() {
182         ValuesEnum[] en = a.enumArrayValue();
183         assertEquals(ValuesEnum.ONE, en[0]);
184         assertEquals(ValuesEnum.TWO, en[1]);
185     }
186
187     public void testClassArrayValue() {
188         Class JavaDoc[] c = a.classArrayValue();
189         assertEquals(Values.class.getName(), c[0].getName());
190         assertEquals(Values.class.getName(), c[1].getName());
191     }
192
193     // issue 303711
194
public void testMethodNode() throws Exception JavaDoc {
195         InputStream JavaDoc is = getClass().getResourceAsStream("/"
196                 + annotations.Values.class.getName().replace('.', '/')
197                 + ".class");
198         ClassReader cr = new ClassReader(is);
199         ClassNode cn = new ClassNode();
200         cr.accept(cn, false);
201     }
202
203     private static final class TestClassLoader extends ClassLoader JavaDoc {
204
205         private final String JavaDoc className;
206
207         private final ClassLoader JavaDoc loader;
208
209         public TestClassLoader(String JavaDoc className, ClassLoader JavaDoc loader) {
210             super();
211             this.className = className;
212             this.loader = loader;
213         }
214
215         public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
216             if (className.equals(name)) {
217                 try {
218                     byte[] bytecode = ValuesDump.dump();
219                     return super.defineClass(className,
220                             bytecode,
221                             0,
222                             bytecode.length);
223                 } catch (Exception JavaDoc ex) {
224                     ex.printStackTrace();
225                     throw new ClassNotFoundException JavaDoc("Load error: "
226                             + ex.toString(), ex);
227                 }
228             }
229
230             return loader.loadClass(name);
231         }
232     }
233 }
234
Popular Tags