CommandTest.java revision dfa40a06dff44f29d8d5e1d3186055ad325fc7b9
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.core.command;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20import org.mockftpserver.core.CommandSyntaxException;
21import org.mockftpserver.core.util.AssertFailedException;
22import org.mockftpserver.test.AbstractTestCase;
23
24import java.util.List;
25
26/**
27 * Tests for the Command class
28 *
29 * @author Chris Mair
30 * @version $Revision$ - $Date$
31 */
32public final class CommandTest extends AbstractTestCase {
33
34    private static final Logger LOG = LoggerFactory.getLogger(CommandTest.class);
35
36    /**
37     * Test the Command(String,String[]) constructor
38     */
39    public void testConstructor() {
40        final String[] PARAMETERS = array("123");
41        Command command = new Command("abc", PARAMETERS);
42        assertEquals("name", "abc", command.getName());
43        assertEquals("parameters", PARAMETERS, command.getParameters());
44    }
45
46    /**
47     * Test the Command(String,List) constructor
48     */
49    public void testConstructor_List() {
50        final List PARAMETERS_LIST = list("123");
51        final String[] PARAMETERS_ARRAY = array("123");
52        Command command = new Command("abc", PARAMETERS_LIST);
53        assertEquals("name", "abc", command.getName());
54        assertEquals("parameters String[]", PARAMETERS_ARRAY, command.getParameters());
55    }
56
57    /**
58     * Test the Constructor method, passing in a null name
59     */
60    public void testConstructor_NullName() {
61        try {
62            new Command(null, EMPTY);
63            fail("Expected AssertFailedException");
64        }
65        catch (AssertFailedException expected) {
66            LOG.info("Expected: " + expected);
67        }
68    }
69
70    /**
71     * Test the Constructor method, passing in a null parameters
72     */
73    public void testConstructor_NullParameters() {
74        try {
75            new Command("OK", (String[]) null);
76            fail("Expected AssertFailedException");
77        }
78        catch (AssertFailedException expected) {
79            LOG.info("Expected: " + expected);
80        }
81    }
82
83    /**
84     * Test the normalizeName() method
85     */
86    public void testNormalizeName() {
87        assertEquals("XXX", "XXX", Command.normalizeName("XXX"));
88        assertEquals("xxx", "XXX", Command.normalizeName("xxx"));
89        assertEquals("Xxx", "XXX", Command.normalizeName("Xxx"));
90    }
91
92    /**
93     * Test the getRequiredParameter method
94     */
95    public void testGetRequiredParameter() {
96        Command command = new Command("abc", array("123", "456"));
97        assertEquals("123", "123", command.getRequiredParameter(0));
98        assertEquals("456", "456", command.getRequiredParameter(1));
99    }
100
101    /**
102     * Test the getRequiredParameter method, when the index is not valid
103     */
104    public void testGetRequiredParameter_IndexNotValid() {
105        Command command = new Command("abc", array("123", "456"));
106        try {
107            command.getRequiredParameter(2);
108            fail("Expected CommandSyntaxException");
109        }
110        catch (CommandSyntaxException expected) {
111            LOG.info("Expected: " + expected);
112        }
113    }
114
115    /**
116     * Test the getOptionalString method
117     */
118    public void testGetOptionalString() {
119        Command command = new Command("abc", array("123", "456"));
120        assertEquals("123", "123", command.getOptionalString(0));
121        assertEquals("456", "456", command.getOptionalString(1));
122        assertEquals("null", null, command.getOptionalString(2));
123    }
124
125    /**
126     * Test the getParameter method
127     */
128    public void testGetParameter() {
129        Command command = new Command("abc", array("123", "456"));
130        assertEquals("123", "123", command.getParameter(0));
131        assertEquals("456", "456", command.getParameter(1));
132        assertEquals("null", null, command.getParameter(2));
133    }
134
135    /**
136     * Test that a Command object is immutable, changing the original parameters passed in to the constructor
137     */
138    public void testImmutable_ChangeOriginalParameters() {
139        final String[] PARAMETERS = {"a", "b", "c"};
140        final Command COMMAND = new Command("command", PARAMETERS);
141        PARAMETERS[2] = "xxx";
142        assertEquals("parameters", COMMAND.getParameters(), new String[]{"a", "b", "c"});
143    }
144
145    /**
146     * Test that a Command object is immutable, changing the parameters returned from getParameters
147     */
148    public void testImmutable_ChangeRetrievedParameters() {
149        final String[] PARAMETERS = {"a", "b", "c"};
150        final Command COMMAND = new Command("command", PARAMETERS);
151        String[] parameters = COMMAND.getParameters();
152        parameters[2] = "xxx";
153        assertEquals("parameters", PARAMETERS, COMMAND.getParameters());
154    }
155
156    /**
157     * Test the equals() method, and tests the hasCode() method implicitly
158     *
159     * @throws Exception
160     */
161    public void testEquals() throws Exception {
162        final Command COMMAND1 = new Command("a", EMPTY);
163        final Command COMMAND2 = new Command("a", EMPTY);
164        final Command COMMAND3 = new Command("b", array("1"));
165        final Command COMMAND4 = new Command("b", array("2"));
166        final Command COMMAND5 = new Command("c", array("1"));
167        _testEquals(COMMAND1, null, false);
168        _testEquals(COMMAND1, COMMAND1, true);
169        _testEquals(COMMAND1, COMMAND2, true);
170        _testEquals(COMMAND1, COMMAND3, false);
171        _testEquals(COMMAND3, COMMAND4, false);
172        _testEquals(COMMAND3, COMMAND5, false);
173    }
174
175    /**
176     * Test that command1 equals command2 if and only if expectedEqual is true
177     *
178     * @param command1      - the first command
179     * @param command2      - the second command
180     * @param expectedEqual - true if command1 is expected to equal command2
181     */
182    private void _testEquals(Command command1, Command command2, boolean expectedEqual) {
183        assertEquals(command1.toString() + " and " + command2, expectedEqual, command1.equals(command2));
184    }
185
186}
187