RemoteFileTest.java revision bd576ae311a45a994ae6b457fb2e5bb0ffe0d6b5
1/* 2 * Copyright 2008 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 */ 16package org.mockftpserver.fake.example; 17 18import org.mockftpserver.fake.FakeFtpServer; 19import org.mockftpserver.fake.UserAccount; 20import org.mockftpserver.fake.filesystem.FileEntry; 21import org.mockftpserver.fake.filesystem.FileSystem; 22import org.mockftpserver.fake.filesystem.UnixFakeFileSystem; 23import org.mockftpserver.stub.example.RemoteFile; 24import org.mockftpserver.test.AbstractTest; 25import org.mockftpserver.test.IntegrationTest; 26 27import java.io.IOException; 28 29/** 30 * Example test using FakeFtpServer, with programmatic configuration. 31 */ 32public class RemoteFileTest extends AbstractTest implements IntegrationTest { 33 34 private static final int PORT = 9981; 35 private static final String HOME_DIR = "/"; 36 private static final String FILE = "/dir/sample.txt"; 37 private static final String CONTENTS = "abcdef 1234567890"; 38 39 private RemoteFile remoteFile; 40 private FakeFtpServer fakeFtpServer; 41 42 public void testReadFile() throws Exception { 43 String contents = remoteFile.readFile(FILE); 44 assertEquals("contents", CONTENTS, contents); 45 } 46 47 public void testReadFileThrowsException() { 48 try { 49 remoteFile.readFile("NoSuchFile.txt"); 50 fail("Expected IOException"); 51 } 52 catch (IOException expected) { 53 // Expected this 54 } 55 } 56 57 protected void setUp() throws Exception { 58 super.setUp(); 59 remoteFile = new RemoteFile(); 60 remoteFile.setServer("localhost"); 61 remoteFile.setPort(PORT); 62 fakeFtpServer = new FakeFtpServer(); 63 fakeFtpServer.setServerControlPort(PORT); 64 65 FileSystem fileSystem = new UnixFakeFileSystem(); 66 fileSystem.add(new FileEntry(FILE, CONTENTS)); 67 fakeFtpServer.setFileSystem(fileSystem); 68 69 UserAccount userAccount = new UserAccount(RemoteFile.USERNAME, RemoteFile.PASSWORD, HOME_DIR); 70 fakeFtpServer.addUserAccount(userAccount); 71 72 fakeFtpServer.start(); 73 } 74 75 /** 76 * @see org.mockftpserver.test.AbstractTest#tearDown() 77 */ 78 protected void tearDown() throws Exception { 79 super.tearDown(); 80 fakeFtpServer.stop(); 81 } 82 83}