org.springframework.tests.aop.interceptor.NopInterceptor Java Examples

The following examples show how to use org.springframework.tests.aop.interceptor.NopInterceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testTargetCanGetProxy() {
	NopInterceptor di = new NopInterceptor();
	INeedsToSeeProxy target = new TargetChecker();
	ProxyFactory proxyFactory = new ProxyFactory(target);
	proxyFactory.setExposeProxy(true);
	assertTrue(proxyFactory.isExposeProxy());

	proxyFactory.addAdvice(0, di);
	INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(proxyFactory);
	assertEquals(0, di.getCount());
	assertEquals(0, target.getCount());
	proxied.incrementViaThis();
	assertEquals("Increment happened", 1, target.getCount());

	assertEquals("Only one invocation via AOP as use of this wasn't proxied", 1, di.getCount());
	// 1 invocation
	assertEquals("Increment happened", 1, proxied.getCount());
	proxied.incrementViaProxy(); // 2 invocations
	assertEquals("Increment happened", 2, target.getCount());
	assertEquals("3 more invocations via AOP as the first call was reentrant through the proxy", 4, di.getCount());
}
 
Example #2
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
	CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
	TestBean target = new TestBean();
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvice(car);
	assertEquals("Advice was wrapped in Advisor and added", car, pf.getAdvisors()[1].getAdvice());
	ITestBean proxied = (ITestBean) createProxy(pf);
	assertEquals(0, car.getCalls());
	int age = 10;
	proxied.setAge(age);
	assertEquals(age, proxied.getAge());
	assertEquals(2, car.getCalls());
	Exception exc = new Exception();
	// On exception it won't be invoked
	try {
		proxied.exceptional(exc);
		fail();
	}
	catch (Throwable t) {
		assertSame(exc, t);
	}
	assertEquals(2, car.getCalls());
}
 
Example #3
Source File: CglibProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testProxyAProxy() {
	ITestBean target = new TestBean();

	mockTargetSource.setTarget(target);
	AdvisedSupport as = new AdvisedSupport();
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	CglibAopProxy cglib = new CglibAopProxy(as);

	ITestBean proxy1 = (ITestBean) cglib.getProxy();

	mockTargetSource.setTarget(proxy1);
	as = new AdvisedSupport(new Class<?>[]{});
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	cglib = new CglibAopProxy(as);

	assertThat(cglib.getProxy(), instanceOf(ITestBean.class));
}
 
Example #4
Source File: CglibProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testExceptionHandling() {
	ExceptionThrower bean = new ExceptionThrower();
	mockTargetSource.setTarget(bean);

	AdvisedSupport as = new AdvisedSupport();
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	AopProxy aop = new CglibAopProxy(as);

	ExceptionThrower proxy = (ExceptionThrower) aop.getProxy();

	try {
		proxy.doTest();
	}
	catch (Exception ex) {
		assertTrue("Invalid exception class", ex instanceof ApplicationContextException);
	}

	assertTrue("Catch was not invoked", proxy.isCatchInvoked());
	assertTrue("Finally was not invoked", proxy.isFinallyInvoked());
}
 
Example #5
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory();
	pc.addInterface(ITestBean.class);
	// Could apply dynamically to getAge/setAge but not to getName
	TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age");
	pc.addAdvisor(dp);
	this.mockTargetSource.setTarget(tb);
	pc.setTargetSource(mockTargetSource);
	ITestBean it = (ITestBean) createProxy(pc);
	assertEquals(0, dp.count);
	it.getAge();
	// Statically vetoed
	assertEquals(0, dp.count);
	it.setAge(11);
	assertEquals(11, it.getAge());
	assertEquals(1, dp.count);
	// Applies statically but not dynamically
	it.setName("joe");
	assertEquals(1, dp.count);
}
 
Example #6
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Check that the string is informative.
 */
