160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/* 260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Copyright 2007 the original author or authors. 360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * you may not use this file except in compliance with the License. 660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * You may obtain a copy of the License at 760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * http://www.apache.org/licenses/LICENSE-2.0 960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Unless required by applicable law or agreed to in writing, software 1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * distributed under the License is distributed on an "AS IS" BASIS, 1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * See the License for the specific language governing permissions and 1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * limitations under the License. 1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpackage org.mockftpserver.stub.example; 1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.CommandNames; 1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.InvocationRecord; 2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.stub.StubFtpServer; 2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.stub.command.RetrCommandHandler; 2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.test.AbstractTest; 2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.test.IntegrationTest; 2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport java.io.IOException; 2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/** 2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Example test using StubFtpServer, with programmatic configuration. 2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpublic class RemoteFileTest extends AbstractTest implements IntegrationTest { 3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair private static final int PORT = 9981; 3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair private static final String FILENAME = "dir/sample.txt"; 3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair private RemoteFile remoteFile; 3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair private StubFtpServer stubFtpServer; 3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Test readFile() method 4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair public void testReadFile() throws Exception { 4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair final String CONTENTS = "abcdef 1234567890"; 4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Replace the default RETR CommandHandler; customize returned file contents 4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair RetrCommandHandler retrCommandHandler = new RetrCommandHandler(); 4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair retrCommandHandler.setFileContents(CONTENTS); 4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler); 4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair stubFtpServer.start(); 5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair String contents = remoteFile.readFile(FILENAME); 5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Verify returned file contents 5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertEquals("contents", CONTENTS, contents); 5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Verify the submitted filename 5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair InvocationRecord invocationRecord = retrCommandHandler.getInvocation(0); 5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair String filename = invocationRecord.getString(RetrCommandHandler.PATHNAME_KEY); 6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertEquals("filename", FILENAME, filename); 6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Test the readFile() method when the FTP transfer fails (returns a non-success reply code) 6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair public void testReadFileThrowsException() { 6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Replace the default RETR CommandHandler; return failure reply code 6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair RetrCommandHandler retrCommandHandler = new RetrCommandHandler(); 7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair retrCommandHandler.setFinalReplyCode(550); 7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler); 7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair stubFtpServer.start(); 7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair try { 7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair remoteFile.readFile(FILENAME); 7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair fail("Expected IOException"); 7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair catch (IOException expected) { 8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Expected this 8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @see org.mockftpserver.test.AbstractTest#setUp() 8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair protected void setUp() throws Exception { 8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair super.setUp(); 8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair remoteFile = new RemoteFile(); 9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair remoteFile.setServer("localhost"); 9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair remoteFile.setPort(PORT); 9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair stubFtpServer = new StubFtpServer(); 9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair stubFtpServer.setServerControlPort(PORT); 9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @see org.mockftpserver.test.AbstractTest#tearDown() 9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair protected void tearDown() throws Exception { 10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair super.tearDown(); 10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair stubFtpServer.stop(); 10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 10360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 10460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair} 105