Author: ecki Date: Sun Jan 18 23:19:58 2015 New Revision: 1652870 URL: http://svn.apache.org/r1652870 Log: [VFS-558] make moveTo and getParent work with ON_CALL
Modified: 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/ProviderRenameTests.java commons/proper/vfs/trunk/src/changes/changes.xml 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=1652870&r1=1652869&r2=1652870&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 Sun Jan 18 23:19:58 2015 @@ -217,23 +217,20 @@ public abstract class AbstractFileObject { // TODO - this may be called when not attached - if (children != null) + if (children != null && childName != null && newType != null) { - if (childName != null && newType != null) + // TODO - figure out if children[] can be replaced by list + final ArrayList<FileName> list = new ArrayList<FileName>(Arrays.asList(children)); + if (newType.equals(FileType.IMAGINARY)) { - // TODO - figure out if children[] can be replaced by list - final ArrayList<FileName> list = new ArrayList<FileName>(Arrays.asList(children)); - if (newType.equals(FileType.IMAGINARY)) - { - list.remove(childName); - } - else - { - list.add(childName); - } - children = new FileName[list.size()]; - list.toArray(children); + list.remove(childName); } + else + { + list.add(childName); + } + children = new FileName[list.size()]; + list.toArray(children); } // removeChildrenCache(); @@ -1419,7 +1416,7 @@ public abstract class AbstractFileObject @Override public FileObject getParent() throws FileSystemException { - if (this == fs.getRoot()) + if (this.compareTo(fs.getRoot()) == 0) // equals is not implements :-/ { if (fs.getParentLayer() == null) { @@ -1438,10 +1435,15 @@ public abstract class AbstractFileObject // Locate the parent of this file if (parent == null) { - parent = fs.resolveFile(fileName.getParent()); + FileName name = fileName.getParent(); + if (name == null) + { + return null; + } + parent = fs.resolveFile(name); } + return parent; } - return parent; } /** @@ -1879,10 +1881,12 @@ public abstract class AbstractFileObject try { attach(); - doRename(destFile); + // remeber type to avoid attach + FileType srcType = getType(); - FileObjectUtils.getAbstractFileObject(destFile).handleCreate(getType()); + doRename(destFile); + FileObjectUtils.getAbstractFileObject(destFile).handleCreate(srcType); destFile.close(); // now the destFile is no longer imaginary. force reattach. handleDelete(); // fire delete-events. This file-object (src) is like deleted. Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderRenameTests.java URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderRenameTests.java?rev=1652870&r1=1652869&r2=1652870&view=diff ============================================================================== --- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderRenameTests.java (original) +++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderRenameTests.java Sun Jan 18 23:19:58 2015 @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import org.apache.commons.vfs2.CacheStrategy; import org.apache.commons.vfs2.Capability; import org.apache.commons.vfs2.FileObject; import org.apache.commons.vfs2.FileSystemException; @@ -118,6 +119,29 @@ public class ProviderRenameTests } /** + * Tests moving a file to empty folder. + * <P> + * This fails with VFS-558, but only with a CacheStrategy.ON_CALL. + */ + public void testRenameFileIntoEmptyFolder() throws Exception + { + final FileObject scratchFolder = createScratchFolder(); + + // Create direct child of the test folder + final FileObject file = scratchFolder.resolveFile("file1.txt"); + assertTrue(!file.exists()); + + final String content = createTestFile(file); + + final FileObject destFolder = scratchFolder.resolveFile("empty-target-folder"); + destFolder.createFolder(); + assertTrue("new destination must be folder", destFolder.getType().hasChildren()); + assertTrue("new destination must be emty", destFolder.getChildren().length == 0); + + moveFile(destFolder, file, content); + } + + /** * Moves a file from a child folder to a parent folder to test what happens when the original folder is now empty. * * See [VFS-298] FTP: Exception is thrown when renaming a file. Modified: commons/proper/vfs/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1652870&r1=1652869&r2=1652870&view=diff ============================================================================== --- commons/proper/vfs/trunk/src/changes/changes.xml (original) +++ commons/proper/vfs/trunk/src/changes/changes.xml Sun Jan 18 23:19:58 2015 @@ -26,6 +26,9 @@ <!-- <action issue="VFS-443" dev="ggregory" type="update" due-to="nickallen"> --> <!-- [Local] Need an easy way to convert from a FileObject to a File. --> <!-- </action> --> + <action issue="VFS-558" dev="ecki" type="fix"> + Make moveTo() and getParent() work with CacheStrategy.ON_CALL. + In case of FTP Provider it would lead otherwise to an UnsupportedOperationException. <action dev="ecki" type="fix"> [sandbox] RACRandomAccessFile is now in org.apache.commons.vfs2.util package (so sandbox has only one overlapping package). </action>