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.StouCommandHandler;
22
23/**
24 * Tests for the StouCommandHandler class
25 *
26 * @version $Revision$ - $Date$
27 *
28 * @author Chris Mair
29 */
30public final class StouCommandHandlerTest extends AbstractCommandHandlerTest {
31
32    private StouCommandHandler 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 StouCommandHandler();
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        final String FILENAME = "abc.txt";
51
52        session.sendReply(ReplyCodes.SEND_DATA_INITIAL_OK, replyTextFor(ReplyCodes.SEND_DATA_INITIAL_OK));
53        session.openDataConnection();
54        session.readData();
55        control(session).setReturnValue(DATA.getBytes());
56        session.closeDataConnection();
57        session.sendReply(ReplyCodes.SEND_DATA_FINAL_OK, formattedReplyTextFor("226.WithFilename", FILENAME));
58        replay(session);
59
60        Command command = new Command(CommandNames.STOU, array(FILENAME1));
61        commandHandler.setFilename(FILENAME);
62        commandHandler.handleCommand(command, session);
63        verify(session);
64
65        verifyNumberOfInvocations(commandHandler, 1);
66        verifyOneDataElement(commandHandler.getInvocation(0), StouCommandHandler.FILE_CONTENTS_KEY, DATA.getBytes());
67    }
68
69}
70