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.

BindingTestCase.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  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. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import org.aspectj.bridge.AbortException;
  14. import org.aspectj.weaver.World;
  15. import org.aspectj.weaver.patterns.BindingTypePattern;
  16. import org.aspectj.weaver.patterns.Bindings;
  17. import org.aspectj.weaver.patterns.PatternParser;
  18. import org.aspectj.weaver.patterns.Pointcut;
  19. import org.aspectj.weaver.patterns.TestScope;
  20. import org.aspectj.weaver.reflect.ReflectionWorld;
  21. public class BindingTestCase extends PatternsTestCase {
  22. public World getWorld() {
  23. return new ReflectionWorld(true, this.getClass().getClassLoader());
  24. }
  25. public void testResolveBindings() {
  26. BindingTypePattern at = new BindingTypePattern(world.resolve("java.lang.Object"), 0, false);
  27. BindingTypePattern bt = new BindingTypePattern(world.resolve("java.lang.Object"), 1, false);
  28. BindingTypePattern[] all = new BindingTypePattern[] { at, bt };
  29. BindingTypePattern[] none = new BindingTypePattern[] { null, null };
  30. BindingTypePattern[] a = new BindingTypePattern[] { at, null };
  31. BindingTypePattern[] b = new BindingTypePattern[] { null, bt };
  32. checkBindings("this(b)", b);
  33. checkBindings("this(java.lang.String)", none);
  34. checkBindings("this(*)", none);
  35. checkBindings("this(a)", a);
  36. try {
  37. checkBindings("args(.., a,..,b)", all);
  38. // checkBindings("args(a,..,b, ..)", all);
  39. fail("shouldn't be implemented yet");
  40. } catch (Throwable ae) {
  41. // // diff world implementations may exit by different means
  42. // } catch (Exception e) {
  43. // // diff world implementations may exit by different means
  44. }
  45. checkBindings("args(a,..,b)", all);
  46. checkBindings("args(b)", b);
  47. checkBindings("args()", none);
  48. checkBindings("this(a) && this(b)", all);
  49. checkBindingFailure("this(a) && this(a)", "multiple");
  50. // checkBindingFailure("this(a) && this(b)");
  51. checkBindingFailure("this(a) || this(b)", "inconsistent");
  52. checkBindingFailure("this(java.lang.String) || this(b)", "inconsistent");
  53. checkBindingFailure("this(a) || this(java.lang.String)", "inconsistent");
  54. checkBindings("this(a) || this(a)", a);
  55. checkBindings("!this(java.lang.String)", none);
  56. checkBindings("!this(java.lang.String) && this(a)", a);
  57. checkBindingFailure("!this(a)", "negation");
  58. // checkBindingFailure("this(a)");
  59. checkBindings("cflow(this(a))", a);
  60. checkBindings("cflow(this(a)) && this(b)", all);
  61. checkBindingFailure("cflow(this(a)) || this(b)", "inconsistent");
  62. checkBindingFailure("cflow(this(a)) && this(a)", "multiple");
  63. checkBindingFailure("!cflow(this(a))", "negation");
  64. // todo
  65. // this should fail since a isn't visible to if
  66. // checkBindingFailure("cflow(if(a != null)) && this(a)");
  67. // checkBinding("cflow(if(a != null) && this(a))", a);
  68. }
  69. /**
  70. * Method checkBindingFailure. (assumes an env where "a" and "b" are formals).
  71. *
  72. * @param string
  73. */
  74. private void checkBindingFailure(String pattern, String prefix) {
  75. PatternParser parser = new PatternParser(pattern);
  76. Pointcut p = parser.parsePointcut();
  77. Bindings actualBindings = new Bindings(2);
  78. try {
  79. p.resolveBindings(makeSimpleScope(), actualBindings);
  80. } catch (AbortException re) {
  81. assertEquals(prefix, re.getIMessage().getMessage().substring(0, prefix.length()));
  82. // System.out.println("expected exception: " + re);
  83. return;
  84. } catch (Throwable t) {
  85. assertTrue(prefix, t.getMessage().contains(prefix));
  86. return;
  87. }
  88. assertTrue("should have failed", false);
  89. }
  90. /**
  91. * Method checkBindings.
  92. *
  93. * @param string
  94. * @param i
  95. */
  96. private void checkBindings(String pattern, BindingTypePattern[] expectedBindings) {
  97. PatternParser parser = new PatternParser(pattern);
  98. Pointcut p = parser.parsePointcut();
  99. Bindings actualBindings = new Bindings(expectedBindings.length);
  100. TestScope simpleScope = makeSimpleScope();
  101. p.resolveBindings(simpleScope, actualBindings);
  102. // System.out.println(actualBindings);
  103. new Bindings(expectedBindings).checkEquals(actualBindings, simpleScope);
  104. }
  105. public TestScope makeSimpleScope() {
  106. return new TestScope(new String[] { "int", "java.lang.String" }, new String[] { "a", "b" }, world);
  107. }
  108. }