Home | History | Annotate | Download | only in stub
      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;
     17 
     18 import org.mockftpserver.core.command.Command;
     19 import org.mockftpserver.core.command.CommandHandler;
     20 import org.mockftpserver.core.command.InvocationRecord;
     21 import org.mockftpserver.core.server.*;
     22 import org.mockftpserver.core.server.AbstractFtpServerTestCase;
     23 import org.mockftpserver.core.session.Session;
     24 import org.mockftpserver.stub.command.AbstractStubCommandHandler;
     25 import org.mockftpserver.stub.command.CwdCommandHandler;
     26 
     27 import java.util.ResourceBundle;
     28 
     29 /**
     30  * Unit tests for StubFtpServer. Also see {@link StubFtpServer_StartTest}
     31  * and {@link StubFtpServerIntegrationTest}.
     32  *
     33  * @author Chris Mair
     34  * @version $Revision$ - $Date$
     35  */
     36 public final class StubFtpServerTest extends AbstractFtpServerTestCase {
     37 
     38     private StubFtpServer stubFtpServer;
     39     private AbstractStubCommandHandler commandHandler;
     40     private CommandHandler commandHandler_NoReplyTextBundle;
     41 
     42     //-------------------------------------------------------------------------
     43     // Extra tests  (Standard tests defined in superclass)
     44     //-------------------------------------------------------------------------
     45 
     46     /**
     47      * Test the setCommandHandler() method, for a CommandHandler that does not implement ResourceBundleAware
     48      */
     49     public void testSetCommandHandler_NotReplyTextBundleAware() {
     50         stubFtpServer.setCommandHandler("ZZZ", commandHandler_NoReplyTextBundle);
     51         assertSame("commandHandler", commandHandler_NoReplyTextBundle, stubFtpServer.getCommandHandler("ZZZ"));
     52     }
     53 
     54     /**
     55      * Test the setCommandHandler() method, for a CommandHandler that implements ReplyTextBundleAware,
     56      * and whose replyTextBundle attribute is null.
     57      */
     58     public void testSetCommandHandler_NullReplyTextBundle() {
     59         stubFtpServer.setCommandHandler("ZZZ", commandHandler);
     60         assertSame("commandHandler", commandHandler, stubFtpServer.getCommandHandler("ZZZ"));
     61         assertSame("replyTextBundle", stubFtpServer.getReplyTextBundle(), commandHandler.getReplyTextBundle());
     62     }
     63 
     64     /**
     65      * Test setReplyTextBaseName() method
     66      */
     67     public void testSetReplyTextBaseName() {
     68         stubFtpServer.setReplyTextBaseName("SampleReplyText");
     69         CwdCommandHandler commandHandler = new CwdCommandHandler();
     70 
     71         // The resource bundle is passed along to new CommandHandlers (if they don't already have one)
     72         stubFtpServer.setCommandHandler("CWD", commandHandler);
     73         ResourceBundle resourceBundle = commandHandler.getReplyTextBundle();
     74         assertEquals("110", "Testing123", resourceBundle.getString("110"));
     75     }
     76 
     77     //-------------------------------------------------------------------------
     78     // Test setup
     79     //-------------------------------------------------------------------------
     80 
     81     /**
     82      * @see org.mockftpserver.test.AbstractTestCase#setUp()
     83      */
     84     protected void setUp() throws Exception {
     85         super.setUp();
     86 
     87         stubFtpServer = (StubFtpServer) ftpServer;
     88 
     89         // Create a CommandHandler instance that also implements ResourceBundleAware
     90         commandHandler = new AbstractStubCommandHandler() {
     91             protected void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
     92             }
     93         };
     94 
     95         // Create a CommandHandler instance that does NOT implement ResourceBundleAware
     96         commandHandler_NoReplyTextBundle = new CommandHandler() {
     97             public void handleCommand(Command command, Session session) throws Exception {
     98             }
     99         };
    100     }
    101 
    102     //-------------------------------------------------------------------------
    103     // Abstract method implementations
    104     //-------------------------------------------------------------------------
    105 
    106     protected AbstractFtpServer createFtpServer() {
    107         return new StubFtpServer();
    108     }
    109 
    110     protected CommandHandler createCommandHandler() {
    111         return new AbstractStubCommandHandler() {
    112             protected void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
    113             }
    114         };
    115     }
    116 
    117     protected void verifyCommandHandlerInitialized(CommandHandler commandHandler) {
    118         AbstractStubCommandHandler stubCommandHandler = (AbstractStubCommandHandler) commandHandler;
    119         assertSame("replyTextBundle", stubFtpServer.getReplyTextBundle(), stubCommandHandler.getReplyTextBundle());
    120     }
    121 
    122 }
    123