/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.vfs2;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests FileExtensionSelector.
 * 
 * @since 2.1
 */
public class FileExtensionSelectorTest
{
    private static FileObject BaseFolder;

    private static final int FileCount = 9;

    private static final int ExtensionCount =3;

    private static final int FilesPerExtensionCount = 3;

    /**
     * Creates a RAM FS.
     * 
     * @throws Exception
     */
    @BeforeClass
    public static void setUpClass() throws Exception
    {
        BaseFolder = VFS.getManager().resolveFile("ram://" + FileExtensionSelectorTest.class.getName());
        BaseFolder.resolveFile("a.htm").createFile();
        BaseFolder.resolveFile("a.html").createFile();
        BaseFolder.resolveFile("a.xhtml").createFile();
        BaseFolder.resolveFile("b.htm").createFile();
        BaseFolder.resolveFile("b.html").createFile();
        BaseFolder.resolveFile("b.xhtml").createFile();
        BaseFolder.resolveFile("c.htm").createFile();
        BaseFolder.resolveFile("c.html").createFile();
        BaseFolder.resolveFile("c.xhtml").createFile();
    }

    /**
     * Deletes RAM FS files.
     * 
     * @throws Exception
     */
    @AfterClass
    public static void tearDownClass() throws Exception
    {
        if (BaseFolder != null)
        {
            BaseFolder.deleteAll();
        }
    }

    /**
     * Tests an empty selector.
     * 
     * @throws Exception
     */
    @Test
    public void testEmpty() throws Exception
    {
        FileSelector selector = new FileExtensionSelector();
        FileObject[] foList = BaseFolder.findFiles(selector);
        Assert.assertEquals(0, foList.length);
    }

    /**
     * Tests many extensions at once.
     * 
     * @throws Exception
     */
    @Test
    public void testManyExtensions() throws Exception
    {
        final FileObject[] foList = BaseFolder.findFiles(Selectors.SELECT_FILES);
        Assert.assertTrue(foList.length > 0);
        // gather file extensions.
        final Set<String> extensionSet = new HashSet<String>();
        for (FileObject fo : foList)
        {
            extensionSet.add(fo.getName().getExtension());
        }
        final String message = extensionSet.toString();
        Assert.assertTrue(message, extensionSet.size() > 0);
        Assert.assertEquals(message, ExtensionCount, extensionSet.size());
        // check all unique extensions
        final FileSelector selector = new FileExtensionSelector(extensionSet);
        final FileObject[] list = BaseFolder.findFiles(selector);
        Assert.assertEquals(FileCount, list.length);
    }

    /**
     * Tests a null selector.
     * 
     * @throws Exception
     */
    @Test
    public void testNullCollection() throws Exception
    {
        FileSelector selector0 = new FileExtensionSelector((Collection<String>) null);
        FileObject[] foList = BaseFolder.findFiles(selector0);
        Assert.assertEquals(0, foList.length);
    }

    /**
     * Tests a null selector.
     * 
     * @throws Exception
     */
    @Test
    public void testNullString() throws Exception
    {
        FileSelector selector0 = new FileExtensionSelector((String) null);
        FileObject[] foList = BaseFolder.findFiles(selector0);
        Assert.assertEquals(0, foList.length);
    }

    /**
     * Tests a one extension selector.
     * 
     * @throws Exception
     */
    @Test
    public void testOneExtension() throws Exception
    {
        final FileObject[] foList = BaseFolder.findFiles(Selectors.SELECT_FILES);
        Assert.assertTrue(foList.length > 0);
        // gather file extensions.
        final Set<String> extensionSet = new HashSet<String>();
        for (FileObject fo : foList)
        {
            extensionSet.add(fo.getName().getExtension());
        }
        Assert.assertEquals(extensionSet.toString(), ExtensionCount, extensionSet.size());
        // check each extension
        for (String extension : extensionSet)
        {
            final FileSelector selector = new FileExtensionSelector(extension);
            final FileObject[] list = BaseFolder.findFiles(selector);
            Assert.assertEquals(FilesPerExtensionCount, list.length);
        }
        // check each file against itself
        for (FileObject fo : foList)
        {
            final FileSelector selector = new FileExtensionSelector(fo.getName().getExtension());
            final FileObject[] list = BaseFolder.findFiles(selector);
            Assert.assertEquals(FilesPerExtensionCount, list.length);
        }
    }

}

@@ -844,24 +844,79 @@
 ns.vfs2;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0Aimport java.util.Arrays;%0D%0A
 import java.
@@ -933,16 +933,16 @@
 ction;%0D%0A
-
 import j
@@ -1341,16 +1341,17 @@
 nCount =
+ 
 3;%0D%0A%0D%0A  
@@ -1668,24 +1668,60 @@
 etName());%0D%0A
+        BaseFolder.createFolder();%0D%0A
         Base
@@ -3004,36 +3004,37 @@
  FileObject%5B%5D fo
-List
+Array
  = BaseFolder.fi
@@ -3087,36 +3087,37 @@
 rt.assertTrue(fo
-List
+Array
 .length %3E 0);%0D%0A 
@@ -3239,36 +3239,37 @@
 leObject fo : fo
-List
+Array
 )%0D%0A        %7B%0D%0A  
@@ -3368,38 +3368,134 @@
 ssage = 
-extensionSet.toString(
+String.format(%22Extensions: %25s; files: %25s%22, extensionSet.toString(),%0D%0A                Arrays.asList(foArray).toString()
 );%0D%0A    
@@ -4779,36 +4779,37 @@
  FileObject%5B%5D fo
-List
+Array
  = BaseFolder.fi
@@ -4870,20 +4870,21 @@
 tTrue(fo
-List
+Array
 .length 
@@ -5014,36 +5014,37 @@
 leObject fo : fo
-List
+Array
 )%0D%0A        %7B%0D%0A  
@@ -5124,51 +5124,188 @@
     
-Assert.assertEquals(extensionSet.toString()
+final String message = String.format(%22Extensions: %25s; files: %25s%22, extensionSet.toString(),%0D%0A                Arrays.asList(foArray).toString());%0D%0A        Assert.assertEquals(message
 , Ex
@@ -5740,20 +5740,21 @@
  fo : fo
-List
+Array
 )%0D%0A     

Expand messages to Assert calls to debug failing Gump tests.