KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > annotation > AnnotationCopyTest


1 /**************************************************************************************
2  * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package test.annotation;
9
10 import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor;
11 import org.codehaus.aspectwerkz.annotation.instrumentation.asm.AsmAnnotationHelper;
12 import org.objectweb.asm.ClassReader;
13 import org.objectweb.asm.attrs.Attributes;
14 import junit.framework.TestCase;
15
16 import java.util.List JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 /**
22  * AW-278
23  * We compile annoation with ASM, and read them back with ASM at weave time
24  * then if offline mode was used, the joinpoint advice list is build based on the
25  * annotation on the original method - thus we need to enforce that the annotations have been copied.
26  * <p/>
27  * Note: this test has dependancy on ASM so we need to add ASM in the classpath without having it remapped with
28  * jarjar (since we do not jarjar the tests)
29  *
30  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur</a>
31  */

32 public class AnnotationCopyTest extends TestCase {
33
34     public void testWeaveAndReadnnotation() throws Throwable JavaDoc {
35         ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
36
37         // grab the bytecode from the file system (not weaved since test are using load time weaving)
38
InputStream JavaDoc is = classLoader.getResourceAsStream("test/annotation/AnnotationTest.class");
39         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
40         for (int b = is.read(); b != -1; b = is.read()) {
41             os.write(b);
42         }
43         byte[] bytes = os.toByteArray();
44
45         // emulate the weaving, which should preserve annotations even if methods are wrapped
46
AspectWerkzPreProcessor awpp = new AspectWerkzPreProcessor();
47         awpp.initialize();
48         byte[] weaved = awpp.preProcess("test.annotation.AnnotationTest", bytes, classLoader);
49
50         // do a visit
51
List JavaDoc annotations = new ArrayList JavaDoc();
52         ClassReader asmReader = new ClassReader(weaved);
53         asmReader.accept(
54                 new AsmAnnotationHelper.MethodAnnotationExtractor(annotations, "publicMethod", "()V", classLoader),
55                 Attributes.getDefaultAttributes(),
56                 true
57         );
58         assertEquals(2, annotations.size());
59     }
60
61     public static void main(String JavaDoc[] args) {
62         junit.textui.TestRunner.run(suite());
63     }
64
65     public static junit.framework.Test suite() {
66         return new junit.framework.TestSuite(AnnotationCopyTest.class);
67     }
68
69
70 }
71
Popular Tags