AlloCommandHandlerTest.java revision 40658190151b7ded3489ff89c301b470155c95f4
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.apache.log4j.Logger; 19import org.mockftpserver.core.command.*; 20import org.mockftpserver.core.command.AbstractCommandHandlerTestCase; 21import org.mockftpserver.core.util.AssertFailedException; 22 23/** 24 * Tests for the AlloCommandHandler class 25 * 26 * @version $Revision$ - $Date$ 27 * 28 * @author Chris Mair 29 */ 30public final class AlloCommandHandlerTest extends AbstractCommandHandlerTestCase { 31 32 private static final Logger LOG = Logger.getLogger(AlloCommandHandlerTest.class); 33 private static final int BYTES1 = 64; 34 private static final int BYTES2 = 555; 35 private static final int RECORD_SIZE = 77; 36 37 private AlloCommandHandler commandHandler; 38 private Command command1; 39 private Command command2; 40 41 /** 42 * Test the handleCommand() method 43 */ 44 public void testHandleCommand() throws Exception { 45 46 session.sendReply(ReplyCodes.ALLO_OK, replyTextFor(ReplyCodes.ALLO_OK)); 47 session.sendReply(ReplyCodes.ALLO_OK, replyTextFor(ReplyCodes.ALLO_OK)); 48 replay(session); 49 50 commandHandler.handleCommand(command1, session); 51 commandHandler.handleCommand(command2, session); 52 verify(session); 53 54 verifyNumberOfInvocations(commandHandler, 2); 55 verifyOneDataElement(commandHandler.getInvocation(0), AlloCommandHandler.NUMBER_OF_BYTES_KEY, new Integer( 56 BYTES1)); 57 verifyTwoDataElements(commandHandler.getInvocation(1), AlloCommandHandler.NUMBER_OF_BYTES_KEY, new Integer( 58 BYTES2), AlloCommandHandler.RECORD_SIZE_KEY, new Integer(RECORD_SIZE)); 59 } 60 61 /** 62 * Test the handleCommand() method, when no numberOfBytes parameter has been specified 63 */ 64 public void testHandleCommand_MissingNumberOfBytesParameter() throws Exception { 65 testHandleCommand_InvalidParameters(commandHandler, CommandNames.ALLO, EMPTY); 66 } 67 68 /** 69 * Test the handleCommand() method, when the recordSize delimiter ("R") parameter is specified, 70 * but is not followed by the recordSize value. 71 */ 72 public void testHandleCommand_RecordSizeDelimiterWithoutValue() throws Exception { 73 try { 74 commandHandler.handleCommand(new Command(CommandNames.ALLO, array("123 R ")), session); 75 fail("Expected AssertFailedException"); 76 } 77 catch (AssertFailedException expected) { 78 LOG.info("Expected: " + expected); 79 } 80 } 81 82 /** 83 * Test the handleCommand() method, when a non-numeric numberOfBytes parameter has been 84 * specified 85 */ 86 public void testHandleCommand_InvalidNumberOfBytesParameter() throws Exception { 87 try { 88 commandHandler.handleCommand(new Command(CommandNames.ALLO, array("xx")), session); 89 fail("Expected NumberFormatException"); 90 } 91 catch (NumberFormatException expected) { 92 LOG.info("Expected: " + expected); 93 } 94 } 95 96 /** 97 * Test the handleCommand() method, when a non-numeric recordSize parameter has been specified 98 */ 99 public void testHandleCommand_InvalidRecordSizeParameter() throws Exception { 100 try { 101 commandHandler.handleCommand(new Command(CommandNames.ALLO, array("123 R xx")), session); 102 fail("Expected NumberFormatException"); 103 } 104 catch (NumberFormatException expected) { 105 LOG.info("Expected: " + expected); 106 } 107 } 108 109 /** 110 * Perform initialization before each test 111 * 112 * @see org.mockftpserver.core.command.AbstractCommandHandlerTestCase#setUp() 113 */ 114 protected void setUp() throws Exception { 115 super.setUp(); 116 commandHandler = new AlloCommandHandler(); 117 commandHandler.setReplyTextBundle(replyTextBundle); 118 command1 = new Command(CommandNames.ALLO, array(Integer.toString(BYTES1))); 119 command2 = new Command(CommandNames.ALLO, array(Integer.toString(BYTES2) + " R " + Integer.toString(RECORD_SIZE))); 120 } 121 122} 123