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