AbstractFtpServerTestCase.java revision 40658190151b7ded3489ff89c301b470155c95f4
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.server; 17 18import org.apache.log4j.Logger; 19import org.mockftpserver.core.command.CommandHandler; 20import org.mockftpserver.core.command.CommandNames; 21import org.mockftpserver.core.session.DefaultSession; 22import org.mockftpserver.core.util.AssertFailedException; 23import org.mockftpserver.test.AbstractTestCase; 24 25import java.net.Socket; 26import java.util.HashMap; 27import java.util.Map; 28 29/** 30 * Abstract superclass for tests of AbstractFtpServer subclasses. 31 * 32 * @author Chris Mair 33 * @version $Revision$ - $Date$ 34 */ 35public abstract class AbstractFtpServerTestCase extends AbstractTestCase { 36 37 protected Logger LOG = Logger.getLogger(getClass()); 38 39 protected AbstractFtpServer ftpServer; 40 private CommandHandler commandHandler; 41 private CommandHandler commandHandler2; 42 43 /** 44 * Test the setCommandHandlers() method 45 */ 46 public void testSetCommandHandlers() { 47 Map mapping = new HashMap(); 48 mapping.put("AAA", commandHandler); 49 mapping.put("BBB", commandHandler2); 50 51 ftpServer.setCommandHandlers(mapping); 52 assertSame("commandHandler1", commandHandler, ftpServer.getCommandHandler("AAA")); 53 assertSame("commandHandler2", commandHandler2, ftpServer.getCommandHandler("BBB")); 54 55 verifyCommandHandlerInitialized(commandHandler); 56 verifyCommandHandlerInitialized(commandHandler2); 57 58 // Make sure default CommandHandlers are still set 59 assertTrue("ConnectCommandHandler", ftpServer.getCommandHandler(CommandNames.CONNECT) != null); 60 } 61 62 /** 63 * Test the setCommandHandlers() method, when the Map is null 64 */ 65 public void testSetCommandHandlers_Null() { 66 try { 67 ftpServer.setCommandHandlers(null); 68 fail("Expected AssertFailedException"); 69 } 70 catch (AssertFailedException expected) { 71 LOG.info("Expected: " + expected); 72 } 73 } 74 75 /** 76 * Test the setCommandHandler() method 77 */ 78 public void testSetCommandHandler() { 79 ftpServer.setCommandHandler("ZZZ", commandHandler2); 80 assertSame("commandHandler", commandHandler2, ftpServer.getCommandHandler("ZZZ")); 81 verifyCommandHandlerInitialized(commandHandler2); 82 } 83 84 /** 85 * Test the setCommandHandler() method, when the commandName is null 86 */ 87 public void testSetCommandHandler_NullCommandName() { 88 CommandHandler commandHandler = (CommandHandler) createMock(CommandHandler.class); 89 try { 90 ftpServer.setCommandHandler(null, commandHandler); 91 fail("Expected AssertFailedException"); 92 } 93 catch (AssertFailedException expected) { 94 LOG.info("Expected: " + expected); 95 } 96 } 97 98 /** 99 * Test the setCommandHandler() method, when the commandHandler is null 100 */ 101 public void testSetCommandHandler_NullCommandHandler() { 102 try { 103 ftpServer.setCommandHandler("ZZZ", null); 104 fail("Expected AssertFailedException"); 105 } 106 catch (AssertFailedException expected) { 107 LOG.info("Expected: " + expected); 108 } 109 } 110 111 public void testSetServerControlPort() { 112 assertEquals("default", 21, ftpServer.getServerControlPort()); 113 ftpServer.setServerControlPort(99); 114 assertEquals("99", 99, ftpServer.getServerControlPort()); 115 } 116 117 /** 118 * Test the setCommandHandler() and getCommandHandler() methods for commands in lower case or mixed case 119 */ 120 public void testLowerCaseOrMixedCaseCommandNames() { 121 ftpServer.setCommandHandler("XXX", commandHandler); 122 assertSame("ZZZ", commandHandler, ftpServer.getCommandHandler("XXX")); 123 assertSame("Zzz", commandHandler, ftpServer.getCommandHandler("Xxx")); 124 assertSame("zzz", commandHandler, ftpServer.getCommandHandler("xxx")); 125 126 ftpServer.setCommandHandler("YyY", commandHandler); 127 assertSame("ZZZ", commandHandler, ftpServer.getCommandHandler("YYY")); 128 assertSame("Zzz", commandHandler, ftpServer.getCommandHandler("Yyy")); 129 assertSame("zzz", commandHandler, ftpServer.getCommandHandler("yyy")); 130 131 ftpServer.setCommandHandler("zzz", commandHandler); 132 assertSame("ZZZ", commandHandler, ftpServer.getCommandHandler("ZZZ")); 133 assertSame("Zzz", commandHandler, ftpServer.getCommandHandler("zzZ")); 134 assertSame("zzz", commandHandler, ftpServer.getCommandHandler("zzz")); 135 } 136 137 /** 138 * Test calling stop() for a server that was never started. 139 */ 140 public void testStopWithoutStart() { 141 ftpServer.stop(); 142 } 143 144 public void testCreateSession() { 145 assertEquals(ftpServer.createSession(new Socket()).getClass(), DefaultSession.class); 146 } 147 148 //------------------------------------------------------------------------- 149 // Test setup 150 //------------------------------------------------------------------------- 151 152 /** 153 * @see org.mockftpserver.test.AbstractTestCase#setUp() 154 */ 155 protected void setUp() throws Exception { 156 super.setUp(); 157 158 ftpServer = createFtpServer(); 159 160 commandHandler = createCommandHandler(); 161 commandHandler2 = createCommandHandler(); 162 } 163 164 //------------------------------------------------------------------------- 165 // Abstract method declarations 166 //------------------------------------------------------------------------- 167 168 protected abstract AbstractFtpServer createFtpServer(); 169 170 protected abstract CommandHandler createCommandHandler(); 171 172 protected abstract void verifyCommandHandlerInitialized(CommandHandler commandHandler); 173 174}