@Test
public void testProxyConfigString() {
	TestBean target = new TestBean();
	ProxyFactory pc = new ProxyFactory(target);
	pc.setInterfaces(ITestBean.class);
	pc.addAdvice(new NopInterceptor());
	MethodBeforeAdvice mba = new CountingBeforeAdvice();
	Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
	pc.addAdvisor(advisor);
	ITestBean proxied = (ITestBean) createProxy(pc);

	String proxyConfigString = ((Advised) proxied).toProxyConfigString();
	assertTrue(proxyConfigString.contains(advisor.toString()));
	assertTrue(proxyConfigString.contains("1 interface"));
}
 
Example #7
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testReentrance() {
	int age1 = 33;

	TestBean target1 = new TestBean();
	ProxyFactory pf1 = new ProxyFactory(target1);
	NopInterceptor di1 = new NopInterceptor();
	pf1.addAdvice(0, di1);
	ITestBean advised1 = (ITestBean) createProxy(pf1);
	advised1.setAge(age1); // = 1 invocation
	advised1.setSpouse(advised1); // = 2 invocations

	assertEquals("one was invoked correct number of times", 2, di1.getCount());

	assertEquals("Advised one has correct age", age1, advised1.getAge()); // = 3 invocations
	assertEquals("one was invoked correct number of times", 3, di1.getCount());

	// = 5 invocations, as reentrant call to spouse is advised also
	assertEquals("Advised spouse has correct age", age1, advised1.getSpouse().getAge());

	assertEquals("one was invoked correct number of times", 5, di1.getCount());
}
 
Example #8
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAdviceImplementsIntroductionInfo() throws Throwable {
	TestBean tb = new TestBean();
	String name = "tony";
	tb.setName(name);
	ProxyFactory pc = new ProxyFactory(tb);
	NopInterceptor di = new NopInterceptor();
	pc.addAdvice(di);
	final long ts = 37;
	pc.addAdvice(new DelegatingIntroductionInterceptor(new TimeStamped() {
		@Override
		public long getTimeStamp() {
			return ts;
		}
	}));

	ITestBean proxied = (ITestBean) createProxy(pc);
	assertEquals(name, proxied.getName());
	TimeStamped intro = (TimeStamped) proxied;
	assertEquals(ts, intro.getTimeStamp());
}
 
Example #9
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testReentrance() {
	int age1 = 33;

	TestBean target1 = new TestBean();
	ProxyFactory pf1 = new ProxyFactory(target1);
	NopInterceptor di1 = new NopInterceptor();
	pf1.addAdvice(0, di1);
	ITestBean advised1 = (ITestBean) createProxy(pf1);
	advised1.setAge(age1); // = 1 invocation
	advised1.setSpouse(advised1); // = 2 invocations

	assertEquals("one was invoked correct number of times", 2, di1.getCount());

	assertEquals("Advised one has correct age", age1, advised1.getAge()); // = 3 invocations
	assertEquals("one was invoked correct number of times", 3, di1.getCount());

	// = 5 invocations, as reentrant call to spouse is advised also
	assertEquals("Advised spouse has correct age", age1, advised1.getSpouse().getAge());

	assertEquals("one was invoked correct number of times", 5, di1.getCount());
}
 
Example #10
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Check that casting to Advised can't get around advice freeze.
 */
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	assertFalse(pc.isFrozen());
	pc.addAdvice(new NopInterceptor());
	ITestBean proxied = (ITestBean) createProxy(pc);
	pc.setFrozen(true);
	Advised advised = (Advised) proxied;

	assertTrue(pc.isFrozen());
	try {
		advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
		fail("Shouldn't be able to add Advisor when frozen");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("frozen"));
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(1, advised.getAdvisors().length);
}
 
Example #11
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testUseAsHashKey() {
	TestBean target1 = new TestBean();
	ProxyFactory pf1 = new ProxyFactory(target1);
	pf1.addAdvice(new NopInterceptor());
	ITestBean proxy1 = (ITestBean) createProxy(pf1);

	TestBean target2 = new TestBean();
	ProxyFactory pf2 = new ProxyFactory(target2);
	pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
	ITestBean proxy2 = (ITestBean) createProxy(pf2);

	HashMap<ITestBean, Object> h = new HashMap<>();
	Object value1 = "foo";
	Object value2 = "bar";
	assertNull(h.get(proxy1));
	h.put(proxy1, value1);
	h.put(proxy2, value2);
	assertEquals(h.get(proxy1), value1);
	assertEquals(h.get(proxy2), value2);
}
 
