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