1c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair/*
2c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Copyright 2007 the original author or authors.
3c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
4c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * you may not use this file except in compliance with the License.
6c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * You may obtain a copy of the License at
7c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
8c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
10c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Unless required by applicable law or agreed to in writing, software
11c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * See the License for the specific language governing permissions and
14c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * limitations under the License.
15c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */
16c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairpackage org.mockftpserver.core.command;
17c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
18c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.apache.log4j.Logger;
19c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.CommandSyntaxException;
20c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.util.AssertFailedException;
21c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.test.AbstractTest;
22c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
23c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport java.util.List;
24c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
25c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair/**
26c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Tests for the Command class
27c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
28c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @author Chris Mair
29c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @version $Revision$ - $Date$
30c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */
31c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairpublic final class CommandTest extends AbstractTest {
32c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
33c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    private static final Logger LOG = Logger.getLogger(CommandTest.class);
34c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
35c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
36c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the Command(String,String[]) constructor
37c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
38c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testConstructor() {
39c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final String[] PARAMETERS = array("123");
40c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Command command = new Command("abc", PARAMETERS);
41c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("name", "abc", command.getName());
42c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("parameters", PARAMETERS, command.getParameters());
43c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
44c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
45c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
46c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the Command(String,List) constructor
47c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
48c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testConstructor_List() {
49c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final List PARAMETERS_LIST = list("123");
50c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final String[] PARAMETERS_ARRAY = array("123");
51c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Command command = new Command("abc", PARAMETERS_LIST);
52c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("name", "abc", command.getName());
53c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("parameters String[]", PARAMETERS_ARRAY, command.getParameters());
54c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
55c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
56c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
57c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the Constructor method, passing in a null name
58c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
59c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testConstructor_NullName() {
60c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        try {
61c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            new Command(null, EMPTY);
62c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            fail("Expected AssertFailedException");
63c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        }
64c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        catch (AssertFailedException expected) {
65c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            LOG.info("Expected: " + expected);
66c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        }
67c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
68c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
69c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
70c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the Constructor method, passing in a null parameters
71c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
72c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testConstructor_NullParameters() {
73c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        try {
74c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            new Command("OK", (String[]) null);
75c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            fail("Expected AssertFailedException");
76c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        }
77c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        catch (AssertFailedException expected) {
78c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            LOG.info("Expected: " + expected);
79c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        }
80c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
81c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
82c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
83c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the normalizeName() method
84c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
85c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testNormalizeName() {
86c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("XXX", "XXX", Command.normalizeName("XXX"));
87c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("xxx", "XXX", Command.normalizeName("xxx"));
88c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("Xxx", "XXX", Command.normalizeName("Xxx"));
89c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
90c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
91c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
92c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the getRequiredParameter method
93c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
94c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testGetRequiredParameter() {
95c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Command command = new Command("abc", array("123", "456"));
96c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("123", "123", command.getRequiredParameter(0));
97c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("456", "456", command.getRequiredParameter(1));
98c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
99c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
100c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
101c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the getRequiredParameter method, when the index is not valid
102c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
103c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testGetRequiredParameter_IndexNotValid() {
104c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Command command = new Command("abc", array("123", "456"));
105c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        try {
106c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            command.getRequiredParameter(2);
107c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            fail("Expected CommandSyntaxException");
108c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        }
109c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        catch (CommandSyntaxException expected) {
110c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair            LOG.info("Expected: " + expected);
111c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        }
112c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
113c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
114c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
115c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the getOptionalString method
116c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
117c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testGetOptionalString() {
118c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Command command = new Command("abc", array("123", "456"));
119c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("123", "123", command.getOptionalString(0));
120c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("456", "456", command.getOptionalString(1));
121c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("null", null, command.getOptionalString(2));
122c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
123c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
124c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
125c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the getParameter method
126c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
127c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testGetParameter() {
128c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Command command = new Command("abc", array("123", "456"));
129c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("123", "123", command.getParameter(0));
130c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("456", "456", command.getParameter(1));
131c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("null", null, command.getParameter(2));
132c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
133c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
134c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
135c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test that a Command object is immutable, changing the original parameters passed in to the constructor
136c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
137c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testImmutable_ChangeOriginalParameters() {
138c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final String[] PARAMETERS = {"a", "b", "c"};
139c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final Command COMMAND = new Command("command", PARAMETERS);
140c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        PARAMETERS[2] = "xxx";
141c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("parameters", COMMAND.getParameters(), new String[]{"a", "b", "c"});
142c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
143c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
144c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
145c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test that a Command object is immutable, changing the parameters returned from getParameters
146c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
147c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testImmutable_ChangeRetrievedParameters() {
148c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final String[] PARAMETERS = {"a", "b", "c"};
149c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final Command COMMAND = new Command("command", PARAMETERS);
150c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        String[] parameters = COMMAND.getParameters();
151c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        parameters[2] = "xxx";
152c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals("parameters", PARAMETERS, COMMAND.getParameters());
153c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
154c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
155c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
156c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test the equals() method, and tests the hasCode() method implicitly
157c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     *
158c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @throws Exception
159c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
160c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public void testEquals() throws Exception {
161c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final Command COMMAND1 = new Command("a", EMPTY);
162c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final Command COMMAND2 = new Command("a", EMPTY);
163c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final Command COMMAND3 = new Command("b", array("1"));
164c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final Command COMMAND4 = new Command("b", array("2"));
165c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final Command COMMAND5 = new Command("c", array("1"));
166c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        _testEquals(COMMAND1, null, false);
167c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        _testEquals(COMMAND1, COMMAND1, true);
168c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        _testEquals(COMMAND1, COMMAND2, true);
169c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        _testEquals(COMMAND1, COMMAND3, false);
170c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        _testEquals(COMMAND3, COMMAND4, false);
171c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        _testEquals(COMMAND3, COMMAND5, false);
172c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
173c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
174c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
175c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Test that command1 equals command2 if and only if expectedEqual is true
176c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     *
177c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @param command1      - the first command
178c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @param command2      - the second command
179c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @param expectedEqual - true if command1 is expected to equal command2
180c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
181c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    private void _testEquals(Command command1, Command command2, boolean expectedEqual) {
182c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        assertEquals(command1.toString() + " and " + command2, expectedEqual, command1.equals(command2));
183c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
184c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
185c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair}
186