📄 abstractaopproxytests.java
字号:
TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
pc.addAdvisor(dp);
pc.setTarget(tb);
ITestBean it = (ITestBean) createProxy(pc);
assertEquals(dp.count, 0);
int age = it.getAge();
assertEquals(dp.count, 1);
it.setAge(11);
assertEquals(it.getAge(), 11);
assertEquals(dp.count, 2);
}
public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(new Class[] { ITestBean.class });
// Could apply dynamically to getAge/setAge but not to getName
TestDynamicPointcutAdvice dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age");
pc.addAdvisor(dp);
this.mockTargetSource.setTarget(tb);
pc.setTargetSource(mockTargetSource);
ITestBean it = (ITestBean) createProxy(pc);
assertEquals(dp.count, 0);
int age = it.getAge();
// Statically vetoed
assertEquals(0, dp.count);
it.setAge(11);
assertEquals(it.getAge(), 11);
assertEquals(dp.count, 1);
// Applies statically but not dynamically
it.setName("joe");
assertEquals(dp.count, 1);
}
public void testStaticMethodPointcut() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(new Class[] { 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);
int age = it.getAge();
assertEquals(di.getCount(), 1);
it.setAge(11);
assertEquals(it.getAge(), 11);
assertEquals(di.getCount(), 2);
}
/**
* There are times when we want to call proceed() twice.
* We can do this if we clone the invocation.
*/
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
pc.addInterface(ITestBean.class);
MethodInterceptor twoBirthdayInterceptor = new MethodInterceptor() {
public Object invoke(MethodInvocation mi) throws Throwable {
// Clone the invocation to proceed three times
// "The Moor's Last Sigh": this technology can cause premature aging
MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
clone1.proceed();
clone2.proceed();
return mi.proceed();
}
};
StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {
public boolean matches(Method m, Class targetClass) {
return "haveBirthday".equals(m.getName());
}
};
pc.addAdvisor(advisor);
ITestBean it = (ITestBean) createProxy(pc);
final int age = 20;
it.setAge(age);
assertEquals(age, it.getAge());
// Should return the age before the third, AOP-induced birthday
assertEquals(age + 2, it.haveBirthday());
// Return the final age produced by 3 birthdays
assertEquals(age + 3, it.getAge());
}
/**
* We want to change the arguments on a clone: it shouldn't affect the original.
*/
public void testCanChangeArgumentsIndependentlyOnClonedInvocation() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
pc.addInterface(ITestBean.class);
/**
* Changes the name, then changes it back.
*/
MethodInterceptor nameReverter = new MethodInterceptor() {
public Object invoke(MethodInvocation mi) throws Throwable {
MethodInvocation clone = ((ReflectiveMethodInvocation) mi).invocableClone();
String oldName = ((ITestBean) mi.getThis()).getName();
clone.getArguments()[0] = oldName;
// Original method invocation should be unaffected by changes to argument list of clone
mi.proceed();
return clone.proceed();
}
};
class NameSaver implements MethodInterceptor {
private List names = new LinkedList();
public Object invoke(MethodInvocation mi) throws Throwable {
names.add(mi.getArguments()[0]);
return mi.proceed();
}
}
NameSaver saver = new NameSaver();
pc.addAdvisor(new DefaultPointcutAdvisor(Pointcuts.SETTERS, nameReverter));
pc.addAdvisor(new DefaultPointcutAdvisor(Pointcuts.SETTERS, saver));
ITestBean it = (ITestBean) createProxy(pc);
String name1 = "tony";
String name2 = "gordon";
tb.setName(name1);
assertEquals(name1, tb.getName());
it.setName(name2);
// NameReverter saved it back
assertEquals(name1, it.getName());
assertEquals(2, saver.names.size());
assertEquals(name2, saver.names.get(0));
assertEquals(name1, saver.names.get(1));
}
public static interface IOverloads {
void overload();
int overload(int i);
String overload(String foo);
void noAdvice();
}
public static class Overloads implements IOverloads {
public void overload() {
}
public int overload(int i) {
return i;
}
public String overload(String s) {
return s;
}
public void noAdvice() {
}
}
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
Overloads target = new Overloads();
ProxyFactory pc = new ProxyFactory(target);
NopInterceptor overLoadVoids = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {
public boolean matches(Method m, Class targetClass) {
return m.getName().equals("overload") && m.getParameterTypes().length == 0;
}
});
NopInterceptor overLoadInts = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {
public boolean matches(Method m, Class targetClass) {
return m.getName().equals("overload") && m.getParameterTypes().length == 1 &&
m.getParameterTypes()[0].equals(int.class);
}
});
IOverloads proxy = (IOverloads) createProxy(pc);
assertEquals(0, overLoadInts.getCount());
assertEquals(0, overLoadVoids.getCount());
proxy.overload();
assertEquals(0, overLoadInts.getCount());
assertEquals(1, overLoadVoids.getCount());
assertEquals(25, proxy.overload(25));
assertEquals(1, overLoadInts.getCount());
assertEquals(1, overLoadVoids.getCount());
proxy.noAdvice();
assertEquals(1, overLoadInts.getCount());
assertEquals(1, overLoadVoids.getCount());
}
public void testProxyIsBoundBeforeTargetSourceInvoked() {
final TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new DebugInterceptor());
pf.setExposeProxy(true);
final ITestBean proxy = (ITestBean) createProxy(pf);
Advised config = (Advised) proxy;
// This class just checks proxy is bound before getTarget() call
config.setTargetSource(new TargetSource() {
public Class getTargetClass() {
return TestBean.class;
}
public boolean isStatic() {
return false;
}
public Object getTarget() throws Exception {
assertEquals(proxy, AopContext.currentProxy());
return target;
}
public void releaseTarget(Object target) throws Exception {
}
});
// Just test anything: it will fail if context wasn't found
assertEquals(0, proxy.getAge());
}
public void testEquals() {
IOther a = new AllInstancesAreEqual();
IOther b = new AllInstancesAreEqual();
NopInterceptor i1 = new NopInterceptor();
NopInterceptor i2 = new NopInterceptor();
ProxyFactory pfa = new ProxyFactory(a);
pfa.addAdvice(i1);
ProxyFactory pfb = new ProxyFactory(b);
pfb.addAdvice(i2);
IOther proxyA = (IOther) createProxy(pfa);
IOther proxyB = (IOther) createProxy(pfb);
assertEquals(pfa.getAdvisors().length, pfb.getAdvisors().length);
assertTrue(a.equals(b));
assertTrue(i1.equals(i2));
assertTrue(proxyA.equals(proxyB));
//assertTrue(a.equals(proxyA));
assertFalse(proxyA.equals(a));
// Equality checks were handled by the proxy
assertEquals(0, i1.getCount());
// When we invoke A, it's NopInterceptor will have count == 1
// and won't think it's equal to B's NopInterceptor
proxyA.absquatulate();
assertEquals(1, i1.getCount());
assertFalse(proxyA.equals(proxyB));
}
public void testBeforeAdvisorIsInvoked() {
CountingBeforeAdvice cba = new CountingBeforeAdvice();
Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
public boolean matches(Method m, Class targetClass) {
return m.getParameterTypes().length == 0;
}
};
TestBean target = new TestBean();
target.setAge(80);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesNoArgs);
assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
ITestBean proxied = (ITestBean) createProxy(pf);
assertEquals(0, cba.getCalls());
assertEquals(0, cba.getCalls("getAge"));
assertEquals(target.getAge(), proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(1, cba.getCalls("getAge"));
assertEquals(0, cba.getCalls("setAge"));
// Won't be advised
proxied.setAge(26);
assertEquals(1, cba.getCalls());
assertEquals(26, proxied.getAge());
}
public void testUserAttributes() throws Throwable {
final Exception unexpectedException = new Exception();
class MapAwareMethodInterceptor implements MethodInterceptor {
private final Map expectedValues;
private final Map valuesToAdd;
public MapAwareMethodInterceptor(Map expectedValues, Map valuesToAdd) {
this.expectedValues = expectedValues;
this.valuesToAdd = valuesToAdd;
}
public Object invoke(MethodInvocation invocation) throws Throwable {
ReflectiveMethodInvocation rmi = (ReflectiveMethodInvocation) invocation;
for (Iterator it = rmi.getUserAttributes().keySet().iterator(); it.hasNext(); ){
Object key = it.next();
assertEquals(expectedValues.get(key), rmi.getUserAttributes().get(key));
}
rmi.getUserAttributes().putAll(valuesToAdd);
return invocation.proceed();
}
};
AdvisedSupport pc = new AdvisedSupport(new Class[] { ITestBean.class });
MapAwareMethodInterceptor mami1 = new MapAwareMethodInterceptor(new HashMap(), new HashMap());
Map firstValuesToAdd = new HashMap();
firstValuesToAdd.put("test", "");
MapAwareMethodInterceptor mami2 = new MapAwareMethodInterceptor(new HashMap(), firstValuesToAdd);
MapAwareMethodInterceptor mami3 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap());
MapAwareMethodInterceptor mami4 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap());
Map secondValuesToAdd = new HashMap();
secondValuesToAdd.put("foo", "bar");
secondValuesToAdd.put("cat", "dog");
MapAwareMethodInterceptor mami5 = new MapAwareMethodInterceptor(firstValuesToAdd, secondValuesToAdd);
Map finalExpected = new HashMap(firstValuesToAdd);
finalExpected.putAll(secondValuesToAdd);
MapAwareMethodInterceptor mami6 = new MapAwareMethodInterceptor(finalExpected, secondValuesToAdd);
pc.addAdvice(mami1);
pc.addAdvice(mami2);
pc.addAdvice(mami3);
pc.addAdvice(mami4);
pc.addAdvice(mami5);
pc.addAdvice(mami6);
// We don't care about the object
pc.setTarget(new TestBean());
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
String newName = "foo";
tb.setName(newName);
assertEquals(newName, tb.getName());
}
public void testMultiAdvice() throws Throwable {
CountingMultiAdvice cca = new CountingMultiAdvice();
Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
public boolean matches(Method m, Class targetClass) {
return m.getParameterTypes().length == 0 || "exceptional".equals(m.getName());
}
};
TestBean target = new TestBean();
target.setAge(80);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesNoArgs);
assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
ITestBean proxied = (ITestBean) createProxy(pf);
assertEquals(0, cca.getCalls());
assertEquals(0, cca.getCalls("getAge"));
assertEquals(target.getAge(), proxied.getAge());
assertEquals(2, cca.getCalls());
assertEquals(2, cca.getCalls("getAge"));
assertEquals(0, cca.getCalls("setAge"));
// Won't be advised
proxied.setAge(26);
assertEquals(2, cca.getCalls());
assertEquals(26, proxied.getAge());
assertEquals(4, cca.getCalls());
try {
proxied.exceptional(new CannotGetJdbcConnectionException("foo", (SQLException)null));
fail("Should have thrown CannotGetJdbcConnectionException");
}
catch (CannotGetJdbcConnectionException ex) {
// expected
}
assertEquals(6, cca.getCalls());
}
public void testBeforeAdviceThrowsException() {
final RuntimeException rex = new RuntimeException();
CountingBeforeAdvice ba = new CountingBeforeAdvice() {
public void before(Method m, Object[] args, Object target) throws Throwable {
super.before(m, args, target);
if (m.getName().startsWith("set"))
throw rex;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -