Skip to content

Commit

Permalink
Fix #1029 Handling absolute paths in LocalFileSystem (7.5) (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanbrakel authored Sep 8, 2020
1 parent cc8c93b commit ce090f1
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Integration Partners
Copyright 2019, 2020 WeAreFrank!
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,12 @@
import nl.nn.adapterframework.doc.IbisDoc;
import nl.nn.adapterframework.util.WildCardFilter;

/**
* {@link IWritableFileSystem FileSystem} representation of the local filesystem.
*
* @author Gerrit van Brakel
*
*/
public class LocalFileSystem implements IWritableFileSystem<File> {

private String root;
Expand Down Expand Up @@ -88,6 +94,9 @@ public File toFile(String filename) {
@Override
public File toFile(String folder, String filename) {
if (StringUtils.isEmpty(folder)) {
if (StringUtils.isEmpty(getRoot())) {
return new File(filename);
}
return new File(getRoot(), filename);
}
if (StringUtils.isEmpty(getRoot())) {
Expand Down Expand Up @@ -178,12 +187,12 @@ public void removeFolder(String folder) throws FileSystemException {
public File renameFile(File f, String newName, boolean force) throws FileSystemException {
File dest;

dest = new File(newName);
dest = new File(f.getParentFile(), newName);
if (dest.exists()) {
if (force)
dest.delete();
else {
throw new FileSystemException("Cannot rename file. Destination file already exists.");
throw new FileSystemException("Cannot rename file to ["+newName+"]. Destination file already exists.");
}
}
f.renameTo(dest);
Expand All @@ -194,13 +203,13 @@ public File moveFile(File f, String destinationFolder, boolean createFolder) thr
File toFolder = toFile(destinationFolder);
if (toFolder.exists()) {
if (!toFolder.isDirectory()) {
throw new FileSystemException("Cannot move file. Destination file ["+toFolder.getName()+"] is not a folder.");
throw new FileSystemException("Cannot move file. Destination file ["+toFolder.getPath()+"] is not a folder.");
}
} else {
if (createFolder)
createFolder(destinationFolder);
else {
throw new FileSystemException("Cannot move file. Destination folder ["+toFolder.getName()+"] does not exist.");
throw new FileSystemException("Cannot move file. Destination folder ["+toFolder.getPath()+"] does not exist.");
}
}
File target=new File(toFolder,f.getName());
Expand Down Expand Up @@ -247,7 +256,8 @@ public String getPhysicalDestinationName() {
}


@IbisDoc({"1", "the folder that serves as the root of this virtual filesystem", "" })
@IbisDoc({"1", "Path to the folder that serves as the root of this virtual filesystem. All specifications of folders or files are relative to this root. "+
"When the root is left unspecified, absolute paths to files and folders can be used", "" })
public void setRoot(String root) {
this.root = root;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public abstract class BasicFileSystemTest<F, FS extends IBasicFileSystem<F>> ext
/**
* Returns the file system
* @return fileSystem
* @throws ConfigurationException
*/
protected abstract FS createFileSystem();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ public abstract class FileSystemTestBase {

/**
* Creates a folder
* @param filename
* @throws Exception
*/
protected abstract void _createFolder(String foldername) throws Exception;

/**
* Deletes the folder
* @param filename
* @throws Exception
*/
protected abstract void _deleteFolder(String folderName) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public abstract class HelperedBasicFileSystemTest<F, FS extends IBasicFileSystem
/**
* Returns the file system
* @return fileSystem
* @throws ConfigurationException
*/
protected abstract IFileSystemTestHelper getFileSystemTestHelper();

Expand Down Expand Up @@ -77,8 +76,6 @@ protected InputStream _readFile(String folder, String filename) throws Exception

/**
* Creates a folder
* @param filename
* @throws Exception
*/
@Override
protected void _createFolder(String foldername) throws Exception {
Expand All @@ -87,8 +84,6 @@ protected void _createFolder(String foldername) throws Exception {

/**
* Deletes the folder
* @param filename
* @throws Exception
*/
@Override
protected void _deleteFolder(String folderName) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public abstract class HelperedFileSystemTestBase extends FileSystemTestBase {
/**
* Returns the file system
* @return fileSystem
* @throws ConfigurationException
*/
protected abstract IFileSystemTestHelper getFileSystemTestHelper();

Expand Down Expand Up @@ -78,8 +77,6 @@ protected InputStream _readFile(String folder, String filename) throws Exception

/**
* Creates a folder
* @param filename
* @throws Exception
*/
@Override
protected void _createFolder(String foldername) throws Exception {
Expand All @@ -88,8 +85,6 @@ protected void _createFolder(String foldername) throws Exception {

/**
* Deletes the folder
* @param filename
* @throws Exception
*/
@Override
protected void _deleteFolder(String folderName) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package nl.nn.adapterframework.filesystem;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;

import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import nl.nn.adapterframework.parameters.Parameter;
import nl.nn.adapterframework.parameters.ParameterList;
import nl.nn.adapterframework.parameters.ParameterValueList;
import nl.nn.adapterframework.stream.Message;

public class LocalFileSystemActorTest extends FileSystemActorTest<File, LocalFileSystem>{

public TemporaryFolder folder;


@Override
protected LocalFileSystem createFileSystem() {
LocalFileSystem result=new LocalFileSystem();
result.setRoot(folder.getRoot().getAbsolutePath());
return result;
}

@Override
public void setUp() throws Exception {
folder = new TemporaryFolder();
folder.create();
super.setUp();
}

@Override
protected IFileSystemTestHelper getFileSystemTestHelper() {
return new LocalFileSystemTestHelper(folder);
}


public void fileSystemActorMoveActionTestNoRoot(String destFolder, boolean createDestFolder) throws Exception {

LocalFileSystem localFileSystemNoRoot=new LocalFileSystem();
String srcFolder=folder.getRoot().getAbsolutePath();

String filename = "sendermove" + FILE1;
String contents = "Tekst om te lezen";

if (createDestFolder && destFolder!=null) {
_createFolder(destFolder);
}
createFile(null, filename, contents);
// deleteFile(folder2, filename);
waitForActionToFinish();

actor.setAction("move");
ParameterList params = new ParameterList();
Parameter p = new Parameter();
p.setName("destination");
p.setValue(srcFolder+"/"+destFolder);
params.add(p);
params.configure();
actor.configure(localFileSystemNoRoot,params,owner);
actor.open();

Message message = new Message(srcFolder+"/"+filename);
ParameterValueList pvl= createParameterValueList(params, message, null);
Object result = actor.doAction(message, pvl, null);

// test
// result should be name of the moved file
assertNotNull("name of moved file should not be null", result);

// TODO: result should point to new location of file
// TODO: contents of result should be contents of original file

assertTrue("file should exist in destination folder ["+destFolder+"]", _fileExists(destFolder, filename));
assertFalse("file should not exist anymore in original folder ["+srcFolder+"]", _fileExists(null, filename));
}


@Test
public void fileSystemActorMoveActionTestRootToFolderNoRoot() throws Exception {
fileSystemActorMoveActionTestNoRoot("folder",true);
}
@Test
public void fileSystemActorMoveActionTestRootToFolderFailIfolderDoesNotExistNoRoot() throws Exception {
thrown.expectMessage("unable to process [move] action for File ["+folder.getRoot().getAbsolutePath()+"/sendermovefile1.txt]: Cannot move file. Destination folder ["+folder.getRoot().getAbsolutePath()+File.separator+"folder] does not exist");
fileSystemActorMoveActionTestNoRoot("folder",false);
}
@Test
public void fileSystemActorMoveActionTestRootToFolderExistsAndAllowToCreateNoRoot() throws Exception {
fileSystemActorMoveActionTestNoRoot("folder",true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nl.nn.adapterframework.filesystem;

import java.io.File;

import org.junit.rules.TemporaryFolder;

import nl.nn.adapterframework.senders.LocalFileSystemSender;

public class LocalFileSystemSenderTest extends FileSystemSenderTest<LocalFileSystemSender, File, LocalFileSystem>{

public TemporaryFolder folder;

@Override
public LocalFileSystemSender createFileSystemSender() {
LocalFileSystemSender result=new LocalFileSystemSender();
result.setRoot(folder.getRoot().getAbsolutePath());
return result;
}

@Override
public void setUp() throws Exception {
folder = new TemporaryFolder();
folder.create();
super.setUp();
}

@Override
protected IFileSystemTestHelper getFileSystemTestHelper() {
return new LocalFileSystemTestHelper(folder);
}

}

0 comments on commit ce090f1

Please sign in to comment.