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