MethodMatchersTests.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:5k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.aop.support;
  17. import java.lang.reflect.Method;
  18. import junit.framework.TestCase;
  19. import org.springframework.aop.MethodMatcher;
  20. import org.springframework.beans.IOther;
  21. import org.springframework.beans.ITestBean;
  22. import org.springframework.beans.TestBean;
  23. import org.springframework.util.SerializationTestUtils;
  24. /**
  25.  * $Id: MethodMatchersTests.java,v 1.4 2004/07/24 18:25:48 johnsonr Exp $
  26.  */
  27. public class MethodMatchersTests extends TestCase {
  28. final Method EXCEPTION_GETMESSAGE;
  29. final Method ITESTBEAN_SETAGE;
  30. final Method ITESTBEAN_GETAGE;
  31. final Method IOTHER_ABSQUATULATE;
  32. /**
  33.  * Constructor for DefaultMethodMatcherTests.
  34.  * @param arg0
  35.  */
  36. public MethodMatchersTests(String arg0) throws Exception {
  37. super(arg0);
  38. EXCEPTION_GETMESSAGE = Exception.class.getMethod("getMessage", null);
  39. ITESTBEAN_GETAGE = ITestBean.class.getMethod("getAge", null);
  40. ITESTBEAN_SETAGE = ITestBean.class.getMethod("setAge", new Class[] { int.class });
  41. IOTHER_ABSQUATULATE = IOther.class.getMethod("absquatulate", null);
  42. }
  43. public void testDefaultMatchesAll() throws Exception {
  44. MethodMatcher defaultMm = MethodMatcher.TRUE;
  45. assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class));
  46. assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
  47. }
  48. public void testMethodMatcherTrueSerializable() throws Exception {
  49. assertSame(SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE), MethodMatcher.TRUE);
  50. }
  51. public void testSingle() throws Exception {
  52. MethodMatcher defaultMm = MethodMatcher.TRUE;
  53. assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class));
  54. assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
  55. defaultMm = MethodMatchers.intersection(defaultMm, new StartsWithMatcher("get"));
  56. assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class));
  57. assertFalse(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
  58. }
  59. public void testDynamicAndStaticMethodMatcherIntersection() throws Exception {
  60. MethodMatcher mm1 = MethodMatcher.TRUE;
  61. MethodMatcher mm2 = new TestDynamicMethodMatcherWhichMatches();
  62. MethodMatcher intersection = MethodMatchers.intersection(mm1, mm2);
  63. assertTrue("Intersection is a dynamic matcher", intersection.isRuntime());
  64. assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class));
  65. assertTrue("3Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object[] { new Integer(5) }));
  66. // Knock out dynamic part
  67. intersection = MethodMatchers.intersection(intersection, new TestDynamicMethodMatcherWhichDoesNotMatch());
  68. assertTrue("Intersection is a dynamic matcher", intersection.isRuntime());
  69. assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class));
  70. assertFalse("3 - not Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object[] { new Integer(5) }));
  71. }
  72. public void testStaticMethodMatcherUnion() throws Exception {
  73. MethodMatcher getterMatcher = new StartsWithMatcher("get");
  74. MethodMatcher setterMatcher = new StartsWithMatcher("set");
  75. MethodMatcher union = MethodMatchers.union(getterMatcher, setterMatcher);
  76. assertFalse("Union is a static matcher", union.isRuntime());
  77. assertTrue("Matched setAge method", union.matches(ITESTBEAN_SETAGE, TestBean.class));
  78. assertTrue("Matched getAge method", union.matches(ITESTBEAN_GETAGE, TestBean.class));
  79. assertFalse("Didn't matched absquatulate method", union.matches(IOTHER_ABSQUATULATE, TestBean.class));
  80. }
  81. public static class StartsWithMatcher extends StaticMethodMatcher {
  82. private String prefix;
  83. public StartsWithMatcher(String s) {
  84. this.prefix = s;
  85. }
  86. public boolean matches(Method m, Class targetClass) {
  87. return m.getName().startsWith(prefix);
  88. }
  89. }
  90. private static class TestDynamicMethodMatcherWhichMatches extends DynamicMethodMatcher {
  91. public boolean matches(Method m, Class targetClass, Object[] args) {
  92. return true;
  93. }
  94. }
  95. private static class TestDynamicMethodMatcherWhichDoesNotMatch extends DynamicMethodMatcher {
  96. public boolean matches(Method m, Class targetClass, Object[] args) {
  97. return false;
  98. }
  99. }
  100. }