Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Added test and code to address bug of changing file names
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Osborn <osbornjd@ornl.gov>
  • Loading branch information
Joe Osborn committed Sep 5, 2019
1 parent 0e03365 commit 7f4e4f5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -93,15 +131,16 @@ 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) {
source = Paths.get(src);
destination = Paths.get(dest);
return;
}

/**
* A function that returns the source path in string form
*
Expand All @@ -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
*
Expand All @@ -121,5 +159,4 @@ public String getDestination() {
return destination.toString();
}


}
Expand Up @@ -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;

Expand Down Expand Up @@ -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.");

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

}


Expand Down

0 comments on commit 7f4e4f5

Please sign in to comment.