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 */ 16 package org.mockftpserver.core.command; 17 18 import org.slf4j.Logger; 19 import org.slf4j.LoggerFactory; 20 import org.easymock.MockControl; 21 import org.mockftpserver.core.session.Session; 22 import org.mockftpserver.test.AbstractTestCase; 23 24 import java.text.MessageFormat; 25 import java.util.ListResourceBundle; 26 import java.util.ResourceBundle; 27 28 /** 29 * Abstract superclass for CommandHandler tests 30 * 31 * @author Chris Mair 32 * @version $Revision$ - $Date$ 33 */ 34 public abstract class AbstractCommandHandlerTestCase extends AbstractTestCase { 35 36 private static final Logger LOG = LoggerFactory.getLogger(AbstractCommandHandlerTestCase.class); 37 38 // Some common test constants 39 protected static final String DIR1 = "dir1"; 40 protected static final String DIR2 = "dir2"; 41 protected static final String FILENAME1 = "sample1.txt"; 42 protected static final String FILENAME2 = "sample2.txt"; 43 44 protected Session session; 45 protected ResourceBundle replyTextBundle; 46 47 /** 48 * Test the handleCommand() method, when one or more parameter is missing or invalid 49 * 50 * @param commandHandler - the CommandHandler to test 51 * @param commandName - the name for the Command 52 * @param parameters - the Command parameters 53 */ 54 protected void testHandleCommand_InvalidParameters(AbstractTrackingCommandHandler commandHandler, 55 String commandName, String[] parameters) throws Exception { 56 Command command = new Command(commandName, parameters); 57 session.sendReply(ReplyCodes.COMMAND_SYNTAX_ERROR, replyTextFor(ReplyCodes.COMMAND_SYNTAX_ERROR)); 58 replay(session); 59 60 commandHandler.handleCommand(command, session); 61 verify(session); 62 63 verifyNumberOfInvocations(commandHandler, 1); 64 verifyNoDataElements(commandHandler.getInvocation(0)); 65 } 66 67 /** 68 * Verify that the CommandHandler contains the specified number of invocation records 69 * 70 * @param commandHandler - the CommandHandler 71 * @param expected - the expected number of invocations 72 */ 73 protected void verifyNumberOfInvocations(InvocationHistory commandHandler, int expected) { 74 assertEquals("number of invocations", expected, commandHandler.numberOfInvocations()); 75 } 76 77 /** 78 * Verify that the InvocationRecord contains no data elements 79 * 80 * @param invocationRecord - the InvocationRecord 81 */ 82 protected void verifyNoDataElements(InvocationRecord invocationRecord) { 83 LOG.info("Verifying: " + invocationRecord); 84 assertEquals("number of data elements", 0, invocationRecord.keySet().size()); 85 } 86 87 /** 88 * Verify that the InvocationRecord contains exactly one data element, with the specified key 89 * and value. 90 * 91 * @param invocationRecord - the InvocationRecord 92 * @param key - the expected key 93 * @param value - the expected value 94 */ 95 protected void verifyOneDataElement(InvocationRecord invocationRecord, String key, Object value) { 96 LOG.info("Verifying: " + invocationRecord); 97 assertEquals("number of data elements", 1, invocationRecord.keySet().size()); 98 assertEqualsAllTypes("value:" + value, value, invocationRecord.getObject(key)); 99 } 100 101 /** 102 * Verify that the InvocationRecord contains exactly two data element, with the specified keys 103 * and values. 104 * 105 * @param invocationRecord - the InvocationRecord 106 * @param key1 - the expected key1 107 * @param value1 - the expected value1 108 * @param key2 - the expected key2 109 * @param value2- the expected value2 110 */ 111 protected void verifyTwoDataElements(InvocationRecord invocationRecord, String key1, Object value1, 112 String key2, Object value2) { 113 114 LOG.info("Verifying: " + invocationRecord); 115 assertEquals("number of data elements", 2, invocationRecord.keySet().size()); 116 assertEqualsAllTypes("value1:" + value1, value1, invocationRecord.getObject(key1)); 117 assertEqualsAllTypes("value2:" + value2, value2, invocationRecord.getObject(key2)); 118 } 119 120 /** 121 * Assert that the actual is equal to the expected, using arrays equality comparison if 122 * necessary 123 * 124 * @param message - the message, used if the comparison fails 125 * @param expected - the expected value 126 * @param actual - the actual value 127 */ 128 private void assertEqualsAllTypes(String message, Object expected, Object actual) { 129 130 if (expected instanceof byte[] || actual instanceof byte[]) { 131 assertEquals(message, (byte[]) expected, (byte[]) actual); 132 } else if (expected instanceof Object[] || actual instanceof Object[]) { 133 assertEquals(message, (Object[]) expected, (Object[]) actual); 134 } else { 135 assertEquals(message, expected, actual); 136 } 137 } 138 139 /** 140 * Perform setup before each test 141 * 142 * @see org.mockftpserver.test.AbstractTestCase#setUp() 143 */ 144 protected void setUp() throws Exception { 145 super.setUp(); 146 147 session = (Session) createMock(Session.class); 148 control(session).setDefaultMatcher(MockControl.ARRAY_MATCHER); 149 control(session).expectAndDefaultReturn(session.getClientHost(), DEFAULT_HOST); 150 151 replyTextBundle = new ListResourceBundle() { 152 protected Object[][] getContents() { 153 return new Object[][]{ 154 {"150", replyTextFor(150)}, 155 {"200", replyTextFor(200)}, 156 {"211", replyTextWithParameterFor(211)}, 157 {"213", replyTextWithParameterFor(213)}, 158 {"214", replyTextWithParameterFor(214)}, 159 {"215", replyTextWithParameterFor(215)}, 160 {"220", replyTextFor(220)}, 161 {"221", replyTextFor(221)}, 162 {"226", replyTextFor(226)}, 163 {"226.WithFilename", replyTextWithParameterFor("226.WithFilename")}, 164 {"227", replyTextWithParameterFor(227)}, 165 {"229", replyTextWithParameterFor(229)}, 166 {"230", replyTextFor(230)}, 167 {"250", replyTextFor(250)}, 168 {"257", replyTextWithParameterFor(257)}, 169 {"331", replyTextFor(331)}, 170 {"350", replyTextFor(350)}, 171 {"501", replyTextFor(501)}, 172 {"502", replyTextFor(502)}, 173 }; 174 } 175 }; 176 } 177 178 /** 179 * Return the test-specific reply text for the specified reply code 180 * 181 * @param replyCode - the reply code 182 * @return the reply text for the specified reply code 183 */ 184 protected String replyTextFor(int replyCode) { 185 return "Reply for " + replyCode; 186 } 187 188 /** 189 * Return the test-specific parameterized reply text for the specified reply code 190 * 191 * @param replyCode - the reply code 192 * @return the reply text for the specified reply code 193 */ 194 protected String replyTextWithParameterFor(int replyCode) { 195 return "Reply for " + replyCode + ":{0}"; 196 } 197 198 /** 199 * Return the test-specific parameterized reply text for the specified messageKey 200 * 201 * @param messageKey - the messageKey 202 * @return the reply text for the specified messageKey 203 */ 204 protected String replyTextWithParameterFor(String messageKey) { 205 return "Reply for " + messageKey + ":{0}"; 206 } 207 208 /** 209 * Return the test-specific reply text for the specified reply code and message parameter 210 * 211 * @param replyCode - the reply code 212 * @param parameter - the message parameter value 213 * @return the reply text for the specified reply code 214 */ 215 protected String formattedReplyTextFor(int replyCode, Object parameter) { 216 return MessageFormat.format(replyTextWithParameterFor(replyCode), objArray(parameter)); 217 } 218 219 /** 220 * Return the test-specific reply text for the specified message key and message parameter 221 * 222 * @param messageKey - the messageKey 223 * @param parameter - the message parameter value 224 * @return the reply text for the specified message key and parameter 225 */ 226 protected String formattedReplyTextFor(String messageKey, Object parameter) { 227 return MessageFormat.format(replyTextWithParameterFor(messageKey), objArray(parameter)); 228 } 229 230 } 231