summaryrefslogtreecommitdiff
path: root/MockFtpServer/src/test/java/org/mockftpserver/stub/command/CommandTest.java
blob: a12175f172e1784b2f0db3bec612ba1c727cad08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright 2007 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockftpserver.stub.command;

import java.util.List;

import org.apache.log4j.Logger;
import org.mockftpserver.core.command.Command;
import org.mockftpserver.core.util.AssertFailedException;
import org.mockftpserver.test.AbstractTest;

/**
 * Tests for the Command class
 * 
 * @version $Revision$ - $Date$
 * 
 * @author Chris Mair
 */
public final class CommandTest extends AbstractTest {

    private static final Logger LOG = Logger.getLogger(CommandTest.class);
    
    /**
     * Test the Command(String,String[]) constructor
     */
    public void testConstructor() {
        final String[] PARAMETERS = array("123");
        Command command = new Command("abc", PARAMETERS);
        assertEquals("name", "abc", command.getName());
        assertEquals("parameters", PARAMETERS, command.getParameters());
    }
    
    /**
     * Test the Command(String,List) constructor
     */
    public void testConstructor_List() {
        final List PARAMETERS_LIST = list("123");
        final String[] PARAMETERS_ARRAY = array("123");
        Command command = new Command("abc", PARAMETERS_LIST);
        assertEquals("name", "abc", command.getName());
        assertEquals("parameters String[]", PARAMETERS_ARRAY, command.getParameters());
    }
    
    /**
     * Test the Constructor method, passing in a null name
     */
    public void testConstructor_NullName() {
        try {
            new Command(null, EMPTY);
            fail("Expected AssertFailedException");
        }
        catch (AssertFailedException expected) {
            LOG.info("Expected: " + expected);
        }
    }
    
    /**
     * Test the Constructor method, passing in a null parameters
     */
    public void testConstructor_NullParameters() {
        try {
            new Command("OK", (String[])null);
            fail("Expected AssertFailedException");
        }
        catch (AssertFailedException expected) {
            LOG.info("Expected: " + expected);
        }
    }

    /**
     * Test the normalizeName() method 
     */
    public void testNormalizeName() {
        assertEquals("XXX", "XXX", Command.normalizeName("XXX"));
        assertEquals("xxx", "XXX", Command.normalizeName("xxx"));
        assertEquals("Xxx", "XXX", Command.normalizeName("Xxx"));
    }
    
    /**
     * Test the getRequiredString method
     */
    public void testGetRequiredString() {
        Command command = new Command("abc", array("123", "456"));
        assertEquals("123", "123", command.getRequiredString(0));
        assertEquals("456", "456", command.getRequiredString(1));
    }
    
    /**
     * Test the getRequiredString method, when the index is not valid
     */
    public void testGetRequiredString_IndexNotValid() {
        Command command = new Command("abc", array("123", "456"));
        try {
            command.getRequiredString(2);
            fail("Expected AssertFailedException");
        }
        catch (AssertFailedException expected) {
            LOG.info("Expected: " + expected);
        }
    }
    
    /**
     * Test the getOptionalString method
     */
    public void testGetOptionalString() {
        Command command = new Command("abc", array("123", "456"));
        assertEquals("123", "123", command.getOptionalString(0));
        assertEquals("456", "456", command.getOptionalString(1));
        assertEquals("null", null, command.getOptionalString(2));
    }
    
    /**
     * Test that a Command object is immutable, changing the original parameters passed in to the constructor
     */
    public void testImmutable_ChangeOriginalParameters() {
        final String[] PARAMETERS = { "a", "b", "c" };
        final Command COMMAND = new Command("command", PARAMETERS);
        PARAMETERS[2] = "xxx";
        assertEquals("parameters", COMMAND.getParameters(), new String[] { "a", "b", "c" });
    }
    
    /**
     * Test that a Command object is immutable, changing the parameters returned from getParameters
     */
    public void testImmutable_ChangeRetrievedParameters() {
        final String[] PARAMETERS = { "a", "b", "c" };
        final Command COMMAND = new Command("command", PARAMETERS);
        String[] parameters = COMMAND.getParameters();
        parameters[2] = "xxx";
        assertEquals("parameters", PARAMETERS, COMMAND.getParameters());
    }
    
    /**
     * Test the equals() method, and tests the hasCode() method implicitly
     * @throws Exception
     */
    public void testEquals() throws Exception {
        final Command COMMAND1 = new Command("a", EMPTY);
        final Command COMMAND2 = new Command("a", EMPTY);
        final Command COMMAND3 = new Command("b", array("1"));
        final Command COMMAND4 = new Command("b", array("2"));
        final Command COMMAND5 = new Command("c", array("1"));
        _testEquals(COMMAND1, null, false);
        _testEquals(COMMAND1, COMMAND1, true);
        _testEquals(COMMAND1, COMMAND2, true);
        _testEquals(COMMAND1, COMMAND3, false);
        _testEquals(COMMAND3, COMMAND4, false);
        _testEquals(COMMAND3, COMMAND5, false);
    }

    /**
     * Test that command1 equals command2 if and only if expectedEqual is true
     * @param command1 - the first command
     * @param command2 - the second command
     * @param expectedEqual - true if command1 is expected to equal command2
     */
    private void _testEquals(Command command1, Command command2, boolean expectedEqual) {
        assertEquals(command1.toString() + " and " + command2, expectedEqual, command1.equals(command2));
    }
    
}