提交 6b1bd85b 编写于 作者: 感冒灵

<feat>: 单元测试

上级 e9dd90a7
......@@ -42,4 +42,8 @@ public class AspectJExpressionPointcutAdvisor implements PointcutAdvisor {
}
return pointcut;
}
public void setExpression(String expression) {
this.expression = expression;
}
}
package cn.noexception.test;
import cn.noexception.container.aop.AdvisedSupport;
import cn.noexception.container.aop.ClassFilter;
import cn.noexception.container.aop.MethodMatcher;
import cn.noexception.container.aop.TargetSource;
import cn.noexception.container.aop.aspectj.AspectJExpressionPointcut;
import cn.noexception.container.aop.aspectj.AspectJExpressionPointcutAdvisor;
import cn.noexception.container.aop.framework.ProxyFactory;
import cn.noexception.container.aop.framework.ReflectiveMethodInvocation;
import cn.noexception.container.aop.framework.adapter.MethodBeforeAdviceInterceptor;
import cn.noexception.test.bean.IUserService;
import cn.noexception.test.bean.UserServiceBeforeAdvice;
import cn.noexception.test.bean.UserServiceInterceptor;
import cn.noexception.test.bean.impl.UserService;
import org.aopalliance.intercept.MethodInterceptor;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class AopTest {
private AdvisedSupport advisedSupport;
@Before
public void init() {
// 目标对象
IUserService userService = new UserService();
// 组装代理信息
advisedSupport = new AdvisedSupport();
advisedSupport.setTargetSource(new TargetSource(userService));
advisedSupport.setMethodInterceptor(new UserServiceInterceptor());
advisedSupport.setMethodMatcher(new AspectJExpressionPointcut("execution(* cn.noexception.test.bean.IUserService.*(..))"));
}
@Test
public void test_proxyFactory() {
advisedSupport.setProxyTargetClass(false);
IUserService proxy = (IUserService) new ProxyFactory(advisedSupport).getProxy();
System.out.println("测试结果:" + proxy.queryUserInfo());
}
@Test
public void test_advisor() {
// 目标对象
IUserService userService = new UserService();
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
advisor.setExpression("execution(* cn.noexception.test.bean.IUserService.*(..))");
advisor.setAdvice(new MethodBeforeAdviceInterceptor(new UserServiceBeforeAdvice()));
ClassFilter classFilter = advisor.getPointcut().getClassFilter();
if (classFilter.matches(userService.getClass())) {
AdvisedSupport advisedSupport = new AdvisedSupport();
TargetSource targetSource = new TargetSource(userService);
advisedSupport.setTargetSource(targetSource);
advisedSupport.setMethodInterceptor((MethodInterceptor) advisor.getAdvice());
advisedSupport.setMethodMatcher(advisor.getPointcut().getMethodMatcher());
advisedSupport.setProxyTargetClass(true);
IUserService proxy = (IUserService) new ProxyFactory(advisedSupport).getProxy();
System.out.println("测试结果:" + proxy.queryUserInfo());
}
}
@Test
public void test_proxy_method() {
// 目标对象(可替换成任何目标对象)
Object targetObj = new UserService();
// AOP 代理
IUserService proxy = (IUserService) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), targetObj.getClass().getInterfaces(), new InvocationHandler() {
// 方法匹配器
MethodMatcher methodMatcher = new AspectJExpressionPointcut("execution(* cn.noexception.test.bean.IUserService.*(..))");
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (methodMatcher.matches(method, targetObj.getClass())) {
// 方法拦截器
MethodInterceptor methodInterceptor = invocation -> {
long start = System.currentTimeMillis();
try {
return invocation.proceed();
} finally {
System.out.println("监控 - Begin By AOP");
System.out.println("方法名称:" + invocation.getMethod().getName());
System.out.println("方法耗时:" + (System.currentTimeMillis() - start) + "ms");
System.out.println("监控 - End\r\n");
}
};
// 反射调用
return methodInterceptor.invoke(new ReflectiveMethodInvocation(targetObj, method, args));
}
return method.invoke(targetObj, args);
}
});
String result = proxy.queryUserInfo();
System.out.println("测试结果:" + result);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册