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