/************************************************
 * Copyright (c) Shen Li. All rights reserved.  *
 * http://joyaop.sourceforge.net                *
 * -------------------------------------------- *
 * Distributable under LGPL license.            *
 * See terms of license at gnu.org.             *
 ************************************************/
package test.config;

import junit.framework.TestCase;
import net.sf.joyaop.AbstractInterceptor;
import net.sf.joyaop.config.Configuration;
import net.sf.joyaop.config.XmlConfiguration;
import net.sf.joyaop.framework.*;
import net.sf.joyaop.impl.Pointcuts;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Shen Li
 */
public class ConfigTest extends TestCase {
    public void testConfig() {
        MockAspectRuntime aspectRuntime = new MockAspectRuntime();
        Configuration config = new XmlConfiguration("test/config/test-config.xml");
        config.configure(aspectRuntime);
        aspectRuntime.verify();
    }

    public static class MockAspectRuntime implements AspectRuntime {
        private final List interceptors = new ArrayList();
        private final List mixins = new ArrayList();
        private final List interfaces = new ArrayList();

        public void addInterceptor(InterceptorAspect interceptorAspect) {
            interceptors.add(interceptorAspect);
        }

        public void addMixin(MixinAspect mixinAspect) {
            mixins.add(mixinAspect);
        }

        public void addInterface(InterfaceAspect interfaceAspect) {
            interfaces.add(interfaceAspect);
        }

        public void verify() {
            assertEquals(3, interceptors.size());
            InterceptorAspect interceptorAspect1 = (InterceptorAspect) interceptors.get(0);
            InterceptorAspect interceptorAspect2 = (InterceptorAspect) interceptors.get(1);
            InterceptorAspect interceptorAspect3 = (InterceptorAspect) interceptors.get(2);
            assertEquals(MockInterceptor.class, interceptorAspect1.getAspectClass());
            assertEquals(MockInterceptor.class, interceptorAspect2.getAspectClass());
            assertEquals(MockInterceptor.class, interceptorAspect3.getAspectClass());
            assertEquals(RuntimeAspect.JVM_SCOPE, interceptorAspect1.getScope());
            assertEquals(RuntimeAspect.THREAD_SCOPE, interceptorAspect2.getScope());
            assertEquals(RuntimeAspect.JVM_SCOPE, interceptorAspect3.getScope());
            assertEquals(Pointcuts.byRegexp("aaa", null), interceptorAspect1.getPointcut());
            assertEquals(Pointcuts.byRegexp("aaa", null), interceptorAspect2.getPointcut());
            assertEquals(Pointcuts.byRegexp("aaa", null), interceptorAspect3.getPointcut());
            assertEquals(2, interceptorAspect1.getPrecedence());
            assertEquals(1, interceptorAspect2.getPrecedence());
            assertEquals(0, interceptorAspect3.getPrecedence());
            assertEquals(1, interceptorAspect1.getParameters().size());
            assertEquals("value1", interceptorAspect1.getParameters().get("param1"));

            assertEquals(2, mixins.size());
            MixinAspect mixinAspect1 = (MixinAspect) mixins.get(0);
            MixinAspect mixinAspect2 = (MixinAspect) mixins.get(1);
            assertEquals(MockMixin.class, mixinAspect1.getInterfaceClass());
            assertEquals(MockMixin.class, mixinAspect2.getInterfaceClass());
            assertEquals(MockMixinImpl.class, mixinAspect1.getAspectClass());
            assertEquals(MockMixinImpl.class, mixinAspect2.getAspectClass());
            assertEquals(RuntimeAspect.INSTANCE_SCOPE, mixinAspect1.getScope());
            assertEquals(RuntimeAspect.JVM_SCOPE, mixinAspect2.getScope());
            assertEquals(Pointcuts.byRegexp("aaa", null), mixinAspect1.getPointcut());
            assertEquals(Pointcuts.byRegexp("aaa", null), mixinAspect2.getPointcut());
            assertEquals(1, mixinAspect1.getParameters().size());
            assertEquals("value2", mixinAspect1.getParameters().get("param2"));

            assertEquals(1, interfaces.size());
            InterfaceAspect interfaceAspect = (InterfaceAspect) interfaces.get(0);
            assertEquals(Serializable.class, interfaceAspect.getAspectClass());
        }

        public AspectFactory getAspectFactory() {
            return null;
        }

        public void setAspectFactory(AspectFactory aspectFactory) {

        }

        public Object newInstance(Class clazz) {
            return null;
        }

        public Object newInstance(Class clazz, Class[] argTypes, Object[] args) {
            return null;
        }

        public Object newInstance(String className) {
            return null;
        }

        public Object newInstance(String className, Class[] argTypes, Object[] args) {
            return null;
        }
    }

    public abstract static class MockInterceptor implements AbstractInterceptor {
    }

    public static interface MockMixin {

    }

    public static class MockMixinImpl implements MockMixin {

    }
}
