package us.test.codecraft.tinyioc.java.us.codecraft.tinyioc.aop;

import org.junit.Test;

import us.codecraft.tinyioc.aop.AdvisedSupport;
import us.codecraft.tinyioc.aop.Cglib2AopProxy;
import us.codecraft.tinyioc.aop.TargetSource;
import us.codecraft.tinyioc.context.ApplicationContext;
import us.codecraft.tinyioc.context.ClassPathXmlApplicationContext;
import us.test.codecraft.tinyioc.HelloWorldService;
import us.test.codecraft.tinyioc.HelloWorldServiceImpl;
import us.test.codecraft.tinyioc.OutputService;
import us.test.codecraft.tinyioc.OutputServiceImpl;

/**
 * @author yihua.huang@dianping.com
 */
public class Cglib2AopProxyTest {
    public static void main(String[] args){
            Cglib2AopProxyTest cglib2AopProxyTest = new Cglib2AopProxyTest();
            try {
                cglib2AopProxyTest.testInterceptor();
            } catch (Exception e) {
                
                // TODO Auto-generated catch block
                e.printStackTrace();
                
            }
    }
	@Test
	public void testInterceptor() throws Exception {
		// --------- helloWorldService without AOP
//		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("tinyioc.xml");
//		HelloWorldService helloWorldService = (HelloWorldService) applicationContext.getBean("helloWorldService");
//		helloWorldService.helloWorld();
	    OutputService outputService = new OutputServiceImpl();
	    HelloWorldService helloWorldService = new HelloWorldServiceImpl("Hello World!",outputService);
	    helloWorldService.helloWorld();

		// --------- helloWorldService with AOP
		// 1. 设置被代理对象(Joinpoint)
		AdvisedSupport advisedSupport = new AdvisedSupport();
		TargetSource targetSource = new TargetSource(helloWorldService, HelloWorldServiceImpl.class,
				HelloWorldService.class);
		advisedSupport.setTargetSource(targetSource);

		// 2. 设置拦截器(Advice)
		TimerInterceptor timerInterceptor = new TimerInterceptor();
		advisedSupport.setMethodInterceptor(timerInterceptor);

		// 3. 创建代理(Proxy)
        Cglib2AopProxy cglib2AopProxy = new Cglib2AopProxy(advisedSupport);
		HelloWorldService helloWorldServiceProxy = (HelloWorldService) cglib2AopProxy.getProxy();

		// 4. 基于AOP的调用
		helloWorldServiceProxy.helloWorld();

	}
}
