160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/*
260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Copyright 2007 the original author or authors.
360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Licensed under the Apache License, Version 2.0 (the "License");
560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * you may not use this file except in compliance with the License.
660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * You may obtain a copy of the License at
760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *      http://www.apache.org/licenses/LICENSE-2.0
960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Unless required by applicable law or agreed to in writing, software
1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * See the License for the specific language governing permissions and
1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * limitations under the License.
1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpackage org.mockftpserver.core.command;
1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.apache.log4j.Logger;
1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.easymock.MockControl;
2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.session.Session;
2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.util.AssertFailedException;
2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.stub.command.AbstractStubCommandHandler;
2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.test.AbstractTest;
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport java.util.ListResourceBundle;
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport java.util.ResourceBundle;
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/**
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Tests for the AbstractCommandHandler class. The class name is prefixed with an
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * underscore so that it is not filtered out by Maven's Surefire test plugin.
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpublic final class _AbstractCommandHandlerTest extends AbstractTest {
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final Logger LOG = Logger.getLogger(_AbstractTrackingCommandHandlerTest.class);
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final int REPLY_CODE1 = 777;
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final int REPLY_CODE2 = 888;
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final String REPLY_TEXT1 = "reply1 ... abcdef";
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final String REPLY_TEXT2 = "abc {0} def";
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final String MESSAGE_KEY = "key.123";
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final String MESSAGE_TEXT = "message.123";
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private AbstractCommandHandler commandHandler;
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Test the quotes utility method
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public void testQuotes() {
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertEquals("abc", "\"abc\"", AbstractStubCommandHandler.quotes("abc"));
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertEquals("<empty>", "\"\"", AbstractStubCommandHandler.quotes(""));
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Test the quotes utility method, passing in a null
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public void testQuotes_Null() {
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        try {
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            AbstractStubCommandHandler.quotes(null);
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            fail("Expected AssertFailedException");
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        }
6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        catch (AssertFailedException expected) {
6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            LOG.info("Expected: " + expected);
6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        }
6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Test the assertValidReplyCode() method
7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public void testAssertValidReplyCode() {
7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // These are valid, so expect no exceptions
7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        commandHandler.assertValidReplyCode(1);
7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        commandHandler.assertValidReplyCode(100);
7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // These are invalid
7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        testAssertValidReplyCodeWithInvalid(0);
7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        testAssertValidReplyCodeWithInvalid(-1);
7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Test the assertValidReplyCode() method , passing in an invalid replyCode value
8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     *
8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @param invalidReplyCode - a reply code that is expected to be invalid
8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private void testAssertValidReplyCodeWithInvalid(int invalidReplyCode) {
8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        try {
8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            commandHandler.assertValidReplyCode(invalidReplyCode);
8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            fail("Expected AssertFailedException");
9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        }
9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        catch (AssertFailedException expected) {
9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            LOG.info("Expected: " + expected);
9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        }
9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Test setup
9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Perform initialization before each test
10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     *
10360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @see org.mockftpserver.test.AbstractTest#setUp()
10460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
10560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    protected void setUp() throws Exception {
10660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        super.setUp();
10760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        Session session = (Session) createMock(Session.class);
10860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        control(session).setDefaultMatcher(MockControl.ARRAY_MATCHER);
10960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        commandHandler = new AbstractCommandHandler() {
11060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            public void handleCommand(Command command, Session session) throws Exception {
11160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            }
11260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        };
11360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        ResourceBundle replyTextBundle = new ListResourceBundle() {
11460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            protected Object[][] getContents() {
11560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                return new Object[][]{
11660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                        {Integer.toString(REPLY_CODE1), REPLY_TEXT1},
11760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                        {Integer.toString(REPLY_CODE2), REPLY_TEXT2},
11860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                        {MESSAGE_KEY, MESSAGE_TEXT}
11960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair                };
12060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            }
12160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        };
12260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        commandHandler.setReplyTextBundle(replyTextBundle);
12360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
12460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
12560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}