177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/*
277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Copyright 2007 the original author or authors.
377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Licensed under the Apache License, Version 2.0 (the "License");
577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * you may not use this file except in compliance with the License.
677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * You may obtain a copy of the License at
777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *      http://www.apache.org/licenses/LICENSE-2.0
977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
1077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Unless required by applicable law or agreed to in writing, software
1177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * See the License for the specific language governing permissions and
1477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * limitations under the License.
1577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */
1677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpackage org.mockftpserver.stub;
1777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
1877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.apache.commons.net.ftp.FTP;
1977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.apache.commons.net.ftp.FTPClient;
2077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.apache.commons.net.ftp.FTPFile;
2177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.apache.log4j.Logger;
2277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.CommandHandler;
2377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.CommandNames;
2477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.InvocationRecord;
2577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.SimpleCompositeCommandHandler;
2677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.StaticReplyCommandHandler;
2777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.stub.command.*;
2877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.test.AbstractTest;
2977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.test.IntegrationTest;
3077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.test.PortTestUtil;
3177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
3277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport java.io.ByteArrayInputStream;
3377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport java.io.ByteArrayOutputStream;
3477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport java.io.IOException;
3577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
3677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/**
3777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Tests for StubFtpServer using the Apache Jakarta Commons Net FTP client.
3877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
3977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @author Chris Mair
4077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @version $Revision$ - $Date$
4177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */
4277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpublic final class StubFtpServerIntegrationTest extends AbstractTest implements IntegrationTest {
4377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
4477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private static final Logger LOG = Logger.getLogger(StubFtpServerIntegrationTest.class);
4577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private static final String SERVER = "localhost";
4677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private static final String USERNAME = "user123";
4777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private static final String PASSWORD = "password";
4877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private static final String FILENAME = "abc.txt";
4977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private static final String ASCII_CONTENTS = "abcdef\tghijklmnopqr";
5077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private static final byte[] BINARY_CONTENTS = new byte[256];
5177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
5277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private StubFtpServer stubFtpServer;
5377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private FTPClient ftpClient;
5477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private RetrCommandHandler retrCommandHandler;
5577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private StorCommandHandler storCommandHandler;
5677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
5777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    //-------------------------------------------------------------------------
5877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    // Tests
5977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    //-------------------------------------------------------------------------
6077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
6177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testLogin() throws Exception {
6277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Connect
6377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Conecting to " + SERVER);
6477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
6577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("connect", 220);
6677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
6777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Login
6877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String userAndPassword = USERNAME + "/" + PASSWORD;
6977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Logging in as " + userAndPassword);
7077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.login(USERNAME, PASSWORD);
7177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to login with " + userAndPassword, success);
7277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("login with " + userAndPassword, 230);
7377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
7477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("isStarted", stubFtpServer.isStarted());
7577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertFalse("isShutdown", stubFtpServer.isShutdown());
7677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
7777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Quit
7877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Quit");
7977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClient.quit();
8077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("quit", 221);
8177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
8277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
8377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testAcct() throws Exception {
8477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
8577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
8677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // ACCT
8777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        int replyCode = ftpClient.acct("123456");
8877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("acct", 230, replyCode);
8977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
9077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
9177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
9277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test the stop() method when no session has ever been started
9377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
9477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testStop_NoSessionEverStarted() throws Exception {
9577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Testing a stop() when no session has ever been started");
9677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
9777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
9877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testHelp() throws Exception {
9977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Modify HELP CommandHandler to return a predefined help message
10077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        final String HELP = "help message";
10177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        HelpCommandHandler helpCommandHandler = (HelpCommandHandler) stubFtpServer.getCommandHandler(CommandNames.HELP);
10277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        helpCommandHandler.setHelpMessage(HELP);
10377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
10477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
10577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
10677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // HELP
10777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String help = ftpClient.listHelp();
10877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Wrong response", help.indexOf(HELP) != -1);
10977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("listHelp", 214);
11077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
11177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
11277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
11377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test the LIST and SYST commands.
11477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
11577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testList() throws Exception {
11677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
11777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
11877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Set directory listing
11977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ListCommandHandler listCommandHandler = (ListCommandHandler) stubFtpServer.getCommandHandler(CommandNames.LIST);
12077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        listCommandHandler.setDirectoryListing("11-09-01 12:30PM  406348 File2350.log\n"
12177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair                + "11-01-01 1:30PM <DIR> 0 archive");
12277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
12377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // LIST
12477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        FTPFile[] files = ftpClient.listFiles();
12577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("number of files", 2, files.length);
12677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyFTPFile(files[0], FTPFile.FILE_TYPE, "File2350.log", 406348L);
12777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyFTPFile(files[1], FTPFile.DIRECTORY_TYPE, "archive", 0L);
12877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("list", 226);
12977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
13077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
13177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
13277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test the LIST, PASV and SYST commands, transferring a directory listing in passive mode
13377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
13477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testList_PassiveMode() throws Exception {
13577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
13677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
13777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClient.enterLocalPassiveMode();
13877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
13977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Set directory listing
14077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ListCommandHandler listCommandHandler = (ListCommandHandler) stubFtpServer.getCommandHandler(CommandNames.LIST);
14177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        listCommandHandler.setDirectoryListing("11-09-01 12:30PM  406348 File2350.log");
14277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
14377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // LIST
14477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        FTPFile[] files = ftpClient.listFiles();
14577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("number of files", 1, files.length);
14677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("list", 226);
14777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
14877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
14977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testNlst() throws Exception {
15077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
15177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
15277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Set directory listing
15377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        NlstCommandHandler nlstCommandHandler = (NlstCommandHandler) stubFtpServer.getCommandHandler(CommandNames.NLST);
15477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        nlstCommandHandler.setDirectoryListing("File1.txt\nfile2.data");
15577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
15677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // NLST
15777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String[] filenames = ftpClient.listNames();
15877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("number of files", 2, filenames.length);
15977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals(filenames[0], "File1.txt");
16077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals(filenames[1], "file2.data");
16177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("listNames", 226);
16277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
16377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
16477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
16577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test printing the current working directory (PWD)
16677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
16777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testPwd() throws Exception {
16877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Modify PWD CommandHandler to return a predefined directory
16977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        final String DIR = "some/dir";
17077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        PwdCommandHandler pwdCommandHandler = (PwdCommandHandler) stubFtpServer.getCommandHandler(CommandNames.PWD);
17177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        pwdCommandHandler.setDirectory(DIR);
17277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
17377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
17477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
17577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // PWD
17677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String dir = ftpClient.printWorkingDirectory();
17777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("Unable to PWD", DIR, dir);
17877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("printWorkingDirectory", 257);
17977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
18077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
18177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testStat() throws Exception {
18277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Modify Stat CommandHandler to return predefined text
18377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        final String STATUS = "some information 123";
18477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        StatCommandHandler statCommandHandler = (StatCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STAT);
18577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        statCommandHandler.setStatus(STATUS);
18677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
18777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
18877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
18977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // STAT
19077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String status = ftpClient.getStatus();
19177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("STAT reply", "211 " + STATUS + ".", status.trim());
19277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("getStatus", 211);
19377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
19477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
19577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
19677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test getting the status (STAT), when the reply text contains multiple lines
19777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
19877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testStat_MultilineReplyText() throws Exception {
19977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Modify Stat CommandHandler to return predefined text
20077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        final String STATUS = "System name: abc.def\nVersion 3.5.7\nNumber of failed logins: 2";
20177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        final String FORMATTED_REPLY_STATUS = "211-System name: abc.def\r\nVersion 3.5.7\r\n211 Number of failed logins: 2.";
20277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        StatCommandHandler statCommandHandler = (StatCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STAT);
20377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        statCommandHandler.setStatus(STATUS);
20477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
20577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
20677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
20777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // STAT
20877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String status = ftpClient.getStatus();
20977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("STAT reply", FORMATTED_REPLY_STATUS, status.trim());
21077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("getStatus", 211);
21177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
21277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
21377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testSyst() throws Exception {
21477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
21577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
21677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // SYST
21777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("getSystemName()", "\"WINDOWS\" system type.", ftpClient.getSystemName());
21877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("syst", 215);
21977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
22077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
22177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testCwd() throws Exception {
22277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Connect
22377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Conecting to " + SERVER);
22477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
22577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("connect", 220);
22677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
22777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // CWD
22877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.changeWorkingDirectory("dir1/dir2");
22977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to CWD", success);
23077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("changeWorkingDirectory", 250);
23177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
23277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
23377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
23477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test changing the current working directory (CWD), when it causes a remote error
23577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
23677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testCwd_Error() throws Exception {
23777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Override CWD CommandHandler to return error reply code
23877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        final int REPLY_CODE = 500;
23977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        StaticReplyCommandHandler cwdCommandHandler = new StaticReplyCommandHandler(REPLY_CODE);
24077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        stubFtpServer.setCommandHandler("CWD", cwdCommandHandler);
24177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
24277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
24377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
24477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // CWD
24577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.changeWorkingDirectory("dir1/dir2");
24677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertFalse("Expected failure", success);
24777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("changeWorkingDirectory", REPLY_CODE);
24877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
24977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
25077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testCdup() throws Exception {
25177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
25277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
25377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // CDUP
25477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.changeToParentDirectory();
25577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to CDUP", success);
25677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("changeToParentDirectory", 200);
25777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
25877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
25977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testDele() throws Exception {
26077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
26177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
26277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // DELE
26377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.deleteFile(FILENAME);
26477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to DELE", success);
26577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("deleteFile", 250);
26677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
26777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
26877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testEprt() throws Exception {
26977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
27077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClient.sendCommand("EPRT", "|2|1080::8:800:200C:417A|5282|");
27177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("EPRT", 200);
27277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
27377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
27477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testEpsv() throws Exception {
27577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
27677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClient.sendCommand("EPSV");
27777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("EPSV", 229);
27877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
27977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
28077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testFeat_UseStaticReplyCommandHandler() throws IOException {
28177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // The FEAT command is not supported out of the box
28277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        final String FEAT_TEXT = "Extensions supported:\n" +
28377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair                "MLST size*;create;modify*;perm;media-type\n" +
28477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair                "SIZE\n" +
28577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair                "COMPRESSION\n" +
28677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair                "END";
28777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        StaticReplyCommandHandler featCommandHandler = new StaticReplyCommandHandler(211, FEAT_TEXT);
28877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        stubFtpServer.setCommandHandler("FEAT", featCommandHandler);
28977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
29077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
29177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals(ftpClient.sendCommand("FEAT"), 211);
29277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info(ftpClient.getReplyString());
29377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
29477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
29577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testMkd() throws Exception {
29677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
29777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
29877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // MKD
29977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.makeDirectory("dir1/dir2");
30077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to CWD", success);
30177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("makeDirectory", 257);
30277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
30377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
30477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testNoop() throws Exception {
30577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
30677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
30777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // NOOP
30877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.sendNoOp();
30977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to NOOP", success);
31077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("NOOP", 200);
31177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
31277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
31377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testRest() throws Exception {
31477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
31577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
31677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // REST
31777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        int replyCode = ftpClient.rest("marker");
31877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("Unable to REST", 350, replyCode);
31977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
32077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
32177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testRmd() throws Exception {
32277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
32377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
32477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // RMD
32577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.removeDirectory("dir1/dir2");
32677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to RMD", success);
32777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("removeDirectory", 250);
32877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
32977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
33077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testRename() throws Exception {
33177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
33277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
33377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Rename (RNFR, RNTO)
33477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.rename(FILENAME, "new_" + FILENAME);
33577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to RENAME", success);
33677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("rename", 250);
33777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
33877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
33977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testAllo() throws Exception {
34077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
34177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
34277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // ALLO
34377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("ALLO", ftpClient.allocate(1024));
34477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("ALLO with recordSize", ftpClient.allocate(1024, 64));
34577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
34677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
34777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
34877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test GET and PUT of ASCII files
34977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
35077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testTransferAsciiFile() throws Exception {
35177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        retrCommandHandler.setFileContents(ASCII_CONTENTS);
35277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
35377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
35477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
35577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Get File
35677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Get File for remotePath [" + FILENAME + "]");
35777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
35877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue(ftpClient.retrieveFile(FILENAME, outputStream));
35977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("File contents=[" + outputStream.toString());
36077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("File contents", ASCII_CONTENTS, outputStream.toString());
36177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
36277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Put File
36377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Put File for local path [" + FILENAME + "]");
36477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ByteArrayInputStream inputStream = new ByteArrayInputStream(ASCII_CONTENTS.getBytes());
36577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue(ftpClient.storeFile(FILENAME, inputStream));
36677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        InvocationRecord invocationRecord = storCommandHandler.getInvocation(0);
36777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        byte[] contents = (byte[]) invocationRecord.getObject(StorCommandHandler.FILE_CONTENTS_KEY);
36877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("File contents=[" + contents + "]");
36977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("File contents", ASCII_CONTENTS.getBytes(), contents);
37077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
37177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
37277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
37377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test GET and PUT of binary files
37477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
37577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testTransferBinaryFiles() throws Exception {
37677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        retrCommandHandler.setFileContents(BINARY_CONTENTS);
37777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
37877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
37977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
38077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
38177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Get File
38277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Get File for remotePath [" + FILENAME + "]");
38377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
38477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("GET", ftpClient.retrieveFile(FILENAME, outputStream));
38577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("GET File length=" + outputStream.size());
38677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("File contents", BINARY_CONTENTS, outputStream.toByteArray());
38777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
38877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Put File
38977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Put File for local path [" + FILENAME + "]");
39077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ByteArrayInputStream inputStream = new ByteArrayInputStream(BINARY_CONTENTS);
39177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("PUT", ftpClient.storeFile(FILENAME, inputStream));
39277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        InvocationRecord invocationRecord = storCommandHandler.getInvocation(0);
39377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        byte[] contents = (byte[]) invocationRecord.getObject(StorCommandHandler.FILE_CONTENTS_KEY);
39477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("PUT File length=" + contents.length);
39577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("File contents", BINARY_CONTENTS, contents);
39677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
39777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
39877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testStou() throws Exception {
39977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        StouCommandHandler stouCommandHandler = (StouCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STOU);
40077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        stouCommandHandler.setFilename(FILENAME);
40177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
40277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
40377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
40477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Stor a File (STOU)
40577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ByteArrayInputStream inputStream = new ByteArrayInputStream(ASCII_CONTENTS.getBytes());
40677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue(ftpClient.storeUniqueFile(FILENAME, inputStream));
40777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        InvocationRecord invocationRecord = stouCommandHandler.getInvocation(0);
40877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        byte[] contents = (byte[]) invocationRecord.getObject(StorCommandHandler.FILE_CONTENTS_KEY);
40977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("File contents=[" + contents + "]");
41077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("File contents", ASCII_CONTENTS.getBytes(), contents);
41177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
41277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
41377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testAppe() throws Exception {
41477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        AppeCommandHandler appeCommandHandler = (AppeCommandHandler) stubFtpServer.getCommandHandler(CommandNames.APPE);
41577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
41677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
41777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
41877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Append a File (APPE)
41977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ByteArrayInputStream inputStream = new ByteArrayInputStream(ASCII_CONTENTS.getBytes());
42077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue(ftpClient.appendFile(FILENAME, inputStream));
42177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        InvocationRecord invocationRecord = appeCommandHandler.getInvocation(0);
42277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        byte[] contents = (byte[]) invocationRecord.getObject(AppeCommandHandler.FILE_CONTENTS_KEY);
42377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("File contents=[" + contents + "]");
42477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("File contents", ASCII_CONTENTS.getBytes(), contents);
42577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
42677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
42777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testAbor() throws Exception {
42877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
42977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
43077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // ABOR
43177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("ABOR", ftpClient.abort());
43277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
43377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
43477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testPasv() throws Exception {
43577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
43677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
43777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // PASV
43877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClient.enterLocalPassiveMode();
43977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // no reply code; the PASV command is sent only when the data connection is opened
44077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
44177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
44277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testMode() throws Exception {
44377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
44477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
44577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // MODE
44677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
44777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to MODE", success);
44877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("setFileTransferMode", 200);
44977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
45077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
45177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testStru() throws Exception {
45277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
45377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
45477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // STRU
45577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        boolean success = ftpClient.setFileStructure(FTP.FILE_STRUCTURE);
45677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("Unable to STRU", success);
45777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("setFileStructure", 200);
45877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
45977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
46077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testSimpleCompositeCommandHandler() throws Exception {
46177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Replace CWD CommandHandler with a SimpleCompositeCommandHandler
46277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        CommandHandler commandHandler1 = new StaticReplyCommandHandler(500);
46377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        CommandHandler commandHandler2 = new CwdCommandHandler();
46477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        SimpleCompositeCommandHandler simpleCompositeCommandHandler = new SimpleCompositeCommandHandler();
46577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
46677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        simpleCompositeCommandHandler.addCommandHandler(commandHandler2);
46777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        stubFtpServer.setCommandHandler("CWD", simpleCompositeCommandHandler);
46877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
46977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // Connect
47077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
47177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
47277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // CWD
47377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertFalse("first", ftpClient.changeWorkingDirectory("dir1/dir2"));
47477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("first", ftpClient.changeWorkingDirectory("dir1/dir2"));
47577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
47677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
47777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testSite() throws Exception {
47877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
47977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
48077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // SITE
48177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        int replyCode = ftpClient.site("parameters,1,2,3");
48277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("SITE", 200, replyCode);
48377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
48477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
48577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testSmnt() throws Exception {
48677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
48777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
48877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // SMNT
48977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertTrue("SMNT", ftpClient.structureMount("dir1/dir2"));
49077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        verifyReplyCode("structureMount", 250);
49177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
49277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
49377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testRein() throws Exception {
49477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
49577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
49677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        // REIN
49777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("REIN", 220, ftpClient.rein());
49877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
49977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
50077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
50177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Test that command names in lowercase or mixed upper/lower case are accepted
50277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
50377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testCommandNamesInLowerOrMixedCase() throws Exception {
50477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
50577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
50677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("rein", 220, ftpClient.sendCommand("rein"));
50777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("rEIn", 220, ftpClient.sendCommand("rEIn"));
50877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("reiN", 220, ftpClient.sendCommand("reiN"));
50977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("Rein", 220, ftpClient.sendCommand("Rein"));
51077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
51177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
51277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void testUnrecognizedCommand() throws Exception {
51377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClientConnect();
51477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
51577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("Unrecognized:XXXX", 502, ftpClient.sendCommand("XXXX"));
51677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
51777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
51877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    // -------------------------------------------------------------------------
51977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    // Test setup and tear-down
52077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    // -------------------------------------------------------------------------
52177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
52277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
52377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Perform initialization before each test
52477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *
52577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @see org.mockftpserver.test.AbstractTest#setUp()
52677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
52777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    protected void setUp() throws Exception {
52877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        super.setUp();
52977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
53077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        for (int i = 0; i < BINARY_CONTENTS.length; i++) {
53177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair            BINARY_CONTENTS[i] = (byte) i;
53277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        }
53377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
53477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        stubFtpServer = new StubFtpServer();
53577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        stubFtpServer.setServerControlPort(PortTestUtil.getFtpServerControlPort());
53677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        stubFtpServer.start();
53777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClient = new FTPClient();
53877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        retrCommandHandler = (RetrCommandHandler) stubFtpServer.getCommandHandler(CommandNames.RETR);
53977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        storCommandHandler = (StorCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STOR);
54077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
54177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
54277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
54377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Perform cleanup after each test
54477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *
54577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @see org.mockftpserver.test.AbstractTest#tearDown()
54677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
54777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    protected void tearDown() throws Exception {
54877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        super.tearDown();
54977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        stubFtpServer.stop();
55077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
55177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
55277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    // -------------------------------------------------------------------------
55377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    // Internal Helper Methods
55477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    // -------------------------------------------------------------------------
55577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
55677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
55777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Connect to the server from the FTPClient
55877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
55977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private void ftpClientConnect() throws IOException {
56077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        ftpClient.connect(SERVER, PortTestUtil.getFtpServerControlPort());
56177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
56277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
56377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
56477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Assert that the FtpClient reply code is equal to the expected value
56577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *
56677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param operation         - the description of the operation performed; used in the error message
56777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param expectedReplyCode - the expected FtpClient reply code
56877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
56977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private void verifyReplyCode(String operation, int expectedReplyCode) {
57077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        int replyCode = ftpClient.getReplyCode();
57177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Reply: operation=\"" + operation + "\" replyCode=" + replyCode);
57277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("Unexpected replyCode for " + operation, expectedReplyCode, replyCode);
57377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
57477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
57577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
57677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Verify that the FTPFile has the specified properties
57777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *
57877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param ftpFile - the FTPFile to verify
57977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param type    - the expected file type
58077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param name    - the expected file name
58177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param size    - the expected file size (will be zero for a directory)
58277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
58377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private void verifyFTPFile(FTPFile ftpFile, int type, String name, long size) {
58477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info(ftpFile);
58577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("type: " + ftpFile, type, ftpFile.getType());
58677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("name: " + ftpFile, name, ftpFile.getName());
58777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        assertEquals("size: " + ftpFile, size, ftpFile.getSize());
58877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
58977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
59077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair}
591