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