Home | History | Annotate | Download | only in example
      1 /*
      2  * Copyright 2007 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.stub.example;
     17 
     18 import org.mockftpserver.core.command.CommandNames;
     19 import org.mockftpserver.core.command.InvocationRecord;
     20 import org.mockftpserver.stub.StubFtpServer;
     21 import org.mockftpserver.stub.command.RetrCommandHandler;
     22 import org.mockftpserver.test.AbstractTestCase;
     23 import org.mockftpserver.test.IntegrationTest;
     24 
     25 import java.io.IOException;
     26 
     27 /**
     28  * Example test using StubFtpServer, with programmatic configuration.
     29  */
     30 public class RemoteFileTest extends AbstractTestCase implements IntegrationTest {
     31 
     32     private static final int PORT = 9981;
     33     private static final String FILENAME = "dir/sample.txt";
     34 
     35     private RemoteFile remoteFile;
     36     private StubFtpServer stubFtpServer;
     37 
     38     /**
     39      * Test readFile() method
     40      */
     41     public void testReadFile() throws Exception {
     42 
     43         final String CONTENTS = "abcdef 1234567890";
     44 
     45         // Replace the default RETR CommandHandler; customize returned file contents
     46         RetrCommandHandler retrCommandHandler = new RetrCommandHandler();
     47         retrCommandHandler.setFileContents(CONTENTS);
     48         stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler);
     49 
     50         stubFtpServer.start();
     51 
     52         String contents = remoteFile.readFile(FILENAME);
     53 
     54         // Verify returned file contents
     55         assertEquals("contents", CONTENTS, contents);
     56 
     57         // Verify the submitted filename
     58         InvocationRecord invocationRecord = retrCommandHandler.getInvocation(0);
     59         String filename = invocationRecord.getString(RetrCommandHandler.PATHNAME_KEY);
     60         assertEquals("filename", FILENAME, filename);
     61     }
     62 
     63     /**
     64      * Test the readFile() method when the FTP transfer fails (returns a non-success reply code)
     65      */
     66     public void testReadFileThrowsException() {
     67 
     68         // Replace the default RETR CommandHandler; return failure reply code
     69         RetrCommandHandler retrCommandHandler = new RetrCommandHandler();
     70         retrCommandHandler.setFinalReplyCode(550);
     71         stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler);
     72 
     73         stubFtpServer.start();
     74 
     75         try {
     76             remoteFile.readFile(FILENAME);
     77             fail("Expected IOException");
     78         }
     79         catch (IOException expected) {
     80             // Expected this
     81         }
     82     }
     83 
     84     /**
     85      * @see org.mockftpserver.test.AbstractTestCase#setUp()
     86      */
     87     protected void setUp() throws Exception {
     88         super.setUp();
     89         remoteFile = new RemoteFile();
     90         remoteFile.setServer("localhost");
     91         remoteFile.setPort(PORT);
     92         stubFtpServer = new StubFtpServer();
     93         stubFtpServer.setServerControlPort(PORT);
     94     }
     95 
     96     /**
     97      * @see org.mockftpserver.test.AbstractTestCase#tearDown()
     98      */
     99     protected void tearDown() throws Exception {
    100         super.tearDown();
    101         stubFtpServer.stop();
    102     }
    103 
    104 }
    105