Author: ggregory
Date: Wed Oct 12 01:05:59 2011
New Revision: 1182159
URL: http://svn.apache.org/viewvc?rev=1182159&view=rev
Log:
[VFS-366] Can't sort a List of FileObject's, FileObject to implement
Comparable<FileObject>
Added:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/test/FileObjectSortTestCase.java
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileObject.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DecoratedFileObject.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileObject.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileObject.java?rev=1182159&r1=1182158&r2=1182159&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileObject.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileObject.java
Wed Oct 12 01:05:59 2011
@@ -17,6 +17,8 @@
package org.apache.commons.vfs2;
import java.net.URL;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import org.apache.commons.vfs2.operations.FileOperations;
@@ -63,13 +65,15 @@ import org.apache.commons.vfs2.operation
* </ul>
* <p/>
* <p>To find files in another file system, use a {@link FileSystemManager}.
+ * <h4>Sorting Files</h4>
+ *<p>Files may be sorted using {@link Arrays#sort(Object[])} and {@link
Collections#sort(List)}.</p>
*
* @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS
team</a>
* @see FileSystemManager
* @see FileContent
* @see FileName
*/
-public interface FileObject
+public interface FileObject extends Comparable<FileObject>
{
/**
* Returns the name of this file.
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DecoratedFileObject.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DecoratedFileObject.java?rev=1182159&r1=1182158&r2=1182159&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DecoratedFileObject.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DecoratedFileObject.java
Wed Oct 12 01:05:59 2011
@@ -54,6 +54,11 @@ public class DecoratedFileObject impleme
decoratedFileObject.close();
}
+ public int compareTo(FileObject fo)
+ {
+ return decoratedFileObject.compareTo(fo);
+ }
+
public void copyFrom(FileObject srcFile, FileSelector selector) throws
FileSystemException
{
decoratedFileObject.copyFrom(srcFile, selector);
@@ -205,4 +210,5 @@ public class DecoratedFileObject impleme
{
return decoratedFileObject.getFileOperations();
}
+
}
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java?rev=1182159&r1=1182158&r2=1182159&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
Wed Oct 12 01:05:59 2011
@@ -1028,6 +1028,17 @@ public abstract class AbstractFileObject
}
/**
+ * Compares two FileObjects (ignores case)
+ */
+ public int compareTo(FileObject fo)
+ {
+ if (fo == null) {
+ return 1;
+ }
+ return this.toString().compareToIgnoreCase(fo.toString());
+ }
+
+ /**
* Copies another file to this file.
* @param file The FileObject to copy.
* @param selector The FileSelector.
Added:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/test/FileObjectSortTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/test/FileObjectSortTestCase.java?rev=1182159&view=auto
==============================================================================
---
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/test/FileObjectSortTestCase.java
(added)
+++
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/test/FileObjectSortTestCase.java
Wed Oct 12 01:05:59 2011
@@ -0,0 +1,119 @@
+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));
+ }
+
+}
Modified:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java?rev=1182159&r1=1182158&r2=1182159&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java
(original)
+++
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java
Wed Oct 12 01:05:59 2011
@@ -18,6 +18,8 @@ package org.apache.commons.vfs2.test;
import java.io.InputStream;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import org.apache.commons.vfs2.Capability;
@@ -161,6 +163,36 @@ public class ProviderReadTests
}
/**
+ * Tests that FileObjects can be sorted.
+ */
+ public void testSort() throws FileSystemException
+ {
+ final FileInfo fileInfo = buildExpectedStructure();
+ final VerifyingFileSelector selector = new
VerifyingFileSelector(fileInfo);
+
+ // Find the files
+ final FileObject[] actualFiles = getReadFolder().findFiles(selector);
+ Arrays.sort(actualFiles);
+ FileObject prevActualFile = actualFiles[0];
+ for (FileObject actualFile : actualFiles) {
+
assertTrue(prevActualFile.toString().compareTo(actualFile.toString()) <= 0);
+ prevActualFile = actualFile;
+ }
+
+ // Compare actual and expected list of files
+ final List<FileObject> expectedFiles = selector.finish();
+ Collections.sort(expectedFiles);
+ assertEquals(expectedFiles.size(), actualFiles.length);
+ final int count = expectedFiles.size();
+ for (int i = 0; i < count; i++)
+ {
+ final FileObject expected = expectedFiles.get(i);
+ final FileObject actual = actualFiles[i];
+ assertEquals(expected, actual);
+ }
+ }
+
+ /**
* Tests that folders have no content.
*/
public void testFolderContent() throws Exception