/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.framework;

import junit.framework.TestCase;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ComponentRegistryTest extends TestCase {
    Registry componentRegistry;

    public void setUp() throws Exception {
        super.setUp();
        componentRegistry = Registry.getInstance();
    }

    public void tearDown() throws Exception {
        super.setUp();
    }

    public void testRegisterComponentByInstance() throws Exception {
        Component test = new TestComponentA();
        ComponentMeta meta = componentRegistry.registerComponent(test);
        System.out.println("ComponentMeta: " + meta);
        System.out.println("ComponentName: " + meta.getComponentName());
    }

    public void testRegisterComponentByParameterConstructor() throws Exception {
        ComponentMeta meta = componentRegistry.registerComponent(TestComponentA.class,
                new Parameter[]{new InstanceParameter("Hello,Everyone!", String.class)},
                null);
        System.out.println("ComponentMeta: " + meta);
        System.out.println("ComponentName: " + meta.getComponentName());

        TestComponent test = (TestComponent) componentRegistry.getComponentInstance(meta.getComponentName());

        System.out.println("ComponentMeta: " + meta);
        assertEquals(test.getName(), "Hello,Everyone!");
    }

    public void testRegisterComponentByParameterMethod() throws Exception {

        ComponentMeta meta = componentRegistry.registerComponent(TestComponentA.class,
                new Parameter[0], // can't simply use null here, use null will auto detect constructor
                new ParameterPack[]{
                    new ParameterPack("name",
                            new InstanceParameter("Hello,Everyone!"))
                });
        System.out.println("ComponentMeta: " + meta);
        System.out.println("ComponentName: " + meta.getComponentName());

        TestComponent test = (TestComponent) componentRegistry.getComponentInstance(meta.getComponentName());

        System.out.println("ComponentMeta: " + meta);
        assertEquals(test.getName(), "Hello,Everyone!");
    }

    public void testCount() throws Exception {
        ComponentMeta meta1 = ComponentMetaFactory.getComponentMeta(TestComponentA.class, new Parameter[0], null, false, true);
        ComponentMeta meta2 = ComponentMetaFactory.getComponentMeta(TestComponentA.class, new Parameter[0], null, false, true);
        ComponentMeta meta3 = ComponentMetaFactory.getComponentMeta(TestComponentA.class, new Parameter[0], null, false, true);
        ComponentMeta meta4 = ComponentMetaFactory.getComponentMeta(TestComponentA.class, new Parameter[0], null, false, true);
        ComponentMeta meta5 = ComponentMetaFactory.getComponentMeta(TestComponentA.class, new Parameter[0], null, false, true);
        componentRegistry.registerComponent(meta1);
        componentRegistry.registerComponent(meta2);
        componentRegistry.registerComponent(meta3);
        componentRegistry.registerComponent(meta4);
        componentRegistry.registerComponent(meta5);

        assertEquals(componentRegistry.countComponents(), 5);
        assertEquals(componentRegistry.countComponentTypes(), 1);
        componentRegistry.unregisterComponent(meta1.getComponentName());
        componentRegistry.unregisterComponent(meta2.getComponentName());
        assertEquals(componentRegistry.countComponents(), 3);
        assertEquals(componentRegistry.countComponentTypes(), 1);
        componentRegistry.unregisterComponent(meta3.getComponentName());
        componentRegistry.unregisterComponent(meta4.getComponentName());
        componentRegistry.unregisterComponent(meta5.getComponentName());
        assertEquals(componentRegistry.countComponents(), 0);
        assertEquals(componentRegistry.countComponentTypes(), 0);

    }

    public void testAutoDetectConstructor() throws Exception {
        ComponentMeta meta = componentRegistry.registerComponent(TestComponentA.class,
                new Parameter[]{new InstanceParameter("Hello,Everyone!", String.class)},
                null);
        System.out.println(componentRegistry.getComponentInstance(meta.getComponentName()));
        ComponentMeta metaB = componentRegistry.registerComponent(TestComponentB.class,
                null,
                null);
        TestComponentB tcb = (TestComponentB) componentRegistry.getComponentInstance(metaB.getComponentName());
        System.out.println(tcb.getTestComponent());
        System.out.println(componentRegistry.countComponents());
    }

    public void testAutoDetectConstructorAndAutoRegister() throws Exception {
        ComponentMeta metaD = componentRegistry.registerComponent(TestComponentD.class,
                null,
                null);
        TestComponentD tcb = (TestComponentD) componentRegistry.getComponentInstance(metaD.getComponentName());
        System.out.println(tcb.getTestComponent());
        System.out.println(componentRegistry.countComponents());
    }
}