Index: FTPTest.java =================================================================== RCS file: FTPTest.java diff -N FTPTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ FTPTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,2216 @@ +/* + * Copyright 2001-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.net.ftp; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; +import org.apache.commons.net.MalformedServerReplyException; +import org.apache.commons.net.SocketClient; +import junit.framework.TestCase; + + +/** + * Unit tests for the FTP class. + * + * @author Joseph Hindsley + */ +public class FTPTest extends TestCase { + + public static void main(String[] args) + { + junit.textui.TestRunner.run(FTPTest.class); + } + + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception + { + super.setUp(); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception + { + super.tearDown(); + } + + // Tests follow + + public void testFTP() + { + try { + final FTP ftp = new FTP(); + + assertEquals("DefaultPort doesn't match.", FTP.DEFAULT_PORT, ftp.getDefaultPort()); + assertEquals("Number of ReplyStrings doesn't match.", 0, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", null, ftp.getReplyString()); + assertEquals("ReplyCode doesn't match.", 0, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", false, ftp.isConnected()); + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + public void testConnect() + { + try { + final FTP ftp = new FTP(); + + // + // Calling _connectAction_() before connecting should fail: + // + try { + ftp._connectAction_(); + fail("Should have caught a NullPointerException."); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + + // + // Create a connection - this calls _connectAction_() under the covers. + // Setting a bad connection return code should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + ftpServer.setConnectResponse("421 Connection closed."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + fail("Should have caught an FTPConnectionClosedException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + + } catch (FTPConnectionClosedException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught an FTPConnectionClosedException: " + e); + } + + + // + // Create a connection - this calls _connectAction_() under the covers. + // Setting a BOGUS connection string should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + ftpServer.setConnectResponse("Something bogus this way comes."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + fail("Should have caught an MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught an MalformedServerReplyException: " + e); + } + + + // + // Create a connection - this calls _connectAction_() under the covers. + // Setting a valid preliminary response only should cause a timeout: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + ftpServer.setConnectResponse("120 Dummy FTP Server preliminary."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + // Give the server 5 seconds to reply: + ftp.setDefaultTimeout(5000); + ftp.connect("localhost", 5432); + fail("Should have caught an SocketTimeoutException."); + } finally { + // Set defaultTimeout back to 0: + ftp.setDefaultTimeout(0); + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + + } catch (SocketTimeoutException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught an SocketTimeoutException: " + e); + } + + + // + // Create a connection - this calls _connectAction_() under the covers. + // Causing the ftp server to not respond at all should have client to timeout: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + ftpServer.setConnectResponse("120 Dummy FTP Server preliminary.\r\n220 Dummy FTP Server connected."); + ftpServer.setSendCmdResponse(false); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + // Give the server 5 seconds to reply: + ftp.setDefaultTimeout(5000); + ftp.connect("localhost", 5432); + fail("Should have caught an SocketTimeoutException."); + } finally { + // Set defaultTimeout back to 0: + ftp.setDefaultTimeout(0); + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + + } catch (SocketTimeoutException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught an SocketTimeoutException: " + e); + } + + + // + // Create a connection - this calls _connectAction_() under the covers. + // Setting a valid preliminary and final connection return code should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + ftpServer.setConnectResponse("120 Dummy FTP Server preliminary.\r\n220 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + assertEquals("DefaultPort doesn't match.", FTP.DEFAULT_PORT, ftp.getDefaultPort()); + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", "220 Dummy FTP Server connected.\r\n", ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 220, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + public void testDisconnect() + { + try { + final FTP ftp = new FTP(); + + // + // Calling disconnect() before connecting should fail: + // + try { + ftp.disconnect(); + fail("Should have caught a NullPointerException."); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + // + // Create a connection - calling disconnect should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.disconnect(); + + assertEquals("DefaultPort doesn't match.", FTP.DEFAULT_PORT, ftp.getDefaultPort()); + assertEquals("Number of ReplyStrings doesn't match.", 0, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", null, ftp.getReplyString()); + // TODO - should reply code be set to 0 when disconnecting? + assertEquals("ReplyCode doesn't match.", 220, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", false, ftp.isConnected()); + // getSoTimeout will throw a NullPointerException if called when there is no socket + //assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + public void testSendCommandString() + { + try { + final FTP ftp = new FTP(); + + // + // Calling sendCommand() before connecting should fail: + // + try { + ftp.sendCommand("some command"); + fail("Should have caught a NullPointerException."); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a 421 response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "421 Connection closed."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command"); + fail("Should have caught a FTPConnectionClosedException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (FTPConnectionClosedException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a FTPConnectionClosedException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a BOGUS response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "Something BOGUS this way comes."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command"); + fail("Should have caught a MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a MalformedServerReplyException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // NO response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setSendCmdResponse(false); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + // Give the server 5 seconds to reply: + ftp.setDefaultTimeout(5000); + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command"); + fail("Should have caught a SocketTimeoutException."); + } finally { + // Set timeout back to 0: + ftp.setDefaultTimeout(0); + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (SocketTimeoutException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a SocketTimeoutException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // an INCOMPLETE response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "20"; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command"); + fail("Should have caught a MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a MalformedServerReplyException: " + e); + } + + + // + // Create a connection - calling sendCommand() with null command and + // server returning a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(null); + + // TODO - this is probably not the behavior we want...Does the code need to + // updated to reject null and blank command strings by throwing an + // IllegalArgumentException or FTPIllegalCommandException? + assertEquals("LastClientRequest doesn't match.", "null", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command"); + + assertEquals("LastClientRequest doesn't match.", "some command", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid Multi-lined response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200-Some valid response line 1.\r\n Line 2\r\n201 Last Line"; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command"); + + assertEquals("LastClientRequest doesn't match.", "some command", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 3, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + + public void testSendCommandStringString() + { + try { + final FTP ftp = new FTP(); + + // + // Calling sendCommand() before connecting should fail: + // + try { + ftp.sendCommand("some command", "some param"); + fail("Should have caught a NullPointerException."); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a 421 response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "421 Connection closed."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command", "some param"); + fail("Should have caught a FTPConnectionClosedException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (FTPConnectionClosedException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a FTPConnectionClosedException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a BOGUS response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "Something BOGUS this way comes."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command", "some param"); + fail("Should have caught a MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a MalformedServerReplyException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // NO response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setSendCmdResponse(false); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + // Give the server 5 seconds to reply: + ftp.setDefaultTimeout(5000); + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command", "some param"); + fail("Should have caught a SocketTimeoutException."); + } finally { + // Set timeout back to 0: + ftp.setDefaultTimeout(0); + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (SocketTimeoutException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a SocketTimeoutException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // an INCOMPLETE response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "20"; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command", "some param"); + fail("Should have caught a MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a MalformedServerReplyException: " + e); + } + + + // + // Create a connection - calling sendCommand() with null command and param and + // server returning a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(null, null); + + // TODO - this is probably not the behavior we want...Does the code need to + // updated to reject null and blank command strings by throwing an + // IllegalArgumentException or FTPIllegalCommandException? + assertEquals("LastClientRequest doesn't match.", "null", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with null command and + // server returning a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(null, "some param"); + + // TODO - this is probably not the behavior we want...Does the code need to + // updated to reject null and blank command strings by throwing an + // IllegalArgumentException or FTPIllegalCommandException? + assertEquals("LastClientRequest doesn't match.", "null some param", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with null param and + // server returning a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command", null); + + assertEquals("LastClientRequest doesn't match.", "some command", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command", "some param"); + + assertEquals("LastClientRequest doesn't match.", "some command some param", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid Multi-lined response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200-Some valid response line 1.\r\n Line 2\r\n201 Last Line"; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand("some command", "some param"); + + assertEquals("LastClientRequest doesn't match.", "some command some param", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 3, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + + public void testSendCommandint() + { + try { + final FTP ftp = new FTP(); + + // + // Calling sendCommand() before connecting should fail: + // + try { + ftp.sendCommand(FTPCommand.NOOP); + fail("Should have caught a NullPointerException."); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + + // + // Create a connection - calling sendCommand() with command not in FTPCommand list and + // server returning a valid response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(5000); + fail("Should have caught a ArrayIndexOutOfBoundsException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (ArrayIndexOutOfBoundsException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a ArrayIndexOutOfBoundsException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a 421 response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "421 Connection closed."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP); + fail("Should have caught a FTPConnectionClosedException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (FTPConnectionClosedException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a FTPConnectionClosedException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a BOGUS response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "Something BOGUS this way comes."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP); + fail("Should have caught a MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a MalformedServerReplyException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // NO response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setSendCmdResponse(false); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + // Give the server 5 seconds to reply: + ftp.setDefaultTimeout(5000); + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP); + fail("Should have caught a SocketTimeoutException."); + } finally { + // Set timeout back to 0: + ftp.setDefaultTimeout(0); + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (SocketTimeoutException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a SocketTimeoutException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // an INCOMPLETE response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "20"; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP); + fail("Should have caught a MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a MalformedServerReplyException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP); + + assertEquals("LastClientRequest doesn't match.", "NOOP", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid Multi-lined response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200-Some valid response line 1.\r\n Line 2\r\n201 Last Line"; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP); + + assertEquals("LastClientRequest doesn't match.", "NOOP", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 3, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + public void testSendCommandintString() + { + try { + final FTP ftp = new FTP(); + + // + // Calling sendCommand() before connecting should fail: + // + try { + ftp.sendCommand(FTPCommand.NOOP, "some param"); + fail("Should have caught a NullPointerException."); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + + // + // Create a connection - calling sendCommand() with command not in FTPCommand list and + // server returning a valid response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(5000, "some param"); + fail("Should have caught a ArrayIndexOutOfBoundsException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (ArrayIndexOutOfBoundsException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a ArrayIndexOutOfBoundsException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a 421 response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "421 Connection closed."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP, "some param"); + fail("Should have caught a FTPConnectionClosedException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (FTPConnectionClosedException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a FTPConnectionClosedException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a BOGUS response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "Something BOGUS this way comes."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP, "some param"); + fail("Should have caught a MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a MalformedServerReplyException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // NO response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setSendCmdResponse(false); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + // Give the server 5 seconds to reply: + ftp.setDefaultTimeout(5000); + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP, "some param"); + fail("Should have caught a SocketTimeoutException."); + } finally { + // Set timeout back to 0: + ftp.setDefaultTimeout(0); + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (SocketTimeoutException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a SocketTimeoutException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // an INCOMPLETE response should fail: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "20"; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP, "some param"); + fail("Should have caught a MalformedServerReplyException."); + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (MalformedServerReplyException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a MalformedServerReplyException: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP, null); + + assertEquals("LastClientRequest doesn't match.", "NOOP", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP, "some param"); + + assertEquals("LastClientRequest doesn't match.", "NOOP some param", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 1, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling sendCommand() with server returning + // a valid Multi-lined response should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200-Some valid response line 1.\r\n Line 2\r\n201 Last Line"; + ftpServer.setNextServerResponse(serverResponse); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.sendCommand(FTPCommand.NOOP, "some param"); + + assertEquals("LastClientRequest doesn't match.", "NOOP some param", ftpServer.getLastClientRequest()); + + assertEquals("Number of ReplyStrings doesn't match.", 3, ftp.getReplyStrings().length); + assertEquals("NewReplyString doesn't match.", true, ftp._newReplyString); + assertEquals("ReplyString doesn't match.", serverResponse + SocketClient.NETASCII_EOL, ftp.getReplyString()); + assertEquals("NewReplyString doesn't match.", false, ftp._newReplyString); + assertEquals("ReplyCode doesn't match.", 200, ftp.getReplyCode()); + assertEquals("DefaultTimeout doesn't match.", 0, ftp.getDefaultTimeout()); + assertEquals("IsConnected doesn't match.", true, ftp.isConnected()); + assertEquals("SoTimeout doesn't match.", ftp.getDefaultTimeout(), ftp.getSoTimeout()); + + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + public void testGetReplyCode() + { + try { + final FTP ftp = new FTP(); + + // + // Calling getReplyCode() before connecting pass: + // + try { + final int rc = ftp.getReplyCode(); + + assertEquals("ReplyCode doesn't match.", 0, rc); + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling getReplyCode() should return last reply code: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + final int rc = ftp.getReplyCode(); + + assertEquals("LastClientRequest doesn't match.", "", ftpServer.getLastClientRequest()); + + assertEquals("ReplyCode doesn't match.", 222, rc); + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + public void testGetReply() + { + try { + final FTP ftp = new FTP(); + + // + // Calling getReply() before connecting should fail: + // + try { + final int rc = ftp.getReply(); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + + // + // Create a connection - calling getReply() without first making a request + // should timeout: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + // Give the server 5 seconds to reply: + ftp.setDefaultTimeout(5000); + ftp.connect("localhost", 5432); + + final int rc = ftp.getReply(); + fail("Should have caught a SocketTimeoutException."); + } finally { + // Set timeout back to 0: + ftp.setDefaultTimeout(0); + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (SocketTimeoutException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a SocketTimeoutException: " + e); + } + + + // + // Create a connection - calling getReply() after making a request: + // Note: I'm faking out the server response by putting 2 responses in + // in the buffer - this will allow my getReply() call to work. + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response.\r\n201 Second valid reponse."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + // Give the server 5 seconds to reply (JIC): + ftp.setDefaultTimeout(5000); + ftp.connect("localhost", 5432); + + ftp.noop(); // prime the server: + + final int rc = ftp.getReply(); + + assertEquals("LastClientRequest doesn't match.", "NOOP", ftpServer.getLastClientRequest()); + + assertEquals("ReplyCode doesn't match.", 201, rc); + + } finally { + // Set timeout back to 0: + ftp.setDefaultTimeout(0); + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + public void testGetReplyStrings() + { + try { + final FTP ftp = new FTP(); + + // + // Calling getReplyStrings() before connecting should fail: + // + try { + final String[] rs = ftp.getReplyStrings(); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + + // + // Create a connection - calling getReplyStrings() without first making a request + // should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + final String[] rs = ftp.getReplyStrings(); + + assertEquals("LastClientRequest doesn't match.", "", ftpServer.getLastClientRequest()); + + assertEquals("Number of strings doesn't match.", 1, rs.length); + assertEquals("ReplyString doesn't match.", "222 Dummy FTP Server connected.", rs[0]); + + } finally { + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling getReplyStrings() after making a request should pass + // - server has single line response: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.noop(); + + final String[] rs = ftp.getReplyStrings(); + + assertEquals("LastClientRequest doesn't match.", "NOOP", ftpServer.getLastClientRequest()); + + assertEquals("Number of strings doesn't match.", 1, rs.length); + assertEquals("ReplyString doesn't match.", "200 Some valid response.", rs[0]); + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling getReplyStrings() after making a request should pass + // - server returns a multi line response: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200-Some valid response line 1.\r\n Line 2\r\n201 Last Line"; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.noop(); + + final String[] rs = ftp.getReplyStrings(); + + assertEquals("LastClientRequest doesn't match.", "NOOP", ftpServer.getLastClientRequest()); + + assertEquals("Number of strings doesn't match.", 3, rs.length); + assertEquals("ReplyString doesn't match.", "200-Some valid response line 1.", rs[0]); + assertEquals("ReplyString doesn't match.", " Line 2", rs[1]); + assertEquals("ReplyString doesn't match.", "201 Last Line", rs[2]); + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + public void testGetReplyString() + { + try { + final FTP ftp = new FTP(); + + // + // Calling getReplyString() before connecting should fail: + // + try { + final String rs = ftp.getReplyString(); + } catch (NullPointerException e) { + // Hizzah! + } catch (Exception e) { + fail("Should have caught a NullPointerException: " + e); + } + + + // + // Create a connection - calling getReplyString() without first making a request + // should pass: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + final String rs = ftp.getReplyString(); + + assertEquals("LastClientRequest doesn't match.", "", ftpServer.getLastClientRequest()); + + assertEquals("ReplyString doesn't match.", "222 Dummy FTP Server connected." + SocketClient.NETASCII_EOL, rs); + + } finally { + + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling getReplyString() after making a request should pass + // - server has single line response: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200 Some valid response."; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.noop(); + + final String rs = ftp.getReplyString(); + + assertEquals("LastClientRequest doesn't match.", "NOOP", ftpServer.getLastClientRequest()); + + assertEquals("ReplyString doesn't match.", "200 Some valid response." + SocketClient.NETASCII_EOL, rs); + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + // + // Create a connection - calling getReplyStrings() after making a request should pass + // - server returns a multi line response: + // + try { + final DummyFTPServer ftpServer = new DummyFTPServer(5432); + final String serverResponse = "200-Some valid response line 1.\r\n Line 2\r\n201 Last Line"; + ftpServer.setNextServerResponse(serverResponse); + ftpServer.setConnectResponse("222 Dummy FTP Server connected."); + Thread ftpServerThread = new Thread(ftpServer, "DummyFTPServer"); + ftpServerThread.start(); + + try { + ftp.connect("localhost", 5432); + + ftp.noop(); + + final String rs = ftp.getReplyString(); + + assertEquals("LastClientRequest doesn't match.", "NOOP", ftpServer.getLastClientRequest()); + + assertEquals("ReplyString doesn't match.", "200-Some valid response line 1.\r\n Line 2\r\n201 Last Line" + SocketClient.NETASCII_EOL, rs); + + } finally { + ftpServer.setStopRunning(true); + ftpServerThread.join(5000); + } + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + + + } catch (Exception e) { + fail("Should NOT have caught an Exception: " + e); + } + } + + + // TODO The following tests need to be implemented. I've left them for + // future implementation mainly because they simply wrap some variation of + // the sendCommand() method. When they get some meat to them, they may be + // worthy. + + + public void testAddProtocolCommandListener() + { + //TODO Implement addProtocolCommandListener(). + // Not sure what ProtocalCommandListener's do so left this for future implementation. + } + + public void testRemoveProtocolCommandistener() + { + //TODO Implement removeProtocolCommandistener(). + // Not sure what ProtocalCommandListener's do so left this for future implementation. + } + + public void testUser() + { + //TODO Implement user(). + } + + public void testPass() + { + //TODO Implement pass(). + } + + public void testAcct() + { + //TODO Implement acct(). + } + + public void testAbor() + { + //TODO Implement abor(). + } + + public void testCwd() + { + //TODO Implement cwd(). + } + + public void testCdup() + { + //TODO Implement cdup(). + } + + public void testQuit() + { + //TODO Implement quit(). + } + + public void testRein() + { + //TODO Implement rein(). + } + + public void testSmnt() + { + //TODO Implement smnt(). + } + + public void testPort() + { + //TODO Implement port(). + } + + public void testPasv() + { + //TODO Implement pasv(). + } + + /* + * Class under test for int type(int, int) + */ + public void testTypeintint() + { + //TODO Implement type(). + // Not sure what the reponse is supposed to look like. + } + + /* + * Class under test for int type(int) + */ + public void testTypeint() + { + //TODO Implement type(). + // Not sure what the reponse is supposed to look like. + } + + public void testStru() + { + //TODO Implement stru(). + } + + public void testMode() + { + //TODO Implement mode(). + } + + public void testRetr() + { + //TODO Implement retr(). + } + + /* + * Class under test for int stou() + */ + public void testStou() + { + //TODO Implement stou(). + } + + /* + * Class under test for int stou(String) + */ + public void testStouString() + { + //TODO Implement stou(). + } + + public void testAppe() + { + //TODO Implement appe(). + } + + /* + * Class under test for int allo(int) + */ + public void testAlloint() + { + //TODO Implement allo(). + } + + /* + * Class under test for int allo(int, int) + */ + public void testAllointint() + { + //TODO Implement allo(). + } + + public void testRest() + { + //TODO Implement rest(). + } + + public void testRnfr() + { + //TODO Implement rnfr(). + } + + public void testRnto() + { + //TODO Implement rnto(). + } + + public void testDele() + { + //TODO Implement dele(). + } + + public void testRmd() + { + //TODO Implement rmd(). + } + + public void testMkd() + { + //TODO Implement mkd(). + } + + public void testPwd() + { + //TODO Implement pwd(). + } + + /* + * Class under test for int list() + */ + public void testList() + { + //TODO Implement list(). + } + + /* + * Class under test for int list(String) + */ + public void testListString() + { + //TODO Implement list(). + } + + /* + * Class under test for int nlst() + */ + public void testNlst() + { + //TODO Implement nlst(). + } + + /* + * Class under test for int nlst(String) + */ + public void testNlstString() + { + //TODO Implement nlst(). + } + + public void testSite() + { + //TODO Implement site(). + } + + public void testSyst() + { + //TODO Implement syst(). + } + + /* + * Class under test for int stat() + */ + public void testStat() + { + //TODO Implement stat(). + } + + /* + * Class under test for int stat(String) + */ + public void testStatString() + { + //TODO Implement stat(). + } + + /* + * Class under test for int help() + */ + public void testHelp() + { + //TODO Implement help(). + } + + /* + * Class under test for int help(String) + */ + public void testHelpString() + { + //TODO Implement help(). + } + + public void testNoop() + { + //TODO Implement noop(). + } + + + /** + * @author Joseph Hindsley + * + * Inner class to create a fake FTP Server to test against. This class + * is written to allow better control over the reponses to client requests. + */ + private class DummyFTPServer implements Runnable { + + private ServerSocket _serverSocket; + private Socket _cmdSocket; + private String _connectResponse = "220 Dummy FTP Server connected."; + + private String _dataHost = "localhost"; + private int _dataPort = 0; + private boolean _dataModeActive = true; + + private String _lastClientRequest = ""; + private String _nextServerResponse = ""; + + private boolean _stopRunning = false; + + private boolean _closeConnectionOnNextRequest = false; + + private long _sleepTime = 0L; + private boolean _sendCmdResponse = true; + + + /** + * + * @throws IOException + */ + public DummyFTPServer(int port) throws IOException { + _serverSocket = new ServerSocket(port); + } + + + /* (non-Javadoc) + * @see java.lang.Runnable#run() + */ + public void run() + { + // Wait for a client to connect: + try { + _cmdSocket = _serverSocket.accept(); + sendCmdResponse(this.getConnectResponse()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + while ( true ) { + + // Stop running if signaled to quit: + if (_stopRunning) { + try { + _serverSocket.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + return; + } + + // Keep looping till the socket is closed: + if (_serverSocket.isClosed()) { + return; + } + + try { + handleRequest(); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + try { + Thread.sleep(_sleepTime); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + + + /** + * Handles the client request by reading 1 line of input and writing + * the given response string as output. If _closeConnectionOnNextRequest is + * true, then it will shut down the thread without returning any value. + * @throws IOException + * @throws InterruptedException + */ + private void handleRequest() throws IOException, InterruptedException + { + final BufferedReader reader = new BufferedReader(new InputStreamReader(_cmdSocket.getInputStream())); + if (reader.ready()) { + _lastClientRequest = reader.readLine().trim(); + //_logger.debug("CMD -> '" + _lastClientRequest + "'"); + + if (_closeConnectionOnNextRequest) { + _stopRunning = true; + } else { + sendCmdResponse(_nextServerResponse); + } + + } + + } + + /** + * @throws IOException + */ + private void sendCmdResponse(final String response) throws IOException + { + if (_sendCmdResponse) { + final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(_cmdSocket.getOutputStream())); + writer.write(response + SocketClient.NETASCII_EOL); + writer.flush(); + //_logger.debug("CMD RESPONSE -> " + response); + } else { + //_logger.debug("No CMD Response sent."); + } + } + + + /** + * @return Returns the lastClientRequest. + */ + public String getLastClientRequest() + { + return this._lastClientRequest; + } + /** + * @param nextServerResponse The nextServerResponse to set. + */ + public void setNextServerResponse(String nextServerResponse) + { + this._nextServerResponse = nextServerResponse; + } + /** + * @param stopRunning The stopRunning to set. + */ + public void setStopRunning(boolean stopRunning) + { + this._stopRunning = stopRunning; + } + /** + * @param sleepTime The sleepTime to set. + */ + public void setSleepTime(long sleepTime) + { + this._sleepTime = sleepTime; + } + public void setSendCmdResponse(boolean val) + { + this._sendCmdResponse = val; + } + public void setCloseConnectionOnNextRequest(boolean closeConnectionOnNextRequest) + { + this._closeConnectionOnNextRequest = closeConnectionOnNextRequest; + } + public String getConnectResponse() + { + return this._connectResponse; + } + public void setConnectResponse(String connectResponse) + { + this._connectResponse = connectResponse; + } + } + +}