package org.apache.webbeans.test.unittests.config;

import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.webbeans.Named;
import javax.webbeans.RequestScoped;

import junit.framework.Assert;

import org.apache.webbeans.config.WebBeansScanner;
import org.apache.webbeans.test.mock.MockServletContext;
import org.apache.webbeans.test.servlet.TestContext;
import org.junit.Before;
import org.junit.Test;

/**
 * Test the {@link WebBeansScanner}.
 */
public class WebBeansScannerTest extends TestContext
{

    public WebBeansScannerTest()
    {
        super(WebBeansScannerTest.class.getName());
    }

    @Before
    public void init()
    {
        super.init();
    }

    @Test
    public void testWebBeansScanner() throws Exception
    {
        WebBeansScanner scanner = new WebBeansScanner();

        ServletContext servletContext = new MockServletContext();
        scanner.scan(servletContext);
        
        // try to re-run the scan
        scanner.scan(servletContext);

        Map<String, Set<String>> classMap = scanner.getANNOTATION_DB().getClassIndex();
        Assert.assertNotNull(classMap);
        Assert.assertFalse(classMap.isEmpty());
        Set<String> testBeanAnnotations = classMap.get(ScannerTestBean.class.getName());

        String[] expectedAnnotations = new String[] { RequestScoped.class.getName(), Named.class.getName() };

        Assert.assertTrue(equalsIgnorePosition(testBeanAnnotations.toArray(), expectedAnnotations));
    }

    /**
     * TODO please review this function and move to some place where all tests
     * can use it! See also
     * EJBInterceptComponentTest#testMultipleInterceptedComponent and other test
     * functions
     * 
     * Compare two arrays regardless of the position of the elements in the
     * arrays. 
     * The complex handling with temporary flags is necessary due to the
     * possibility of having multiple occurrences of the same element in the
     * arrays. In this case both arrays have to contain the exactly same amount
     * of those elements.
     * 
     * This is only suited for smaller arrays (e.g. count < 100) since the
     * algorithm uses a product of both arrays.
     * 
     * If one likes to use this for larger arrays, we'd have to use hashes.
     * 
     * @param arr1
     * @param arr2
     * @return
     */
    public static boolean equalsIgnorePosition(Object[] arr1, Object[] arr2)
    {
        if (arr1 == null && arr2 == null)
        {
            return true;
        }

        if (arr1 == null || arr2 == null)
        {
            return false;
        }

        if (arr1.length != arr2.length)
        {
            return false;
        }

        boolean[] found1 = new boolean[arr1.length];
        boolean[] found2 = new boolean[arr2.length];

        for (int i1 = 0; i1 < arr1.length; i1++)
        {
            Object o1 = arr1[i1];

            for (int i2 = 0; i2 < arr2.length; i2++)
            {
                Object o2 = arr2[i2];

                // if they are equal and not found already
                if (o1.equals(o2) && found2[i2] == false)
                {
                    // mark the entries in both arrays as found
                    found1[i1] = true;
                    found2[i2] = true;
                    break;
                }
            }
        }

        for (int i = 0; i < found1.length; i++)
        {
            if (!found1[i] || !found2[i])
            {
                return false;
            }
        }
        return true;
    }
}

@@ -379,32 +379,75 @@
 et.TestContext;%0A
+import org.apache.webbeans.util.ArrayUtil;%0A
 import org.junit
@@ -1466,16 +1466,26 @@
 ertTrue(
+ArrayUtil.
 equalsIg
@@ -1561,2056 +1561,7 @@
   %7D%0A
-%0A    /**%0A     * TODO please review this function and move to some place where all tests%0A     * can use it! See also%0A     * EJBInterceptComponentTest#testMultipleInterceptedComponent and other test%0A     * functions%0A     * %0A     * Compare two arrays regardless of the position of the elements in the%0A     * arrays. %0A     * The complex handling with temporary flags is necessary due to the%0A     * possibility of having multiple occurrences of the same element in the%0A     * arrays. In this case both arrays have to contain the exactly same amount%0A     * of those elements.%0A     * %0A     * This is only suited for smaller arrays (e.g. count %3C 100) since the%0A     * algorithm uses a product of both arrays.%0A     * %0A     * If one likes to use this for larger arrays, we'd have to use hashes.%0A     * %0A     * @param arr1%0A     * @param arr2%0A     * @return%0A     */%0A    public static boolean equalsIgnorePosition(Object%5B%5D arr1, Object%5B%5D arr2)%0A    %7B%0A        if (arr1 == null && arr2 == null)%0A        %7B%0A            return true;%0A        %7D%0A%0A        if (arr1 == null %7C%7C arr2 == null)%0A        %7B%0A            return false;%0A        %7D%0A%0A        if (arr1.length != arr2.length)%0A        %7B%0A            return false;%0A        %7D%0A%0A        boolean%5B%5D found1 = new boolean%5Barr1.length%5D;%0A        boolean%5B%5D found2 = new boolean%5Barr2.length%5D;%0A%0A        for (int i1 = 0; i1 %3C arr1.length; i1++)%0A        %7B%0A            Object o1 = arr1%5Bi1%5D;%0A%0A            for (int i2 = 0; i2 %3C arr2.length; i2++)%0A            %7B%0A                Object o2 = arr2%5Bi2%5D;%0A%0A                // if they are equal and not found already%0A                if (o1.equals(o2) && found2%5Bi2%5D == false)%0A                %7B%0A                    // mark the entries in both arrays as found%0A                    found1%5Bi1%5D = true;%0A                    found2%5Bi2%5D = true;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A%0A        for (int i = 0; i %3C found1.length; i++)%0A        %7B%0A            if (!found1%5Bi%5D %7C%7C !found2%5Bi%5D)%0A            %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D%0A
+ 
 %7D%0A

Update to use ArrayUtil.