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

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