Example #12
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Check that the string is informative.
 */
@Test
public void testProxyConfigString() {
	TestBean target = new TestBean();
	ProxyFactory pc = new ProxyFactory(target);
	pc.setInterfaces(ITestBean.class);
	pc.addAdvice(new NopInterceptor());
	MethodBeforeAdvice mba = new CountingBeforeAdvice();
	Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
	pc.addAdvisor(advisor);
	ITestBean proxied = (ITestBean) createProxy(pc);

	String proxyConfigString = ((Advised) proxied).toProxyConfigString();
	assertTrue(proxyConfigString.contains(advisor.toString()));
	assertTrue(proxyConfigString.contains("1 interface"));
}
 
Example #13
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory();
	pc.addInterface(ITestBean.class);
	TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
	pc.addAdvisor(dp);
	pc.setTarget(tb);
	ITestBean it = (ITestBean) createProxy(pc);
	assertEquals(0, dp.count);
	it.getAge();
	assertEquals(1, dp.count);
	it.setAge(11);
	assertEquals(11, it.getAge());
	assertEquals(2, dp.count);
}
 
Example #14
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory();
	pc.addInterface(ITestBean.class);
	// Could apply dynamically to getAge/setAge but not to getName
	TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age");
	pc.addAdvisor(dp);
	this.mockTargetSource.setTarget(tb);
	pc.setTargetSource(mockTargetSource);
	ITestBean it = (ITestBean) createProxy(pc);
	assertEquals(0, dp.count);
	it.getAge();
	// Statically vetoed
	assertEquals(0, dp.count);
	it.setAge(11);
	assertEquals(11, it.getAge());
	assertEquals(1, dp.count);
	// Applies statically but not dynamically
	it.setName("joe");
	assertEquals(1, dp.count);
}
 
Example #15
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testStaticMethodPointcut() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory();
	pc.addInterface(ITestBean.class);
	NopInterceptor di = new NopInterceptor();
	TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge");
	pc.addAdvisor(sp);
	pc.setTarget(tb);
	ITestBean it = (ITestBean) createProxy(pc);
	assertEquals(di.getCount(), 0);
	it.getAge();
	assertEquals(di.getCount(), 1);
	it.setAge(11);
	assertEquals(it.getAge(), 11);
	assertEquals(di.getCount(), 2);
}
 
Example #16
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCannotAddInterceptorWhenFrozen() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	assertFalse(pc.isFrozen());
	pc.addAdvice(new NopInterceptor());
	ITestBean proxied = (ITestBean) createProxy(pc);
	pc.setFrozen(true);
	try {
		pc.addAdvice(0, new NopInterceptor());
		fail("Shouldn't be able to add interceptor when frozen");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("frozen"));
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(1, ((Advised) proxied).getAdvisors().length);
}
 
Example #17
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
	CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
	TestBean target = new TestBean();
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvice(car);
	assertEquals("Advice was wrapped in Advisor and added", car, pf.getAdvisors()[1].getAdvice());
	ITestBean proxied = (ITestBean) createProxy(pf);
	assertEquals(0, car.getCalls());
	int age = 10;
	proxied.setAge(age);
	assertEquals(age, proxied.getAge());
	assertEquals(2, car.getCalls());
	Exception exc = new Exception();
	// On exception it won't be invoked
	try {
		proxied.exceptional(exc);
		fail();
	}
	catch (Throwable t) {
		assertSame(exc, t);
	}
	assertEquals(2, car.getCalls());
}
 
Example #18
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Check that casting to Advised can't get around advice freeze.
 */
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	assertFalse(pc.isFrozen());
	pc.addAdvice(new NopInterceptor());
	ITestBean proxied = (ITestBean) createProxy(pc);
	pc.setFrozen(true);
	Advised advised = (Advised) proxied;

	assertTrue(pc.isFrozen());
	try {
		advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
		fail("Shouldn't be able to add Advisor when frozen");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("frozen"));
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(1, advised.getAdvisors().length);
}
 
