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 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 Javassist 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   *
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 testJavassistASMAnnotation() 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.getResourceAsStream("test/annotation/AnnotationTest.class");
38          ByteArrayOutputStream os = new ByteArrayOutputStream();
39          for (int b = is.read(); b != -1; b = is.read()) {
40              os.write(b);
41          }
42          byte[] bytes = os.toByteArray();
43  
44          // emulate the weaving, which should preserve annotations even if methods are wrapped
45          AspectWerkzPreProcessor awpp = new AspectWerkzPreProcessor();
46          awpp.initialize(null);
47          byte[] weaved = awpp.preProcess("test.annotation.AnnotationTest", bytes, classLoader);
48  
49          // do a visit
50          List annotations = new ArrayList();
51          ClassReader asmReader = new ClassReader(weaved);
52          asmReader.accept(
53                  new AsmAnnotationHelper.MethodAnnotationExtractor(annotations, "publicMethod", "()V", classLoader),
54                  AsmAnnotationHelper.ANNOTATIONS_ATTRIBUTES,
55                  true
56          );
57          assertEquals(2, annotations.size());
58      }
59  
60      public static void main(String[] args) {
61          junit.textui.TestRunner.run(suite());
62      }
63  
64      public static junit.framework.Test suite() {
65          return new junit.framework.TestSuite(AnnotationCopyTest.class);
66      }
67  
68  
69  
70  }