EprtCommandHandlerTest.java revision 3e469b93fd10bc09ea2c088516168bf6a5cbaa43
1/*
2 * Copyright 2009 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
23import java.net.InetAddress;
24
25/**
26 * Tests for the EprtCommandHandler class
27 *
28 * @author Chris Mair
29 * @version $Revision$ - $Date$
30 */
31public final class EprtCommandHandlerTest extends AbstractCommandHandlerTest {
32
33    private static final String[] PARAMETERS_INSUFFICIENT = EMPTY;
34    private static final String[] PARAMETERS_IPV4 = {"|1|132.235.1.2|6275|"};
35    private static final InetAddress HOST_IPV4 = inetAddress("132.235.1.2");
36    private static final String[] PARAMETERS_IPV6 = {"|2|1080::8:800:200C:417A|6275|"};
37    private static final InetAddress HOST_IPV6 = inetAddress("1080::8:800:200C:417A");
38    private static final int PORT = 6275;
39
40    private EprtCommandHandler commandHandler;
41
42    public void testHandleCommand_IPv4() throws Exception {
43        final Command COMMAND = new Command(CommandNames.EPRT, PARAMETERS_IPV4);
44
45        session.setClientDataPort(PORT);
46        session.setClientDataHost(HOST_IPV4);
47        session.sendReply(ReplyCodes.EPRT_OK, replyTextFor(ReplyCodes.EPRT_OK));
48        replay(session);
49
50        commandHandler.handleCommand(COMMAND, session);
51        verify(session);
52
53        verifyNumberOfInvocations(commandHandler, 1);
54        verifyTwoDataElements(commandHandler.getInvocation(0),
55                PortCommandHandler.HOST_KEY, HOST_IPV4,
56                PortCommandHandler.PORT_KEY, new Integer(PORT));
57    }
58
59    public void testHandleCommand_IPv6() throws Exception {
60        final Command COMMAND = new Command(CommandNames.EPRT, PARAMETERS_IPV6);
61
62        session.setClientDataPort(PORT);
63        session.setClientDataHost(HOST_IPV6);
64        session.sendReply(ReplyCodes.EPRT_OK, replyTextFor(ReplyCodes.EPRT_OK));
65        replay(session);
66
67        commandHandler.handleCommand(COMMAND, session);
68        verify(session);
69
70        verifyNumberOfInvocations(commandHandler, 1);
71        verifyTwoDataElements(commandHandler.getInvocation(0),
72                PortCommandHandler.HOST_KEY, HOST_IPV6,
73                PortCommandHandler.PORT_KEY, new Integer(PORT));
74    }
75
76    public void testHandleCommand_MissingRequiredParameter() throws Exception {
77        testHandleCommand_InvalidParameters(commandHandler, CommandNames.EPRT, PARAMETERS_INSUFFICIENT);
78    }
79
80    /**
81     * Perform initialization before each test
82     *
83     * @see org.mockftpserver.core.command.AbstractCommandHandlerTest#setUp()
84     */
85    protected void setUp() throws Exception {
86        super.setUp();
87        commandHandler = new EprtCommandHandler();
88        commandHandler.setReplyTextBundle(replyTextBundle);
89    }
90
91}