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.easymock.MockControl;
20import org.mockftpserver.core.session.Session;
21import org.mockftpserver.core.util.AssertFailedException;
22import org.mockftpserver.stub.command.AbstractStubCommandHandler;
23import org.mockftpserver.test.AbstractTest;
24
25import java.util.ListResourceBundle;
26import java.util.ResourceBundle;
27
28/**
29 * Tests for the AbstractCommandHandler 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 _AbstractCommandHandlerTest extends AbstractTest {
36
37    private static final Logger LOG = Logger.getLogger(_AbstractTrackingCommandHandlerTest.class);
38    private static final int REPLY_CODE1 = 777;
39    private static final int REPLY_CODE2 = 888;
40    private static final String REPLY_TEXT1 = "reply1 ... abcdef";
41    private static final String REPLY_TEXT2 = "abc {0} def";
42    private static final String MESSAGE_KEY = "key.123";
43    private static final String MESSAGE_TEXT = "message.123";
44
45    private AbstractCommandHandler commandHandler;
46
47    /**
48     * Test the quotes utility method
49     */
50    public void testQuotes() {
51        assertEquals("abc", "\"abc\"", AbstractStubCommandHandler.quotes("abc"));
52        assertEquals("<empty>", "\"\"", AbstractStubCommandHandler.quotes(""));
53    }
54
55    /**
56     * Test the quotes utility method, passing in a null
57     */
58    public void testQuotes_Null() {
59        try {
60            AbstractStubCommandHandler.quotes(null);
61            fail("Expected AssertFailedException");
62        }
63        catch (AssertFailedException expected) {
64            LOG.info("Expected: " + expected);
65        }
66    }
67
68    /**
69     * Test the assertValidReplyCode() method
70     */
71    public void testAssertValidReplyCode() {
72        // These are valid, so expect no exceptions
73        commandHandler.assertValidReplyCode(1);
74        commandHandler.assertValidReplyCode(100);
75
76        // These are invalid
77        testAssertValidReplyCodeWithInvalid(0);
78        testAssertValidReplyCodeWithInvalid(-1);
79    }
80
81    /**
82     * Test the assertValidReplyCode() method , passing in an invalid replyCode value
83     *
84     * @param invalidReplyCode - a reply code that is expected to be invalid
85     */
86    private void testAssertValidReplyCodeWithInvalid(int invalidReplyCode) {
87        try {
88            commandHandler.assertValidReplyCode(invalidReplyCode);
89            fail("Expected AssertFailedException");
90        }
91        catch (AssertFailedException expected) {
92            LOG.info("Expected: " + expected);
93        }
94    }
95
96    //-------------------------------------------------------------------------
97    // Test setup
98    //-------------------------------------------------------------------------
99
100    /**
101     * Perform initialization before each test
102     *
103     * @see org.mockftpserver.test.AbstractTest#setUp()
104     */
105    protected void setUp() throws Exception {
106        super.setUp();
107        Session session = (Session) createMock(Session.class);
108        control(session).setDefaultMatcher(MockControl.ARRAY_MATCHER);
109        commandHandler = new AbstractCommandHandler() {
110            public void handleCommand(Command command, Session session) throws Exception {
111            }
112        };
113        ResourceBundle replyTextBundle = new ListResourceBundle() {
114            protected Object[][] getContents() {
115                return new Object[][]{
116                        {Integer.toString(REPLY_CODE1), REPLY_TEXT1},
117                        {Integer.toString(REPLY_CODE2), REPLY_TEXT2},
118                        {MESSAGE_KEY, MESSAGE_TEXT}
119                };
120            }
121        };
122        commandHandler.setReplyTextBundle(replyTextBundle);
123    }
124
125}