01: /**************************************************************************************
02: * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package test.annotation;
08:
09: import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor;
10: import org.codehaus.aspectwerkz.annotation.instrumentation.asm.AsmAnnotationHelper;
11: import org.objectweb.asm.ClassReader;
12: import org.objectweb.asm.attrs.Attributes;
13: import junit.framework.TestCase;
14:
15: import java.util.List;
16: import java.util.ArrayList;
17: import java.io.ByteArrayOutputStream;
18: import java.io.InputStream;
19:
20: /**
21: * AW-278
22: * We compile annoation with ASM, and read them back with ASM at weave time
23: * then if offline mode was used, the joinpoint advice list is build based on the
24: * annotation on the original method - thus we need to enforce that the annotations have been copied.
25: * <p/>
26: * Note: this test has dependancy on ASM so we need to add ASM in the classpath without having it remapped with
27: * jarjar (since we do not jarjar the tests)
28: *
29: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
30: */
31: public class AnnotationCopyTest extends TestCase {
32:
33: public void testWeaveAndReadnnotation() throws Throwable {
34: ClassLoader classLoader = this .getClass().getClassLoader();
35:
36: // grab the bytecode from the file system (not weaved since test are using load time weaving)
37: InputStream is = classLoader
38: .getResourceAsStream("test/annotation/AnnotationTest.class");
39: ByteArrayOutputStream os = new ByteArrayOutputStream();
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(
49: "test.annotation.AnnotationTest", bytes, classLoader);
50:
51: // do a visit
52: List annotations = new ArrayList();
53: ClassReader asmReader = new ClassReader(weaved);
54: asmReader
55: .accept(
56: new AsmAnnotationHelper.MethodAnnotationExtractor(
57: annotations, "publicMethod", "()V",
58: classLoader), Attributes
59: .getDefaultAttributes(), true);
60: assertEquals(2, annotations.size());
61: }
62:
63: public static void main(String[] args) {
64: junit.textui.TestRunner.run(suite());
65: }
66:
67: public static junit.framework.Test suite() {
68: return new junit.framework.TestSuite(AnnotationCopyTest.class);
69: }
70:
71: }
|