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.easymock.MockControl;
     19 import org.mockftpserver.core.command.AbstractCommandHandlerTestCase;
     20 import org.mockftpserver.core.command.Command;
     21 import org.mockftpserver.core.command.CommandNames;
     22 import org.mockftpserver.core.command.ReplyCodes;
     23 
     24 /**
     25  * Tests for the NlstCommandHandler class
     26  *
     27  * @author Chris Mair
     28  * @version $Revision$ - $Date$
     29  */
     30 public final class NlstCommandHandlerTest extends AbstractCommandHandlerTestCase {
     31 
     32     private NlstCommandHandler commandHandler;
     33 
     34     /**
     35      * Test the handleCommand() method
     36      */
     37     public void testHandleCommand() throws Exception {
     38         final String DIR_LISTING = " directory listing\nabc.txt\ndef.log\n";
     39         final String DIR_LISTING_TRIMMED = DIR_LISTING.trim();
     40         ((NlstCommandHandler) commandHandler).setDirectoryListing(DIR_LISTING);
     41 
     42         for (int i = 0; i < 2; i++) {
     43             session.sendReply(ReplyCodes.TRANSFER_DATA_INITIAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_INITIAL_OK));
     44             session.openDataConnection();
     45             byte[] bytes = DIR_LISTING_TRIMMED.getBytes();
     46             session.sendData(bytes, bytes.length);
     47             control(session).setMatcher(MockControl.ARRAY_MATCHER);
     48             session.closeDataConnection();
     49             session.sendReply(ReplyCodes.TRANSFER_DATA_FINAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_FINAL_OK));
     50         }
     51         replay(session);
     52 
     53         Command command1 = new Command(CommandNames.LIST, array(DIR1));
     54         Command command2 = new Command(CommandNames.LIST, EMPTY);
     55         commandHandler.handleCommand(command1, session);
     56         commandHandler.handleCommand(command2, session);
     57         verify(session);
     58 
     59         verifyNumberOfInvocations(commandHandler, 2);
     60         verifyOneDataElement(commandHandler.getInvocation(0), NlstCommandHandler.PATHNAME_KEY, DIR1);
     61         verifyOneDataElement(commandHandler.getInvocation(1), NlstCommandHandler.PATHNAME_KEY, null);
     62     }
     63 
     64     /**
     65      * Perform initialization before each test
     66      *
     67      * @see org.mockftpserver.core.command.AbstractCommandHandlerTestCase#setUp()
     68      */
     69     protected void setUp() throws Exception {
     70         super.setUp();
     71         commandHandler = new NlstCommandHandler();
     72         commandHandler.setReplyTextBundle(replyTextBundle);
     73     }
     74 
     75 }
     76