StubFtpServer_MultipleClientsIntegrationTest.java revision 93102446a7b7c3d17888064b4e2e4e5cb534e6d0
1/* 2 * Copyright 2007 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.mockftpserver.stub; 17 18import org.apache.commons.net.ftp.FTPClient; 19import org.apache.log4j.Logger; 20import org.mockftpserver.core.command.Command; 21import org.mockftpserver.core.command.CommandNames; 22import org.mockftpserver.core.command.InvocationRecord; 23import org.mockftpserver.core.session.Session; 24import org.mockftpserver.stub.StubFtpServer; 25import org.mockftpserver.stub.command.AbstractStubCommandHandler; 26import org.mockftpserver.test.AbstractTest; 27import org.mockftpserver.test.IntegrationTest; 28 29/** 30 * StubFtpServer tests for multiple FTP clients using the Apache Jakarta Commons Net FTP client. 31 * 32 * @version $Revision: 93 $ - $Date: 2007-10-30 19:26:16 -0400 (Tue, 30 Oct 2007) $ 33 * 34 * @author Chris Mair 35 */ 36public final class StubFtpServer_MultipleClientsIntegrationTest extends AbstractTest implements 37 IntegrationTest { 38 39 private static final Logger LOG = Logger.getLogger(StubFtpServer_MultipleClientsIntegrationTest.class); 40 private static final String SERVER = "localhost"; 41 42 // Custom CommandHandler for PWD so that we can verify unique session-specific responses. 43 // Send back the hashCode for the Session as the reply text. 44 private static class CustomPwdCommandHandler extends AbstractStubCommandHandler { 45 protected void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception { 46 String replyText = quotes(Integer.toString(session.hashCode())); 47 sendReply(session, 257, null, replyText, null); 48 } 49 } 50 51 private StubFtpServer stubFtpServer; 52 private FTPClient ftpClient1; 53 private FTPClient ftpClient2; 54 private FTPClient ftpClient3; 55 56 /** 57 * Test that multiple simultaneous clients can connect and establish sessions. 58 */ 59 public void testMultipleClients() throws Exception { 60 61 // Connect from client 1 62 LOG.info("connect() to ftpClient1"); 63 ftpClient1.connect(SERVER); 64 String sessionId1 = ftpClient1.printWorkingDirectory(); 65 LOG.info("PWD(1) reply =[" + sessionId1 + "]"); 66 67 // Connect from client 2 68 LOG.info("connect() to ftpClient2"); 69 ftpClient2.connect(SERVER); 70 String sessionId2 = ftpClient2.printWorkingDirectory(); 71 LOG.info("PWD(2) reply =[" + sessionId2 + "]"); 72 73 // Connect from client 3 74 LOG.info("connect() to ftpClient3"); 75 ftpClient3.connect(SERVER); 76 String sessionId3 = ftpClient3.printWorkingDirectory(); 77 LOG.info("PWD(3) reply =[" + sessionId3 + "]"); 78 79 // Make sure all session ids are unique 80 assertNotSame("sessionId1 vs sessionId2", sessionId1, sessionId2); 81 assertNotSame("sessionId2 vs sessionId3", sessionId2, sessionId3); 82 assertNotSame("sessionId1 vs sessionId3", sessionId1, sessionId3); 83 84 // Now make sure that the replies from the existing sessions remain consistent 85 assertEquals("reply from session1", sessionId1, ftpClient1.printWorkingDirectory()); 86 assertEquals("reply from session2", sessionId2, ftpClient2.printWorkingDirectory()); 87 assertEquals("reply from session3", sessionId3, ftpClient3.printWorkingDirectory()); 88 } 89 90 // ------------------------------------------------------------------------- 91 // Test setup and tear-down 92 // ------------------------------------------------------------------------- 93 94 /** 95 * Perform initialization before each test 96 * @see org.mockftpserver.test.AbstractTest#setUp() 97 */ 98 protected void setUp() throws Exception { 99 super.setUp(); 100 101 stubFtpServer = new StubFtpServer(); 102 stubFtpServer.setCommandHandler(CommandNames.PWD, new CustomPwdCommandHandler()); 103 stubFtpServer.start(); 104 105 ftpClient1 = new FTPClient(); 106 ftpClient2 = new FTPClient(); 107 ftpClient3 = new FTPClient(); 108 109 ftpClient1.setDefaultTimeout(1000); 110 ftpClient2.setDefaultTimeout(1000); 111 ftpClient3.setDefaultTimeout(1000); 112 } 113 114 /** 115 * Perform cleanup after each test 116 * @see org.mockftpserver.test.AbstractTest#tearDown() 117 */ 118 protected void tearDown() throws Exception { 119 super.tearDown(); 120 121 LOG.info("Cleaning up..."); 122 if (ftpClient1.isConnected()) { 123 ftpClient1.disconnect(); 124 } 125 if (ftpClient2.isConnected()) { 126 ftpClient2.disconnect(); 127 } 128 if (ftpClient3.isConnected()) { 129 ftpClient3.disconnect(); 130 } 131 132 stubFtpServer.stop(); 133 } 134 135} 136