1ee809ac2dbd55c2955e29a54e16d89588296781cchrismair/* 2ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * Copyright 2008 the original author or authors. 3ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * 4ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * you may not use this file except in compliance with the License. 6ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * You may obtain a copy of the License at 7ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * 8ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * http://www.apache.org/licenses/LICENSE-2.0 9ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * 10ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * Unless required by applicable law or agreed to in writing, software 11ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * distributed under the License is distributed on an "AS IS" BASIS, 12ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * See the License for the specific language governing permissions and 14ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * limitations under the License. 15ee809ac2dbd55c2955e29a54e16d89588296781cchrismair */ 16ee809ac2dbd55c2955e29a54e16d89588296781cchrismairpackage org.mockftpserver.fake.example; 17ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 18bd576ae311a45a994ae6b457fb2e5bb0ffe0d6b5chrismairimport org.mockftpserver.fake.FakeFtpServer; 19bd576ae311a45a994ae6b457fb2e5bb0ffe0d6b5chrismairimport org.mockftpserver.fake.UserAccount; 20ee809ac2dbd55c2955e29a54e16d89588296781cchrismairimport org.mockftpserver.fake.filesystem.FileEntry; 21ee809ac2dbd55c2955e29a54e16d89588296781cchrismairimport org.mockftpserver.fake.filesystem.FileSystem; 22ee809ac2dbd55c2955e29a54e16d89588296781cchrismairimport org.mockftpserver.fake.filesystem.UnixFakeFileSystem; 23ee809ac2dbd55c2955e29a54e16d89588296781cchrismairimport org.mockftpserver.stub.example.RemoteFile; 2440658190151b7ded3489ff89c301b470155c95f4chrismairimport org.mockftpserver.test.*; 2540658190151b7ded3489ff89c301b470155c95f4chrismairimport org.mockftpserver.test.AbstractTestCase; 26ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 27ee809ac2dbd55c2955e29a54e16d89588296781cchrismairimport java.io.IOException; 28ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 29ee809ac2dbd55c2955e29a54e16d89588296781cchrismair/** 30ee809ac2dbd55c2955e29a54e16d89588296781cchrismair * Example test using FakeFtpServer, with programmatic configuration. 31ee809ac2dbd55c2955e29a54e16d89588296781cchrismair */ 3240658190151b7ded3489ff89c301b470155c95f4chrismairpublic class RemoteFileTest extends AbstractTestCase implements IntegrationTest { 33ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 34ee809ac2dbd55c2955e29a54e16d89588296781cchrismair private static final String HOME_DIR = "/"; 35ee809ac2dbd55c2955e29a54e16d89588296781cchrismair private static final String FILE = "/dir/sample.txt"; 36ee809ac2dbd55c2955e29a54e16d89588296781cchrismair private static final String CONTENTS = "abcdef 1234567890"; 37ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 38ee809ac2dbd55c2955e29a54e16d89588296781cchrismair private RemoteFile remoteFile; 39ee809ac2dbd55c2955e29a54e16d89588296781cchrismair private FakeFtpServer fakeFtpServer; 40ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 41ee809ac2dbd55c2955e29a54e16d89588296781cchrismair public void testReadFile() throws Exception { 42ee809ac2dbd55c2955e29a54e16d89588296781cchrismair String contents = remoteFile.readFile(FILE); 43ee809ac2dbd55c2955e29a54e16d89588296781cchrismair assertEquals("contents", CONTENTS, contents); 44ee809ac2dbd55c2955e29a54e16d89588296781cchrismair } 45ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 46ee809ac2dbd55c2955e29a54e16d89588296781cchrismair public void testReadFileThrowsException() { 47ee809ac2dbd55c2955e29a54e16d89588296781cchrismair try { 48ee809ac2dbd55c2955e29a54e16d89588296781cchrismair remoteFile.readFile("NoSuchFile.txt"); 49ee809ac2dbd55c2955e29a54e16d89588296781cchrismair fail("Expected IOException"); 50ee809ac2dbd55c2955e29a54e16d89588296781cchrismair } 51ee809ac2dbd55c2955e29a54e16d89588296781cchrismair catch (IOException expected) { 52ee809ac2dbd55c2955e29a54e16d89588296781cchrismair // Expected this 53ee809ac2dbd55c2955e29a54e16d89588296781cchrismair } 54ee809ac2dbd55c2955e29a54e16d89588296781cchrismair } 55ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 56ee809ac2dbd55c2955e29a54e16d89588296781cchrismair protected void setUp() throws Exception { 57ee809ac2dbd55c2955e29a54e16d89588296781cchrismair super.setUp(); 58ee809ac2dbd55c2955e29a54e16d89588296781cchrismair fakeFtpServer = new FakeFtpServer(); 592cfd46ef00809da42b315de79e7a457ceb40f70bchrismair fakeFtpServer.setServerControlPort(0); // use any free port 60ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 61ee809ac2dbd55c2955e29a54e16d89588296781cchrismair FileSystem fileSystem = new UnixFakeFileSystem(); 62ee809ac2dbd55c2955e29a54e16d89588296781cchrismair fileSystem.add(new FileEntry(FILE, CONTENTS)); 63ee809ac2dbd55c2955e29a54e16d89588296781cchrismair fakeFtpServer.setFileSystem(fileSystem); 64ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 657bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair UserAccount userAccount = new UserAccount(RemoteFile.USERNAME, RemoteFile.PASSWORD, HOME_DIR); 667bf26c51e6b1f13707ad90ebb60429eab992fe32chrismair fakeFtpServer.addUserAccount(userAccount); 67ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 68ee809ac2dbd55c2955e29a54e16d89588296781cchrismair fakeFtpServer.start(); 692cfd46ef00809da42b315de79e7a457ceb40f70bchrismair int port = fakeFtpServer.getServerControlPort(); 702cfd46ef00809da42b315de79e7a457ceb40f70bchrismair 712cfd46ef00809da42b315de79e7a457ceb40f70bchrismair remoteFile = new RemoteFile(); 722cfd46ef00809da42b315de79e7a457ceb40f70bchrismair remoteFile.setServer("localhost"); 732cfd46ef00809da42b315de79e7a457ceb40f70bchrismair remoteFile.setPort(port); 74ee809ac2dbd55c2955e29a54e16d89588296781cchrismair } 75ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 76ee809ac2dbd55c2955e29a54e16d89588296781cchrismair protected void tearDown() throws Exception { 77ee809ac2dbd55c2955e29a54e16d89588296781cchrismair super.tearDown(); 78ee809ac2dbd55c2955e29a54e16d89588296781cchrismair fakeFtpServer.stop(); 79ee809ac2dbd55c2955e29a54e16d89588296781cchrismair } 80ee809ac2dbd55c2955e29a54e16d89588296781cchrismair 81ee809ac2dbd55c2955e29a54e16d89588296781cchrismair}