RegexpMethodPointcutAdvisorIntegrationTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
Category:

Java Develop

Development Platform:

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 junit.framework.TestCase;
  18. import org.springframework.aop.framework.Advised;
  19. import org.springframework.aop.interceptor.NopInterceptor;
  20. import org.springframework.aop.interceptor.SerializableNopInterceptor;
  21. import org.springframework.beans.ITestBean;
  22. import org.springframework.beans.Person;
  23. import org.springframework.beans.TestBean;
  24. import org.springframework.beans.factory.BeanFactory;
  25. import org.springframework.context.support.ClassPathXmlApplicationContext;
  26. import org.springframework.util.SerializationTestUtils;
  27. /**
  28.  * 
  29.  * @author Rod Johnson
  30.  */
  31. public class RegexpMethodPointcutAdvisorIntegrationTests extends TestCase {
  32. /**
  33.  * Constructor for RegexpMethodPointcutAroundAdvisorTests.
  34.  * @param arg0
  35.  */
  36. public RegexpMethodPointcutAdvisorIntegrationTests(String arg0) {
  37. super(arg0);
  38. }
  39. public void testSinglePattern() throws Throwable {
  40. BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml"); 
  41. ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
  42. // Interceptor behind regexp advisor
  43. NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
  44. assertEquals(0, nop.getCount());
  45. int newAge = 12;
  46. // Not advised
  47. advised.exceptional(null);
  48. assertEquals(0, nop.getCount());
  49. advised.setAge(newAge);
  50. assertEquals(newAge, advised.getAge());
  51. // Only setter fired
  52. assertEquals(1, nop.getCount());
  53. }
  54. public void testMultiplePatterns() throws Throwable {
  55. BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml"); 
  56. // This is a CGLIB proxy, so we can proxy it to the target class
  57. TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised");
  58. // Interceptor behind regexp advisor
  59. NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
  60. assertEquals(0, nop.getCount());
  61. int newAge = 12;
  62. // Not advised
  63. advised.exceptional(null);
  64. assertEquals(0, nop.getCount());
  65. // This is proxied
  66. advised.absquatulate();
  67. assertEquals(1, nop.getCount());
  68. advised.setAge(newAge);
  69. assertEquals(newAge, advised.getAge());
  70. // Only setter fired
  71. assertEquals(2, nop.getCount());
  72. }
  73. public void testSerialization() throws Throwable {
  74. BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml"); 
  75. // This is a CGLIB proxy, so we can proxy it to the target class
  76. Person p = (Person) bf.getBean("serializableSettersAdvised");
  77. // Interceptor behind regexp advisor
  78. NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
  79. assertEquals(0, nop.getCount());
  80. int newAge = 12;
  81. // Not advised
  82. assertEquals(0, p.getAge());
  83. assertEquals(0, nop.getCount());
  84. // This is proxied
  85. p.setAge(newAge);
  86. assertEquals(1, nop.getCount());
  87. p.setAge(newAge);
  88. assertEquals(newAge, p.getAge());
  89. // Only setter fired
  90. assertEquals(2, nop.getCount());
  91. // Serialize and continue...
  92. p = (Person) SerializationTestUtils.serializeAndDeserialize(p);
  93. assertEquals(newAge, p.getAge());
  94. // Remembers count, but we need to get a new reference to nop...
  95. nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
  96. assertEquals(2, nop.getCount());
  97. assertEquals("serializableSettersAdvised", p.getName());
  98. p.setAge(newAge + 1);
  99. assertEquals(3, nop.getCount());
  100. assertEquals(newAge + 1, p.getAge());
  101. }
  102. }