From 27bdfe0fa7b4adae488fa2ca2ed8f54af559fa19 Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Mon, 29 Oct 2007 14:02:12 +0000 Subject: [PATCH] LMS-103 Method INode.moveTo() added, implemented, and tested. SVN: 2274 --- .../ch/systemsx/cisd/bds/storage/INode.java | 6 ++++ .../bds/storage/filesystem/AbstractNode.java | 23 ++++++++++++ .../bds/storage/filesystem/Directory.java | 35 +------------------ .../cisd/bds/storage/filesystem/Link.java | 6 ++++ .../bds/storage/filesystem/DirectoryTest.java | 23 ++++++++++++ .../cisd/bds/storage/filesystem/FileTest.java | 16 +++++++++ 6 files changed, 75 insertions(+), 34 deletions(-) diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/INode.java b/bds/source/java/ch/systemsx/cisd/bds/storage/INode.java index b4cc3ea9fd6..2d9621d6d8e 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/INode.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/INode.java @@ -42,4 +42,10 @@ public interface INode * </p> */ public void extractTo(final File directory); + + /** + * Moves this node and all descendants to the specified directory of the file system. + * This node will be automatically removed from its parent. + */ + public void moveTo(final File directory); } diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java index 435296f62c4..c667619d9d7 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java @@ -21,12 +21,30 @@ import java.io.File; import ch.systemsx.cisd.bds.storage.IDirectory; import ch.systemsx.cisd.bds.storage.INode; import ch.systemsx.cisd.bds.storage.StorageException; +import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; /** * @author Franz-Josef Elmer */ abstract class AbstractNode implements INode { + protected final static void moveFileToDirectory(final File source, final File directory) + throws EnvironmentFailureException + { + assert source != null; + assert directory != null && directory.isDirectory(); + final File destination = new File(directory, source.getName()); + if (destination.exists() == false) + { + final boolean successful = source.renameTo(destination); + if (successful == false) + { + throw EnvironmentFailureException.fromTemplate("Couldn't not move file '%s' to directory '%s'.", + source.getAbsolutePath(), directory.getAbsolutePath()); + } + } + } + protected final File nodeFile; AbstractNode(File file) @@ -53,6 +71,11 @@ abstract class AbstractNode implements INode return dir == null ? null : new Directory(dir); } + public void moveTo(File directory) + { + moveFileToDirectory(nodeFile, directory); + } + @Override public String toString() { diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java index c694c78cbc2..108c981c2bc 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java @@ -20,8 +20,6 @@ import java.io.IOException; import java.util.Iterator; import org.apache.commons.io.FileUtils; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; import ch.systemsx.cisd.bds.storage.IDirectory; import ch.systemsx.cisd.bds.storage.IFile; @@ -29,9 +27,6 @@ import ch.systemsx.cisd.bds.storage.ILink; import ch.systemsx.cisd.bds.storage.INode; import ch.systemsx.cisd.bds.storage.StorageException; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; -import ch.systemsx.cisd.common.logging.Log4jSimpleLogger; -import ch.systemsx.cisd.common.logging.LogCategory; -import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.utilities.FileUtilities; /** @@ -40,10 +35,6 @@ import ch.systemsx.cisd.common.utilities.FileUtilities; class Directory extends AbstractNode implements IDirectory { - private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, AbstractNode.class); - - private static final Log4jSimpleLogger errorLogger = new Log4jSimpleLogger(Level.ERROR, operationLog); - Directory(java.io.File directory) { super(directory); @@ -53,30 +44,6 @@ class Directory extends AbstractNode implements IDirectory } } - private final static void moveFileToDirectory(final java.io.File source, final java.io.File directory) - throws EnvironmentFailureException - { - assert source != null; - assert directory != null && directory.isDirectory(); - final java.io.File destination = new java.io.File(directory, source.getName()); - if (destination.exists() == false) - { - final boolean successful = source.renameTo(destination); - if (successful == false) - { - throw EnvironmentFailureException.fromTemplate("Couldn't not move file '%s' to directory '%s'.", source - .getAbsolutePath(), directory.getAbsolutePath()); - } - } else - { - if (operationLog.isInfoEnabled()) - { - operationLog.info(String.format("Destination file '%s' already exists. Will not overwrite", destination - .getAbsolutePath())); - } - } - } - // // IDirectory // @@ -203,7 +170,7 @@ class Directory extends AbstractNode implements IDirectory final java.io.File file = abstractNode.nodeFile; if (file.isDirectory()) { - if (FileUtilities.deleteRecursively(file, errorLogger) == false) + if (FileUtilities.deleteRecursively(file, null) == false) { throw EnvironmentFailureException.fromTemplate("Couldn't remove directory '%s'.", file .getAbsolutePath()); diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Link.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Link.java index 9debc53f089..ee49db08b15 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Link.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Link.java @@ -64,4 +64,10 @@ class Link implements ILink { // TODO Auto-generated method stub } + + public void moveTo(File directory) + { + // TODO Auto-generated method stub + } + } diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/DirectoryTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/DirectoryTest.java index 6e293a12b67..ef286616652 100644 --- a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/DirectoryTest.java +++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/DirectoryTest.java @@ -22,6 +22,7 @@ import static org.testng.AssertJUnit.assertNotNull; import static org.testng.AssertJUnit.assertTrue; import java.io.File; +import java.util.Iterator; import org.testng.AssertJUnit; import org.testng.annotations.Test; @@ -154,6 +155,28 @@ public class DirectoryTest extends StorageTestCase assertEquals(true, new File(TEST_DIR, "dir").exists()); } + @Test + public void testMoveTo() + { + File dir = new File(TEST_DIR, "dir"); + dir.mkdirs(); + IDirectory directory = new Directory(dir); + directory.addKeyValuePair("p1", "property 1"); + IDirectory subdir = directory.makeDirectory("subdir"); + subdir.addKeyValuePair("p2", "property 2"); + subdir.moveTo(TEST_DIR); + + Iterator<INode> iterator = directory.iterator(); + assertEquals(true, iterator.hasNext()); + INode node = iterator.next(); + assertEquals("p1", node.getName()); + assertEquals("property 1", ((IFile) node).getStringContent().trim()); + assertEquals(false, iterator.hasNext()); + File subdir2 = new File(TEST_DIR, "subdir"); + assertEquals(true, subdir2.isDirectory()); + assertEquals("property 2", FileUtilities.loadToString(new File(subdir2, "p2")).trim()); + } + @Test public void testAddFileWithCopy() { diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileTest.java index 363c64788ca..1de18aeec8e 100644 --- a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileTest.java +++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileTest.java @@ -25,6 +25,8 @@ import java.io.InputStream; import org.apache.commons.io.IOUtils; import org.testng.annotations.Test; +import ch.systemsx.cisd.bds.storage.IDirectory; +import ch.systemsx.cisd.bds.storage.IFile; import ch.systemsx.cisd.common.utilities.FileUtilities; /** @@ -57,6 +59,20 @@ public class FileTest extends StorageTestCase assertEquals("Hello\nworld!\n", FileUtilities.loadToString(new java.io.File(subdir, stringFile.getName()))); } + @Test + public void testMoveTo() + { + java.io.File dir = new java.io.File(TEST_DIR, "dir"); + dir.mkdirs(); + IDirectory directory = new Directory(dir); + IFile file = directory.addKeyValuePair("p1", "property 1"); + + file.moveTo(TEST_DIR); + + assertEquals(false, directory.iterator().hasNext()); + assertEquals("property 1", FileUtilities.loadToString(new java.io.File(TEST_DIR, "p1")).trim()); + } + @Test public void testGetInputStream() throws Exception { -- GitLab