chrismair | 00dc7bd | 2014-05-11 21:21:28 +0000 | [diff] [blame] | 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 | */
|
| 16 | package org.mockftpserver.stub;
|
| 17 |
|
| 18 | import org.apache.commons.net.ftp.FTPClient;
|
| 19 | import org.slf4j.Logger;
|
| 20 | import org.slf4j.LoggerFactory;
|
| 21 | import org.mockftpserver.core.command.Command;
|
| 22 | import org.mockftpserver.core.command.CommandNames;
|
| 23 | import org.mockftpserver.core.command.InvocationRecord;
|
| 24 | import org.mockftpserver.core.session.Session;
|
| 25 | import org.mockftpserver.stub.command.AbstractStubCommandHandler;
|
| 26 | import org.mockftpserver.test.AbstractTestCase;
|
| 27 | import org.mockftpserver.test.IntegrationTest;
|
| 28 | import org.mockftpserver.test.PortTestUtil;
|
| 29 |
|
| 30 | /**
|
| 31 | * StubFtpServer tests for multiple FTP clients using the Apache Jakarta Commons Net FTP client.
|
| 32 | *
|
| 33 | * @version $Revision$ - $Date$
|
| 34 | *
|
| 35 | * @author Chris Mair
|
| 36 | */
|
| 37 | public final class StubFtpServer_MultipleClientsIntegrationTest extends AbstractTestCase implements
|
| 38 | IntegrationTest {
|
| 39 |
|
| 40 | private static final Logger LOG = LoggerFactory.getLogger(StubFtpServer_MultipleClientsIntegrationTest.class);
|
| 41 | private static final String SERVER = "localhost";
|
| 42 |
|
| 43 | // Custom CommandHandler for PWD so that we can verify unique session-specific responses.
|
| 44 | // Send back the hashCode for the Session as the reply text.
|
| 45 | private static class CustomPwdCommandHandler extends AbstractStubCommandHandler {
|
| 46 | protected void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
|
| 47 | String replyText = quotes(Integer.toString(session.hashCode()));
|
| 48 | sendReply(session, 257, null, replyText, null);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | private StubFtpServer stubFtpServer;
|
| 53 | private FTPClient ftpClient1;
|
| 54 | private FTPClient ftpClient2;
|
| 55 | private FTPClient ftpClient3;
|
| 56 |
|
| 57 | /**
|
| 58 | * Test that multiple simultaneous clients can connect and establish sessions.
|
| 59 | */
|
| 60 | public void testMultipleClients() throws Exception {
|
| 61 |
|
| 62 | // Connect from client 1
|
| 63 | LOG.info("connect() to ftpClient1");
|
| 64 | ftpClient1.connect(SERVER, PortTestUtil.getFtpServerControlPort());
|
| 65 | String sessionId1 = ftpClient1.printWorkingDirectory();
|
| 66 | LOG.info("PWD(1) reply =[" + sessionId1 + "]");
|
| 67 |
|
| 68 | // Connect from client 2
|
| 69 | LOG.info("connect() to ftpClient2");
|
| 70 | ftpClient2.connect(SERVER, PortTestUtil.getFtpServerControlPort());
|
| 71 | String sessionId2 = ftpClient2.printWorkingDirectory();
|
| 72 | LOG.info("PWD(2) reply =[" + sessionId2 + "]");
|
| 73 |
|
| 74 | // Connect from client 3
|
| 75 | LOG.info("connect() to ftpClient3");
|
| 76 | ftpClient3.connect(SERVER, PortTestUtil.getFtpServerControlPort());
|
| 77 | String sessionId3 = ftpClient3.printWorkingDirectory();
|
| 78 | LOG.info("PWD(3) reply =[" + sessionId3 + "]");
|
| 79 |
|
| 80 | // Make sure all session ids are unique
|
| 81 | assertNotSame("sessionId1 vs sessionId2", sessionId1, sessionId2);
|
| 82 | assertNotSame("sessionId2 vs sessionId3", sessionId2, sessionId3);
|
| 83 | assertNotSame("sessionId1 vs sessionId3", sessionId1, sessionId3);
|
| 84 |
|
| 85 | // Now make sure that the replies from the existing sessions remain consistent
|
| 86 | assertEquals("reply from session1", sessionId1, ftpClient1.printWorkingDirectory());
|
| 87 | assertEquals("reply from session2", sessionId2, ftpClient2.printWorkingDirectory());
|
| 88 | assertEquals("reply from session3", sessionId3, ftpClient3.printWorkingDirectory());
|
| 89 | }
|
| 90 |
|
| 91 | // -------------------------------------------------------------------------
|
| 92 | // Test setup and tear-down
|
| 93 | // -------------------------------------------------------------------------
|
| 94 |
|
| 95 | /**
|
| 96 | * Perform initialization before each test
|
| 97 | * @see org.mockftpserver.test.AbstractTestCase#setUp()
|
| 98 | */
|
| 99 | protected void setUp() throws Exception {
|
| 100 | super.setUp();
|
| 101 |
|
| 102 | stubFtpServer = new StubFtpServer();
|
| 103 | stubFtpServer.setServerControlPort(PortTestUtil.getFtpServerControlPort());
|
| 104 | stubFtpServer.setCommandHandler(CommandNames.PWD, new CustomPwdCommandHandler());
|
| 105 | stubFtpServer.start();
|
| 106 |
|
| 107 | ftpClient1 = new FTPClient();
|
| 108 | ftpClient2 = new FTPClient();
|
| 109 | ftpClient3 = new FTPClient();
|
| 110 |
|
| 111 | ftpClient1.setDefaultTimeout(1000);
|
| 112 | ftpClient2.setDefaultTimeout(1000);
|
| 113 | ftpClient3.setDefaultTimeout(1000);
|
| 114 | }
|
| 115 |
|
| 116 | /**
|
| 117 | * Perform cleanup after each test
|
| 118 | * @see org.mockftpserver.test.AbstractTestCase#tearDown()
|
| 119 | */
|
| 120 | protected void tearDown() throws Exception {
|
| 121 | super.tearDown();
|
| 122 |
|
| 123 | LOG.info("Cleaning up...");
|
| 124 | if (ftpClient1.isConnected()) {
|
| 125 | ftpClient1.disconnect();
|
| 126 | }
|
| 127 | if (ftpClient2.isConnected()) {
|
| 128 | ftpClient2.disconnect();
|
| 129 | }
|
| 130 | if (ftpClient3.isConnected()) {
|
| 131 | ftpClient3.disconnect();
|
| 132 | }
|
| 133 |
|
| 134 | stubFtpServer.stop();
|
| 135 | }
|
| 136 |
|
| 137 | }
|