PortCommandHandlerTest.java revision 93102446a7b7c3d17888064b4e2e4e5cb534e6d0
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 java.net.InetAddress;
19import java.net.UnknownHostException;
20
21import org.apache.log4j.Logger;
22import org.mockftpserver.core.command.Command;
23import org.mockftpserver.core.command.CommandNames;
24import org.mockftpserver.core.command.ReplyCodes;
25import org.mockftpserver.core.util.AssertFailedException;
26import org.mockftpserver.stub.command.PortCommandHandler;
27
28/**
29 * Tests for the PortCommandHandler class
30 *
31 * @version $Revision: 88 $ - $Date: 2007-10-27 20:18:11 -0400 (Sat, 27 Oct 2007) $
32 *
33 * @author Chris Mair
34 */
35public final class PortCommandHandlerTest extends AbstractCommandHandlerTest {
36
37    private static final Logger LOG = Logger.getLogger(PortCommandHandlerTest.class);
38    private static final String[] PARAMETERS = new String[] { "11", "22", "33", "44", "1", "206" };
39    private static final String[] PARAMETERS_INSUFFICIENT = new String[] {"7", "29", "99", "11", "77"};
40    private static final int PORT = (1 << 8) + 206;
41    private static final InetAddress HOST = inetAddress("11.22.33.44");
42
43    private PortCommandHandler commandHandler;
44
45    /**
46     * Test the handleCommand() method
47     */
48    public void testHandleCommand() throws Exception {
49        final Command COMMAND = new Command(CommandNames.PORT, PARAMETERS);
50
51        session.setClientDataPort(PORT);
52        session.setClientDataHost(HOST);
53        session.sendReply(ReplyCodes.PORT_OK, replyTextFor(ReplyCodes.PORT_OK));
54        replay(session);
55
56        commandHandler.handleCommand(COMMAND, session);
57        verify(session);
58
59        verifyNumberOfInvocations(commandHandler, 1);
60        verifyTwoDataElements(commandHandler.getInvocation(0),
61                PortCommandHandler.HOST_KEY, HOST,
62                PortCommandHandler.PORT_KEY, new Integer(PORT));
63    }
64
65    /**
66     * Test the handleCommand() method, when not enough parameters have been specified
67     */
68    public void testHandleCommand_InsufficientParameters() throws Exception {
69        testHandleCommand_InvalidParameters(commandHandler, CommandNames.PORT, PARAMETERS_INSUFFICIENT);
70    }
71
72    /**
73     * Test the parseHost() method
74     * @throws UnknownHostException
75     */
76    public void testParseHost() throws UnknownHostException {
77        InetAddress host = PortCommandHandler.parseHost(PARAMETERS);
78        assertEquals("InetAddress", HOST, host);
79    }
80
81    /**
82     * Test the parsePortNumber() method
83     */
84    public void testParsePortNumber() {
85        int portNumber = PortCommandHandler.parsePortNumber(PARAMETERS);
86        assertEquals("portNumber", PORT, portNumber);
87    }
88
89    /**
90     * Test the parseHost() method, passing in null
91     * @throws UnknownHostException
92     */
93    public void testParseHost_Null() throws UnknownHostException {
94        try {
95            PortCommandHandler.parseHost(null);
96            fail("Expected AssertFailedException");
97        }
98        catch (AssertFailedException expected) {
99            LOG.info("Expected: " + expected);
100        }
101    }
102
103    /**
104     * Test the parseHost() method, passing in a String[] with not enough parameters
105     * @throws UnknownHostException
106     */
107    public void testParseHost_InsufficientParameters() throws UnknownHostException {
108        try {
109            PortCommandHandler.parseHost(PARAMETERS_INSUFFICIENT);
110            fail("Expected AssertFailedException");
111        }
112        catch (AssertFailedException expected) {
113            LOG.info("Expected: " + expected);
114        }
115    }
116
117    /**
118     * Test the parsePortNumber() method, passing in null
119     * @throws UnknownHostException
120     */
121    public void testParsePortNumber_Null() throws UnknownHostException {
122        try {
123            PortCommandHandler.parsePortNumber(null);
124            fail("Expected AssertFailedException");
125        }
126        catch (AssertFailedException expected) {
127            LOG.info("Expected: " + expected);
128        }
129    }
130
131    /**
132     * Test the parsePortNumber() method, passing in a String[] with not enough parameters
133     * @throws UnknownHostException
134     */
135    public void testParsePortNumber_InsufficientParameters() throws UnknownHostException {
136        try {
137            PortCommandHandler.parsePortNumber(PARAMETERS_INSUFFICIENT);
138            fail("Expected AssertFailedException");
139        }
140        catch (AssertFailedException expected) {
141            LOG.info("Expected: " + expected);
142        }
143    }
144
145    /**
146     * Perform initialization before each test
147     *
148     * @see org.mockftpserver.stub.command.AbstractCommandHandlerTest#setUp()
149     */
150    protected void setUp() throws Exception {
151        super.setUp();
152        commandHandler = new PortCommandHandler();
153        commandHandler.setReplyTextBundle(replyTextBundle);
154   }
155
156}
157