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.mockftpserver.core.command.AbstractCommandHandlerTest;
19import org.mockftpserver.core.command.Command;
20import org.mockftpserver.core.command.CommandNames;
21import org.mockftpserver.core.command.ReplyCodes;
22
23/**
24 * Tests for the StorCommandHandler class
25 *
26 * @author Chris Mair
27 * @version $Revision$ - $Date$
28 */
29public final class StorCommandHandlerTest extends AbstractCommandHandlerTest {
30
31    private StorCommandHandler commandHandler;
32
33    /**
34     * Perform initialization before each test
35     *
36     * @see org.mockftpserver.core.command.AbstractCommandHandlerTest#setUp()
37     */
38    protected void setUp() throws Exception {
39        super.setUp();
40        commandHandler = new StorCommandHandler();
41        commandHandler.setReplyTextBundle(replyTextBundle);
42    }
43
44    /**
45     * Test the handleCommand() method, as well as the getFileContents() and clearFileContents() methods
46     */
47    public void testHandleCommand() throws Exception {
48        final String DATA = "ABC";
49
50        session.sendReply(ReplyCodes.TRANSFER_DATA_INITIAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_INITIAL_OK));
51        session.openDataConnection();
52        session.readData();
53        control(session).setReturnValue(DATA.getBytes());
54        session.closeDataConnection();
55        session.sendReply(ReplyCodes.TRANSFER_DATA_FINAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_FINAL_OK));
56        replay(session);
57
58        Command command = new Command(CommandNames.STOR, array(FILENAME1));
59        commandHandler.handleCommand(command, session);
60        verify(session);
61
62        verifyNumberOfInvocations(commandHandler, 1);
63        verifyTwoDataElements(commandHandler.getInvocation(0), StorCommandHandler.PATHNAME_KEY, FILENAME1,
64                StorCommandHandler.FILE_CONTENTS_KEY, DATA.getBytes());
65    }
66
67    /**
68     * Test the handleCommand() method, when no pathname parameter has been specified
69     */
70    public void testHandleCommand_MissingPathnameParameter() throws Exception {
71        testHandleCommand_InvalidParameters(commandHandler, CommandNames.STOR, EMPTY);
72    }
73
74}
75