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