You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SynchronizationTransformTests.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*******************************************************************************
  2. * Copyright (c) 2006 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Andy Clement - initial implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc152;
  12. import java.io.BufferedReader;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileOutputStream;
  16. import java.io.FileReader;
  17. import java.io.PrintStream;
  18. import java.net.URL;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import org.aspectj.apache.bcel.classfile.JavaClass;
  23. import org.aspectj.apache.bcel.classfile.Method;
  24. import org.aspectj.testing.XMLBasedAjcTestCase;
  25. import org.aspectj.testing.util.TestUtil;
  26. import org.aspectj.testing.util.TestUtil.LineStream;
  27. import org.aspectj.weaver.ReferenceType;
  28. import org.aspectj.weaver.ResolvedType;
  29. import org.aspectj.weaver.World;
  30. import org.aspectj.weaver.bcel.BcelObjectType;
  31. import org.aspectj.weaver.bcel.BcelWorld;
  32. import org.aspectj.weaver.bcel.LazyClassGen;
  33. import org.aspectj.weaver.bcel.LazyMethodGen;
  34. import junit.framework.Test;
  35. /**
  36. * Method transformation, example:
  37. *
  38. * public synchronized void m(); Code: Stack=2, Locals=1, Args_size=1 0: getstatic #2; //Field
  39. * java/lang/System.err:Ljava/io/PrintStream; 3: ldc #3; //String hello 5: invokevirtual #4; //Method
  40. * java/io/PrintStream.println:(Ljava/lang/String;)V 8: getstatic #2; //Field java/lang/System.err:Ljava/io/PrintStream; 11: ldc #5;
  41. * //String world 13: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 16: return LineNumberTable: line
  42. * 4: 0 line 5: 8 line 6: 16
  43. *
  44. * public void m2(); Code: Stack=2, Locals=3, Args_size=1 0: aload_0 1: dup 2: astore_1 3: monitorenter 4: getstatic #2; //Field
  45. * java/lang/System.err:Ljava/io/PrintStream; 7: ldc #3; //String hello 9: invokevirtual #4; //Method
  46. * java/io/PrintStream.println:(Ljava/lang/String;)V 12: getstatic #2; //Field java/lang/System.err:Ljava/io/PrintStream; 15: ldc
  47. * #5; //String world 17: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 20: aload_1 21: monitorexit
  48. * 22: goto 30 25: astore_2 26: aload_1 27: monitorexit 28: aload_2 29: athrow 30: return Exception table: from to target type 4 22
  49. * 25 any 25 28 25 any
  50. */
  51. public class SynchronizationTransformTests extends XMLBasedAjcTestCase {
  52. private static boolean regenerate;
  53. static {
  54. regenerate = false;
  55. }
  56. private World world;
  57. public void testInvestigatingTransforming() {
  58. runTest("investigation");
  59. checkMethod("Investigation", "b"); // similar output to One.b
  60. checkMethod("Investigation", "c");
  61. checkMethod("Investigation", "d");
  62. checkMethod("Investigation", "e");
  63. }
  64. public void testTransform1() {
  65. runTest("One");
  66. checkMethod("One", "b");
  67. checkMethod("One", "c");
  68. checkMethod("One", "e");
  69. }
  70. // before() on execution jp
  71. public void testTransform2() {
  72. runTest("Two");
  73. checkMethod("C", "ma");
  74. }
  75. public void testTransform2XlintOff() {
  76. runTest("Two - xlintoff");
  77. checkMethod("C", "ma");
  78. }
  79. // after() returning/after() throwing on execution jp
  80. // after() returning -> make all returns go through the same exit point and make
  81. // it call the advice
  82. // after() throwing -> add a catch block that calls the advice
  83. public void testTransform3() {
  84. runTest("Three");
  85. checkMethod("C", "m3");
  86. checkMethod("C", "m32");
  87. checkMethod("C", "m33"); // like m() but synchronized block
  88. checkMethod("C", "m34"); // like m2() but synchronized block
  89. }
  90. // like testTransform3() but pointcuts explicitly specify synchronized
  91. public void testTransform4() {
  92. runTest("Four");
  93. checkMethod("C", "m");
  94. checkMethod("C", "m2");
  95. }
  96. // Java5 variant
  97. public void testStaticSynchronizedMethodTransformJava5() {
  98. runTest("Five - Java5");
  99. checkMethod("C", "b");
  100. }
  101. // < Java5 variant
  102. public void testStaticSynchronizedMethodTransformPreJava5() {
  103. runTest("Six - preJava5");
  104. checkMethod("C", "bbb");
  105. }
  106. public void testLockPcdOnTransformedNonStaticMethod() {
  107. runTest("lock pcd on transformed non-static method");
  108. }
  109. public void testUnlockPcdOnTransformedNonStaticMethod() {
  110. runTest("unlock pcd on transformed non-static method");
  111. }
  112. public void testLockPcdOnTransformedStaticMethod() {
  113. runTest("lock pcd on transformed static method - J5");
  114. }
  115. public void testUnlockPcdOnTransformedStaticMethod() {
  116. runTest("unlock pcd on transformed static method - J5");
  117. }
  118. public void testLockPcdOnTransformedStaticMethodPreJ5() {
  119. runTest("lock pcd on transformed static method - preJ5");
  120. }
  121. public void testUnlockPcdOnTransformedStaticMethodPreJ5() {
  122. runTest("unlock pcd on transformed static method - preJ5");
  123. }
  124. public void testJoinpointsEnabledButNoLock() {
  125. runTest("joinpoints enabled but no lock");
  126. }
  127. public void testTransformWithLTW() {
  128. runTest("transform with LTW");
  129. }
  130. public void testTransformStaticMethodPreJava5() {
  131. runTest("transform static method - preJ5");
  132. }
  133. public void testTransformStaticMethodPreJava5_2() {
  134. runTest("transform static method - packages - preJ5");
  135. }
  136. // more complex code sequences...
  137. public void testOtherTargeters() {
  138. runTest("other targeters");
  139. }
  140. // --- infrastructure below
  141. private void checkMethod(String typename, String methodname) {
  142. LazyMethodGen m = getMethod(typename, methodname);
  143. File expectedF = new File(".." + File.separator + "tests" + File.separator + "features152" + File.separator
  144. + "synchronization" + File.separator + "transformed" + File.separator + "expected" + File.separator + typename
  145. + "." + methodname + ".txt");
  146. if (regenerate) {
  147. saveMethod(expectedF, m);
  148. } else {
  149. compareMethod(expectedF, m);
  150. }
  151. }
  152. private LazyMethodGen getMethod(String typename, String methodname) {
  153. BcelObjectType type = getBcelObjectFor(typename);
  154. LazyClassGen lcg = type.getLazyClassGen();
  155. List<LazyMethodGen> methods = lcg.getMethodGens();
  156. for (LazyMethodGen element: methods) {
  157. if (element.getName().equals(methodname)) {
  158. return element;
  159. }
  160. }
  161. return null;
  162. }
  163. private BcelObjectType getBcelObjectFor(String clazzname) {
  164. ensureWorldSetup();
  165. ResolvedType rt = world.resolve(clazzname);
  166. if (rt == null)
  167. fail("Couldn't find class " + clazzname);
  168. ReferenceType rtt = (ReferenceType) rt;
  169. BcelObjectType bot = (BcelObjectType) rtt.getDelegate();
  170. return bot;
  171. }
  172. private void ensureWorldSetup() {
  173. if (world == null) {
  174. world = new BcelWorld(getSandboxDirectory() + File.pathSeparator + System.getProperty("java.class.path"));
  175. }
  176. }
  177. protected Method getMethod(JavaClass cl, String methodname) {
  178. Method[] methods = cl.getMethods();
  179. for (Method m : methods) {
  180. if (m.getName().equals(methodname)) {
  181. return m;
  182. }
  183. }
  184. return null;
  185. }
  186. public void dump(String title, String[] strs) {
  187. System.err.println(title);
  188. for (int i = 0; i < strs.length; i++) {
  189. System.err.println(i + ") " + strs[i]);
  190. }
  191. }
  192. private void compareMethod(File f, LazyMethodGen m) {
  193. BufferedReader fr;
  194. if (!f.exists()) {
  195. fail("Can't find expected output file " + f);
  196. }
  197. try {
  198. // Load the file in
  199. fr = new BufferedReader(new FileReader(f));
  200. String line = null;
  201. List<String> originalFileContents = new ArrayList<>();
  202. while ((line = fr.readLine()) != null)
  203. originalFileContents.add(line);
  204. String[] fileContents = (String[]) originalFileContents.toArray(new String[] {});
  205. LineStream ls = new TestUtil.LineStream();
  206. m.print(ls, null);
  207. String[] lines = ls.getLines();
  208. for (int i = 0; i < lines.length; i++) {
  209. String existingLine = lines[i];
  210. if (!fileContents[i].contains("MethodDeclarationLineNumber") && !fileContents[i].equals(existingLine)) {
  211. dump("File contents:", fileContents);
  212. dump("Actual:", lines);
  213. fail("\nDifference in method " + m.getName() + " on line " + i + " between the expected:\n" + fileContents[i]
  214. + "\nand the found:\n" + existingLine);
  215. }
  216. }
  217. } catch (Exception e) {
  218. fail("Unexpected exception saving weaving messages:" + e);
  219. }
  220. }
  221. private String stringify(List<String> l) {
  222. StringBuffer result = new StringBuffer();
  223. for (String str : l) {
  224. result.append(str);
  225. result.append("\n");
  226. }
  227. return result.toString();
  228. }
  229. private void saveMethod(File f, LazyMethodGen m) {
  230. System.out.println("Saving method into " + f.getName());
  231. try {
  232. m.print(new PrintStream(new FileOutputStream(f)), null);
  233. } catch (FileNotFoundException e) {
  234. e.printStackTrace();
  235. fail("Couldn't store the method in file " + f);
  236. }
  237. }
  238. // --- helpers
  239. // Half finished - could check there is only one relationship for unlock() rather than two - but
  240. // that seems to be the case anyway (peculiar...)
  241. // private void checkModel1() {
  242. // // Verifies only one unlock relationship, not two
  243. // IProgramElement unlockNode =
  244. // AsmManager.getDefault().getHierarchy().findElementForLabel(AsmManager.getDefault().getHierarchy().getRoot(),
  245. // IProgramElement.Kind.CODE,"unlock(void java.lang.Object.<unlock>(java.lang.Object))");
  246. // assertTrue("Couldn't find the unlock node",unlockNode!=null);
  247. // List l = AsmManager.getDefault().getRelationshipMap().get(unlockNode);
  248. // assertTrue("should be one entry :"+l,l!=null && l.size()==1);
  249. // IRelationship ir = (IRelationship)l.get(0);
  250. // System.err.println(ir);
  251. // List targs = ir.getTargets();
  252. // System.err.println(targs.size());
  253. // System.err.println(targs.get(0));
  254. // }
  255. // ---
  256. public static Test suite() {
  257. return XMLBasedAjcTestCase.loadSuite(SynchronizationTransformTests.class);
  258. }
  259. protected URL getSpecFile() {
  260. return getClassResource("synchronization.xml");
  261. }
  262. public void tearDown() {
  263. world = null;
  264. }
  265. }