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.

ThisOrTargetTestCase.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * 2005 Contributors
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * PARC initial implementation
  12. * Adrian Colyer, runtime reflection extensions
  13. * ******************************************************************/
  14. package org.aspectj.weaver.patterns;
  15. import java.io.IOException;
  16. import java.lang.reflect.Method;
  17. import junit.framework.TestCase;
  18. import org.aspectj.util.LangUtil;
  19. import org.aspectj.weaver.tools.JoinPointMatch;
  20. import org.aspectj.weaver.tools.PointcutExpression;
  21. import org.aspectj.weaver.tools.PointcutParameter;
  22. import org.aspectj.weaver.tools.PointcutParser;
  23. import org.aspectj.weaver.tools.ShadowMatch;
  24. /**
  25. * @author hugunin
  26. *
  27. * To change this generated comment edit the template variable "typecomment": Window>Preferences>Java>Templates. To enable
  28. * and disable the creation of type comments go to Window>Preferences>Java>Code Generation.
  29. */
  30. public class ThisOrTargetTestCase extends TestCase {
  31. private boolean needToSkip = false;
  32. /** this condition can occur on the build machine only, and is way too complex to fix right now... */
  33. private boolean needToSkipPointcutParserTests() {
  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. /**
  47. * Constructor for PatternTestCase.
  48. *
  49. * @param name
  50. */
  51. public ThisOrTargetTestCase(String name) {
  52. super(name);
  53. }
  54. public void testMatchJP() throws Exception {
  55. if (needToSkip) {
  56. return;
  57. }
  58. PointcutParser parser = PointcutParser
  59. .getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this.getClass().getClassLoader());
  60. PointcutExpression thisEx = parser.parsePointcutExpression("this(Exception)");
  61. PointcutExpression thisIOEx = parser.parsePointcutExpression("this(java.io.IOException)");
  62. PointcutExpression targetEx = parser.parsePointcutExpression("target(Exception)");
  63. PointcutExpression targetIOEx = parser.parsePointcutExpression("target(java.io.IOException)");
  64. Method toString = Object.class.getMethod("toString", new Class[0]);
  65. checkMatches(thisEx.matchesMethodCall(toString, toString), new Exception(), null, null);
  66. checkNoMatch(thisIOEx.matchesMethodCall(toString, toString), new Exception(), null, null);
  67. checkNoMatch(targetEx.matchesMethodCall(toString, toString), new Exception(), new Object(), null);
  68. checkNoMatch(targetIOEx.matchesMethodCall(toString, toString), new Exception(), new Exception(), null);
  69. checkMatches(thisEx.matchesMethodCall(toString, toString), new IOException(), null, null);
  70. checkMatches(thisIOEx.matchesMethodCall(toString, toString), new IOException(), null, null);
  71. checkNoMatch(thisEx.matchesMethodCall(toString, toString), new Object(), null, null);
  72. checkNoMatch(thisIOEx.matchesMethodCall(toString, toString), new Exception(), null, null);
  73. checkMatches(targetEx.matchesMethodCall(toString, toString), new Exception(), new Exception(), null);
  74. checkNoMatch(targetIOEx.matchesMethodCall(toString, toString), new Exception(), new Exception(), null);
  75. checkMatches(targetIOEx.matchesMethodCall(toString, toString), new Exception(), new IOException(), null);
  76. }
  77. public void testBinding() throws Exception {
  78. if (needToSkip) {
  79. return;
  80. }
  81. PointcutParser parser = PointcutParser
  82. .getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this.getClass().getClassLoader());
  83. PointcutParameter ex = parser.createPointcutParameter("ex", Exception.class);
  84. PointcutParameter ioEx = parser.createPointcutParameter("ioEx", IOException.class);
  85. PointcutExpression thisEx = parser.parsePointcutExpression("this(ex)", Exception.class, new PointcutParameter[] { ex });
  86. PointcutExpression targetIOEx = parser.parsePointcutExpression("target(ioEx)", Exception.class,
  87. new PointcutParameter[] { ioEx });
  88. Method toString = Object.class.getMethod("toString", new Class[0]);
  89. ShadowMatch sMatch = thisEx.matchesMethodCall(toString, toString);
  90. Exception exceptionParameter = new Exception();
  91. IOException ioExceptionParameter = new IOException();
  92. JoinPointMatch jpMatch = null;
  93. jpMatch = sMatch.matchesJoinPoint(null, null, null);// 318899
  94. assertFalse(jpMatch.matches());
  95. jpMatch = sMatch.matchesJoinPoint(exceptionParameter, null, null);
  96. assertTrue("should match", jpMatch.matches());
  97. PointcutParameter[] bindings = jpMatch.getParameterBindings();
  98. assertEquals("one binding", 1, bindings.length);
  99. assertEquals("should be exceptionParameter", exceptionParameter, bindings[0].getBinding());
  100. assertEquals("ex", bindings[0].getName());
  101. sMatch = targetIOEx.matchesMethodCall(toString, toString);
  102. jpMatch = sMatch.matchesJoinPoint(exceptionParameter, ioExceptionParameter, null);
  103. assertTrue("should match", jpMatch.matches());
  104. bindings = jpMatch.getParameterBindings();
  105. assertEquals("one binding", 1, bindings.length);
  106. assertEquals("should be ioExceptionParameter", ioExceptionParameter, bindings[0].getBinding());
  107. assertEquals("ioEx", bindings[0].getName());
  108. }
  109. private void checkMatches(ShadowMatch sMatch, Object thisObj, Object targetObj, Object[] args) {
  110. assertTrue("match expected", sMatch.matchesJoinPoint(thisObj, targetObj, args).matches());
  111. }
  112. private void checkNoMatch(ShadowMatch sMatch, Object thisObj, Object targetObj, Object[] args) {
  113. assertFalse("no match expected", sMatch.matchesJoinPoint(thisObj, targetObj, args).matches());
  114. }
  115. /**
  116. * Method checkSerialization.
  117. *
  118. * @param string
  119. */
  120. // private void checkSerialization(String string) throws IOException {
  121. // Pointcut p = makePointcut(string);
  122. // ByteArrayOutputStream bo = new ByteArrayOutputStream();
  123. // DataOutputStream out = new DataOutputStream(bo);
  124. // p.write(out);
  125. // out.close();
  126. //
  127. // ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  128. // DataInputStream in = new DataInputStream(bi);
  129. // Pointcut newP = Pointcut.read(in, null);
  130. //
  131. // assertEquals("write/read", p, newP);
  132. // }
  133. }