RegexpMethodPointcutAdvisorIntegrationTests.java 3.8 KB
Newer Older
1
/*
C
Chris Beams 已提交
2
 * Copyright 2002-2008 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop.support;

19
import static org.junit.Assert.assertEquals;
C
Chris Beams 已提交
20
import static test.util.TestResourceUtils.qualifiedResource;
21

22
import org.junit.Test;
23 24
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.BeanFactory;
25
import org.springframework.beans.factory.xml.XmlBeanFactory;
C
Chris Beams 已提交
26
import org.springframework.core.io.Resource;
27

C
Chris Beams 已提交
28 29
import test.aop.NopInterceptor;
import test.aop.SerializableNopInterceptor;
C
Chris Beams 已提交
30 31 32
import test.beans.ITestBean;
import test.beans.Person;
import test.beans.TestBean;
C
Chris Beams 已提交
33
import test.util.SerializationTestUtils;
C
Chris Beams 已提交
34

35 36
/**
 * @author Rod Johnson
37
 * @author Chris Beams
38
 */
C
Chris Beams 已提交
39 40
public final class RegexpMethodPointcutAdvisorIntegrationTests {
	
C
Chris Beams 已提交
41 42
	private static final Resource CONTEXT =
		qualifiedResource(RegexpMethodPointcutAdvisorIntegrationTests.class, "context.xml");
43

44
	@Test
45
	public void testSinglePattern() throws Throwable {
C
Chris Beams 已提交
46
		BeanFactory bf = new XmlBeanFactory(CONTEXT); 
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
		ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
		// Interceptor behind regexp advisor
		NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
		assertEquals(0, nop.getCount());
		
		int newAge = 12;
		// Not advised
		advised.exceptional(null);
		assertEquals(0, nop.getCount());
		advised.setAge(newAge);
		assertEquals(newAge, advised.getAge());
		// Only setter fired
		assertEquals(1, nop.getCount());
	}
	
62
	@Test
63
	public void testMultiplePatterns() throws Throwable {
C
Chris Beams 已提交
64
		BeanFactory bf = new XmlBeanFactory(CONTEXT); 
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		// This is a CGLIB proxy, so we can proxy it to the target class
		TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised");
		// Interceptor behind regexp advisor
		NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
		assertEquals(0, nop.getCount());
	
		int newAge = 12;
		// Not advised
		advised.exceptional(null);
		assertEquals(0, nop.getCount());
		
		// This is proxied
		advised.absquatulate();
		assertEquals(1, nop.getCount());
		advised.setAge(newAge);
		assertEquals(newAge, advised.getAge());
		// Only setter fired
		assertEquals(2, nop.getCount());
	}
	
85
	@Test
86
	public void testSerialization() throws Throwable {
C
Chris Beams 已提交
87
		BeanFactory bf = new XmlBeanFactory(CONTEXT); 
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		// This is a CGLIB proxy, so we can proxy it to the target class
		Person p = (Person) bf.getBean("serializableSettersAdvised");
		// Interceptor behind regexp advisor
		NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
		assertEquals(0, nop.getCount());
	
		int newAge = 12;
		// Not advised
		assertEquals(0, p.getAge());
		assertEquals(0, nop.getCount());
		
		// This is proxied
		p.setAge(newAge);
		assertEquals(1, nop.getCount());
		p.setAge(newAge);
		assertEquals(newAge, p.getAge());
		// Only setter fired
		assertEquals(2, nop.getCount());
		
		// Serialize and continue...
		p = (Person) SerializationTestUtils.serializeAndDeserialize(p);
		assertEquals(newAge, p.getAge());
		// Remembers count, but we need to get a new reference to nop...
		nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
		assertEquals(2, nop.getCount());
		assertEquals("serializableSettersAdvised", p.getName());
		p.setAge(newAge + 1);
		assertEquals(3, nop.getCount());
		assertEquals(newAge + 1, p.getAge());
	}

}