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.stub.command; 17 18import java.util.ListResourceBundle; 19import java.util.ResourceBundle; 20 21import org.apache.log4j.Logger; 22import org.easymock.MockControl; 23import org.mockftpserver.core.command.Command; 24import org.mockftpserver.core.command.InvocationRecord; 25import org.mockftpserver.core.session.Session; 26import org.mockftpserver.core.util.AssertFailedException; 27import org.mockftpserver.test.AbstractTest; 28 29/** 30 * Tests for the AbstractStubCommandHandler class. The class name is prefixed with an underscore 31 * so that it is not filtered out by Maven's Surefire test plugin. 32 * 33 * @version $Revision$ - $Date$ 34 * 35 * @author Chris Mair 36 */ 37public final class _AbstractStubCommandHandlerTest extends AbstractTest { 38 39 private static final Logger LOG = Logger.getLogger(_AbstractStubCommandHandlerTest.class); 40 private static final int REPLY_CODE1 = 777; 41 private static final int REPLY_CODE2 = 888; 42 private static final String REPLY_TEXT1 = "reply1 ... abcdef"; 43 private static final String REPLY_TEXT2 = "abc {0} def"; 44 private static final String REPLY_TEXT2_FORMATTED = "abc 123 def"; 45 private static final String MESSAGE_KEY = "key.123"; 46 private static final String MESSAGE_TEXT = "message.123"; 47 private static final String OVERRIDE_REPLY_TEXT = "overridden reply ... abcdef"; 48 private static final Object ARG = "123"; 49 50 private AbstractStubCommandHandler commandHandler; 51 private Session session; 52 private ResourceBundle replyTextBundle; 53 54 /** 55 * Test the sendReply(Session) method 56 */ 57 public void testSendReply() { 58 session.sendReply(REPLY_CODE1, REPLY_TEXT1); 59 replay(session); 60 61 commandHandler.setReplyCode(REPLY_CODE1); 62 commandHandler.sendReply(session); 63 verify(session); 64 } 65 66 /** 67 * Test the sendReply(Session) method, when the replyText has been set 68 */ 69 public void testSendReply_SetReplyText() { 70 session.sendReply(REPLY_CODE1, OVERRIDE_REPLY_TEXT); 71 replay(session); 72 73 commandHandler.setReplyCode(REPLY_CODE1); 74 commandHandler.setReplyText(OVERRIDE_REPLY_TEXT); 75 commandHandler.sendReply(session); 76 verify(session); 77 } 78 79 /** 80 * Test the sendReply(Session) method, when the replyMessageKey has been set 81 */ 82 public void testSendReply_SetReplyMessageKey() { 83 session.sendReply(REPLY_CODE1, REPLY_TEXT2); 84 replay(session); 85 86 commandHandler.setReplyCode(REPLY_CODE1); 87 commandHandler.setReplyMessageKey(Integer.toString(REPLY_CODE2)); 88 commandHandler.sendReply(session); 89 verify(session); 90 } 91 92 /** 93 * Test the sendReply(Session) method, when the replyCode has not been set 94 */ 95 public void testSendReply_ReplyCodeNotSet() { 96 try { 97 commandHandler.sendReply(session); 98 fail("Expected AssertFailedException"); 99 } 100 catch (AssertFailedException expected) { 101 LOG.info("Expected: " + expected); 102 } 103 } 104 105 /** 106 * Test the sendReply(Session,Object) method 107 */ 108 public void testSendReply_MessageParameter() { 109 session.sendReply(REPLY_CODE2, REPLY_TEXT2); 110 session.sendReply(REPLY_CODE2, REPLY_TEXT2_FORMATTED); 111 replay(session); 112 113 commandHandler.setReplyCode(REPLY_CODE2); 114 commandHandler.sendReply(session); 115 commandHandler.sendReply(session, ARG); 116 verify(session); 117 } 118 119 /** 120 * Test the setReplyCode() method, passing in an invalid value 121 */ 122 public void testSetReplyCode_Invalid() { 123 try { 124 commandHandler.setReplyCode(0); 125 fail("Expected AssertFailedException"); 126 } 127 catch (AssertFailedException expected) { 128 LOG.info("Expected: " + expected); 129 } 130 } 131 132 //------------------------------------------------------------------------- 133 // Test setup 134 //------------------------------------------------------------------------- 135 136 /** 137 * Perform initialization before each test 138 * @see org.mockftpserver.test.AbstractTest#setUp() 139 */ 140 protected void setUp() throws Exception { 141 super.setUp(); 142 session = (Session) createMock(Session.class); 143 control(session).setDefaultMatcher(MockControl.ARRAY_MATCHER); 144 commandHandler = new AbstractStubCommandHandler() { 145 public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception { 146 } 147 }; 148 replyTextBundle = new ListResourceBundle() { 149 protected Object[][] getContents() { 150 return new Object[][] { 151 { Integer.toString(REPLY_CODE1), REPLY_TEXT1 }, 152 { Integer.toString(REPLY_CODE2), REPLY_TEXT2 }, 153 { MESSAGE_KEY, MESSAGE_TEXT } 154 }; 155 } 156 }; 157 commandHandler.setReplyTextBundle(replyTextBundle); 158 } 159 160} 161