package org.apache.commons.vfs2.provider.test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.VFS;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests FileObject sorting.
 */
public class FileObjectSortTestCase {

    /**
     * The size of arrays to sort.
     */
    private static final int SIZE = 100;

    // Consider @Immutable
    private static FileSystem VfsFileSystem;

    // Consider @Immutable
    private static FileObject[] SortedArray;

    // Consider @Immutable
    private static FileObject[] UnSortedArray;

    private static FileObject resolveFile(final FileSystem fs, int i) throws FileSystemException {
        return fs.resolveFile(String.format("%010d", i));
    }

    @BeforeClass
    public static void setUpClass() throws FileSystemException {
        VfsFileSystem = VFS.getManager().createVirtualFileSystem("vfs://").getFileSystem();
        SortedArray = new FileObject[SIZE];
        for (int i = 0; i < SIZE; i++) {
            SortedArray[i] = FileObjectSortTestCase.resolveFile(VfsFileSystem, i);
        }
        UnSortedArray = new FileObject[SIZE];
        for (int i = 0; i < SIZE; i++) {
            UnSortedArray[i] = FileObjectSortTestCase.resolveFile(VfsFileSystem, SIZE - i - 1);
        }
    }

    /**
     * Tests that sorting ignores case.
     * 
     * @throws FileSystemException
     */
    @Test
    public void testSortArrayIgnoreCase() throws FileSystemException {
        final FileObject file1 = VfsFileSystem.resolveFile("A1");
        final FileObject file2 = VfsFileSystem.resolveFile("a2");
        final FileObject file3 = VfsFileSystem.resolveFile("A3");
        FileObject[] actualArray = { file3, file1, file2, file1, file2 };
        FileObject[] expectedArray = { file1, file1, file2, file2, file3 };
        Arrays.sort(actualArray);
        Assert.assertArrayEquals(expectedArray, actualArray);
    }

    /**
     * Tests sorting an array
     * 
     * @throws FileSystemException
     */
    @Test
    public void testSortArrayMoveAll() throws FileSystemException {
        FileObject[] actualArray = UnSortedArray.clone();
        Assert.assertFalse(Arrays.equals(UnSortedArray, SortedArray));
        Arrays.sort(actualArray);
        Assert.assertArrayEquals(SortedArray, actualArray);
    }

    /**
     * Tests that sorting an array already in oder does not mess it up.
     * 
     * @throws FileSystemException
     */
    @Test
    public void testSortArrayMoveNone() throws FileSystemException {
        FileObject[] actualArray = SortedArray.clone();
        Arrays.sort(actualArray);
        Assert.assertArrayEquals(SortedArray, actualArray);
    }

    /**
     * Tests sorting a list
     * 
     * @throws FileSystemException
     */
    @Test
    public void testSortListMoveAll() throws FileSystemException {
        List<FileObject> actualList = Arrays.asList(UnSortedArray);
        List<FileObject> expectedSortedList = Arrays.asList(SortedArray);
        Assert.assertFalse(actualList.equals(expectedSortedList));
        Collections.sort(actualList);
        Assert.assertTrue(actualList.equals(expectedSortedList));
    }

    /**
     * Tests that sorting a list already in oder does not mess it up.
     * 
     * @throws FileSystemException
     */
    @Test
    public void testSortListMoveNone() throws FileSystemException {
        List<FileObject> actualList = Arrays.asList(SortedArray);
        List<FileObject> expectedSortedList = Arrays.asList(SortedArray);
        Collections.sort(actualList);
        Assert.assertTrue(actualList.equals(expectedSortedList));
    }

}

@@ -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 
@@ -1241,16 +1241,25 @@
 rting.%0D%0A
+ * $Id$%0D%0A
  */%0D%0Apub

Add missing ASL header.