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

import junit.framework.TestCase;
import net.sf.joyaop.AbstractInterceptor;
import net.sf.joyaop.Interceptor;
import net.sf.joyaop.Invocation;
import net.sf.joyaop.extension.picocontainer.PicoAspectFactory;
import net.sf.joyaop.framework.AspectFactory;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.defaults.DefaultPicoContainer;

/**
 * @author Shen Li
 */
public class PicoAspectFactoryTest extends TestCase {
    public void testInstantiateAspects() {
        MutablePicoContainer picoContainer = new DefaultPicoContainer();
        picoContainer.registerComponentImplementation(Foo.class);
        AspectFactory aspectFactory = new PicoAspectFactory(picoContainer);
        MockInterceptor mockInterceptor = (MockInterceptor) aspectFactory.newInstance(MockInterceptor.class);
        assertNotNull(mockInterceptor.getFoo());
        assertFalse(mockInterceptor == aspectFactory.newInstance(MockInterceptor.class));
        MockAbstractInterceptor mockAbstractInterceptor =
                (MockAbstractInterceptor) aspectFactory.newInstance(MockAbstractInterceptor.class);
        assertNotNull(mockAbstractInterceptor.getFoo());
        assertFalse(mockAbstractInterceptor == aspectFactory.newInstance(MockAbstractInterceptor.class));
    }

    public static abstract class BaseAspect {
        private Foo foo;

        protected BaseAspect(Foo foo) {
            this.foo = foo;
        }

        public Foo getFoo() {
            return foo;
        }
    }

    public static class MockInterceptor extends BaseAspect implements Interceptor {
        public MockInterceptor(Foo foo) {
            super(foo);
        }

        public Object intercept(Invocation invocation) throws Throwable {
            return null;
        }
    }

    public static abstract class MockAbstractInterceptor extends BaseAspect implements AbstractInterceptor {
        protected MockAbstractInterceptor(Foo foo) {
            super(foo);
        }

        public Object execute() throws Throwable {
            return null;
        }
    }

    public static class Foo {

    }
}
