19d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/*
29d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Copyright 2007 the original author or authors.
39d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
49d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Licensed under the Apache License, Version 2.0 (the "License");
59d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * you may not use this file except in compliance with the License.
69d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * You may obtain a copy of the License at
79d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
89d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *      http://www.apache.org/licenses/LICENSE-2.0
99d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Unless required by applicable law or agreed to in writing, software
119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * distributed under the License is distributed on an "AS IS" BASIS,
129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * See the License for the specific language governing permissions and
149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * limitations under the License.
159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpackage org.mockftpserver.stub.example;
179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport java.io.IOException;
199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.CommandNames;
219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.InvocationRecord;
229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.stub.StubFtpServer;
239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.stub.command.RetrCommandHandler;
249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.test.AbstractTest;
259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.test.IntegrationTest;
269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/**
289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Example test using StubFtpServer, with programmatic configuration.
299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpublic class RemoteFileTest extends AbstractTest implements IntegrationTest {
319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private static final int PORT = 9981;
339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private static final String FILENAME = "dir/sample.txt";
349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private RemoteFile remoteFile;
369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private StubFtpServer stubFtpServer;
379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Test readFile() method
409d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
419d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public void testReadFile() throws Exception {
429d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
439d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        final String CONTENTS = "abcdef 1234567890";
449d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
459d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        // Replace the default RETR CommandHandler; customize returned file contents
469d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        RetrCommandHandler retrCommandHandler = new RetrCommandHandler();
479d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        retrCommandHandler.setFileContents(CONTENTS);
489d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler);
499d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
509d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        stubFtpServer.start();
519d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
529d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        String contents = remoteFile.readFile(FILENAME);
539d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
549d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        // Verify returned file contents
559d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assertEquals("contents", CONTENTS, contents);
569d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
579d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        // Verify the submitted filename
589d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        InvocationRecord invocationRecord = retrCommandHandler.getInvocation(0);
599d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        String filename = invocationRecord.getString(RetrCommandHandler.PATHNAME_KEY);
609d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assertEquals("filename", FILENAME, filename);
619d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
629d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
639d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
649d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Test the readFile() method when the FTP transfer fails (returns a non-success reply code)
659d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
669d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public void testReadFileThrowsException() {
679d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
689d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        // Replace the default RETR CommandHandler; return failure reply code
699d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        RetrCommandHandler retrCommandHandler = new RetrCommandHandler();
709d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        retrCommandHandler.setFinalReplyCode(550);
719d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler);
729d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
739d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        stubFtpServer.start();
749d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
759d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        try {
769d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            remoteFile.readFile(FILENAME);
779d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            fail("Expected IOException");
789d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
799d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        catch (IOException expected) {
809d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            // Expected this
819d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
829d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
839d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
849d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
859d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @see org.mockftpserver.test.AbstractTest#setUp()
869d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
879d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    protected void setUp() throws Exception {
889d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        super.setUp();
899d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        remoteFile = new RemoteFile();
909d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        remoteFile.setServer("localhost");
919d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        remoteFile.setPort(PORT);
929d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        stubFtpServer = new StubFtpServer();
939d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        stubFtpServer.setServerControlPort(PORT);
949d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
959d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
969d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
979d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @see org.mockftpserver.test.AbstractTest#tearDown()
989d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
999d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    protected void tearDown() throws Exception {
1009d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        super.tearDown();
1019d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        stubFtpServer.stop();
1029d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
1039d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1049d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair}
105