From 7f4e4f559ed62f3eb633c9b67964fc0fc07de354 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 5 Sep 2019 18:11:22 -0400 Subject: [PATCH] Added test and code to address bug of changing file names Signed-off-by: Joe Osborn --- .../ice/commands/LocalMoveFileCommand.java | 61 +++++++++++++++---- .../commands/IFileHandlerFactoryTest.java | 14 ++++- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/LocalMoveFileCommand.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/LocalMoveFileCommand.java index dbdb76f8c..a979bb7cb 100644 --- a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/LocalMoveFileCommand.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/LocalMoveFileCommand.java @@ -13,18 +13,21 @@ package org.eclipse.ice.commands; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; + /** * Child class for moving a file locally without a remote connection * * @author Joe Osborn * */ -public class LocalMoveFileCommand extends LocalCommand{ +public class LocalMoveFileCommand extends LocalCommand { /** * The path to the source file which is to be copied @@ -65,19 +68,54 @@ public CommandStatus execute() { */ @Override protected CommandStatus run() { - + // Get the directory structure to test if we are moving to a new directory // or simply changing the name of a file String[] sourceDirs = source.toString().split("/"); String[] destinationDirs = destination.toString().split("/"); + boolean different = false; + // If the number of directories is different, then the source/destination are + // definitely different + if(sourceDirs.length != destinationDirs.length) + different = true; + // Otherwise they are the same length and we can iterate over either happily + else { + for(int i = 0; i< sourceDirs.length-1; i++) { + if(!sourceDirs[i].equals(destinationDirs[i])) { + different = true; + break; + } + } + } + // If all of the directories are the same, check to see if the final entry in the path + // is either a filename or a directory by looking at its .* file extension - try { - Files.move(source, destination.resolve(source.getFileName())); - } catch (IOException e) { - e.printStackTrace(); + String[] sourceFileNames = sourceDirs[sourceDirs.length-1].split("\\."); + String[] destinationFileNames = destinationDirs[destinationDirs.length-1].split("\\."); + + // Check if the file extensions are equal to one another + boolean sameFileExt = sourceFileNames[sourceFileNames.length-1].equals(destinationFileNames[destinationFileNames.length-1]); + + // If the directory paths are the same and the files have the same file extension + // type, then we are just changing a file name + if(!different && sameFileExt) { + try { + // destinationDirs will have the desired file name in the length-1 entry + Files.move(source, source.resolveSibling(destinationDirs[destinationDirs.length-1]), REPLACE_EXISTING); + }catch (IOException e) { + e.printStackTrace(); + } } - + // All other cases the file is moving directory + else { + try { + Files.move(source, destination.resolve(source.getFileName())); + } catch (IOException e) { + e.printStackTrace(); + } + } + return CommandStatus.RUNNING; } @@ -93,7 +131,8 @@ public CommandStatus cancel() { /** * This function sets the Paths for source and destination to the given strings - * @param src - string corresponding to the source file + * + * @param src - string corresponding to the source file * @param dest - string corresponding to the destination file */ public void setConfiguration(String src, String dest) { @@ -101,7 +140,7 @@ public void setConfiguration(String src, String dest) { destination = Paths.get(dest); return; } - + /** * A function that returns the source path in string form * @@ -110,8 +149,7 @@ public void setConfiguration(String src, String dest) { public String getSource() { return source.toString(); } - - + /** * A function that returns the destination path in string form * @@ -121,5 +159,4 @@ public String getDestination() { return destination.toString(); } - } diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/IFileHandlerFactoryTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/IFileHandlerFactoryTest.java index d17911a4b..5373f52a0 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/IFileHandlerFactoryTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/IFileHandlerFactoryTest.java @@ -22,6 +22,7 @@ import org.eclipse.ice.commands.CommandStatus; import org.eclipse.ice.commands.FileHandlerFactory; import org.eclipse.ice.commands.IFileHandler; +import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -91,7 +92,7 @@ public void setUp() throws Exception { * @throws java.lang.Exception */ - //@After + @After public void tearDown() throws Exception { System.out.println("Delete temporary files/directories that were created."); @@ -143,7 +144,7 @@ public void tearDown() throws Exception { * {@link org.eclipse.ice.commands.FileHandlerFactory#getFileHandler()} * and local file copying. */ - //@Test + @Test public void testLocalFileHandlerFactoryCopyCommand() { IFileHandler handler = null; @@ -209,7 +210,7 @@ public void testLocalFileHandlerFactoryMoveCommand() { * does not exist. Tests * {@link org.eclipse.ice.commands.FileHandlerFactory#getFileHandler()} */ - //@Test + @Test public void testLocalFileHandlerFactoryDestinationNonExistant() { IFileHandler handler = null; @@ -279,6 +280,13 @@ public void testLocalFileHandlerFactoryChangeName() { } catch (IOException e) { e.printStackTrace(); } + + + // If the file was successfully created, delete it here + // Needs a special delete since the filename was created in this function + File fileToDelete = new File(localNewName); + fileToDelete.delete(); + }