Example #19
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCannotAddInterceptorWhenFrozen() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	assertFalse(pc.isFrozen());
	pc.addAdvice(new NopInterceptor());
	ITestBean proxied = (ITestBean) createProxy(pc);
	pc.setFrozen(true);
	try {
		pc.addAdvice(0, new NopInterceptor());
		fail("Shouldn't be able to add interceptor when frozen");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("frozen"));
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(1, ((Advised) proxied).getAdvisors().length);
}
 
Example #20
Source File: RegexpMethodPointcutAdvisorIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testMultiplePatterns() throws Throwable {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
	// 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());
}
 
Example #21
Source File: ControlFlowPointcutTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testMatches() {
	TestBean target = new TestBean();
	target.setAge(27);
	NopInterceptor nop = new NopInterceptor();
	ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge");
	ProxyFactory pf = new ProxyFactory(target);
	ITestBean proxied = (ITestBean) pf.getProxy();
	pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop));

	// Not advised, not under One
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(0, nop.getCount());

	// Will be advised
	assertEquals(target.getAge(), new One().getAge(proxied));
	assertEquals(1, nop.getCount());

	// Won't be advised
	assertEquals(target.getAge(), new One().nomatch(proxied));
	assertEquals(1, nop.getCount());
	assertEquals(3, cflow.getEvaluations());
}
 
Example #22
Source File: ProxyFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRemoveAdvisorByReference() {
	TestBean target = new TestBean();
	ProxyFactory pf = new ProxyFactory(target);
	NopInterceptor nop = new NopInterceptor();
	CountingBeforeAdvice cba = new CountingBeforeAdvice();
	Advisor advisor = new DefaultPointcutAdvisor(cba);
	pf.addAdvice(nop);
	pf.addAdvisor(advisor);
	ITestBean proxied = (ITestBean) pf.getProxy();
	proxied.setAge(5);
	assertEquals(1, cba.getCalls());
	assertEquals(1, nop.getCount());
	assertTrue(pf.removeAdvisor(advisor));
	assertEquals(5, proxied.getAge());
	assertEquals(1, cba.getCalls());
	assertEquals(2, nop.getCount());
	assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null)));
}
 
Example #23
Source File: ProxyFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testInterceptorInclusionMethods() {
	class MyInterceptor implements MethodInterceptor {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			throw new UnsupportedOperationException();
		}
	}

	NopInterceptor di = new NopInterceptor();
	NopInterceptor diUnused = new NopInterceptor();
	ProxyFactory factory = new ProxyFactory(new TestBean());
	factory.addAdvice(0, di);
	assertThat(factory.getProxy(), instanceOf(ITestBean.class));
	assertTrue(factory.adviceIncluded(di));
	assertTrue(!factory.adviceIncluded(diUnused));
	assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 1);
	assertTrue(factory.countAdvicesOfType(MyInterceptor.class) == 0);

	factory.addAdvice(0, diUnused);
	assertTrue(factory.adviceIncluded(diUnused));
	assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 2);
}
 
Example #24
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory();
	pc.addInterface(ITestBean.class);
	TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
	pc.addAdvisor(dp);
	pc.setTarget(tb);
	ITestBean it = (ITestBean) createProxy(pc);
	assertEquals(0, dp.count);
	it.getAge();
	assertEquals(1, dp.count);
	it.setAge(11);
	assertEquals(11, it.getAge());
	assertEquals(2, dp.count);
}
 
Example #25
Source File: CglibProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testProtectedMethodInvocation() {
	ProtectedMethodTestBean bean = new ProtectedMethodTestBean();
	bean.value = "foo";
	mockTargetSource.setTarget(bean);

	AdvisedSupport as = new AdvisedSupport();
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	AopProxy aop = new CglibAopProxy(as);

	ProtectedMethodTestBean proxy = (ProtectedMethodTestBean) aop.getProxy();
	assertTrue(AopUtils.isCglibProxy(proxy));
	assertEquals(proxy.getClass().getClassLoader(), bean.getClass().getClassLoader());
	assertEquals("foo", proxy.getString());
}
 
