19d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/*
29d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Copyright 2007 the original author or authors.
39d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
49d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Licensed under the Apache License, Version 2.0 (the "License");
59d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * you may not use this file except in compliance with the License.
69d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * You may obtain a copy of the License at
79d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
89d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *      http://www.apache.org/licenses/LICENSE-2.0
99d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Unless required by applicable law or agreed to in writing, software
119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * distributed under the License is distributed on an "AS IS" BASIS,
129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * See the License for the specific language governing permissions and
149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * limitations under the License.
159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpackage org.mockftpserver.core.command;
179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.apache.log4j.Logger;
199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.Command;
209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.StaticReplyCommandHandler;
219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.util.AssertFailedException;
229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.stub.command.AbstractCommandHandlerTest;
239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/**
259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Tests for the StaticReplyCommandHandler class
269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @version $Revision$ - $Date$
289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @author Chris Mair
309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpublic final class StaticReplyCommandHandlerTest extends AbstractCommandHandlerTest {
329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private static final Logger LOG = Logger.getLogger(StaticReplyCommandHandlerTest.class);
349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private static final int REPLY_CODE = 999;
359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private static final String REPLY_TEXT = "some text 123";
369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private static final Command COMMAND = new Command("ANY", EMPTY);
379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    private StaticReplyCommandHandler commandHandler;
399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
409d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
419d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Test the constructor that takes a replyCode, passing in a null
429d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
439d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public void testConstructor_String_InvalidReplyCode() {
449d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        try {
459d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            new StaticReplyCommandHandler(-1);
469d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            fail("Expected AssertFailedException");
479d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
489d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        catch (AssertFailedException expected) {
499d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            LOG.info("Expected: " + expected);
509d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
519d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
529d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
539d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
549d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Test the constructor that takes a replyCode and replyText, passing in a null replyCode
559d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
569d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public void testConstructor_StringString_InvalidReplyCode() {
579d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        try {
589d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            new StaticReplyCommandHandler(-99, "text");
599d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            fail("Expected AssertFailedException");
609d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
619d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        catch (AssertFailedException expected) {
629d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            LOG.info("Expected: " + expected);
639d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
649d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
659d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
669d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
679d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Test the setReplyCode() method, passing in a null
689d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
699d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public void testSetReplyCode_Invalid() {
709d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        try {
719d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            commandHandler.setReplyCode(-1);
729d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            fail("Expected AssertFailedException");
739d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
749d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        catch (AssertFailedException expected) {
759d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            LOG.info("Expected: " + expected);
769d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
779d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
789d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
799d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
809d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Test the handleCommand() method when the replyText attribute has not been set.
819d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * So, use whatever replyText has been configured in the replyCodeMapping
829d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @throws Exception
839d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
849d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public void testHandleCommand_ReplyTextNotSet() throws Exception {
859d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        commandHandler.setReplyCode(250);
869d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
879d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        session.sendReply(250, replyTextFor(250));
889d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        replay(session);
899d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
909d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        commandHandler.handleCommand(COMMAND, session);
919d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        verify(session);
929d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
939d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        verifyNumberOfInvocations(commandHandler, 1);
949d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        verifyNoDataElements(commandHandler.getInvocation(0));
959d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
969d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
979d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
989d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Test the handleCommand() method, when the replyCode and replyText are both set
999d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @throws Exception
1009d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
1019d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public void testHandleCommand_SetReplyText() throws Exception {
1029d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        commandHandler.setReplyCode(REPLY_CODE);
1039d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        commandHandler.setReplyText(REPLY_TEXT);
1049d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1059d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        session.sendReply(REPLY_CODE, REPLY_TEXT);
1069d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        replay(session);
1079d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1089d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        commandHandler.handleCommand(COMMAND, session);
1099d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        verify(session);
1109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        verifyNumberOfInvocations(commandHandler, 1);
1129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        verifyNoDataElements(commandHandler.getInvocation(0));
1139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
1149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
1169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Test the handleCommand() method when the replyCode attribute has not been set
1179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @throws Exception
1189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
1199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    public void testHandleCommand_ReplyCodeNotSet() throws Exception {
1209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        try {
1229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            commandHandler.handleCommand(COMMAND, session);
1239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            fail("Expected AssertFailedException");
1249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
1259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        catch (AssertFailedException expected) {
1269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            LOG.info("Expected: " + expected);
1279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
1289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        verifyNumberOfInvocations(commandHandler, 1);
1309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        verifyNoDataElements(commandHandler.getInvocation(0));
1319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
1329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
1349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @see org.mockftpserver.stub.command.AbstractCommandHandlerTest#setUp()
1359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
1369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    protected void setUp() throws Exception {
1379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        super.setUp();
1389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        commandHandler = new StaticReplyCommandHandler();
1399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        commandHandler.setReplyTextBundle(replyTextBundle);
1409d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
1419d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
1429d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair}
143