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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.util.*;
  14. import org.aspectj.weaver.bcel.*;
  15. import org.aspectj.weaver.bcel.BcelShadow;
  16. import junit.framework.TestCase;
  17. import org.aspectj.bridge.AbortException;
  18. import org.aspectj.weaver.*;
  19. public class BindingTestCase extends TestCase {
  20. public BindingTestCase(String arg0) {
  21. super(arg0);
  22. }
  23. World world = new BcelWorld();
  24. public void testResolveBindings() {
  25. BindingTypePattern at = new BindingTypePattern(world.resolve("java.lang.Object"), 0);
  26. BindingTypePattern bt = new BindingTypePattern(world.resolve("java.lang.Object"), 1);
  27. BindingTypePattern[] all = new BindingTypePattern[] {at, bt};
  28. BindingTypePattern[] none = new BindingTypePattern[] {null, null};
  29. BindingTypePattern[] a = new BindingTypePattern[] {at, null};
  30. BindingTypePattern[] b = new BindingTypePattern[] {null, bt};
  31. checkBindings("this(b)",b);
  32. checkBindings("this(Foo)", none);
  33. checkBindings("this(*)", none);
  34. checkBindings("this(a)", a);
  35. try {checkBindings("args(.., a,..,b)", all);
  36. //checkBindings("args(a,..,b, ..)", all);
  37. fail("shouldn't be implemented yet");
  38. } catch (AbortException ae) {
  39. // not implemented yet
  40. }
  41. checkBindings("args(a,..,b)", all);
  42. checkBindings("args(b)", b);
  43. checkBindings("args()", none);
  44. checkBindings("this(a) && this(b)", all);
  45. checkBindingFailure("this(a) && this(a)", "multiple");
  46. //checkBindingFailure("this(a) && this(b)");
  47. checkBindingFailure("this(a) || this(b)", "inconsistent");
  48. checkBindingFailure("this(A) || this(b)", "inconsistent");
  49. checkBindingFailure("this(a) || this(B)", "inconsistent");
  50. checkBindings("this(a) || this(a)", a);
  51. checkBindings("!this(Foo)", none);
  52. checkBindings("!this(Foo) && this(a)", a);
  53. checkBindingFailure("!this(a)", "negation");
  54. //checkBindingFailure("this(a)");
  55. checkBindings("cflow(this(a))", a);
  56. checkBindings("cflow(this(a)) && this(b)", all);
  57. checkBindingFailure("cflow(this(a)) || this(b)", "inconsistent");
  58. checkBindingFailure("cflow(this(a)) && this(a)", "multiple");
  59. checkBindingFailure("!cflow(this(a))", "negation");
  60. // todo
  61. // this should fail since a isn't visible to if
  62. //checkBindingFailure("cflow(if(a != null)) && this(a)");
  63. //checkBinding("cflow(if(a != null) && this(a))", a);
  64. }
  65. /**
  66. * Method checkBindingFailure. (assumes an env where "a" and "b" are formals).
  67. * @param string
  68. */
  69. private void checkBindingFailure(String pattern, String prefix) {
  70. PatternParser parser = new PatternParser(pattern);
  71. Pointcut p = parser.parsePointcut();
  72. Bindings actualBindings = new Bindings(2);
  73. try {
  74. p.resolveBindings(makeSimpleScope(), actualBindings);
  75. } catch (AbortException re) {
  76. assertEquals(prefix, re.getIMessage().getMessage().substring(0, prefix.length()));
  77. //System.out.println("expected exception: " + re);
  78. return;
  79. }
  80. assertTrue("should have failed", false);
  81. }
  82. /**
  83. * Method checkBindings.
  84. * @param string
  85. * @param i
  86. */
  87. private void checkBindings(String pattern, BindingTypePattern[] expectedBindings) {
  88. PatternParser parser = new PatternParser(pattern);
  89. Pointcut p = parser.parsePointcut();
  90. Bindings actualBindings = new Bindings(expectedBindings.length);
  91. TestScope simpleScope = makeSimpleScope();
  92. p.resolveBindings(simpleScope, actualBindings);
  93. //System.out.println(actualBindings);
  94. new Bindings(expectedBindings).checkEquals(actualBindings, simpleScope);
  95. }
  96. public TestScope makeSimpleScope() {
  97. return new TestScope(new String[] {"int", "java.lang.String"}, new String[] {"a", "b"}, world);
  98. }
  99. }