View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.test;
18  
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.vfs2.FileObject;
24  import org.apache.commons.vfs2.FileSystem;
25  import org.apache.commons.vfs2.FileSystemException;
26  import org.apache.commons.vfs2.VFS;
27  import org.junit.Assert;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  
31  /**
32   * Tests FileObject sorting.
33   *
34   * $Id$
35   */
36  public class FileObjectSortTestCase {
37  
38      /**
39       * The size of arrays to sort.
40       */
41      private static final int SIZE = 100;
42  
43      // Consider @Immutable
44      private static FileSystem VfsFileSystem;
45  
46      // Consider @Immutable
47      private static FileObject[] SortedArray;
48  
49      // Consider @Immutable
50      private static FileObject[] UnSortedArray;
51  
52      private static FileObject resolveFile(final FileSystem fs, final int i) throws FileSystemException {
53          return fs.resolveFile(String.format("%010d", i));
54      }
55  
56      @BeforeClass
57      public static void setUpClass() throws FileSystemException {
58          VfsFileSystem = VFS.getManager().createVirtualFileSystem("vfs://").getFileSystem();
59          SortedArray = new FileObject[SIZE];
60          for (int i = 0; i < SIZE; i++) {
61              SortedArray[i] = FileObjectSortTestCase.resolveFile(VfsFileSystem, i);
62          }
63          UnSortedArray = new FileObject[SIZE];
64          for (int i = 0; i < SIZE; i++) {
65              UnSortedArray[i] = FileObjectSortTestCase.resolveFile(VfsFileSystem, SIZE - i - 1);
66          }
67      }
68  
69      /**
70       * Tests that sorting ignores case.
71       *
72       * @throws FileSystemException
73       */
74      @Test
75      public void testSortArrayIgnoreCase() throws FileSystemException {
76          final FileObject file1 = VfsFileSystem.resolveFile("A1");
77          final FileObject file2 = VfsFileSystem.resolveFile("a2");
78          final FileObject file3 = VfsFileSystem.resolveFile("A3");
79          final FileObject[] actualArray = { file3, file1, file2, file1, file2 };
80          final FileObject[] expectedArray = { file1, file1, file2, file2, file3 };
81          Arrays.sort(actualArray);
82          Assert.assertArrayEquals(expectedArray, actualArray);
83      }
84  
85      /**
86       * Tests sorting an array
87       *
88       * @throws FileSystemException
89       */
90      @Test
91      public void testSortArrayMoveAll() throws FileSystemException {
92          final FileObject[] actualArray = UnSortedArray.clone();
93          Assert.assertFalse(Arrays.equals(UnSortedArray, SortedArray));
94          Arrays.sort(actualArray);
95          Assert.assertArrayEquals(SortedArray, actualArray);
96      }
97  
98      /**
99       * Tests that sorting an array already in oder does not mess it up.
100      *
101      * @throws FileSystemException
102      */
103     @Test
104     public void testSortArrayMoveNone() throws FileSystemException {
105         final FileObject[] actualArray = SortedArray.clone();
106         Arrays.sort(actualArray);
107         Assert.assertArrayEquals(SortedArray, actualArray);
108     }
109 
110     /**
111      * Tests sorting a list
112      *
113      * @throws FileSystemException
114      */
115     @Test
116     public void testSortListMoveAll() throws FileSystemException {
117         final List<FileObject> actualList = Arrays.asList(UnSortedArray);
118         final List<FileObject> expectedSortedList = Arrays.asList(SortedArray);
119         Assert.assertNotEquals(actualList, expectedSortedList);
120         Collections.sort(actualList);
121         Assert.assertEquals(actualList, expectedSortedList);
122     }
123 
124     /**
125      * Tests that sorting a list already in oder does not mess it up.
126      *
127      * @throws FileSystemException
128      */
129     @Test
130     public void testSortListMoveNone() throws FileSystemException {
131         final List<FileObject> actualList = Arrays.asList(SortedArray);
132         final List<FileObject> expectedSortedList = Arrays.asList(SortedArray);
133         Collections.sort(actualList);
134         Assert.assertEquals(actualList, expectedSortedList);
135     }
136 
137 }