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.

Ajc154Tests.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*******************************************************************************
  2. * Copyright (c) 2006 IBM
  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 API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc154;
  12. import java.lang.reflect.Field;
  13. import org.aspectj.apache.bcel.classfile.ConstantPool;
  14. import org.aspectj.apache.bcel.classfile.JavaClass;
  15. import org.aspectj.apache.bcel.classfile.LineNumber;
  16. import org.aspectj.apache.bcel.classfile.LineNumberTable;
  17. import org.aspectj.apache.bcel.classfile.Method;
  18. import org.aspectj.apache.bcel.generic.MethodGen;
  19. import org.aspectj.testing.XMLBasedAjcTestCase;
  20. import org.aspectj.weaver.tools.ContextBasedMatcher;
  21. import org.aspectj.weaver.tools.FuzzyBoolean;
  22. import org.aspectj.weaver.tools.MatchingContext;
  23. import org.aspectj.weaver.tools.PointcutDesignatorHandler;
  24. import junit.framework.Test;
  25. /**
  26. * These are tests for AspectJ1.5.4
  27. */
  28. public class Ajc154Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
  29. // public void testNewDesignatorsReferencePointcuts_pr205907() {
  30. // BeanDesignatorHandler beanHandler = new BeanDesignatorHandler();
  31. // Set set = new HashSet();
  32. // set.add(beanHandler);
  33. // PatternParser.setTestDesignators(set);
  34. // //parser.registerPointcutDesignatorHandler(beanHandler);
  35. // runTest("new pointcut designators in a reference pointcut");
  36. // }
  37. // public void testAfterThrowingAnnotationStyle_pr211674_1() { runTest("after throwing annotation style problem - 1");}
  38. // public void testAfterThrowingAnnotationStyle_pr211674_2() { runTest("after throwing annotation style problem - 2");}
  39. // crappy solution - see the bug
  40. // public void testCflowLtwProblem_pr166647_1() {
  41. // try {
  42. // runTest("ltw and cflow problem");
  43. // } catch (AssertionFailedError afe) {
  44. // // this is OK.... sadly
  45. // // at least lets check we warned the user it was going to happen:
  46. // String stderr = (getLastRunResult() == null ? "" : getLastRunResult().getStdErr());
  47. // // Expected line:
  48. // // [WeavingURLClassLoader] warning XML Defined aspects must be woven in cases where cflow pointcuts are involved.
  49. // // Currently the include/exclude patterns exclude 'x.Aspect2' [Xlint:mustWeaveXmlDefinedAspects]
  50. // assertTrue("Did not see warning about needing to weave xml defined aspects", stderr
  51. // .indexOf("warning XML Defined aspects must be woven in cases where cflow pointcuts are involved.") != -1);
  52. // assertTrue("Xlint warning was expected '[Xlint:mustWeaveXmlDefinedAspects]'", stderr
  53. // .indexOf("[Xlint:mustWeaveXmlDefinedAspects]") != -1);
  54. // }
  55. // }
  56. // Testing some issues with declare at type
  57. public void testDeclareAtTypeProblems_pr211052_1() {
  58. runTest("declare atType problems - 1");
  59. }
  60. public void testDeclareAtTypeProblems_pr211052_2() {
  61. runTest("declare atType problems - 2");
  62. }
  63. public void testDeclareAtTypeProblems_pr211052_3() {
  64. runTest("declare atType problems - 3");
  65. }
  66. public void testDeclareAtTypeProblems_pr211052_4() {
  67. runTest("declare atType problems - 4");
  68. }
  69. public void testDeclareAtTypeProblems_pr211052_5() {
  70. runTest("declare atType problems - 5");
  71. }
  72. // declare at type and binary weaving
  73. public void testDeclareAtTypeProblems_pr211052_6() {
  74. runTest("declare atType problems - 6");
  75. }
  76. public void testDeclareAtTypeProblems_pr211052_7() {
  77. runTest("declare atType problems - 7");
  78. }
  79. public void testNPEWithMissingAtAspectAnnotationInPointcutLibrary_pr162539_1() {
  80. runTest("NPE with missing @aspect annotation in pointcut library - 1");
  81. }
  82. public void testNPEWithMissingAtAspectAnnotationInPointcutLibrary_pr162539_2() {
  83. runTest("NPE with missing @aspect annotation in pointcut library - 2");
  84. }
  85. public void testWrongNumberOfTypeParameters_pr176991() {
  86. runTest("wrong number of type parameters");
  87. }
  88. public void testArgNamesDoesNotWork_pr148381_1() {
  89. runTest("argNames does not work - simple");
  90. }
  91. public void testArgNamesDoesNotWork_pr148381_2() {
  92. runTest("argNames does not work - error1");
  93. }
  94. public void testArgNamesDoesNotWork_pr148381_3() {
  95. runTest("argNames does not work - error2");
  96. }
  97. public void testArgNamesDoesNotWork_pr148381_4() {
  98. runTest("argNames does not work - error3");
  99. }
  100. public void testDecpProblemWhenTargetAlreadyImplements_pr169432_1() {
  101. runTest("declare parents problem when target already implements interface - 1");
  102. }
  103. public void testDecpProblemWhenTargetAlreadyImplements_pr169432_2() {
  104. runTest("declare parents problem when target already implements interface - 2");
  105. }
  106. public void testDecpProblemWhenTargetAlreadyImplements_pr169432_3() {
  107. runTest("declare parents problem when target already implements interface - 3");
  108. }
  109. public void testVariousLtwAroundProblems_pr209019_1() {
  110. runTest("various issues with ltw and around advice - 1");
  111. }
  112. public void testVariousLtwAroundProblems_pr209019_2() {
  113. runTest("various issues with ltw and around advice - 2");
  114. }
  115. public void testVariousLtwAroundProblems_pr209019_3() {
  116. runTest("various issues with ltw and around advice - 3");
  117. }
  118. public void testVariousLtwAroundProblems_pr209019_4() {
  119. runTest("various issues with ltw and around advice - 4");
  120. }
  121. public void testAbstractAnnotationStylePointcutWithContext_pr202088() {
  122. runTest("abstract annotation style pointcut with context");
  123. }
  124. public void testNoErrorForAtDecpInNormalClass_pr169428() {
  125. runTest("no error for atDecp in normal class");
  126. }
  127. public void testJarsZipsNonStandardSuffix_pr186673() {
  128. runTest("jars and zips with non-standard suffix");
  129. }
  130. public void testItdOnGenericInnerInterface_pr203646() {
  131. runTest("npe with itd on inner generic interface");
  132. }
  133. public void testItdOnGenericInnerInterface_pr203646_A() {
  134. runTest("npe with itd on inner generic interface - exampleA");
  135. }
  136. public void testItdOnGenericInnerInterface_pr203646_B() {
  137. runTest("npe with itd on inner generic interface - exampleB");
  138. }
  139. public void testItdOnGenericInnerInterface_pr203646_C() {
  140. runTest("npe with itd on inner generic interface - exampleC");
  141. }
  142. public void testItdOnGenericInnerInterface_pr203646_D() {
  143. runTest("npe with itd on inner generic interface - exampleD");
  144. }
  145. // public void testItdOnGenericInnerInterface_pr203646_E() { runTest("npe with itd on inner generic interface - exampleE");} //
  146. // needs parser change
  147. public void testItdOnGenericInnerInterface_pr203646_F() {
  148. runTest("npe with itd on inner generic interface - exampleF");
  149. }
  150. public void testItdOnGenericInnerInterface_pr203646_G() {
  151. runTest("npe with itd on inner generic interface - exampleG");
  152. }
  153. public void testItdClashForTypesFromAspectPath_pr206732() {
  154. runTest("itd clash for types from aspectpath");
  155. }
  156. // public void testAnnotationStyleAndMultiplePackages_pr197719() {
  157. // runTest("annotation style syntax and cross package extension"); }
  158. /**
  159. * Complex test that attempts to damage a class like a badly behaved bytecode transformer would and checks if AspectJ can cope.
  160. *
  161. * @throws NoSuchFieldException
  162. * @throws SecurityException
  163. * @throws IllegalAccessException
  164. * @throws IllegalArgumentException
  165. */
  166. public void testCopingWithGarbage_pr175806_1() throws ClassNotFoundException, SecurityException, NoSuchFieldException,
  167. IllegalArgumentException, IllegalAccessException {
  168. // Compile the program we are going to mess with
  169. runTest("coping with bad tables");
  170. // Load up the class and the method 'main' we are interested in
  171. JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), "A");
  172. Method[] meths = jc.getMethods();
  173. Method oneWeWant = null;
  174. for (int i = 0; i < meths.length && oneWeWant == null; i++) {
  175. Method method = meths[i];
  176. if (method.getName().equals("main")) {
  177. oneWeWant = meths[i];
  178. }
  179. }
  180. /**
  181. * For the main method: Stack=2, Locals=3, Args_size=1 0: iconst_5 1: istore_1 2: ldc #18; //String 3 4: astore_2 5:
  182. * getstatic #24; //Field java/lang/System.out:Ljava/io/PrintStream; 8: aload_2 9: invokevirtual #30; //Method
  183. * java/io/PrintStream.println:(Ljava/lang/String;)V 12: goto 23 15: pop 16: getstatic #24; //Field
  184. * java/lang/System.out:Ljava/io/PrintStream; 19: iload_1 20: invokevirtual #33; //Method java/io/PrintStream.println:(I)V
  185. * 23: return Exception table: from to target type 2 15 15 Class java/lang/Exception
  186. *
  187. * LineNumberTable: line 4: 0 line 6: 2 line 7: 5 line 8: 15 line 9: 16 line 11: 23 LocalVariableTable: Start Length Slot
  188. * Name Signature 0 24 0 argv [Ljava/lang/String; 2 22 1 i I 5 10 2 s Ljava/lang/String;
  189. */
  190. ConstantPool cp = oneWeWant.getConstantPool();
  191. // ConstantPool cpg = new ConstantPool(cp);
  192. // Damage the line number table, entry 2 (Line7:5) so it points to an invalid (not on an instruction boundary) position of 6
  193. Field ff = LineNumber.class.getDeclaredField("startPC");
  194. ff.setAccessible(true);
  195. ff.set(oneWeWant.getLineNumberTable().getLineNumberTable()[2], new Integer(6));
  196. // oneWeWant.getLineNumberTable().getLineNumberTable()[2].setStartPC(6);
  197. // Should be 'rounded down' when transforming it into a MethodGen, new position will be '5'
  198. // System.out.println("BEFORE\n"+oneWeWant.getLineNumberTable().toString());
  199. MethodGen toTransform = new MethodGen(oneWeWant, "A", cp, false);
  200. LineNumberTable lnt = toTransform.getMethod().getLineNumberTable();
  201. assertTrue("Should have been 'rounded down' to position 5 but is " + lnt.getLineNumberTable()[2].getStartPC(), lnt
  202. .getLineNumberTable()[2].getStartPC() == 5);
  203. // System.out.println("AFTER\n"+lnt.toString());
  204. }
  205. public void testCopingWithGarbage_pr175806_2() throws ClassNotFoundException {
  206. // Compile the program we are going to mess with
  207. runTest("coping with bad tables");
  208. // Load up the class and the method 'main' we are interested in
  209. JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), "A");
  210. Method[] meths = jc.getMethods();
  211. Method oneWeWant = null;
  212. for (int i = 0; i < meths.length && oneWeWant == null; i++) {
  213. Method method = meths[i];
  214. if (method.getName().equals("main")) {
  215. oneWeWant = meths[i];
  216. }
  217. }
  218. // see previous test for dump of main method
  219. ConstantPool cp = oneWeWant.getConstantPool();
  220. // ConstantPoolGen cpg = new ConstantPoolGen(cp);
  221. // Damage the local variable table, entry 2 (" 2 22 1 i I") so it points to an invalid start pc of 3
  222. oneWeWant.getLocalVariableTable().getLocalVariable(1).setStartPC(3);
  223. // Should be 'rounded down' when transforming it into a MethodGen, new position will be '2'
  224. // This next line will go BANG with an NPE if we don't correctly round the start pc down to 2
  225. new MethodGen(oneWeWant, "A", cp, true);
  226. }
  227. public void testGenericAspectGenericPointcut_pr174449() {
  228. runTest("problem with generic aspect and generic pointcut");
  229. }
  230. public void testGenericAspectGenericPointcut_noinline_pr174449() {
  231. runTest("problem with generic aspect and generic pointcut - noinline");
  232. }
  233. public void testGenericMethodsAndOrdering_ok_pr171953_2() {
  234. runTest("problem with generic methods and ordering - ok");
  235. }
  236. public void testGenericMethodsAndOrdering_bad_pr171953_2() {
  237. runTest("problem with generic methods and ordering - bad");
  238. }
  239. public void testItdAndJoinpointSignatureCollection_ok_pr171953() {
  240. runTest("problem with itd and join point signature collection - ok");
  241. }
  242. public void testItdAndJoinpointSignatureCollection_bad_pr171953() {
  243. runTest("problem with itd and join point signature collection - bad");
  244. }
  245. public void testGenericMethodsAndItds_pr171952() {
  246. runTest("generic methods and ITDs");
  247. }
  248. // public void testUsingDecpAnnotationWithoutAspectAnnotation_pr169428() {
  249. // runTest("using decp annotation without aspect annotation");}
  250. public void testItdsParameterizedParameters_pr170467() {
  251. runTest("itds and parameterized parameters");
  252. }
  253. public void testComplexGenerics_pr168044() {
  254. runTest("complex generics - 1");
  255. }
  256. public void testIncorrectlyMarkingFieldTransient_pr168063() {
  257. runTest("incorrectly marking field transient");
  258. }
  259. public void testInheritedAnnotations_pr169706() {
  260. runTest("inherited annotations");
  261. }
  262. public void testGenericFieldNPE_pr165885() {
  263. runTest("generic field npe");
  264. }
  265. public void testIncorrectOptimizationOfIstore_pr166084() {
  266. runTest("incorrect optimization of istore");
  267. }
  268. public void testDualParameterizationsNotAllowed_pr165631() {
  269. runTest("dual parameterizations not allowed");
  270. }
  271. public void testSuppressWarnings1_pr166238() {
  272. runTest("Suppress warnings1");
  273. }
  274. public void testSuppressWarnings2_pr166238() {
  275. runTest("Suppress warnings2");
  276. }
  277. public void testNullReturnedFromGetField_pr172107() {
  278. runTest("null returned from getField()");
  279. }
  280. // ///////////////////////////////////////
  281. public static Test suite() {
  282. return XMLBasedAjcTestCase.loadSuite(Ajc154Tests.class);
  283. }
  284. protected java.net.URL getSpecFile() {
  285. return getClassResource("ajc154.xml");
  286. }
  287. // ---
  288. private class BeanDesignatorHandler implements PointcutDesignatorHandler {
  289. private String askedToParse;
  290. public boolean simulateDynamicTest = false;
  291. public String getDesignatorName() {
  292. return "bean";
  293. }
  294. /*
  295. * (non-Javadoc)
  296. *
  297. * @see org.aspectj.weaver.tools.PointcutDesignatorHandler#parse(java.lang.String)
  298. */
  299. public ContextBasedMatcher parse(String expression) {
  300. this.askedToParse = expression;
  301. return new BeanPointcutExpression(expression, this.simulateDynamicTest);
  302. }
  303. public String getExpressionLastAskedToParse() {
  304. return this.askedToParse;
  305. }
  306. }
  307. private class BeanPointcutExpression implements ContextBasedMatcher {
  308. private final String beanNamePattern;
  309. private final boolean simulateDynamicTest;
  310. public BeanPointcutExpression(String beanNamePattern, boolean simulateDynamicTest) {
  311. this.beanNamePattern = beanNamePattern;
  312. this.simulateDynamicTest = simulateDynamicTest;
  313. }
  314. public boolean couldMatchJoinPointsInType(Class aClass) {
  315. return true;
  316. }
  317. /*
  318. * (non-Javadoc)
  319. *
  320. * @see org.aspectj.weaver.tools.ContextBasedMatcher#couldMatchJoinPointsInType(java.lang.Class)
  321. */
  322. public boolean couldMatchJoinPointsInType(Class aClass, MatchingContext context) {
  323. if (this.beanNamePattern.equals(context.getBinding("beanName"))) {
  324. return true;
  325. } else {
  326. return false;
  327. }
  328. }
  329. /*
  330. * (non-Javadoc)
  331. *
  332. * @see org.aspectj.weaver.tools.ContextBasedMatcher#mayNeedDynamicTest()
  333. */
  334. public boolean mayNeedDynamicTest() {
  335. return this.simulateDynamicTest;
  336. }
  337. public FuzzyBoolean matchesStatically(MatchingContext matchContext) {
  338. if (this.simulateDynamicTest) {
  339. return FuzzyBoolean.MAYBE;
  340. }
  341. if (this.beanNamePattern.equals(matchContext.getBinding("beanName"))) {
  342. return FuzzyBoolean.YES;
  343. } else {
  344. return FuzzyBoolean.NO;
  345. }
  346. }
  347. /*
  348. * (non-Javadoc)
  349. *
  350. * @see org.aspectj.weaver.tools.ContextBasedMatcher#matchesDynamically(org.aspectj.weaver.tools.MatchingContext)
  351. */
  352. public boolean matchesDynamically(MatchingContext matchContext) {
  353. return this.beanNamePattern.equals(matchContext.getBinding("beanName"));
  354. }
  355. }
  356. }