/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 */
package org.xwiki.component.annotation;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.xwiki.component.descriptor.ComponentDependency;
import org.xwiki.component.descriptor.ComponentDescriptor;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;

import junit.framework.TestCase;

/**
 * Unit tests for {@link ComponentAnnotationLoader}.
 * 
 * @version $Id: $
 * @since 1.8.1
 */
public class ComponentAnnotationLoaderTest extends TestCase
{
    @ComponentRole
    public interface FieldRole
    {
    }

    @Component
    public class FieldroleImpl implements FieldRole
    {
    }
    
    @Component("special")
    public class SpecialFieldRoleImpl implements FieldRole
    {
    }

    @ComponentRole
    public interface Role
    {
    }

    @ComponentRole
    public interface ExtendedRole extends Role
    {
    }

    @Component
    public class RoleImpl implements ExtendedRole
    {
        @Requirement
        private FieldRole fieldRole;
        
        @Requirement("special")
        private FieldRole specialFieldRole;
        
    }

    @Component
    public class SuperRoleImpl extends RoleImpl
    {
    }

    public void testFindComponentRoleClasses()
    {
        ComponentAnnotationLoader loader = new ComponentAnnotationLoader();
        List<Class<?>> classes = loader.findComponentRoleClasses(RoleImpl.class);
        assertEquals(2, classes.size());
        assertEquals(Role.class.getName(), classes.get(0).getName());
        assertEquals(ExtendedRole.class.getName(), classes.get(1).getName());
    }

    public void testFindComponentRoleClasseWhenClassExtension()
    {
        ComponentAnnotationLoader loader = new ComponentAnnotationLoader();
        List<Class<?>> classes = loader.findComponentRoleClasses(SuperRoleImpl.class);
        assertEquals(2, classes.size());
        assertEquals(Role.class.getName(), classes.get(0).getName());
        assertEquals(ExtendedRole.class.getName(), classes.get(1).getName());
    }

    public void testCreateComponentDescriptor()
    {
        ComponentAnnotationLoader loader = new ComponentAnnotationLoader();
        ComponentDescriptor descriptor = loader.createComponentDescriptor(RoleImpl.class, ExtendedRole.class);
        assertEquals(RoleImpl.class.getName(), descriptor.getImplementation());
        assertEquals(ExtendedRole.class.getName(), descriptor.getRole());
        assertEquals("default", descriptor.getRoleHint());
        assertEquals(ComponentInstantiationStrategy.SINGLETON, descriptor.getInstantiationStrategy());
        Collection<ComponentDependency> deps = descriptor.getComponentDependencies(); 
        assertEquals(2, deps.size());
        Iterator<ComponentDependency> it = deps.iterator();
        ComponentDependency dep = it.next(); 
        assertEquals(FieldRole.class.getName(), dep.getRole());
        assertEquals("default", dep.getRoleHint());
        dep = it.next();
        assertEquals(FieldRole.class.getName(), dep.getRole());
        assertEquals("special", dep.getRoleHint());
    }
}

@@ -2137,32 +2137,38 @@
 )%0A    %7B%0A        
+assert
 ComponentAnnotat
@@ -2164,111 +2164,266 @@
 nent
-AnnotationLoader loader = new ComponentAnnotationLoader();%0A        List%3CClass%3C?%3E%3E classes = loader.find
+RoleClasses(RoleImpl.class);%0A    %7D%0A%0A    /**%0A     * Verify that we get the same result when we use a class that extends another class (i.e. inheritance%0A     * works).%0A     */%0A    public void testFindComponentRoleClasseWhenClassExtension()%0A    %7B%0A        assert
 Comp
@@ -2431,32 +2431,37 @@
 nentRoleClasses(
+Super
 RoleImpl.class);
@@ -2469,190 +2469,390 @@
     
+%7D%0A%0A
     
-assertEquals(2, classes.size());%0A        assertEquals
+public void testCreateComponentDescriptor()%0A    %7B%0A        assertComponentDescriptor
 (Role
+Impl
 .class
-.getName(), classes.get(0).getName());%0A        assertEquals(ExtendedRole.class.getName(), classes.get(1).getName()
+);%0A    %7D%0A    %0A    /**%0A     * Verify that we get the same result when we use a class that extends another class (i.e. inheritance%0A     * works).%0A     */%0A    public void testCreateComponentDescriptorWhenClassExtension()%0A    %7B%0A        assertComponentDescriptor(SuperRoleImpl.class
 );%0A 
@@ -2860,33 +2860,36 @@
   %7D%0A
+    
 %0A    p
-ublic void testFind
+rivate void assert
 Comp
@@ -2903,35 +2903,43 @@
 leClasse
-When
+s(
 Class
-Extension(
+%3C ? %3E componentClass
 )%0A    %7B%0A
@@ -3075,39 +3075,34 @@
 RoleClasses(
-SuperRoleImpl.c
+componentC
 lass);%0A     
@@ -3295,55 +3295,81 @@
   %7D%0A
+    
 %0A    p
-ublic void testCreateComponentDescriptor(
+rivate void assertComponentDescriptor(Class%3C ? %3E componentClass
 )%0A  
@@ -3518,34 +3518,34 @@
 tDescriptor(
-RoleImpl.c
+componentC
 lass, Extend
@@ -3580,26 +3580,26 @@
 tEquals(
-RoleImpl.c
+componentC
 lass.get

Allow component mappings to be done using annotations