blob: 5cc98f2933c5d00d092517be1241d51dc949c1dd [file] [log] [blame]
chrismair00dc7bd2014-05-11 21:21:28 +00001/*
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.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20import org.easymock.MockControl;
21import org.mockftpserver.core.session.Session;
22import org.mockftpserver.core.util.AssertFailedException;
23import org.mockftpserver.test.AbstractTestCase;
24
25import java.util.ListResourceBundle;
26import java.util.ResourceBundle;
27
28/**
29 * Tests for the AbstractTrackingCommandHandler class. The class name is prefixed with an
30 * underscore so that it is not filtered out by Maven's Surefire test plugin.
31 *
32 * @author Chris Mair
33 * @version $Revision$ - $Date$
34 */
35public final class _AbstractTrackingCommandHandlerTest extends AbstractTestCase {
36
37 private static final Logger LOG = LoggerFactory.getLogger(_AbstractTrackingCommandHandlerTest.class);
38 private static final String COMMAND_NAME = "abc";
39 private static final Object ARG = "123";
40 private static final Object[] ARGS = {ARG};
41 private static final Command COMMAND = new Command(COMMAND_NAME, EMPTY);
42 private static final Command COMMAND_WITH_ARGS = new Command(COMMAND_NAME, EMPTY);
43 private static final int REPLY_CODE1 = 777;
44 private static final int REPLY_CODE2 = 888;
45 private static final int REPLY_CODE3 = 999;
46 private static final String REPLY_TEXT1 = "reply1 ... abcdef";
47 private static final String REPLY_TEXT2 = "abc {0} def";
48 private static final String REPLY_TEXT2_FORMATTED = "abc 123 def";
49 private static final String OVERRIDE_REPLY_TEXT = "overridden reply ... abcdef";
50 private static final String MESSAGE_KEY = "key.123";
51 private static final String MESSAGE_TEXT = "message.123";
52
53 private AbstractTrackingCommandHandler commandHandler;
54 private Session session;
55
56 /**
57 * Test the handleCommand(Command,Session) method
58 */
59 public void testHandleCommand() throws Exception {
60 assertEquals("before", 0, commandHandler.numberOfInvocations());
61 commandHandler.handleCommand(COMMAND, session);
62 assertEquals("after", 1, commandHandler.numberOfInvocations());
63 assertTrue("locked", commandHandler.getInvocation(0).isLocked());
64 }
65
66 /**
67 * Test the handleCommand(Command,Session) method, passing in a null Command
68 */
69 public void testHandleCommand_NullCommand() throws Exception {
70 try {
71 commandHandler.handleCommand(null, session);
72 fail("Expected AssertFailedException");
73 }
74 catch (AssertFailedException expected) {
75 LOG.info("Expected: " + expected);
76 }
77 }
78
79 /**
80 * Test the handleCommand(Command,Session) method, passing in a null Session
81 */
82 public void testHandleCommand_NullSession() throws Exception {
83 try {
84 commandHandler.handleCommand(COMMAND, null);
85 fail("Expected AssertFailedException");
86 }
87 catch (AssertFailedException expected) {
88 LOG.info("Expected: " + expected);
89 }
90 }
91
92 /**
93 * Test the numberOfInvocations(), addInvocationRecord() and clearInvocationRecord() methods
94 */
95 public void testInvocationHistory() throws Exception {
96 control(session).expectAndDefaultReturn(session.getClientHost(), DEFAULT_HOST);
97 replay(session);
98
99 assertEquals("none", 0, commandHandler.numberOfInvocations());
100 commandHandler.handleCommand(COMMAND, session);
101 assertEquals("1", 1, commandHandler.numberOfInvocations());
102 commandHandler.handleCommand(COMMAND, session);
103 assertEquals("2", 2, commandHandler.numberOfInvocations());
104 commandHandler.clearInvocations();
105 assertEquals("cleared", 0, commandHandler.numberOfInvocations());
106 }
107
108 /**
109 * Test the getInvocation() method
110 *
111 * @throws Exception
112 */
113 public void testGetInvocation() throws Exception {
114 control(session).expectAndDefaultReturn(session.getClientHost(), DEFAULT_HOST);
115 replay(session);
116
117 commandHandler.handleCommand(COMMAND, session);
118 commandHandler.handleCommand(COMMAND_WITH_ARGS, session);
119 assertSame("1", COMMAND, commandHandler.getInvocation(0).getCommand());
120 assertSame("2", COMMAND_WITH_ARGS, commandHandler.getInvocation(1).getCommand());
121 }
122
123 /**
124 * Test the getInvocation() method, passing in an invalid index
125 */
126 public void testGetInvocation_IndexOutOfBounds() throws Exception {
127 commandHandler.handleCommand(COMMAND, session);
128 try {
129 commandHandler.getInvocation(2);
130 fail("Expected IndexOutOfBoundsException");
131 }
132 catch (IndexOutOfBoundsException expected) {
133 LOG.info("Expected: " + expected);
134 }
135 }
136
137 /**
138 * Test the sendReply() method, when no message arguments are specified
139 */
140 public void testSendReply() {
141 session.sendReply(REPLY_CODE1, REPLY_TEXT1);
142 session.sendReply(REPLY_CODE1, MESSAGE_TEXT);
143 session.sendReply(REPLY_CODE1, OVERRIDE_REPLY_TEXT);
144 session.sendReply(REPLY_CODE3, null);
145 replay(session);
146
147 commandHandler.sendReply(session, REPLY_CODE1, null, null, null);
148 commandHandler.sendReply(session, REPLY_CODE1, MESSAGE_KEY, null, null);
149 commandHandler.sendReply(session, REPLY_CODE1, MESSAGE_KEY, OVERRIDE_REPLY_TEXT, null);
150 commandHandler.sendReply(session, REPLY_CODE3, null, null, null);
151
152 verify(session);
153 }
154
155 /**
156 * Test the sendReply() method, passing in message arguments
157 */
158 public void testSendReply_WithMessageArguments() {
159 session.sendReply(REPLY_CODE1, REPLY_TEXT2_FORMATTED);
160 replay(session);
161
162 commandHandler.sendReply(session, REPLY_CODE1, null, REPLY_TEXT2, ARGS);
163
164 verify(session);
165 }
166
167 /**
168 * Test the sendReply() method, passing in a null Session
169 */
170 public void testSendReply_NullSession() {
171 try {
172 commandHandler.sendReply(null, REPLY_CODE1, REPLY_TEXT1, null, null);
173 fail("Expected AssertFailedException");
174 }
175 catch (AssertFailedException expected) {
176 LOG.info("Expected: " + expected);
177 }
178 }
179
180 /**
181 * Test the sendReply() method, passing in an invalid replyCode
182 */
183 public void testSendReply_InvalidReplyCode() {
184 try {
185 commandHandler.sendReply(session, 0, REPLY_TEXT1, null, null);
186 fail("Expected AssertFailedException");
187 }
188 catch (AssertFailedException expected) {
189 LOG.info("Expected: " + expected);
190 }
191 }
192
193 //-------------------------------------------------------------------------
194 // Test setup
195 //-------------------------------------------------------------------------
196
197 /**
198 * Perform initialization before each test
199 *
200 * @see org.mockftpserver.test.AbstractTestCase#setUp()
201 */
202 protected void setUp() throws Exception {
203 super.setUp();
204 session = (Session) createMock(Session.class);
205 control(session).setDefaultMatcher(MockControl.ARRAY_MATCHER);
206 commandHandler = new AbstractTrackingCommandHandler() {
207 public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
208 }
209 };
210 ResourceBundle replyTextBundle = new ListResourceBundle() {
211 protected Object[][] getContents() {
212 return new Object[][]{
213 {Integer.toString(REPLY_CODE1), REPLY_TEXT1},
214 {Integer.toString(REPLY_CODE2), REPLY_TEXT2},
215 {MESSAGE_KEY, MESSAGE_TEXT}
216 };
217 }
218 };
219 commandHandler.setReplyTextBundle(replyTextBundle);
220 }
221
222}