summaryrefslogtreecommitdiff
path: root/tags/2.3/src/test/java/org/mockftpserver/core/command/StaticReplyCommandHandlerTest.java
blob: 2e7c0cde6cfce0c01f8da88bb1ab1d318e7f6833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright 2007 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockftpserver.core.command;

import org.apache.log4j.Logger;
import org.mockftpserver.core.util.AssertFailedException;

/**
 * Tests for the StaticReplyCommandHandler class
 * 
 * @version $Revision$ - $Date$
 * 
 * @author Chris Mair
 */
public final class StaticReplyCommandHandlerTest extends AbstractCommandHandlerTestCase {

    private static final Logger LOG = Logger.getLogger(StaticReplyCommandHandlerTest.class);
    private static final int REPLY_CODE = 999;
    private static final String REPLY_TEXT = "some text 123";
    private static final Command COMMAND = new Command("ANY", EMPTY);
    
    private StaticReplyCommandHandler commandHandler;
    
    /**
     * Test the constructor that takes a replyCode, passing in a null
     */
    public void testConstructor_String_InvalidReplyCode() {
        try {
            new StaticReplyCommandHandler(-1);
            fail("Expected AssertFailedException");
        }
        catch (AssertFailedException expected) {
            LOG.info("Expected: " + expected);
        }
    }
    
    /**
     * Test the constructor that takes a replyCode and replyText, passing in a null replyCode
     */
    public void testConstructor_StringString_InvalidReplyCode() {
        try {
            new StaticReplyCommandHandler(-99, "text");
            fail("Expected AssertFailedException");
        }
        catch (AssertFailedException expected) {
            LOG.info("Expected: " + expected);
        }
    }
    
    /**
     * Test the setReplyCode() method, passing in a null
     */
    public void testSetReplyCode_Invalid() {
        try {
            commandHandler.setReplyCode(-1);
            fail("Expected AssertFailedException");
        }
        catch (AssertFailedException expected) {
            LOG.info("Expected: " + expected);
        }
    }
    
    /**
     * Test the handleCommand() method when the replyText attribute has not been set.
     * So, use whatever replyText has been configured in the replyCodeMapping
     * @throws Exception
     */
    public void testHandleCommand_ReplyTextNotSet() throws Exception {
        commandHandler.setReplyCode(250);
        
        session.sendReply(250, replyTextFor(250));
        replay(session);
        
        commandHandler.handleCommand(COMMAND, session);
        verify(session);
        
        verifyNumberOfInvocations(commandHandler, 1);
        verifyNoDataElements(commandHandler.getInvocation(0));
    }
    
    /**
     * Test the handleCommand() method, when the replyCode and replyText are both set
     * @throws Exception
     */
    public void testHandleCommand_SetReplyText() throws Exception {
        commandHandler.setReplyCode(REPLY_CODE);
        commandHandler.setReplyText(REPLY_TEXT);
        
        session.sendReply(REPLY_CODE, REPLY_TEXT);
        replay(session);
        
        commandHandler.handleCommand(COMMAND, session);
        verify(session);
        
        verifyNumberOfInvocations(commandHandler, 1);
        verifyNoDataElements(commandHandler.getInvocation(0));
    }
    
    /**
     * Test the handleCommand() method when the replyCode attribute has not been set
     * @throws Exception
     */
    public void testHandleCommand_ReplyCodeNotSet() throws Exception {

        try {
            commandHandler.handleCommand(COMMAND, session);
            fail("Expected AssertFailedException");
        }
        catch (AssertFailedException expected) {
            LOG.info("Expected: " + expected);
        }
        
        verifyNumberOfInvocations(commandHandler, 1);
        verifyNoDataElements(commandHandler.getInvocation(0));
    }
    
    /**
     * @see AbstractCommandHandlerTestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        commandHandler = new StaticReplyCommandHandler();
        commandHandler.setReplyTextBundle(replyTextBundle);
    }
    
}