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