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.easymock.MockControl;
19import org.mockftpserver.core.command.Command;
20import org.mockftpserver.core.command.CommandNames;
21import org.mockftpserver.core.command.ReplyCodes;
22import org.mockftpserver.stub.command.ListCommandHandler;
23
24/**
25 * Tests for the ListCommandHandler class
26 *
27 * @version $Revision$ - $Date$
28 *
29 * @author Chris Mair
30 */
31public final class ListCommandHandlerTest extends AbstractCommandHandlerTest {
32
33    private ListCommandHandler commandHandler;
34
35    /**
36     * Test the handleCommand() method
37     * @throws Exception
38     */
39    public void testHandleCommand() throws Exception {
40        final String DIR_LISTING = " directory listing\nabc.txt\ndef.log\n";
41        final String DIR_LISTING_TRIMMED = DIR_LISTING.trim();
42        ((ListCommandHandler)commandHandler).setDirectoryListing(DIR_LISTING);
43
44        for (int i = 0; i < 2; i++) {
45            session.sendReply(ReplyCodes.SEND_DATA_INITIAL_OK, replyTextFor(ReplyCodes.SEND_DATA_INITIAL_OK));
46            session.openDataConnection();
47            byte[] bytes = DIR_LISTING_TRIMMED.getBytes();
48            session.sendData(bytes, bytes.length);
49            control(session).setMatcher(MockControl.ARRAY_MATCHER);
50            session.closeDataConnection();
51            session.sendReply(ReplyCodes.SEND_DATA_FINAL_OK, replyTextFor(ReplyCodes.SEND_DATA_FINAL_OK));
52        }
53        replay(session);
54
55        Command command1 = new Command(CommandNames.LIST, array(DIR1));
56        Command command2 = new Command(CommandNames.LIST, EMPTY);
57        commandHandler.handleCommand(command1, session);
58        commandHandler.handleCommand(command2, session);
59        verify(session);
60
61        verifyNumberOfInvocations(commandHandler, 2);
62        verifyOneDataElement(commandHandler.getInvocation(0), ListCommandHandler.PATHNAME_KEY, DIR1);
63        verifyOneDataElement(commandHandler.getInvocation(1), ListCommandHandler.PATHNAME_KEY, null);
64    }
65
66    /**
67     * Perform initialization before each test
68     * @see org.mockftpserver.stub.command.AbstractCommandHandlerTest#setUp()
69     */
70    protected void setUp() throws Exception {
71        super.setUp();
72        commandHandler = new ListCommandHandler();
73        commandHandler.setReplyTextBundle(replyTextBundle);
74    }
75
76}
77