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 11KB

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