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.

PointcutParserTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * ******************************************************************/
  10. package org.aspectj.weaver.tools;
  11. import java.util.HashSet;
  12. import java.util.Properties;
  13. import java.util.Set;
  14. import org.aspectj.bridge.AbortException;
  15. import org.aspectj.bridge.IMessage;
  16. import org.aspectj.bridge.IMessage.Kind;
  17. import org.aspectj.bridge.IMessageHandler;
  18. import org.aspectj.util.LangUtil;
  19. import org.aspectj.weaver.patterns.PatternParser;
  20. import org.aspectj.weaver.patterns.Pointcut;
  21. import org.aspectj.weaver.patterns.PointcutRewriter;
  22. import junit.framework.Assert;
  23. import junit.framework.TestCase;
  24. /**
  25. * Test cases for the PointcutParser class
  26. */
  27. public class PointcutParserTest extends TestCase {
  28. private boolean needToSkip = false;
  29. /** this condition can occur on the build machine only, and is way too complex to fix right now... */
  30. private boolean needToSkipPointcutParserTests() {
  31. try {
  32. Class.forName("org.aspectj.weaver.reflect.Java15ReflectionBasedReferenceTypeDelegate", false, this.getClass()
  33. .getClassLoader());// ReflectionBasedReferenceTypeDelegate.class.getClassLoader());
  34. } catch (ClassNotFoundException cnfEx) {
  35. return true;
  36. }
  37. return false;
  38. }
  39. protected void setUp() throws Exception {
  40. super.setUp();
  41. needToSkip = needToSkipPointcutParserTests();
  42. }
  43. public void testGetAllSupportedPointcutPrimitives() {
  44. if (needToSkip) {
  45. return;
  46. }
  47. Set<PointcutPrimitive> s = PointcutParser.getAllSupportedPointcutPrimitives();
  48. assertEquals("Should be 21 elements in the set", 21, s.size());
  49. assertFalse("Should not contain if pcd", s.contains(PointcutPrimitive.IF));
  50. assertFalse("Should not contain cflow pcd", s.contains(PointcutPrimitive.CFLOW));
  51. assertFalse("Should not contain cflowbelow pcd", s.contains(PointcutPrimitive.CFLOW_BELOW));
  52. }
  53. public void testEmptyConstructor() {
  54. if (needToSkip) {
  55. return;
  56. }
  57. PointcutParser parser = PointcutParser
  58. .getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this.getClass().getClassLoader());
  59. Set<PointcutPrimitive> s = parser.getSupportedPrimitives();
  60. assertEquals("Should be 21 elements in the set", 21, s.size());
  61. assertFalse("Should not contain if pcd", s.contains(PointcutPrimitive.IF));
  62. assertFalse("Should not contain cflow pcd", s.contains(PointcutPrimitive.CFLOW));
  63. assertFalse("Should not contain cflowbelow pcd", s.contains(PointcutPrimitive.CFLOW_BELOW));
  64. }
  65. public void testSetConstructor() {
  66. if (needToSkip) {
  67. return;
  68. }
  69. Set<PointcutPrimitive> p = PointcutParser.getAllSupportedPointcutPrimitives();
  70. PointcutParser parser = PointcutParser
  71. .getPointcutParserSupportingSpecifiedPrimitivesAndUsingSpecifiedClassLoaderForResolution(p, this.getClass()
  72. .getClassLoader());
  73. assertEquals("Should use the set we pass in", p, parser.getSupportedPrimitives());
  74. Set<PointcutPrimitive> q = new HashSet<>();
  75. q.add(PointcutPrimitive.ARGS);
  76. parser = PointcutParser.getPointcutParserSupportingSpecifiedPrimitivesAndUsingSpecifiedClassLoaderForResolution(q, this
  77. .getClass().getClassLoader());
  78. assertEquals("Should have only one element in set", 1, parser.getSupportedPrimitives().size());
  79. assertEquals("Should only have ARGS pcd", PointcutPrimitive.ARGS, parser.getSupportedPrimitives().iterator().next());
  80. }
  81. public void testParsePointcutExpression() {
  82. if (needToSkip) {
  83. return;
  84. }
  85. PointcutParser p = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this
  86. .getClass().getClassLoader());
  87. IMessageHandler current = p.setCustomMessageHandler(new IgnoreWarningsMessageHandler());
  88. try {
  89. p.parsePointcutExpression("(adviceexecution() || execution(* *.*(..)) || handler(Exception) || "
  90. + "call(Foo Bar+.*(Goo)) || get(* foo) || set(Foo+ (Goo||Moo).s*) || "
  91. + "initialization(Foo.new(..)) || preinitialization(*.new(Foo,..)) || "
  92. + "staticinitialization(org.xzy.abc..*)) && (this(Foo) || target(Boo) ||" + "args(A,B,C)) && !handler(X)");
  93. } finally {
  94. p.setCustomMessageHandler(current);
  95. }
  96. try {
  97. p.parsePointcutExpression("gobble-de-gook()");
  98. fail("Expected IllegalArgumentException");
  99. } catch (IllegalArgumentException ex) {
  100. }
  101. }
  102. public void testParseExceptionErrorMessages() {
  103. if (needToSkip) {
  104. return;
  105. }
  106. PointcutParser p = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this
  107. .getClass().getClassLoader());
  108. try {
  109. p.parsePointcutExpression("execution(int Foo.*(..) && args(Double)");
  110. fail("Expected IllegalArgumentException");
  111. } catch (IllegalArgumentException ex) {
  112. assertTrue("Pointcut is not well-formed message",
  113. ex.getMessage().startsWith("Pointcut is not well-formed: expecting ')' at character position 24"));
  114. }
  115. }
  116. public void testOperatorPrecedence_319190() throws Exception {
  117. if (needToSkip) {
  118. return;
  119. }
  120. String s = null;
  121. Pointcut p = null;
  122. s = "(execution(* A.credit(float)) || execution(* A.debit(float))) && this(acc) && args(am) || execution(* C.*(Account, float)) && args(acc, am)";
  123. p = new PatternParser(s).parsePointcut();
  124. Assert.assertEquals(
  125. "(((execution(* A.credit(float)) || execution(* A.debit(float))) && (this(acc) && args(am))) || (execution(* C.*(Account, float)) && args(acc, am)))",
  126. p.toString());
  127. s = "(if(true) || if(false)) && this(acc) && args(am) || if(true) && args(acc, am)";
  128. p = new PatternParser(s).parsePointcut();
  129. // bugged was: ((if(true) || if(false)) && (this(acc) && (args(am) || (if(true) && args(acc, am)))))
  130. Assert.assertEquals("(((if(true) || if(false)) && (this(acc) && args(am))) || (if(true) && args(acc, am)))", p.toString());
  131. p = new PointcutRewriter().rewrite(p);
  132. Assert.assertEquals("(((this(acc) && args(am)) && if(true)) || (args(acc, am) && if(true)))", p.toString());
  133. s = "if(true) && if(false) || if(true)";
  134. p = new PatternParser(s).parsePointcut();
  135. assertEquals("((if(true) && if(false)) || if(true))", p.toString());
  136. p = new PointcutRewriter().rewrite(p);
  137. Assert.assertEquals("if(true)", p.toString());
  138. }
  139. public void testParseIfPCD() {
  140. if (needToSkip) {
  141. return;
  142. }
  143. PointcutParser p = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this
  144. .getClass().getClassLoader());
  145. try {
  146. p.parsePointcutExpression("if(true)");
  147. fail("Expected UnsupportedPointcutPrimitiveException");
  148. } catch (UnsupportedPointcutPrimitiveException ex) {
  149. assertEquals("Should not support IF", PointcutPrimitive.IF, ex.getUnsupportedPrimitive());
  150. }
  151. }
  152. public void testParseCflowPCDs() {
  153. if (needToSkip) {
  154. return;
  155. }
  156. PointcutParser p = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this
  157. .getClass().getClassLoader());
  158. try {
  159. p.parsePointcutExpression("cflow(this(t))");
  160. fail("Expected UnsupportedPointcutPrimitiveException");
  161. } catch (UnsupportedPointcutPrimitiveException ex) {
  162. assertEquals("Should not support CFLOW", PointcutPrimitive.CFLOW, ex.getUnsupportedPrimitive());
  163. }
  164. try {
  165. p.parsePointcutExpression("cflowbelow(this(t))");
  166. fail("Expected UnsupportedPointcutPrimitiveException");
  167. } catch (UnsupportedPointcutPrimitiveException ex) {
  168. assertEquals("Should not support CFLOW_BELOW", PointcutPrimitive.CFLOW_BELOW, ex.getUnsupportedPrimitive());
  169. }
  170. }
  171. public void testParseReferencePCDs() {
  172. if (needToSkip) {
  173. return;
  174. }
  175. Set pcKinds = PointcutParser.getAllSupportedPointcutPrimitives();
  176. pcKinds.remove(PointcutPrimitive.REFERENCE);
  177. PointcutParser p = PointcutParser.getPointcutParserSupportingSpecifiedPrimitivesAndUsingSpecifiedClassLoaderForResolution(
  178. pcKinds, this.getClass().getClassLoader());
  179. try {
  180. p.parsePointcutExpression("bananas(String)");
  181. fail("Expected UnsupportedPointcutPrimitiveException");
  182. } catch (UnsupportedPointcutPrimitiveException ex) {
  183. assertTrue(ex.getUnsupportedPrimitive() == PointcutPrimitive.REFERENCE);
  184. }
  185. }
  186. public void testParseUnsupportedPCDs() {
  187. if (needToSkip) {
  188. return;
  189. }
  190. Set s = new HashSet();
  191. PointcutParser p = PointcutParser.getPointcutParserSupportingSpecifiedPrimitivesAndUsingSpecifiedClassLoaderForResolution(
  192. s, this.getClass().getClassLoader());
  193. try {
  194. p.parsePointcutExpression("args(x)");
  195. fail("Expected UnsupportedPointcutPrimitiveException");
  196. } catch (UnsupportedPointcutPrimitiveException ex) {
  197. assertEquals("Args", PointcutPrimitive.ARGS, ex.getUnsupportedPrimitive());
  198. }
  199. try {
  200. p.parsePointcutExpression("within(x)");
  201. fail("Expected UnsupportedPointcutPrimitiveException");
  202. } catch (UnsupportedPointcutPrimitiveException ex) {
  203. assertEquals("Within", PointcutPrimitive.WITHIN, ex.getUnsupportedPrimitive());
  204. }
  205. try {
  206. p.parsePointcutExpression("withincode(new(..))");
  207. fail("Expected UnsupportedPointcutPrimitiveException");
  208. } catch (UnsupportedPointcutPrimitiveException ex) {
  209. assertEquals("Withincode", PointcutPrimitive.WITHIN_CODE, ex.getUnsupportedPrimitive());
  210. }
  211. try {
  212. p.parsePointcutExpression("handler(Exception)");
  213. fail("Expected UnsupportedPointcutPrimitiveException");
  214. } catch (UnsupportedPointcutPrimitiveException ex) {
  215. assertEquals("handler", PointcutPrimitive.HANDLER, ex.getUnsupportedPrimitive());
  216. }
  217. try {
  218. p.parsePointcutExpression("this(X)");
  219. fail("Expected UnsupportedPointcutPrimitiveException");
  220. } catch (UnsupportedPointcutPrimitiveException ex) {
  221. assertEquals("this", PointcutPrimitive.THIS, ex.getUnsupportedPrimitive());
  222. }
  223. try {
  224. p.parsePointcutExpression("target(X)");
  225. fail("Expected UnsupportedPointcutPrimitiveException");
  226. } catch (UnsupportedPointcutPrimitiveException ex) {
  227. assertEquals("target", PointcutPrimitive.TARGET, ex.getUnsupportedPrimitive());
  228. }
  229. try {
  230. p.parsePointcutExpression("this(X) && target(Y)");
  231. fail("Expected UnsupportedPointcutPrimitiveException");
  232. } catch (UnsupportedPointcutPrimitiveException ex) {
  233. assertEquals("This", PointcutPrimitive.THIS, ex.getUnsupportedPrimitive());
  234. }
  235. try {
  236. p.parsePointcutExpression("this(X) || target(Y)");
  237. fail("Expected UnsupportedPointcutPrimitiveException");
  238. } catch (UnsupportedPointcutPrimitiveException ex) {
  239. assertEquals("This", PointcutPrimitive.THIS, ex.getUnsupportedPrimitive());
  240. }
  241. try {
  242. p.parsePointcutExpression("!this(X)");
  243. fail("Expected UnsupportedPointcutPrimitiveException");
  244. } catch (UnsupportedPointcutPrimitiveException ex) {
  245. assertEquals("This", PointcutPrimitive.THIS, ex.getUnsupportedPrimitive());
  246. }
  247. try {
  248. p.parsePointcutExpression("call(* *.*(..))");
  249. fail("Expected UnsupportedPointcutPrimitiveException");
  250. } catch (UnsupportedPointcutPrimitiveException ex) {
  251. assertEquals("Call", PointcutPrimitive.CALL, ex.getUnsupportedPrimitive());
  252. }
  253. try {
  254. p.parsePointcutExpression("execution(* *.*(..))");
  255. fail("Expected UnsupportedPointcutPrimitiveException");
  256. } catch (UnsupportedPointcutPrimitiveException ex) {
  257. assertEquals("Execution", PointcutPrimitive.EXECUTION, ex.getUnsupportedPrimitive());
  258. }
  259. try {
  260. p.parsePointcutExpression("get(* *)");
  261. fail("Expected UnsupportedPointcutPrimitiveException");
  262. } catch (UnsupportedPointcutPrimitiveException ex) {
  263. assertEquals("Get", PointcutPrimitive.GET, ex.getUnsupportedPrimitive());
  264. }
  265. try {
  266. p.parsePointcutExpression("set(* *)");
  267. fail("Expected UnsupportedPointcutPrimitiveException");
  268. } catch (UnsupportedPointcutPrimitiveException ex) {
  269. assertEquals("Set", PointcutPrimitive.SET, ex.getUnsupportedPrimitive());
  270. }
  271. try {
  272. p.parsePointcutExpression("initialization(new(..))");
  273. fail("Expected UnsupportedPointcutPrimitiveException");
  274. } catch (UnsupportedPointcutPrimitiveException ex) {
  275. assertEquals("Initialization", PointcutPrimitive.INITIALIZATION, ex.getUnsupportedPrimitive());
  276. }
  277. try {
  278. p.parsePointcutExpression("preinitialization(new(..))");
  279. fail("Expected UnsupportedPointcutPrimitiveException");
  280. } catch (UnsupportedPointcutPrimitiveException ex) {
  281. assertEquals("Prc-init", PointcutPrimitive.PRE_INITIALIZATION, ex.getUnsupportedPrimitive());
  282. }
  283. try {
  284. p.parsePointcutExpression("staticinitialization(T)");
  285. fail("Expected UnsupportedPointcutPrimitiveException");
  286. } catch (UnsupportedPointcutPrimitiveException ex) {
  287. assertEquals("Staticinit", PointcutPrimitive.STATIC_INITIALIZATION, ex.getUnsupportedPrimitive());
  288. }
  289. }
  290. public void testFormals() {
  291. if (needToSkip) {
  292. return;
  293. }
  294. PointcutParser parser = PointcutParser
  295. .getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this.getClass().getClassLoader());
  296. PointcutParameter param = parser.createPointcutParameter("x", String.class);
  297. PointcutExpression pc = parser.parsePointcutExpression("args(x)", null, new PointcutParameter[] { param });
  298. assertEquals("args(x)", pc.getPointcutExpression());
  299. try {
  300. pc = parser.parsePointcutExpression("args(String)", null, new PointcutParameter[] { param });
  301. fail("Expecting IllegalArgumentException");
  302. } catch (IllegalArgumentException ex) {
  303. assertTrue("formal unbound", ex.getMessage().contains("formal unbound"));
  304. }
  305. try {
  306. pc = parser.parsePointcutExpression("args(y)");
  307. fail("Expecting IllegalArgumentException");
  308. } catch (IllegalArgumentException ex) {
  309. assertTrue("no match for type name", ex.getMessage().contains("warning no match for this type name: y"));
  310. }
  311. }
  312. public void testXLintConfiguration() {
  313. if (needToSkip) {
  314. return;
  315. }
  316. PointcutParser p = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this
  317. .getClass().getClassLoader());
  318. try {
  319. p.parsePointcutExpression("this(FooBar)");
  320. } catch (IllegalArgumentException ex) {
  321. assertTrue("should have xlint:invalidAbsoluteTypeName", ex.getMessage().contains("Xlint:invalidAbsoluteTypeName"));
  322. }
  323. Properties props = new Properties();
  324. props.put("invalidAbsoluteTypeName", "ignore");
  325. p.setLintProperties(props);
  326. p.parsePointcutExpression("this(FooBar)");
  327. }
  328. private static class IgnoreWarningsMessageHandler implements IMessageHandler {
  329. public boolean handleMessage(IMessage message) throws AbortException {
  330. if (message.getKind() != IMessage.WARNING) {
  331. throw new RuntimeException("unexpected message: " + message.toString());
  332. }
  333. return true;
  334. }
  335. public boolean isIgnoring(Kind kind) {
  336. if (kind != IMessage.ERROR) {
  337. return true;
  338. }
  339. return false;
  340. }
  341. public void dontIgnore(Kind kind) {
  342. }
  343. public void ignore(Kind kind) {
  344. }
  345. }
  346. }