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;
17  import java.util.ArrayList;
18  import java.io.ByteArrayOutputStream;
19  import java.io.InputStream;
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 {
35          ClassLoader classLoader = this.getClass().getClassLoader();
36  
37          // grab the bytecode from the file system (not weaved since test are using load time weaving)
38          InputStream is = classLoader.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("test.annotation.AnnotationTest", bytes, classLoader);
49  
50          // do a visit
51          List annotations = new ArrayList();
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[] 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  }