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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import junit.framework.Test;
  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. /**
  35. * Method transformation, example:
  36. *
  37. * public synchronized void m(); Code: Stack=2, Locals=1, Args_size=1 0: getstatic #2; //Field
  38. * java/lang/System.err:Ljava/io/PrintStream; 3: ldc #3; //String hello 5: invokevirtual #4; //Method
  39. * java/io/PrintStream.println:(Ljava/lang/String;)V 8: getstatic #2; //Field java/lang/System.err:Ljava/io/PrintStream; 11: ldc #5;
  40. * //String world 13: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 16: return LineNumberTable: line
  41. * 4: 0 line 5: 8 line 6: 16
  42. *
  43. * 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
  44. * java/lang/System.err:Ljava/io/PrintStream; 7: ldc #3; //String hello 9: invokevirtual #4; //Method
  45. * java/io/PrintStream.println:(Ljava/lang/String;)V 12: getstatic #2; //Field java/lang/System.err:Ljava/io/PrintStream; 15: ldc
  46. * #5; //String world 17: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 20: aload_1 21: monitorexit
  47. * 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
  48. * 25 any 25 28 25 any
  49. */
  50. public class SynchronizationTransformTests extends XMLBasedAjcTestCase {
  51. private static boolean regenerate;
  52. static {
  53. regenerate = false;
  54. }
  55. private World world;
  56. public void testInvestigatingTransforming() {
  57. runTest("investigation");
  58. checkMethod("Investigation", "b"); // similar output to One.b
  59. checkMethod("Investigation", "c");
  60. checkMethod("Investigation", "d");
  61. checkMethod("Investigation", "e");
  62. }
  63. public void testTransform1() {
  64. runTest("One");
  65. checkMethod("One", "b");
  66. checkMethod("One", "c");
  67. checkMethod("One", "e");
  68. }
  69. // before() on execution jp
  70. public void testTransform2() {
  71. runTest("Two");
  72. checkMethod("C", "ma");
  73. }
  74. public void testTransform2XlintOff() {
  75. runTest("Two - xlintoff");
  76. checkMethod("C", "ma");
  77. }
  78. // after() returning/after() throwing on execution jp
  79. // after() returning -> make all returns go through the same exit point and make
  80. // it call the advice
  81. // after() throwing -> add a catch block that calls the advice
  82. public void testTransform3() {
  83. runTest("Three");
  84. checkMethod("C", "m3");
  85. checkMethod("C", "m32");
  86. checkMethod("C", "m33"); // like m() but synchronized block
  87. checkMethod("C", "m34"); // like m2() but synchronized block
  88. }
  89. // like testTransform3() but pointcuts explicitly specify synchronized
  90. public void testTransform4() {
  91. runTest("Four");
  92. checkMethod("C", "m");
  93. checkMethod("C", "m2");
  94. }
  95. // Java5 variant
  96. public void testStaticSynchronizedMethodTransformJava5() {
  97. runTest("Five - Java5");
  98. checkMethod("C", "b");
  99. }
  100. // < Java5 variant
  101. public void testStaticSynchronizedMethodTransformPreJava5() {
  102. runTest("Six - preJava5");
  103. checkMethod("C", "bbb");
  104. }
  105. public void testLockPcdOnTransformedNonStaticMethod() {
  106. runTest("lock pcd on transformed non-static method");
  107. }
  108. public void testUnlockPcdOnTransformedNonStaticMethod() {
  109. runTest("unlock pcd on transformed non-static method");
  110. }
  111. public void testLockPcdOnTransformedStaticMethod() {
  112. runTest("lock pcd on transformed static method - J5");
  113. }
  114. public void testUnlockPcdOnTransformedStaticMethod() {
  115. runTest("unlock pcd on transformed static method - J5");
  116. }
  117. public void testLockPcdOnTransformedStaticMethodPreJ5() {
  118. runTest("lock pcd on transformed static method - preJ5");
  119. }
  120. public void testUnlockPcdOnTransformedStaticMethodPreJ5() {
  121. runTest("unlock pcd on transformed static method - preJ5");
  122. }
  123. public void testJoinpointsEnabledButNoLock() {
  124. runTest("joinpoints enabled but no lock");
  125. }
  126. public void testTransformWithLTW() {
  127. runTest("transform with LTW");
  128. }
  129. public void testTransformStaticMethodPreJava5() {
  130. runTest("transform static method - preJ5");
  131. }
  132. public void testTransformStaticMethodPreJava5_2() {
  133. runTest("transform static method - packages - preJ5");
  134. }
  135. // more complex code sequences...
  136. public void testOtherTargeters() {
  137. runTest("other targeters");
  138. }
  139. // --- infrastructure below
  140. private void checkMethod(String typename, String methodname) {
  141. LazyMethodGen m = getMethod(typename, methodname);
  142. File expectedF = new File(".." + File.separator + "tests" + File.separator + "features152" + File.separator
  143. + "synchronization" + File.separator + "transformed" + File.separator + "expected" + File.separator + typename
  144. + "." + methodname + ".txt");
  145. if (regenerate) {
  146. saveMethod(expectedF, m);
  147. } else {
  148. compareMethod(expectedF, m);
  149. }
  150. }
  151. private LazyMethodGen getMethod(String typename, String methodname) {
  152. BcelObjectType type = getBcelObjectFor(typename);
  153. LazyClassGen lcg = type.getLazyClassGen();
  154. List<LazyMethodGen> methods = lcg.getMethodGens();
  155. for (LazyMethodGen element: methods) {
  156. if (element.getName().equals(methodname)) {
  157. return element;
  158. }
  159. }
  160. return null;
  161. }
  162. private BcelObjectType getBcelObjectFor(String clazzname) {
  163. ensureWorldSetup();
  164. ResolvedType rt = world.resolve(clazzname);
  165. if (rt == null)
  166. fail("Couldn't find class " + clazzname);
  167. ReferenceType rtt = (ReferenceType) rt;
  168. BcelObjectType bot = (BcelObjectType) rtt.getDelegate();
  169. return bot;
  170. }
  171. private void ensureWorldSetup() {
  172. if (world == null) {
  173. world = new BcelWorld(getSandboxDirectory() + File.pathSeparator + System.getProperty("java.class.path"));
  174. }
  175. }
  176. protected Method getMethod(JavaClass cl, String methodname) {
  177. Method[] methods = cl.getMethods();
  178. for (int i = 0; i < methods.length; i++) {
  179. Method m = methods[i];
  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].indexOf("MethodDeclarationLineNumber") == -1 && !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 (Iterator<String> iter = l.iterator(); iter.hasNext();) {
  224. String str = iter.next();
  225. result.append(str);
  226. result.append("\n");
  227. }
  228. return result.toString();
  229. }
  230. private void saveMethod(File f, LazyMethodGen m) {
  231. System.out.println("Saving method into " + f.getName());
  232. try {
  233. m.print(new PrintStream(new FileOutputStream(f)), null);
  234. } catch (FileNotFoundException e) {
  235. e.printStackTrace();
  236. fail("Couldn't store the method in file " + f);
  237. }
  238. }
  239. // --- helpers
  240. // Half finished - could check there is only one relationship for unlock() rather than two - but
  241. // that seems to be the case anyway (peculiar...)
  242. // private void checkModel1() {
  243. // // Verifies only one unlock relationship, not two
  244. // IProgramElement unlockNode =
  245. // AsmManager.getDefault().getHierarchy().findElementForLabel(AsmManager.getDefault().getHierarchy().getRoot(),
  246. // IProgramElement.Kind.CODE,"unlock(void java.lang.Object.<unlock>(java.lang.Object))");
  247. // assertTrue("Couldn't find the unlock node",unlockNode!=null);
  248. // List l = AsmManager.getDefault().getRelationshipMap().get(unlockNode);
  249. // assertTrue("should be one entry :"+l,l!=null && l.size()==1);
  250. // IRelationship ir = (IRelationship)l.get(0);
  251. // System.err.println(ir);
  252. // List targs = ir.getTargets();
  253. // System.err.println(targs.size());
  254. // System.err.println(targs.get(0));
  255. // }
  256. // ---
  257. public static Test suite() {
  258. return XMLBasedAjcTestCase.loadSuite(SynchronizationTransformTests.class);
  259. }
  260. protected File getSpecFile() {
  261. return getClassResource("synchronization.xml");
  262. }
  263. public void tearDown() {
  264. world = null;
  265. }
  266. }