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.example; 1777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 1877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.CommandNames; 1977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.InvocationRecord; 2077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.stub.StubFtpServer; 2177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.stub.command.RetrCommandHandler; 2277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.test.AbstractTest; 2377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.test.IntegrationTest; 2477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 2577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport java.io.IOException; 2677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 2777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/** 2877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Example test using StubFtpServer, with programmatic configuration. 2977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 3077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpublic class RemoteFileTest extends AbstractTest implements IntegrationTest { 3177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 3277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair private static final int PORT = 9981; 3377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair private static final String FILENAME = "dir/sample.txt"; 3477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 3577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair private RemoteFile remoteFile; 3677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair private StubFtpServer stubFtpServer; 3777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 3877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 3977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Test readFile() method 4077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 4177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair public void testReadFile() throws Exception { 4277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 4377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair final String CONTENTS = "abcdef 1234567890"; 4477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 4577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair // Replace the default RETR CommandHandler; customize returned file contents 4677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair RetrCommandHandler retrCommandHandler = new RetrCommandHandler(); 4777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair retrCommandHandler.setFileContents(CONTENTS); 4877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler); 4977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 5077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair stubFtpServer.start(); 5177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 5277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair String contents = remoteFile.readFile(FILENAME); 5377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 5477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair // Verify returned file contents 5577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair assertEquals("contents", CONTENTS, contents); 5677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 5777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair // Verify the submitted filename 5877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair InvocationRecord invocationRecord = retrCommandHandler.getInvocation(0); 5977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair String filename = invocationRecord.getString(RetrCommandHandler.PATHNAME_KEY); 6077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair assertEquals("filename", FILENAME, filename); 6177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 6277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 6377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 6477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Test the readFile() method when the FTP transfer fails (returns a non-success reply code) 6577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 6677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair public void testReadFileThrowsException() { 6777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 6877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair // Replace the default RETR CommandHandler; return failure reply code 6977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair RetrCommandHandler retrCommandHandler = new RetrCommandHandler(); 7077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair retrCommandHandler.setFinalReplyCode(550); 7177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler); 7277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 7377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair stubFtpServer.start(); 7477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 7577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair try { 7677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair remoteFile.readFile(FILENAME); 7777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair fail("Expected IOException"); 7877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 7977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair catch (IOException expected) { 8077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair // Expected this 8177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 8277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 8377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 8477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 8577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @see org.mockftpserver.test.AbstractTest#setUp() 8677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 8777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair protected void setUp() throws Exception { 8877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair super.setUp(); 8977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair remoteFile = new RemoteFile(); 9077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair remoteFile.setServer("localhost"); 9177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair remoteFile.setPort(PORT); 9277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair stubFtpServer = new StubFtpServer(); 9377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair stubFtpServer.setServerControlPort(PORT); 9477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 9577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 9677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair /** 9777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @see org.mockftpserver.test.AbstractTest#tearDown() 9877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */ 9977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair protected void tearDown() throws Exception { 10077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair super.tearDown(); 10177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair stubFtpServer.stop(); 10277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair } 10377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair 10477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair} 105