|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -20,6 +20,7 @@ import java.io.Serializable; |
|
|
|
|
|
|
|
|
|
|
|
import org.aopalliance.intercept.MethodInterceptor; |
|
|
|
import org.aopalliance.intercept.MethodInterceptor; |
|
|
|
import org.aopalliance.intercept.MethodInvocation; |
|
|
|
import org.aopalliance.intercept.MethodInvocation; |
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.aop.interceptor.ExposeInvocationInterceptor; |
|
|
|
import org.springframework.aop.interceptor.ExposeInvocationInterceptor; |
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
@ -28,7 +29,6 @@ import org.springframework.tests.sample.beans.ITestBean; |
|
|
|
import org.springframework.tests.sample.beans.TestBean; |
|
|
|
import org.springframework.tests.sample.beans.TestBean; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.mockito.BDDMockito.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @since 13.03.2003 |
|
|
|
* @since 13.03.2003 |
|
|
|
@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.*; |
|
|
|
* @author Chris Beams |
|
|
|
* @author Chris Beams |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("serial") |
|
|
|
@SuppressWarnings("serial") |
|
|
|
public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable { |
|
|
|
public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected Object createProxy(ProxyCreatorSupport as) { |
|
|
|
protected Object createProxy(ProxyCreatorSupport as) { |
|
|
|
@ -52,6 +52,8 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
return new JdkDynamicAopProxy(as); |
|
|
|
return new JdkDynamicAopProxy(as); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testNullConfig() { |
|
|
|
public void testNullConfig() { |
|
|
|
try { |
|
|
|
try { |
|
|
|
new JdkDynamicAopProxy(null); |
|
|
|
new JdkDynamicAopProxy(null); |
|
|
|
@ -62,6 +64,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testProxyIsJustInterface() throws Throwable { |
|
|
|
public void testProxyIsJustInterface() throws Throwable { |
|
|
|
TestBean raw = new TestBean(); |
|
|
|
TestBean raw = new TestBean(); |
|
|
|
raw.setAge(32); |
|
|
|
raw.setAge(32); |
|
|
|
@ -74,32 +77,32 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
assertTrue(!(proxy instanceof TestBean)); |
|
|
|
assertTrue(!(proxy instanceof TestBean)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testInterceptorIsInvokedWithNoTarget() throws Throwable { |
|
|
|
public void testInterceptorIsInvokedWithNoTarget() throws Throwable { |
|
|
|
// Test return value
|
|
|
|
// Test return value
|
|
|
|
int age = 25; |
|
|
|
final Integer age = 25; |
|
|
|
MethodInterceptor mi = mock(MethodInterceptor.class); |
|
|
|
MethodInterceptor mi = (invocation -> age); |
|
|
|
|
|
|
|
|
|
|
|
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class }); |
|
|
|
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class}); |
|
|
|
pc.addAdvice(mi); |
|
|
|
pc.addAdvice(mi); |
|
|
|
AopProxy aop = createAopProxy(pc); |
|
|
|
AopProxy aop = createAopProxy(pc); |
|
|
|
|
|
|
|
|
|
|
|
given(mi.invoke(null)).willReturn(age); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ITestBean tb = (ITestBean) aop.getProxy(); |
|
|
|
ITestBean tb = (ITestBean) aop.getProxy(); |
|
|
|
assertTrue("correct return value", tb.getAge() == age); |
|
|
|
assertTrue("correct return value", tb.getAge() == age); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable { |
|
|
|
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable { |
|
|
|
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() { |
|
|
|
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void assertions(MethodInvocation invocation) { |
|
|
|
protected void assertions(MethodInvocation invocation) { |
|
|
|
assertTrue(invocation.getThis() == this); |
|
|
|
assertTrue(invocation.getThis() == this); |
|
|
|
assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(), |
|
|
|
assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(), |
|
|
|
invocation.getMethod().getDeclaringClass() == ITestBean.class); |
|
|
|
invocation.getMethod().getDeclaringClass() == ITestBean.class); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class, IOther.class }); |
|
|
|
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class, IOther.class}); |
|
|
|
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); |
|
|
|
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); |
|
|
|
TrapTargetInterceptor tii = new TrapTargetInterceptor() { |
|
|
|
TrapTargetInterceptor tii = new TrapTargetInterceptor() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -126,10 +129,11 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
//assertTrue(target.invocation == tii.invocation);
|
|
|
|
//assertTrue(target.invocation == tii.invocation);
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testProxyNotWrappedIfIncompatible() { |
|
|
|
public void testProxyNotWrappedIfIncompatible() { |
|
|
|
FooBar bean = new FooBar(); |
|
|
|
FooBar bean = new FooBar(); |
|
|
|
ProxyCreatorSupport as = new ProxyCreatorSupport(); |
|
|
|
ProxyCreatorSupport as = new ProxyCreatorSupport(); |
|
|
|
as.setInterfaces(new Class<?>[] {Foo.class}); |
|
|
|
as.setInterfaces(Foo.class); |
|
|
|
as.setTarget(bean); |
|
|
|
as.setTarget(bean); |
|
|
|
|
|
|
|
|
|
|
|
Foo proxy = (Foo) createProxy(as); |
|
|
|
Foo proxy = (Foo) createProxy(as); |
|
|
|
@ -138,6 +142,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testEqualsAndHashCodeDefined() throws Exception { |
|
|
|
public void testEqualsAndHashCodeDefined() throws Exception { |
|
|
|
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class}); |
|
|
|
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class}); |
|
|
|
as.setTarget(new Person()); |
|
|
|
as.setTarget(new Person()); |
|
|
|
@ -149,7 +154,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static interface Foo { |
|
|
|
public interface Foo { |
|
|
|
|
|
|
|
|
|
|
|
Bar getBarThis(); |
|
|
|
Bar getBarThis(); |
|
|
|
|
|
|
|
|
|
|
|
@ -157,8 +162,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static interface Bar { |
|
|
|
public interface Bar { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -176,7 +180,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static interface Named { |
|
|
|
public interface Named { |
|
|
|
|
|
|
|
|
|
|
|
String getName(); |
|
|
|
String getName(); |
|
|
|
|
|
|
|
|
|
|
|
@ -201,11 +205,8 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements |
|
|
|
public boolean equals(Object o) { |
|
|
|
public boolean equals(Object o) { |
|
|
|
if (this == o) return true; |
|
|
|
if (this == o) return true; |
|
|
|
if (o == null || getClass() != o.getClass()) return false; |
|
|
|
if (o == null || getClass() != o.getClass()) return false; |
|
|
|
|
|
|
|
Person person = (Person) o; |
|
|
|
final Person person = (Person) o; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!name.equals(person.name)) return false; |
|
|
|
if (!name.equals(person.name)) return false; |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|