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.

ArgsTestCase.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.weaver.patterns;
  12. import java.lang.reflect.Method;
  13. import junit.framework.TestCase;
  14. import org.aspectj.util.LangUtil;
  15. import org.aspectj.weaver.tools.JoinPointMatch;
  16. import org.aspectj.weaver.tools.PointcutExpression;
  17. import org.aspectj.weaver.tools.PointcutParameter;
  18. import org.aspectj.weaver.tools.PointcutParser;
  19. import org.aspectj.weaver.tools.ShadowMatch;
  20. /**
  21. * @author colyer
  22. *
  23. */
  24. public class ArgsTestCase extends TestCase {
  25. PointcutExpression wildcardArgs;
  26. PointcutExpression oneA;
  27. PointcutExpression oneAandaC;
  28. PointcutExpression BthenAnything;
  29. PointcutExpression singleArg;
  30. public void testMatchJP() throws Exception {
  31. if (needToSkip)
  32. return;
  33. Method oneAArg = B.class.getMethod("x", new Class[] { A.class });
  34. Method oneBArg = B.class.getMethod("y", new Class[] { B.class });
  35. Method acArgs = C.class.getMethod("z", new Class[] { A.class, C.class });
  36. Method baArgs = C.class.getMethod("t", new Class[] { B.class, A.class });
  37. checkMatches(wildcardArgs.matchesMethodExecution(oneAArg), new B(), new B(), new Object[] { new A() });
  38. checkMatches(wildcardArgs.matchesMethodExecution(oneBArg), new B(), new B(), new Object[] { new B() });
  39. checkMatches(wildcardArgs.matchesMethodExecution(acArgs), new C(), new C(), new Object[] { new B(), new C() });
  40. checkMatches(wildcardArgs.matchesMethodExecution(baArgs), new C(), new C(), new Object[] { new B(), new B() });
  41. checkMatches(oneA.matchesMethodExecution(oneAArg), new B(), new B(), new Object[] { new A() });
  42. checkMatches(oneA.matchesMethodExecution(oneBArg), new B(), new B(), new Object[] { new B() });
  43. checkNoMatch(oneA.matchesMethodExecution(acArgs), new C(), new C(), new Object[] { new B(), new C() });
  44. checkNoMatch(oneA.matchesMethodExecution(baArgs), new C(), new C(), new Object[] { new B(), new B() });
  45. checkNoMatch(oneAandaC.matchesMethodExecution(oneAArg), new B(), new B(), new Object[] { new A() });
  46. checkNoMatch(oneAandaC.matchesMethodExecution(oneBArg), new B(), new B(), new Object[] { new B() });
  47. checkMatches(oneAandaC.matchesMethodExecution(acArgs), new C(), new C(), new Object[] { new B(), new C() });
  48. checkNoMatch(oneAandaC.matchesMethodExecution(baArgs), new C(), new C(), new Object[] { new B(), new B() });
  49. checkNoMatch(BthenAnything.matchesMethodExecution(oneAArg), new B(), new B(), new Object[] { new A() });
  50. checkMatches(BthenAnything.matchesMethodExecution(oneBArg), new B(), new B(), new Object[] { new B() });
  51. checkNoMatch(BthenAnything.matchesMethodExecution(acArgs), new C(), new C(), new Object[] { new A(), new C() });
  52. checkMatches(BthenAnything.matchesMethodExecution(baArgs), new C(), new C(), new Object[] { new B(), new B() });
  53. checkMatches(singleArg.matchesMethodExecution(oneAArg), new B(), new B(), new Object[] { new A() });
  54. checkMatches(singleArg.matchesMethodExecution(oneBArg), new B(), new B(), new Object[] { new B() });
  55. checkNoMatch(singleArg.matchesMethodExecution(acArgs), new C(), new C(), new Object[] { new B(), new C() });
  56. checkNoMatch(singleArg.matchesMethodExecution(baArgs), new C(), new C(), new Object[] { new B(), new B() });
  57. }
  58. public void testBinding() throws Exception {
  59. if (needToSkip)
  60. return;
  61. PointcutParser parser = PointcutParser
  62. .getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(A.class.getClassLoader());
  63. PointcutParameter a = parser.createPointcutParameter("a", A.class);
  64. A theParameter = new A();
  65. PointcutExpression bindA = parser.parsePointcutExpression("args(a,*)", A.class, new PointcutParameter[] { a });
  66. Method acArgs = C.class.getMethod("z", new Class[] { A.class, C.class });
  67. ShadowMatch sMatch = bindA.matchesMethodExecution(acArgs);
  68. JoinPointMatch jpMatch = sMatch.matchesJoinPoint(new A(), new A(), new Object[] { theParameter });
  69. assertTrue("should match", jpMatch.matches());
  70. PointcutParameter[] bindings = jpMatch.getParameterBindings();
  71. assertTrue("one parameter", bindings.length == 1);
  72. assertEquals("should be bound to the arg value", theParameter, bindings[0].getBinding());
  73. PointcutParameter c = parser.createPointcutParameter("c", C.class);
  74. C cParameter = new C();
  75. PointcutExpression bindAandC = parser.parsePointcutExpression("args(a,c)", A.class, new PointcutParameter[] { a, c });
  76. sMatch = bindAandC.matchesMethodExecution(acArgs);
  77. jpMatch = sMatch.matchesJoinPoint(new A(), new A(), new Object[] { theParameter, cParameter });
  78. assertTrue("should match", jpMatch.matches());
  79. bindings = jpMatch.getParameterBindings();
  80. assertTrue("two parameters", bindings.length == 2);
  81. assertEquals("should be bound to the a arg value", theParameter, bindings[0].getBinding());
  82. assertEquals("should be bound to the c arg value", cParameter, bindings[1].getBinding());
  83. assertEquals("a", bindings[0].getName());
  84. assertEquals("c", bindings[1].getName());
  85. }
  86. public void testMatchJPWithPrimitiveTypes() throws Exception {
  87. if (needToSkip)
  88. return;
  89. try {
  90. PointcutParser parser = PointcutParser
  91. .getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(A.class.getClassLoader());
  92. PointcutExpression oneInt = parser.parsePointcutExpression("args(int)");
  93. PointcutExpression oneInteger = parser.parsePointcutExpression("args(Integer)");
  94. Method oneIntM = A.class.getMethod("anInt", new Class[] { int.class });
  95. Method oneIntegerM = A.class.getMethod("anInteger", new Class[] { Integer.class });
  96. if (LangUtil.is15VMOrGreater()) {
  97. checkMatches(oneInt.matchesMethodExecution(oneIntM), new A(), new A(), new Object[] { new Integer(5) });
  98. checkMatches(oneInt.matchesMethodExecution(oneIntegerM), new A(), new A(), new Object[] { new Integer(5) });
  99. checkMatches(oneInteger.matchesMethodExecution(oneIntM), new A(), new A(), new Object[] { new Integer(5) });
  100. checkMatches(oneInteger.matchesMethodExecution(oneIntegerM), new A(), new A(), new Object[] { new Integer(5) });
  101. } else {
  102. checkMatches(oneInt.matchesMethodExecution(oneIntM), new A(), new A(), new Object[] { new Integer(5) });
  103. checkNoMatch(oneInt.matchesMethodExecution(oneIntegerM), new A(), new A(), new Object[] { new Integer(5) });
  104. checkNoMatch(oneInteger.matchesMethodExecution(oneIntM), new A(), new A(), new Object[] { new Integer(5) });
  105. checkMatches(oneInteger.matchesMethodExecution(oneIntegerM), new A(), new A(), new Object[] { new Integer(5) });
  106. }
  107. } catch (Exception ex) {
  108. fail("Unexpected exception " + ex);
  109. }
  110. }
  111. private void checkMatches(ShadowMatch sMatch, Object thisOjb, Object targetObj, Object[] args) {
  112. assertTrue("match expected", sMatch.matchesJoinPoint(thisOjb, targetObj, args).matches());
  113. }
  114. private void checkNoMatch(ShadowMatch sMatch, Object thisOjb, Object targetObj, Object[] args) {
  115. assertFalse("no match expected", sMatch.matchesJoinPoint(thisOjb, targetObj, args).matches());
  116. }
  117. @SuppressWarnings("unused")
  118. private static class A {
  119. public void anInt(int i) {
  120. }
  121. public void anInteger(Integer i) {
  122. }
  123. }
  124. @SuppressWarnings("unused")
  125. private static class B extends A {
  126. public void x(A a) {
  127. }
  128. public void y(B b) {
  129. }
  130. }
  131. @SuppressWarnings("unused")
  132. private static class C {
  133. public void z(A a, C c) {
  134. }
  135. public void t(B b, A a) {
  136. }
  137. }
  138. private boolean needToSkip = false;
  139. /** this condition can occur on the build machine only, and is way too complex to fix right now... */
  140. private boolean needToSkipPointcutParserTests() {
  141. if (!LangUtil.is15VMOrGreater())
  142. return false;
  143. try {
  144. Class.forName("org.aspectj.weaver.reflect.Java15ReflectionBasedReferenceTypeDelegate", false, this.getClass()
  145. .getClassLoader());// ReflectionBasedReferenceTypeDelegate.class.getClassLoader());
  146. } catch (ClassNotFoundException cnfEx) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. protected void setUp() throws Exception {
  152. super.setUp();
  153. needToSkip = needToSkipPointcutParserTests();
  154. if (needToSkip)
  155. return;
  156. PointcutParser parser = PointcutParser
  157. .getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(A.class.getClassLoader());
  158. wildcardArgs = parser.parsePointcutExpression("args(..)");
  159. oneA = parser.parsePointcutExpression("args(org.aspectj.weaver.patterns.ArgsTestCase.A)");
  160. oneAandaC = parser
  161. .parsePointcutExpression("args(org.aspectj.weaver.patterns.ArgsTestCase.A,org.aspectj.weaver.patterns.ArgsTestCase.C)");
  162. BthenAnything = parser.parsePointcutExpression("args(org.aspectj.weaver.patterns.ArgsTestCase.B,..)");
  163. singleArg = parser.parsePointcutExpression("args(*)");
  164. }
  165. }