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.apache.log4j.Logger;
     19 import org.easymock.MockControl;
     20 import org.mockftpserver.core.command.Command;
     21 import org.mockftpserver.core.command.CommandNames;
     22 import org.mockftpserver.core.command.ReplyCodes;
     23 import org.mockftpserver.core.util.AssertFailedException;
     24 import org.mockftpserver.stub.command.RetrCommandHandler;
     25 
     26 /**
     27  * Tests for the RetrCommandHandler class
     28  *
     29  * @version $Revision$ - $Date$
     30  *
     31  * @author Chris Mair
     32  */
     33 public final class RetrCommandHandlerTest extends AbstractCommandHandlerTest {
     34 
     35     private static final Logger LOG = Logger.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      * @throws Exception
     94      */
     95     public void testHandleCommand() throws Exception {
     96         final String FILE_CONTENTS = "abc_123 456";
     97         commandHandler.setFileContents(FILE_CONTENTS);
     98 
     99         session.sendReply(ReplyCodes.SEND_DATA_INITIAL_OK, replyTextFor(ReplyCodes.SEND_DATA_INITIAL_OK));
    100         session.openDataConnection();
    101         session.sendData(FILE_CONTENTS.getBytes(), FILE_CONTENTS.length());
    102         control(session).setMatcher(MockControl.ARRAY_MATCHER);
    103         session.closeDataConnection();
    104         session.sendReply(ReplyCodes.SEND_DATA_FINAL_OK, replyTextFor(ReplyCodes.SEND_DATA_FINAL_OK));
    105         replay(session);
    106 
    107         Command command = new Command(CommandNames.RETR, array(FILENAME1));
    108         commandHandler.handleCommand(command, session);
    109         verify(session);
    110 
    111         verifyNumberOfInvocations(commandHandler, 1);
    112         verifyOneDataElement(commandHandler.getInvocation(0), RetrCommandHandler.PATHNAME_KEY, FILENAME1);
    113     }
    114 
    115     /**
    116      * Test the handleCommand() method, when no pathname parameter has been specified
    117      */
    118     public void testHandleCommand_MissingPathnameParameter() throws Exception {
    119         testHandleCommand_InvalidParameters(commandHandler, CommandNames.RETR, EMPTY);
    120     }
    121 
    122     /**
    123      * Perform initialization before each test
    124      * @see org.mockftpserver.stub.command.AbstractCommandHandlerTest#setUp()
    125      */
    126     protected void setUp() throws Exception {
    127         super.setUp();
    128         commandHandler = new RetrCommandHandler();
    129         commandHandler.setReplyTextBundle(replyTextBundle);
    130     }
    131 
    132 }
    133