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 java.util.HashMap;
     19 import java.util.Map;
     20 import java.util.ResourceBundle;
     21 
     22 
     23 import org.apache.log4j.Logger;
     24 import org.mockftpserver.core.command.Command;
     25 import org.mockftpserver.core.command.CommandHandler;
     26 import org.mockftpserver.core.command.InvocationRecord;
     27 import org.mockftpserver.core.session.Session;
     28 import org.mockftpserver.core.util.AssertFailedException;
     29 import org.mockftpserver.stub.StubFtpServer;
     30 import org.mockftpserver.stub.command.AbstractStubCommandHandler;
     31 import org.mockftpserver.stub.command.CwdCommandHandler;
     32 import org.mockftpserver.test.AbstractTest;
     33 
     34 /**
     35  * Unit tests for StubFtpServer. Also see {@link StubFtpServer_StartTest}
     36  * and {@link StubFtpServerIntegrationTest}.
     37  *
     38  * @version $Revision$ - $Date$
     39  *
     40  * @author Chris Mair
     41  */
     42 public final class StubFtpServerTest extends AbstractTest {
     43 
     44     private static final Logger LOG = Logger.getLogger(StubFtpServerTest.class);
     45 
     46     private StubFtpServer stubFtpServer;
     47     private AbstractStubCommandHandler commandHandler;
     48     private CommandHandler commandHandler_NoReplyTextBundle;
     49 
     50     /**
     51      * Test the setCommandHandlers() method
     52      */
     53     public void testSetCommandHandlers() {
     54         Map mapping = new HashMap();
     55         mapping.put("AAA", commandHandler);
     56         mapping.put("BBB", commandHandler_NoReplyTextBundle);
     57 
     58         stubFtpServer.setCommandHandlers(mapping);
     59         assertSame("commandHandler1", commandHandler, stubFtpServer.getCommandHandler("AAA"));
     60         assertSame("commandHandler2", commandHandler_NoReplyTextBundle, stubFtpServer.getCommandHandler("BBB"));
     61 
     62         assertSame("replyTextBundle", stubFtpServer.replyTextBundle, commandHandler.getReplyTextBundle());
     63 
     64         // Make sure default CommandHandlers are still set
     65         assertEquals("CwdCommandHandler", CwdCommandHandler.class, stubFtpServer.getCommandHandler("CWD").getClass());
     66     }
     67 
     68     /**
     69      * Test the setCommandHandlers() method, when the Map is null
     70      */
     71     public void testSetCommandHandlers_Null() {
     72         try {
     73             stubFtpServer.setCommandHandlers(null);
     74             fail("Expected AssertFailedException");
     75         }
     76         catch (AssertFailedException expected) {
     77             LOG.info("Expected: " + expected);
     78         }
     79     }
     80 
     81     /**
     82      * Test the setCommandHandler() method, for a CommandHandler that does not implement ResourceBundleAware
     83      */
     84     public void testSetCommandHandler_NotReplyTextBundleAware() {
     85         stubFtpServer.setCommandHandler("ZZZ", commandHandler_NoReplyTextBundle);
     86         assertSame("commandHandler", commandHandler_NoReplyTextBundle, stubFtpServer.getCommandHandler("ZZZ"));
     87     }
     88 
     89     /**
     90      * Test the setCommandHandler() method, for a CommandHandler that implements ReplyTextBundleAware,
     91      * and whose replyTextBundle attribute is null.
     92      */
     93     public void testSetCommandHandler_NullReplyTextBundle() {
     94         stubFtpServer.setCommandHandler("ZZZ", commandHandler);
     95         assertSame("commandHandler", commandHandler, stubFtpServer.getCommandHandler("ZZZ"));
     96         assertSame("replyTextBundle", stubFtpServer.replyTextBundle, commandHandler.getReplyTextBundle());
     97     }
     98 
     99     /**
    100      * Test the setCommandHandler() method, when the commandName is null
    101      */
    102     public void testSetCommandHandler_NullCommandName() {
    103         CommandHandler commandHandler = (CommandHandler) createMock(CommandHandler.class);
    104         try {
    105             stubFtpServer.setCommandHandler(null, commandHandler);
    106             fail("Expected AssertFailedException");
    107         }
    108         catch (AssertFailedException expected) {
    109             LOG.info("Expected: " + expected);
    110         }
    111     }
    112 
    113     /**
    114      * Test the setCommandHandler() method, when the commandHandler is null
    115      */
    116     public void testSetCommandHandler_NullCommandHandler() {
    117         try {
    118             stubFtpServer.setCommandHandler("ZZZ", null);
    119             fail("Expected AssertFailedException");
    120         }
    121         catch (AssertFailedException expected) {
    122             LOG.info("Expected: " + expected);
    123         }
    124     }
    125 
    126     /**
    127      * Test setReplyTextBaseName() method
    128      */
    129     public void testSetReplyTextBaseName() {
    130         stubFtpServer.setReplyTextBaseName("SampleReplyText");
    131         CwdCommandHandler commandHandler = new CwdCommandHandler();
    132 
    133         // The resource bundle is passed along to new CommandHandlers (if they don't already have one)
    134         stubFtpServer.setCommandHandler("CWD", commandHandler);
    135         ResourceBundle resourceBundle = commandHandler.getReplyTextBundle();
    136         assertEquals("110", "Testing123", resourceBundle.getString("110"));
    137     }
    138 
    139     /**
    140      * Test the setCommandHandler() and getCommandHandler() methods for commands in lower case or mixed case
    141      */
    142     public void testLowerCaseOrMixedCaseCommandNames() {
    143         stubFtpServer.setCommandHandler("XXX", commandHandler);
    144         assertSame("ZZZ", commandHandler, stubFtpServer.getCommandHandler("XXX"));
    145         assertSame("Zzz", commandHandler, stubFtpServer.getCommandHandler("Xxx"));
    146         assertSame("zzz", commandHandler, stubFtpServer.getCommandHandler("xxx"));
    147 
    148         stubFtpServer.setCommandHandler("YyY", commandHandler);
    149         assertSame("ZZZ", commandHandler, stubFtpServer.getCommandHandler("YYY"));
    150         assertSame("Zzz", commandHandler, stubFtpServer.getCommandHandler("Yyy"));
    151         assertSame("zzz", commandHandler, stubFtpServer.getCommandHandler("yyy"));
    152 
    153         stubFtpServer.setCommandHandler("zzz", commandHandler);
    154         assertSame("ZZZ", commandHandler, stubFtpServer.getCommandHandler("ZZZ"));
    155         assertSame("Zzz", commandHandler, stubFtpServer.getCommandHandler("zzZ"));
    156         assertSame("zzz", commandHandler, stubFtpServer.getCommandHandler("zzz"));
    157     }
    158 
    159     //-------------------------------------------------------------------------
    160     // Test setup
    161     //-------------------------------------------------------------------------
    162 
    163     /**
    164      * @see org.mockftpserver.test.AbstractTest#setUp()
    165      */
    166     protected void setUp() throws Exception {
    167         super.setUp();
    168 
    169         stubFtpServer = new StubFtpServer();
    170 
    171         // Create a CommandHandler instance that also implements ResourceBundleAware
    172         commandHandler = new AbstractStubCommandHandler() {
    173             protected void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
    174             }
    175         };
    176 
    177         // Create a CommandHandler instance that does NOT implement ResourceBundleAware
    178         commandHandler_NoReplyTextBundle = new CommandHandler() {
    179             public void handleCommand(Command command, Session session) throws Exception {
    180             }
    181         };
    182     }
    183 
    184 }
    185