blob: 634f18235bc54f2f76ae6b7d2e22f295c4e17ae9 [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.mockftpserver.core.util.AssertFailedException;
21
22/**
23 * Tests for the StaticReplyCommandHandler class
24 *
25 * @version $Revision$ - $Date$
26 *
27 * @author Chris Mair
28 */
29public final class StaticReplyCommandHandlerTest extends AbstractCommandHandlerTestCase {
30
31 private static final Logger LOG = LoggerFactory.getLogger(StaticReplyCommandHandlerTest.class);
32 private static final int REPLY_CODE = 999;
33 private static final String REPLY_TEXT = "some text 123";
34 private static final Command COMMAND = new Command("ANY", EMPTY);
35
36 private StaticReplyCommandHandler commandHandler;
37
38 /**
39 * Test the constructor that takes a replyCode, passing in a null
40 */
41 public void testConstructor_String_InvalidReplyCode() {
42 try {
43 new StaticReplyCommandHandler(-1);
44 fail("Expected AssertFailedException");
45 }
46 catch (AssertFailedException expected) {
47 LOG.info("Expected: " + expected);
48 }
49 }
50
51 /**
52 * Test the constructor that takes a replyCode and replyText, passing in a null replyCode
53 */
54 public void testConstructor_StringString_InvalidReplyCode() {
55 try {
56 new StaticReplyCommandHandler(-99, "text");
57 fail("Expected AssertFailedException");
58 }
59 catch (AssertFailedException expected) {
60 LOG.info("Expected: " + expected);
61 }
62 }
63
64 /**
65 * Test the setReplyCode() method, passing in a null
66 */
67 public void testSetReplyCode_Invalid() {
68 try {
69 commandHandler.setReplyCode(-1);
70 fail("Expected AssertFailedException");
71 }
72 catch (AssertFailedException expected) {
73 LOG.info("Expected: " + expected);
74 }
75 }
76
77 /**
78 * Test the handleCommand() method when the replyText attribute has not been set.
79 * So, use whatever replyText has been configured in the replyCodeMapping
80 * @throws Exception
81 */
82 public void testHandleCommand_ReplyTextNotSet() throws Exception {
83 commandHandler.setReplyCode(250);
84
85 session.sendReply(250, replyTextFor(250));
86 replay(session);
87
88 commandHandler.handleCommand(COMMAND, session);
89 verify(session);
90
91 verifyNumberOfInvocations(commandHandler, 1);
92 verifyNoDataElements(commandHandler.getInvocation(0));
93 }
94
95 /**
96 * Test the handleCommand() method, when the replyCode and replyText are both set
97 * @throws Exception
98 */
99 public void testHandleCommand_SetReplyText() throws Exception {
100 commandHandler.setReplyCode(REPLY_CODE);
101 commandHandler.setReplyText(REPLY_TEXT);
102
103 session.sendReply(REPLY_CODE, REPLY_TEXT);
104 replay(session);
105
106 commandHandler.handleCommand(COMMAND, session);
107 verify(session);
108
109 verifyNumberOfInvocations(commandHandler, 1);
110 verifyNoDataElements(commandHandler.getInvocation(0));
111 }
112
113 /**
114 * Test the handleCommand() method when the replyCode attribute has not been set
115 * @throws Exception
116 */
117 public void testHandleCommand_ReplyCodeNotSet() throws Exception {
118
119 try {
120 commandHandler.handleCommand(COMMAND, session);
121 fail("Expected AssertFailedException");
122 }
123 catch (AssertFailedException expected) {
124 LOG.info("Expected: " + expected);
125 }
126
127 verifyNumberOfInvocations(commandHandler, 1);
128 verifyNoDataElements(commandHandler.getInvocation(0));
129 }
130
131 /**
132 * @see AbstractCommandHandlerTestCase#setUp()
133 */
134 protected void setUp() throws Exception {
135 super.setUp();
136 commandHandler = new StaticReplyCommandHandler();
137 commandHandler.setReplyTextBundle(replyTextBundle);
138 }
139
140}