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