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.

AnnotationPatternTestCase.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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.patterns;
  11. import org.aspectj.bridge.AbortException;
  12. import org.aspectj.util.LangUtil;
  13. import org.aspectj.weaver.AnnotatedElement;
  14. import org.aspectj.weaver.AnnotationAJ;
  15. import org.aspectj.weaver.ResolvedType;
  16. import org.aspectj.weaver.UnresolvedType;
  17. import org.aspectj.weaver.WeaverTestCase;
  18. import org.aspectj.weaver.bcel.BcelWorld;
  19. import junit.framework.TestCase;
  20. public class AnnotationPatternTestCase extends TestCase {
  21. public void testParseSimpleAnnotationPattern() {
  22. PatternParser p = new PatternParser("@Foo");
  23. AnnotationTypePattern foo = p.maybeParseAnnotationPattern();
  24. foo = foo.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  25. assertTrue("ExactAnnotationTypePattern", foo instanceof ExactAnnotationTypePattern);
  26. assertEquals("Foo", UnresolvedType.forSignature("LFoo;"), ((ExactAnnotationTypePattern) foo).annotationType);
  27. }
  28. public void testParseAndAnnotationPattern() {
  29. PatternParser p = new PatternParser("@Foo @Goo");
  30. AnnotationTypePattern fooAndGoo = p.maybeParseAnnotationPattern();
  31. assertTrue("AndAnnotationTypePattern", fooAndGoo instanceof AndAnnotationTypePattern);
  32. assertEquals("@(Foo) @(Goo)", fooAndGoo.toString());
  33. fooAndGoo = fooAndGoo.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  34. assertEquals("@Foo @Goo", fooAndGoo.toString());
  35. AnnotationTypePattern left = ((AndAnnotationTypePattern) fooAndGoo).getLeft();
  36. AnnotationTypePattern right = ((AndAnnotationTypePattern) fooAndGoo).getRight();
  37. assertEquals("Foo", UnresolvedType.forSignature("LFoo;"), ((ExactAnnotationTypePattern) left).annotationType);
  38. assertEquals("Goo", UnresolvedType.forSignature("LGoo;"), ((ExactAnnotationTypePattern) right).annotationType);
  39. }
  40. //
  41. // public void testParseOrAnnotationPattern() {
  42. // PatternParser p = new PatternParser("@Foo || @Goo");
  43. // AnnotationTypePattern fooOrGoo = p.parseAnnotationTypePattern();
  44. // assertTrue("OrAnnotationTypePattern",fooOrGoo instanceof
  45. // OrAnnotationTypePattern);
  46. // assertEquals("(@Foo || @Goo)",fooOrGoo.toString());
  47. // AnnotationTypePattern left =
  48. // ((OrAnnotationTypePattern)fooOrGoo).getLeft();
  49. // AnnotationTypePattern right =
  50. // ((OrAnnotationTypePattern)fooOrGoo).getRight();
  51. // assertEquals("Foo",UnresolvedType.forName("Foo"),((
  52. // ExactAnnotationTypePattern)left).annotationType);
  53. // assertEquals("Goo",UnresolvedType.forName("Goo"),((
  54. // ExactAnnotationTypePattern)right).annotationType);
  55. // }
  56. //
  57. public void testParseNotAnnotationPattern() {
  58. PatternParser p = new PatternParser("!@Foo");
  59. AnnotationTypePattern notFoo = p.maybeParseAnnotationPattern();
  60. assertTrue("NotAnnotationTypePattern", notFoo instanceof NotAnnotationTypePattern);
  61. notFoo = notFoo.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  62. assertEquals("!@Foo", notFoo.toString());
  63. AnnotationTypePattern body = ((NotAnnotationTypePattern) notFoo).getNegatedPattern();
  64. assertEquals("Foo", UnresolvedType.forName("Foo"), ((ExactAnnotationTypePattern) body).annotationType);
  65. }
  66. public void testParseBracketedAnnotationPattern() {
  67. PatternParser p = new PatternParser("(@Foo)");
  68. AnnotationTypePattern foo = p.maybeParseAnnotationPattern();
  69. // cannot start with ( so, we get ANY
  70. assertEquals("ANY", AnnotationTypePattern.ANY, foo);
  71. }
  72. public void testParseFQAnnPattern() {
  73. PatternParser p = new PatternParser("@org.aspectj.Foo");
  74. AnnotationTypePattern foo = p.maybeParseAnnotationPattern();
  75. assertEquals("@(org.aspectj.Foo)", foo.toString());
  76. }
  77. public void testParseComboPattern() {
  78. // PatternParser p = new PatternParser("!((@Foo || @Goo) && !@Boo)");
  79. PatternParser p = new PatternParser("@(Foo || Goo)!@Boo");
  80. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  81. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  82. AndAnnotationTypePattern atp = (AndAnnotationTypePattern) ap;
  83. NotAnnotationTypePattern notBoo = (NotAnnotationTypePattern) atp.getRight();
  84. // ExactAnnotationTypePattern boo = (ExactAnnotationTypePattern)
  85. notBoo.getNegatedPattern();
  86. // AnnotationTypePattern fooOrGoo = (AnnotationTypePattern)
  87. atp.getLeft();
  88. assertEquals("@((Foo || Goo)) !@Boo", ap.toString());
  89. }
  90. // public void testParseAndOrPattern() {
  91. // PatternParser p = new PatternParser("@Foo && @Boo || @Goo");
  92. // AnnotationTypePattern andOr = p.parseAnnotationTypePattern();
  93. // assertTrue("Should be or pattern",andOr instanceof
  94. // OrAnnotationTypePattern);
  95. // }
  96. //
  97. public void testParseBadPattern() {
  98. PatternParser p = new PatternParser("@@Foo");
  99. try {
  100. p.maybeParseAnnotationPattern();
  101. fail("ParserException expected");
  102. } catch (ParserException pEx) {
  103. assertEquals("name pattern", pEx.getMessage());
  104. }
  105. }
  106. public void testParseBadPattern2() {
  107. PatternParser p = new PatternParser("Foo");
  108. AnnotationTypePattern bad = p.maybeParseAnnotationPattern();
  109. assertEquals("ANY", AnnotationTypePattern.ANY, bad);
  110. }
  111. public void testParseNameOrVarAnnotationPattern() {
  112. PatternParser p = new PatternParser("Foo");
  113. AnnotationTypePattern foo = p.parseAnnotationNameOrVarTypePattern();
  114. assertTrue("ExactAnnotationTypePattern expected", foo != null);
  115. assertEquals("Foo", UnresolvedType.forName("Foo"), ((ExactAnnotationTypePattern) foo).annotationType);
  116. }
  117. public void testParseNameOrVarAnnotationPatternWithNot() {
  118. PatternParser p = new PatternParser("!@Foo");
  119. try {
  120. // AnnotationTypePattern bad =
  121. p.parseAnnotationNameOrVarTypePattern();
  122. fail("ParserException expected");
  123. } catch (ParserException pEx) {
  124. assertEquals("identifier", pEx.getMessage());
  125. }
  126. }
  127. public void testParseNameOrVarAnnotationPatternWithOr() {
  128. PatternParser p = new PatternParser("Foo || Boo");
  129. AnnotationTypePattern foo = p.parseAnnotationNameOrVarTypePattern();
  130. // rest of pattern not consumed...
  131. assertTrue("ExactAnnotationTypePattern", foo instanceof ExactAnnotationTypePattern);
  132. assertEquals("Foo", UnresolvedType.forName("Foo"), ((ExactAnnotationTypePattern) foo).annotationType);
  133. }
  134. public void testParseNameOrVarAnnotationWithBinding() {
  135. PatternParser p = new PatternParser("foo");
  136. AnnotationTypePattern foo = p.parseAnnotationNameOrVarTypePattern();
  137. assertTrue("ExactAnnotationTypePattern", foo instanceof ExactAnnotationTypePattern);
  138. assertEquals("@foo", ((ExactAnnotationTypePattern) foo).toString());
  139. }
  140. public void testParseNameOrVarAnnotationPatternWithAnd() {
  141. PatternParser p = new PatternParser("Foo Boo");
  142. AnnotationTypePattern foo = p.parseAnnotationNameOrVarTypePattern();
  143. // rest of pattern not consumed...
  144. assertEquals("@Foo", foo.toString());
  145. }
  146. public void testMaybeParseAnnotationPattern() {
  147. PatternParser p = new PatternParser("@Foo");
  148. AnnotationTypePattern a = p.maybeParseAnnotationPattern();
  149. assertNotNull("Should find annotation pattern", a);
  150. p = new PatternParser("Foo && Boo");
  151. a = p.maybeParseAnnotationPattern();
  152. assertEquals("Should be ANY pattern for a non-match", AnnotationTypePattern.ANY, a);
  153. }
  154. public void testParseTypePatternsWithAnnotations() {
  155. PatternParser p = new PatternParser("@Foo *");
  156. TypePattern t = p.parseTypePattern();
  157. assertTrue("AnyWithAnnotationTypePattern", t instanceof AnyWithAnnotationTypePattern);
  158. AnnotationTypePattern atp = t.annotationPattern;
  159. assertEquals("@(Foo)", atp.toString());
  160. assertEquals("(@(Foo) *)", t.toString());
  161. }
  162. public void testParseTypePatternsWithAnnotationsComplex() {
  163. PatternParser p = new PatternParser("(@(Foo || Boo) (Foo || Boo))");
  164. TypePattern t = p.parseTypePattern();
  165. assertTrue("OrTypePattern", t instanceof OrTypePattern);
  166. assertEquals("((@((Foo || Boo)) Foo) || (@((Foo || Boo)) Boo))", t.toString());
  167. }
  168. public void testNotSyntax() {
  169. PatternParser p = new PatternParser("!@Foo (Foo || Boo))");
  170. TypePattern t = p.parseTypePattern();
  171. assertTrue("OrTypePattern", t instanceof OrTypePattern);
  172. assertEquals("((!@(Foo) Foo) || (!@(Foo) Boo))", t.toString());
  173. }
  174. public void testParseMethodOrConstructorSigNoAP() {
  175. PatternParser p = new PatternParser("* *.*(..)");
  176. SignaturePattern s = p.parseMethodOrConstructorSignaturePattern();
  177. assertEquals("Any annotation", AnnotationTypePattern.ANY, s.getAnnotationPattern());
  178. assertEquals("Any return", "*", s.getReturnType().toString());
  179. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  180. assertEquals("Any name", "*", s.getName().toString());
  181. assertEquals("* *.*(..)", s.toString());
  182. }
  183. public void testParseMethodOrConstructorSigSimpleAP() {
  184. PatternParser p = new PatternParser("@Foo * *.*(..)");
  185. SignaturePattern s = p.parseMethodOrConstructorSignaturePattern();
  186. assertEquals("@(Foo) annotation", "@(Foo)", s.getAnnotationPattern().toString());
  187. assertEquals("Any return", "*", s.getReturnType().toString());
  188. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  189. assertEquals("Any name", "*", s.getName().toString());
  190. assertEquals("@(Foo) * *.*(..)", s.toString());
  191. }
  192. public void testParseMethodOrConstructorSigComplexAP() {
  193. PatternParser p = new PatternParser("!@(Foo || Goo) * *.*(..)");
  194. SignaturePattern s = p.parseMethodOrConstructorSignaturePattern();
  195. assertEquals("complex annotation", "!@((Foo || Goo))", s.getAnnotationPattern().toString());
  196. assertEquals("Any return", "*", s.getReturnType().toString());
  197. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  198. assertEquals("Any name", "*", s.getName().toString());
  199. assertEquals("!@((Foo || Goo)) * *.*(..)", s.toString());
  200. }
  201. public void testParseMethodFieldSigNoAP() {
  202. PatternParser p = new PatternParser("* *.*");
  203. SignaturePattern s = p.parseFieldSignaturePattern();
  204. assertEquals("Any annotation", AnnotationTypePattern.ANY, s.getAnnotationPattern());
  205. assertEquals("Any field type", "*", s.getReturnType().toString());
  206. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  207. assertEquals("Any name", "*", s.getName().toString());
  208. assertEquals("* *.*", s.toString());
  209. }
  210. public void testParseFieldSigSimpleAP() {
  211. PatternParser p = new PatternParser("@Foo * *.*");
  212. SignaturePattern s = p.parseFieldSignaturePattern();
  213. assertEquals("@Foo annotation", "@(Foo)", s.getAnnotationPattern().toString());
  214. assertEquals("Any field type", "*", s.getReturnType().toString());
  215. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  216. assertEquals("Any name", "*", s.getName().toString());
  217. assertEquals("@(Foo) * *.*", s.toString());
  218. }
  219. public void testParseFieldSigComplexAP() {
  220. PatternParser p = new PatternParser("!@(Foo || Goo) * *.*");
  221. SignaturePattern s = p.parseFieldSignaturePattern();
  222. assertEquals("complex annotation", "!@((Foo || Goo))", s.getAnnotationPattern().toString());
  223. assertEquals("Any field type", "*", s.getReturnType().toString());
  224. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  225. assertEquals("Any name", "*", s.getName().toString());
  226. assertEquals("!@((Foo || Goo)) * *.*", s.toString());
  227. }
  228. public void testExactAnnotationPatternMatching() {
  229. PatternParser p = new PatternParser("@Foo");
  230. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  231. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  232. AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] { "Foo" });
  233. assertTrue("matches element with Foo", ap.matches(ae).alwaysTrue());
  234. AnnotatedElementImpl ae2 = new AnnotatedElementImpl(new String[] { "Boo" });
  235. assertTrue("does not match element with Boo", ap.matches(ae2).alwaysFalse());
  236. }
  237. public void testBindingAnnotationPatternMatching() {
  238. PatternParser p = new PatternParser("foo");
  239. AnnotationTypePattern ap = p.parseAnnotationNameOrVarTypePattern();
  240. try {
  241. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  242. } catch (AbortException abEx) {
  243. assertEquals("Binding not supported in @pcds (1.5.0 M1 limitation): null", abEx.getMessage());
  244. }
  245. // uncomment these next lines once binding is supported
  246. // AnnotatedElementImpl ae = new AnnotatedElementImpl(new
  247. // String[]{"Foo"});
  248. // assertTrue("matches element with Foo",ap.matches(ae).alwaysTrue())
  249. // ;
  250. // AnnotatedElementImpl ae2 = new AnnotatedElementImpl(new
  251. // String[]{"Boo"});
  252. // assertTrue("does not match element with Boo",ap.matches(ae2).
  253. // alwaysFalse());
  254. }
  255. public void testAndAnnotationPatternMatching() {
  256. PatternParser p = new PatternParser("@Foo @Boo");
  257. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  258. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  259. AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] { "Foo", "Boo" });
  260. assertTrue("matches foo and boo", ap.matches(ae).alwaysTrue());
  261. ae = new AnnotatedElementImpl(new String[] { "Foo" });
  262. assertTrue("does not match foo", ap.matches(ae).alwaysFalse());
  263. ae = new AnnotatedElementImpl(new String[] { "Boo" });
  264. assertTrue("does not match boo", ap.matches(ae).alwaysFalse());
  265. ae = new AnnotatedElementImpl(new String[] { "Goo" });
  266. assertTrue("does not match goo", ap.matches(ae).alwaysFalse());
  267. }
  268. //
  269. // public void testOrAnnotationPatternMatching() {
  270. // PatternParser p = new PatternParser("@Foo || @Boo");
  271. // AnnotationTypePattern ap = p.parseAnnotationTypePattern();
  272. // ap = ap.resolveBindings(makeSimpleScope(),new Bindings(3),true);
  273. // AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[]
  274. // {"Foo","Boo"});
  275. // assertTrue("matches foo and boo",ap.matches(ae).alwaysTrue());
  276. // ae = new AnnotatedElementImpl(new String[] {"Foo"});
  277. // assertTrue("matches foo",ap.matches(ae).alwaysTrue());
  278. // ae = new AnnotatedElementImpl(new String[] {"Boo"});
  279. // assertTrue("matches boo",ap.matches(ae).alwaysTrue());
  280. // ae = new AnnotatedElementImpl(new String[] {"Goo"});
  281. // assertTrue("does not match goo",ap.matches(ae).alwaysFalse());
  282. // }
  283. //
  284. public void testNotAnnotationPatternMatching() {
  285. PatternParser p = new PatternParser("!@Foo");
  286. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  287. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  288. AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] { "Foo", "Boo" });
  289. assertTrue("does not match foo and boo", ap.matches(ae).alwaysFalse());
  290. ae = new AnnotatedElementImpl(new String[] { "Boo" });
  291. assertTrue("matches boo", ap.matches(ae).alwaysTrue());
  292. }
  293. public void testAnyAnnotationPatternMatching() {
  294. AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] { "Foo", "Boo" });
  295. assertTrue("always matches", AnnotationTypePattern.ANY.matches(ae).alwaysTrue());
  296. ae = new AnnotatedElementImpl(new String[] {});
  297. assertTrue("always matches", AnnotationTypePattern.ANY.matches(ae).alwaysTrue());
  298. }
  299. public TestScope makeSimpleScope() {
  300. BcelWorld bWorld = new BcelWorld(WeaverTestCase.TESTDATA_PATH + "/testcode.jar"); // testcode contains Foo/Boo/Goo/etc
  301. bWorld.setBehaveInJava5Way(true);
  302. return new TestScope(new String[] { "int", "java.lang.String", "Foo", "Boo", "Goo" }, new String[] { "a", "b", "foo",
  303. "boo", "goo" }, bWorld);
  304. }
  305. // put test cases for AnnotationPatternList matching in separate test
  306. // class...
  307. static class AnnotatedElementImpl implements AnnotatedElement {
  308. private String[] annotationTypes;
  309. public AnnotatedElementImpl(String[] annotationTypes) {
  310. this.annotationTypes = annotationTypes;
  311. }
  312. public boolean hasAnnotation(UnresolvedType ofType) {
  313. for (String annotationType : annotationTypes) {
  314. if (annotationType.equals(ofType.getName())) {
  315. return true;
  316. }
  317. }
  318. return false;
  319. }
  320. /*
  321. * (non-Javadoc)
  322. *
  323. * @see org.aspectj.weaver.AnnotatedElement#getAnnotationTypes()
  324. */
  325. public ResolvedType[] getAnnotationTypes() {
  326. // TODO Auto-generated method stub
  327. return null;
  328. }
  329. public AnnotationAJ getAnnotationOfType(UnresolvedType ofType) {
  330. // TODO Auto-generated method stub
  331. return null;
  332. }
  333. }
  334. }