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