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