Home | History | Annotate | Download | only in command
      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.command;
     17 
     18 import org.slf4j.Logger;
     19 import org.slf4j.LoggerFactory;
     20 import org.easymock.MockControl;
     21 import org.mockftpserver.core.command.AbstractCommandHandlerTestCase;
     22 import org.mockftpserver.core.command.Command;
     23 import org.mockftpserver.core.command.CommandNames;
     24 import org.mockftpserver.core.command.ReplyCodes;
     25 import org.mockftpserver.core.util.AssertFailedException;
     26 
     27 /**
     28  * Tests for the RetrCommandHandler class
     29  *
     30  * @author Chris Mair
     31  * @version $Revision$ - $Date$
     32  */
     33 public final class RetrCommandHandlerTest extends AbstractCommandHandlerTestCase {
     34 
     35     private static final Logger LOG = LoggerFactory.getLogger(RetrCommandHandlerTest.class);
     36 
     37     private RetrCommandHandler commandHandler;
     38 
     39     /**
     40      * Test the constructor that takes a String, passing in a null
     41      */
     42     public void testConstructor_String_Null() {
     43         try {
     44             new RetrCommandHandler((String) null);
     45             fail("Expected AssertFailedException");
     46         }
     47         catch (AssertFailedException expected) {
     48             LOG.info("Expected: " + expected);
     49         }
     50     }
     51 
     52     /**
     53      * Test the constructor that takes a byte[], passing in a null
     54      */
     55     public void testConstructor_ByteArray_Null() {
     56         try {
     57             new RetrCommandHandler((byte[]) null);
     58             fail("Expected AssertFailedException");
     59         }
     60         catch (AssertFailedException expected) {
     61             LOG.info("Expected: " + expected);
     62         }
     63     }
     64 
     65     /**
     66      * Test the setFileContents(String) method, passing in a null
     67      */
     68     public void testSetFileContents_String_Null() {
     69         try {
     70             commandHandler.setFileContents((String) null);
     71             fail("Expected AssertFailedException");
     72         }
     73         catch (AssertFailedException expected) {
     74             LOG.info("Expected: " + expected);
     75         }
     76     }
     77 
     78     /**
     79      * Test the setFileContents(byte[]) method, passing in a null
     80      */
     81     public void testSetFileContents_ByteArray_Null() {
     82         try {
     83             commandHandler.setFileContents((byte[]) null);
     84             fail("Expected AssertFailedException");
     85         }
     86         catch (AssertFailedException expected) {
     87             LOG.info("Expected: " + expected);
     88         }
     89     }
     90 
     91     /**
     92      * Test the handleCommand() method
     93      *
     94      * @throws Exception
     95      */
     96     public void testHandleCommand() throws Exception {
     97         final String FILE_CONTENTS = "abc_123 456";
     98         commandHandler.setFileContents(FILE_CONTENTS);
     99 
    100         session.sendReply(ReplyCodes.TRANSFER_DATA_INITIAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_INITIAL_OK));
    101         session.openDataConnection();
    102         session.sendData(FILE_CONTENTS.getBytes(), FILE_CONTENTS.length());
    103         control(session).setMatcher(MockControl.ARRAY_MATCHER);
    104         session.closeDataConnection();
    105         session.sendReply(ReplyCodes.TRANSFER_DATA_FINAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_FINAL_OK));
    106         replay(session);
    107 
    108         Command command = new Command(CommandNames.RETR, array(FILENAME1));
    109         commandHandler.handleCommand(command, session);
    110         verify(session);
    111 
    112         verifyNumberOfInvocations(commandHandler, 1);
    113         verifyOneDataElement(commandHandler.getInvocation(0), RetrCommandHandler.PATHNAME_KEY, FILENAME1);
    114     }
    115 
    116     /**
    117      * Test the handleCommand() method, when no pathname parameter has been specified
    118      */
    119     public void testHandleCommand_MissingPathnameParameter() throws Exception {
    120         testHandleCommand_InvalidParameters(commandHandler, CommandNames.RETR, EMPTY);
    121     }
    122 
    123     /**
    124      * Perform initialization before each test
    125      *
    126      * @see org.mockftpserver.core.command.AbstractCommandHandlerTestCase#setUp()
    127      */
    128     protected void setUp() throws Exception {
    129         super.setUp();
    130         commandHandler = new RetrCommandHandler();
    131         commandHandler.setReplyTextBundle(replyTextBundle);
    132     }
    133 
    134 }
    135