StubFtpServer_MultipleClientsIntegrationTest.java revision 848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79
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.command.AbstractStubCommandHandler;
25import org.mockftpserver.test.AbstractTest;
26import org.mockftpserver.test.IntegrationTest;
27import org.mockftpserver.test.PortTestUtil;
28
29/**
30 * StubFtpServer tests for multiple FTP clients using the Apache Jakarta Commons Net FTP client.
31 *
32 * @version $Revision$ - $Date$
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, PortTestUtil.getFtpServerControlPort());
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, PortTestUtil.getFtpServerControlPort());
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, PortTestUtil.getFtpServerControlPort());
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.setServerControlPort(PortTestUtil.getFtpServerControlPort());
103        stubFtpServer.setCommandHandler(CommandNames.PWD, new CustomPwdCommandHandler());
104        stubFtpServer.start();
105
106        ftpClient1 = new FTPClient();
107        ftpClient2 = new FTPClient();
108        ftpClient3 = new FTPClient();
109
110        ftpClient1.setDefaultTimeout(1000);
111        ftpClient2.setDefaultTimeout(1000);
112        ftpClient3.setDefaultTimeout(1000);
113    }
114
115    /**
116     * Perform cleanup after each test
117     * @see org.mockftpserver.test.AbstractTest#tearDown()
118     */
119    protected void tearDown() throws Exception {
120        super.tearDown();
121
122        LOG.info("Cleaning up...");
123        if (ftpClient1.isConnected()) {
124            ftpClient1.disconnect();
125        }
126        if (ftpClient2.isConnected()) {
127            ftpClient2.disconnect();
128        }
129        if (ftpClient3.isConnected()) {
130            ftpClient3.disconnect();
131        }
132
133        stubFtpServer.stop();
134    }
135
136}
137