chrismair | 00dc7bd | 2014-05-11 21:21:28 +0000 | [diff] [blame] | 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.mockftpserver.core.session.Session;
|
| 21 | import org.mockftpserver.core.util.AssertFailedException;
|
| 22 | import org.mockftpserver.test.AbstractTestCase;
|
| 23 |
|
| 24 | import java.util.ArrayList;
|
| 25 | import java.util.List;
|
| 26 | import java.util.ListResourceBundle;
|
| 27 | import java.util.ResourceBundle;
|
| 28 |
|
| 29 | /**
|
| 30 | * Tests for SimpleCompositeCommandHandler
|
| 31 | *
|
| 32 | * @version $Revision$ - $Date$
|
| 33 | *
|
| 34 | * @author Chris Mair
|
| 35 | */
|
| 36 | public class SimpleCompositeCommandHandlerTest extends AbstractTestCase {
|
| 37 |
|
| 38 | private static final Logger LOG = LoggerFactory.getLogger(SimpleCompositeCommandHandlerTest.class);
|
| 39 |
|
| 40 | private SimpleCompositeCommandHandler simpleCompositeCommandHandler;
|
| 41 | private Session session;
|
| 42 | private Command command;
|
| 43 | private CommandHandler commandHandler1;
|
| 44 | private CommandHandler commandHandler2;
|
| 45 | private CommandHandler commandHandler3;
|
| 46 |
|
| 47 | /**
|
| 48 | * Test the handleCommand() method
|
| 49 | */
|
| 50 | public void testHandleCommand_OneHandler_OneInvocation() throws Exception {
|
| 51 | simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
|
| 52 |
|
| 53 | commandHandler1.handleCommand(command, session);
|
| 54 | replay(commandHandler1);
|
| 55 |
|
| 56 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 57 | verify(commandHandler1);
|
| 58 | }
|
| 59 |
|
| 60 | /**
|
| 61 | * Test the handleCommand() method, with two CommandHandler defined, but with multiple invocation
|
| 62 | */
|
| 63 | public void testHandleCommand_TwoHandlers() throws Exception {
|
| 64 | simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
|
| 65 | simpleCompositeCommandHandler.addCommandHandler(commandHandler2);
|
| 66 |
|
| 67 | commandHandler1.handleCommand(command, session);
|
| 68 | commandHandler2.handleCommand(command, session);
|
| 69 | replayAll();
|
| 70 |
|
| 71 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 72 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 73 | verifyAll();
|
| 74 | }
|
| 75 |
|
| 76 | /**
|
| 77 | * Test the handleCommand() method, with three CommandHandler defined, and multiple invocation
|
| 78 | */
|
| 79 | public void testHandleCommand_ThreeHandlers() throws Exception {
|
| 80 |
|
| 81 | List list = new ArrayList();
|
| 82 | list.add(commandHandler1);
|
| 83 | list.add(commandHandler2);
|
| 84 | list.add(commandHandler3);
|
| 85 | simpleCompositeCommandHandler.setCommandHandlers(list);
|
| 86 |
|
| 87 | commandHandler1.handleCommand(command, session);
|
| 88 | commandHandler2.handleCommand(command, session);
|
| 89 | commandHandler3.handleCommand(command, session);
|
| 90 | replayAll();
|
| 91 |
|
| 92 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 93 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 94 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 95 | verifyAll();
|
| 96 | }
|
| 97 |
|
| 98 | /**
|
| 99 | * Test the handleCommand() method, with a single CommandHandler defined, but too many invocations
|
| 100 | */
|
| 101 | public void testHandleCommand_OneHandler_TooManyInvocations() throws Exception {
|
| 102 | simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
|
| 103 |
|
| 104 | commandHandler1.handleCommand(command, session);
|
| 105 | replay(commandHandler1);
|
| 106 |
|
| 107 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 108 |
|
| 109 | // Second invocation throws an exception
|
| 110 | try {
|
| 111 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 112 | fail("Expected AssertFailedException");
|
| 113 | }
|
| 114 | catch (AssertFailedException expected) {
|
| 115 | LOG.info("Expected: " + expected);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | /**
|
| 120 | * Test the handleCommand_NoHandlersDefined() method
|
| 121 | */
|
| 122 | public void testHandleCommand_NoHandlersDefined() throws Exception {
|
| 123 | try {
|
| 124 | simpleCompositeCommandHandler.handleCommand(command, session);
|
| 125 | fail("Expected AssertFailedException");
|
| 126 | }
|
| 127 | catch(AssertFailedException expected) {
|
| 128 | LOG.info("Expected: " + expected);
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | /**
|
| 133 | * Test the handleCommand(Command,Session) method, passing in a null Command
|
| 134 | */
|
| 135 | public void testHandleCommand_NullCommand() throws Exception {
|
| 136 | try {
|
| 137 | simpleCompositeCommandHandler.handleCommand(null, session);
|
| 138 | fail("Expected AssertFailedException");
|
| 139 | }
|
| 140 | catch (AssertFailedException expected) {
|
| 141 | LOG.info("Expected: " + expected);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | /**
|
| 146 | * Test the handleCommand(Command,Session) method, passing in a null Session
|
| 147 | */
|
| 148 | public void testHandleCommand_NullSession() throws Exception {
|
| 149 | try {
|
| 150 | simpleCompositeCommandHandler.handleCommand(command, null);
|
| 151 | fail("Expected AssertFailedException");
|
| 152 | }
|
| 153 | catch (AssertFailedException expected) {
|
| 154 | LOG.info("Expected: " + expected);
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | /**
|
| 159 | * Test the addCommandHandler(CommandHandler) method, passing in a null CommandHandler
|
| 160 | */
|
| 161 | public void testAddCommandHandler_NullCommandHandler() throws Exception {
|
| 162 | try {
|
| 163 | simpleCompositeCommandHandler.addCommandHandler(null);
|
| 164 | fail("Expected AssertFailedException");
|
| 165 | }
|
| 166 | catch (AssertFailedException expected) {
|
| 167 | LOG.info("Expected: " + expected);
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | /**
|
| 172 | * Test the setCommandHandlers(List) method, passing in a null
|
| 173 | */
|
| 174 | public void testSetCommandHandlers_Null() throws Exception {
|
| 175 | try {
|
| 176 | simpleCompositeCommandHandler.setCommandHandlers(null);
|
| 177 | fail("Expected AssertFailedException");
|
| 178 | }
|
| 179 | catch (AssertFailedException expected) {
|
| 180 | LOG.info("Expected: " + expected);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | /**
|
| 185 | * Test the getCommandHandler(int) method, passing in an index for which no CommandHandler is defined
|
| 186 | */
|
| 187 | public void testGetCommandHandler_UndefinedIndex() throws Exception {
|
| 188 | simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
|
| 189 | try {
|
| 190 | simpleCompositeCommandHandler.getCommandHandler(1);
|
| 191 | fail("Expected AssertFailedException");
|
| 192 | }
|
| 193 | catch (AssertFailedException expected) {
|
| 194 | LOG.info("Expected: " + expected);
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | /**
|
| 199 | * Test the getCommandHandler(int) method
|
| 200 | */
|
| 201 | public void testGetCommandHandler() throws Exception {
|
| 202 | simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
|
| 203 | simpleCompositeCommandHandler.addCommandHandler(commandHandler2);
|
| 204 | assertSame("index 0", commandHandler1, simpleCompositeCommandHandler.getCommandHandler(0));
|
| 205 | assertSame("index 1", commandHandler2, simpleCompositeCommandHandler.getCommandHandler(1));
|
| 206 | }
|
| 207 |
|
| 208 | /**
|
| 209 | * Test the getCommandHandler(int) method, passing in a negative index
|
| 210 | */
|
| 211 | public void testGetCommandHandler_NegativeIndex() throws Exception {
|
| 212 | simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
|
| 213 | try {
|
| 214 | simpleCompositeCommandHandler.getCommandHandler(-1);
|
| 215 | fail("Expected AssertFailedException");
|
| 216 | }
|
| 217 | catch (AssertFailedException expected) {
|
| 218 | LOG.info("Expected: " + expected);
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | /**
|
| 223 | * Test the getReplyTextBundle() method
|
| 224 | */
|
| 225 | public void testGetReplyTextBundle() {
|
| 226 | assertNull(simpleCompositeCommandHandler.getReplyTextBundle());
|
| 227 | }
|
| 228 |
|
| 229 | /**
|
| 230 | * Test the setReplyTextBundle() method
|
| 231 | */
|
| 232 | public void testSetReplyTextBundle() {
|
| 233 |
|
| 234 | AbstractTrackingCommandHandler replyTextBundleAwareCommandHandler1 = new StaticReplyCommandHandler();
|
| 235 | AbstractTrackingCommandHandler replyTextBundleAwareCommandHandler2 = new StaticReplyCommandHandler();
|
| 236 | simpleCompositeCommandHandler.addCommandHandler(replyTextBundleAwareCommandHandler1);
|
| 237 | simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
|
| 238 | simpleCompositeCommandHandler.addCommandHandler(replyTextBundleAwareCommandHandler2);
|
| 239 |
|
| 240 | ResourceBundle resourceBundle = new ListResourceBundle() {
|
| 241 | protected Object[][] getContents() {
|
| 242 | return null;
|
| 243 | }
|
| 244 | };
|
| 245 |
|
| 246 | simpleCompositeCommandHandler.setReplyTextBundle(resourceBundle);
|
| 247 | assertSame("1", resourceBundle, replyTextBundleAwareCommandHandler1.getReplyTextBundle());
|
| 248 | assertSame("2", resourceBundle, replyTextBundleAwareCommandHandler1.getReplyTextBundle());
|
| 249 | }
|
| 250 |
|
| 251 | //-------------------------------------------------------------------------
|
| 252 | // Test setup
|
| 253 | //-------------------------------------------------------------------------
|
| 254 |
|
| 255 | /**
|
| 256 | * Perform initialization before each test
|
| 257 | * @see org.mockftpserver.test.AbstractTestCase#setUp()
|
| 258 | */
|
| 259 | protected void setUp() throws Exception {
|
| 260 | super.setUp();
|
| 261 | simpleCompositeCommandHandler = new SimpleCompositeCommandHandler();
|
| 262 | session = (Session) createMock(Session.class);
|
| 263 | command = new Command("cmd", EMPTY);
|
| 264 | commandHandler1 = (CommandHandler) createMock(CommandHandler.class);
|
| 265 | commandHandler2 = (CommandHandler) createMock(CommandHandler.class);
|
| 266 | commandHandler3 = (CommandHandler) createMock(CommandHandler.class);
|
| 267 | }
|
| 268 |
|
| 269 | }
|