package org.apache.commons.vfs2;

import java.util.Iterator;

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

/**
 * Tests {@link FileObject}s with iterators its implementation the {@link Iterable} interface to allow a FileObject to be the target of the
 * Java 5 "foreach" statement.
 */
public class FileIteratorTest
{

    private static FileObject BaseFolder;

    private final static int FileCount = 14;

    /**
     * Creates a RAM FS.
     * 
     * @throws Exception
     */
    @BeforeClass
    public static void setUpClass() throws Exception
    {
        BaseFolder = VFS.getManager().resolveFile("ram://" + FileIteratorTest.class.getName());
        BaseFolder.deleteAll();
        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();
        BaseFolder.resolveFile("subdir1").createFolder();
        BaseFolder.resolveFile("subdir1/subfile1.txt").createFile();
        BaseFolder.resolveFile("subdir2").createFolder();
        BaseFolder.resolveFile("subdir2/subfile1.txt").createFile();
    }

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

    @Test
    public void testIterator() throws FileSystemException
    {
        final FileObject[] findFiles = BaseFolder.findFiles(Selectors.SELECT_ALL);
        Assert.assertEquals(FileCount, findFiles.length);
        final FileObject[] listFiles = BaseFolder.getChildren();
        Assert.assertTrue(FileCount > listFiles.length);
        int i = 0;
        for (FileObject actualFile : BaseFolder)
        {
            FileObject expectedFile = findFiles[i];
            Assert.assertEquals(expectedFile, actualFile);
            i++;
        }
        Iterator<FileObject> iter = BaseFolder.iterator();
        i = 0;
        while (iter.hasNext())
        {
            FileObject expectedFile = findFiles[i];
            Assert.assertEquals(expectedFile, iter.next());
            i++;
        }
    }
}

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

Add missing Apache license header.