Skip to content

Commit

Permalink
Refinements
Browse files Browse the repository at this point in the history
Cleaned up FtpConnection class. Removed unnecessary try/catch
  • Loading branch information
Josh Greatrex committed May 6, 2014
1 parent 169e628 commit 6752056
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
47 changes: 22 additions & 25 deletions src/main/java/jftp/connection/FtpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,21 @@ public void changeDirectory(String directory) throws FtpException {

@Override
public String printWorkingDirectory() throws FtpException {

try {

return client.printWorkingDirectory();

} catch (IOException e) {

throw new FtpException("Unable to print the working directory", e);
}
}

@Override
public List<FtpFile> listFiles() throws FtpException {

String currentDirectory = "";

try {

currentDirectory = client.printWorkingDirectory();

return listFiles(currentDirectory);

} catch (IOException e) {

throw new FtpException(String.format(FILE_LISTING_ERROR_MESSAGE, currentDirectory), e);
}
return listFiles(printWorkingDirectory());
}

@Override
Expand All @@ -86,10 +75,18 @@ public List<FtpFile> listFiles(String remotePath) throws FtpException {

try {

FTPFile[] ftpFiles = client.listFiles(remotePath);
String originalWorkingDirectory = printWorkingDirectory();

changeDirectory(remotePath);

String newWorkingDirectory = printWorkingDirectory();

FTPFile[] ftpFiles = client.listFiles(newWorkingDirectory);

for (FTPFile file : ftpFiles)
files.add(toFtpFile(file));
files.add(toFtpFile(file, newWorkingDirectory));

changeDirectory(originalWorkingDirectory);

} catch (IOException e) {

Expand All @@ -115,17 +112,17 @@ public void download(FtpFile file, String localDirectory) throws FtpException {
ensureFileHasSuccessfullyDownloaded(hasDownloaded);

} catch (FileNotFoundException e) {

throw new FtpException(String.format(FILE_STREAM_OPEN_FAIL_MESSAGE, localDestination), e);

} catch (IOException e) {

throw new FtpException(String.format(FILE_DOWNLOAD_FAILURE_MESSAGE, file.getName()), e);
}
}

@Override
public void upload(String localFilePath, String remoteDirectory) throws FtpException {
public void upload(String localFilePath, String remoteDirectory) throws FtpException {

try {

Expand All @@ -138,10 +135,10 @@ public void upload(String localFilePath, String remoteDirectory) throws FtpExce
ensureFileHasSuccessfullyUploaded(hasUploaded);

} catch (FileNotFoundException e) {

throw new FtpException(String.format(COULD_NOT_FIND_FILE_MESSAGE, localFilePath), e);
} catch (IOException e) {

throw new FtpException("Upload may not have completed.", e);
}

Expand Down Expand Up @@ -169,11 +166,11 @@ private void ensureFileHasSuccessfullyDownloaded(boolean hasDownloaded) {
throw new FtpException("Server returned failure while downloading.");
}

private FtpFile toFtpFile(FTPFile ftpFile) throws IOException {
private FtpFile toFtpFile(FTPFile ftpFile, String filePath) throws IOException {

String name = ftpFile.getName();
long fileSize = ftpFile.getSize();
String fullPath = String.format("%s%s%s", client.printWorkingDirectory(), FILE_SEPARATOR, ftpFile.getName());
String fullPath = filePath + FILE_SEPARATOR + ftpFile.getName();
long mTime = ftpFile.getTimestamp().getTime().getTime();
boolean isDirectory = ftpFile.isDirectory();

Expand Down
26 changes: 22 additions & 4 deletions src/test/java/jftp/connection/FtpConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void setUp() throws IOException {

mockFtpClient = mock(FTPClient.class);

when(mockFtpClient.changeWorkingDirectory(DIRECTORY_PATH)).thenReturn(true);
when(mockFtpClient.changeWorkingDirectory(anyString())).thenReturn(true);
when(mockFtpClient.printWorkingDirectory()).thenReturn(DIRECTORY_PATH);
when(mockFtpClient.retrieveFile(anyString(), any(OutputStream.class))).thenReturn(true);

Expand Down Expand Up @@ -165,11 +165,12 @@ public void returnedFtpFilesShouldHaveCorrectModifiedDateTimesAgainstThem() {

@Test
public void whenListingFilesAndGivingRelativePathThenThatPathShouldBeUsedAlongsideCurrentWorkingDir() throws IOException {

ftpConnection.changeDirectory(DIRECTORY_PATH);

when(mockFtpClient.printWorkingDirectory()).thenReturn(DIRECTORY_PATH + "/relativePath");

ftpConnection.listFiles("relativePath");

verify(mockFtpClient).listFiles("relativePath");
verify(mockFtpClient).listFiles(DIRECTORY_PATH + "/relativePath");
}

@Test
Expand Down Expand Up @@ -312,6 +313,23 @@ public void ifClientThrowsExceptionWhenTryingToGetWorkingDirectoryThenCatchExcep
ftpConnection.printWorkingDirectory();
}

@Test
public void whenListingFilesOnDifferentPathTheClientShouldCDToThatPathThenCDBackWhenFinished() throws IOException {

when(mockFtpClient.printWorkingDirectory()).thenReturn("initial/directory").thenReturn("another/path");

ftpConnection.changeDirectory("initial/directory");
ftpConnection.listFiles("another/path");

InOrder inOrder = Mockito.inOrder(mockFtpClient);

inOrder.verify(mockFtpClient).printWorkingDirectory();
inOrder.verify(mockFtpClient).changeWorkingDirectory("another/path");
inOrder.verify(mockFtpClient).printWorkingDirectory();
inOrder.verify(mockFtpClient).listFiles("another/path");
inOrder.verify(mockFtpClient).changeWorkingDirectory("initial/directory");
}

private FTPFile[] createRemoteFTPFiles() {

Calendar calendar = Calendar.getInstance();
Expand Down

0 comments on commit 6752056

Please sign in to comment.