Example #26
Source File: CglibProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPackageMethodInvocation() {
	PackageMethodTestBean bean = new PackageMethodTestBean();
	bean.value = "foo";
	mockTargetSource.setTarget(bean);

	AdvisedSupport as = new AdvisedSupport();
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	AopProxy aop = new CglibAopProxy(as);

	PackageMethodTestBean proxy = (PackageMethodTestBean) aop.getProxy();
	assertTrue(AopUtils.isCglibProxy(proxy));
	assertEquals(proxy.getClass().getClassLoader(), bean.getClass().getClassLoader());
	assertEquals("foo", proxy.getString());
}
 
Example #27
Source File: CglibProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testProxyAProxy() {
	ITestBean target = new TestBean();

	mockTargetSource.setTarget(target);
	AdvisedSupport as = new AdvisedSupport();
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	CglibAopProxy cglib = new CglibAopProxy(as);

	ITestBean proxy1 = (ITestBean) cglib.getProxy();

	mockTargetSource.setTarget(proxy1);
	as = new AdvisedSupport(new Class<?>[]{});
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	cglib = new CglibAopProxy(as);

	assertThat(cglib.getProxy(), instanceOf(ITestBean.class));
}
 
Example #28
Source File: CglibProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testProxyAProxyWithAdditionalInterface() {
	ITestBean target = new TestBean();
	mockTargetSource.setTarget(target);

	AdvisedSupport as = new AdvisedSupport();
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	as.addInterface(Serializable.class);
	CglibAopProxy cglib = new CglibAopProxy(as);

	ITestBean proxy1 = (ITestBean) cglib.getProxy();

	mockTargetSource.setTarget(proxy1);
	as = new AdvisedSupport(new Class<?>[]{});
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	cglib = new CglibAopProxy(as);

	ITestBean proxy2 = (ITestBean) cglib.getProxy();
	assertTrue(proxy2 instanceof Serializable);
}
 
Example #29
Source File: CglibProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testExceptionHandling() {
	ExceptionThrower bean = new ExceptionThrower();
	mockTargetSource.setTarget(bean);

	AdvisedSupport as = new AdvisedSupport();
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	AopProxy aop = new CglibAopProxy(as);

	ExceptionThrower proxy = (ExceptionThrower) aop.getProxy();

	try {
		proxy.doTest();
	}
	catch (Exception ex) {
		assertTrue("Invalid exception class", ex instanceof ApplicationContextException);
	}

	assertTrue("Catch was not invoked", proxy.isCatchInvoked());
	assertTrue("Finally was not invoked", proxy.isFinallyInvoked());
}
 
Example #30
Source File: ProxyFactoryBeanTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * The instances are equal, but do not have object identity.
 * Interceptors and interfaces and the target are the same.
 */
@Test
public void testSingletonInstancesAreEqual() {
	ITestBean test1 = (ITestBean) factory.getBean("test1");
	ITestBean test1_1 = (ITestBean) factory.getBean("test1");
	//assertTrue("Singleton instances ==", test1 == test1_1);
	assertEquals("Singleton instances ==", test1, test1_1);
	test1.setAge(25);
	assertEquals(test1.getAge(), test1_1.getAge());
	test1.setAge(250);
	assertEquals(test1.getAge(), test1_1.getAge());
	Advised pc1 = (Advised) test1;
	Advised pc2 = (Advised) test1_1;
	assertArrayEquals(pc1.getAdvisors(), pc2.getAdvisors());
	int oldLength = pc1.getAdvisors().length;
	NopInterceptor di = new NopInterceptor();
	pc1.addAdvice(1, di);
	assertArrayEquals(pc1.getAdvisors(), pc2.getAdvisors());
	assertEquals("Now have one more advisor", oldLength + 1, pc2.getAdvisors().length);
	assertEquals(di.getCount(), 0);
	test1.setAge(5);
	assertEquals(test1_1.getAge(), test1.getAge());
	assertEquals(di.getCount(), 3);
}