chrismair | 00dc7bd | 2014-05-11 21:21:28 +0000 | [diff] [blame] | 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 | */
|
| 16 | package org.mockftpserver.stub.command;
|
| 17 |
|
| 18 | import org.slf4j.Logger;
|
| 19 | import org.slf4j.LoggerFactory;
|
| 20 | import org.mockftpserver.core.command.Command;
|
| 21 | import org.mockftpserver.core.command.InvocationRecord;
|
| 22 | import org.mockftpserver.core.session.Session;
|
| 23 | import org.mockftpserver.core.util.AssertFailedException;
|
| 24 | import org.mockftpserver.test.AbstractTestCase;
|
| 25 |
|
| 26 | import java.util.ListResourceBundle;
|
| 27 | import java.util.ResourceBundle;
|
| 28 |
|
| 29 | /**
|
| 30 | * Tests for AbstractStubDataCommandHandler. The class name is prefixed with an underscore
|
| 31 | * so that it is not filtered out by Maven's Surefire test plugin.
|
| 32 | *
|
| 33 | * @version $Revision$ - $Date$
|
| 34 | *
|
| 35 | * @author Chris Mair
|
| 36 | */
|
| 37 | public final class _AbstractStubDataCommandHandlerTest extends AbstractTestCase {
|
| 38 |
|
| 39 | private static final Logger LOG = LoggerFactory.getLogger(_AbstractStubDataCommandHandlerTest.class);
|
| 40 | private static final Command COMMAND = new Command("command", EMPTY);
|
| 41 | private static final InvocationRecord INVOCATION_RECORD = new InvocationRecord(COMMAND, DEFAULT_HOST);
|
| 42 |
|
| 43 | private static final String REPLY_TEXT150 = "reply 150 ... abcdef";
|
| 44 | private static final String REPLY_TEXT226 = "reply 226 ... abcdef";
|
| 45 | private static final String REPLY_TEXT222 = "reply 222 ... abcdef";
|
| 46 | private static final String REPLY_TEXT333 = "reply 333 ... abcdef";
|
| 47 | private static final String REPLY_TEXT444 = "reply 444 ... abcdef";
|
| 48 |
|
| 49 | private Session session;
|
| 50 | private ResourceBundle replyTextBundle;
|
| 51 | private AbstractStubDataCommandHandler commandHandler;
|
| 52 |
|
| 53 | /**
|
| 54 | * Test the handleCommand() method
|
| 55 | */
|
| 56 | public void testHandleCommand() throws Exception {
|
| 57 |
|
| 58 | session.sendReply(150, REPLY_TEXT150);
|
| 59 | session.openDataConnection();
|
| 60 | session.sendReply(222, REPLY_TEXT222);
|
| 61 | session.sendReply(333, REPLY_TEXT333);
|
| 62 | session.sendReply(444, REPLY_TEXT444);
|
| 63 | session.closeDataConnection();
|
| 64 | session.sendReply(226, REPLY_TEXT226);
|
| 65 | replay(session);
|
| 66 |
|
| 67 | // Define CommandHandler test subclass
|
| 68 | commandHandler = new AbstractStubDataCommandHandler() {
|
| 69 | protected void beforeProcessData(Command c, Session s, InvocationRecord ir) {
|
| 70 | verifyParameters(c, s, ir);
|
| 71 | // Send unique reply code so that we can verify proper method invocation and ordering
|
| 72 | session.sendReply(222, REPLY_TEXT222);
|
| 73 | }
|
| 74 |
|
| 75 | protected void processData(Command c, Session s, InvocationRecord ir) {
|
| 76 | verifyParameters(c, s, ir);
|
| 77 | // Send unique reply code so that we can verify proper method invocation and ordering
|
| 78 | session.sendReply(333, REPLY_TEXT333);
|
| 79 | }
|
| 80 |
|
| 81 | protected void afterProcessData(Command c, Session s, InvocationRecord ir) {
|
| 82 | verifyParameters(c, s, ir);
|
| 83 | // Send unique reply code so that we can verify proper method invocation and ordering
|
| 84 | session.sendReply(444, REPLY_TEXT444);
|
| 85 | }
|
| 86 |
|
| 87 | private void verifyParameters(Command c, Session s, InvocationRecord ir) {
|
| 88 | assertSame("command", COMMAND, c);
|
| 89 | assertSame("session", session, s);
|
| 90 | assertSame("invocationRecord", INVOCATION_RECORD, ir);
|
| 91 | }
|
| 92 | };
|
| 93 |
|
| 94 | commandHandler.setReplyTextBundle(replyTextBundle);
|
| 95 | commandHandler.handleCommand(COMMAND, session, INVOCATION_RECORD);
|
| 96 |
|
| 97 | verify(session);
|
| 98 | }
|
| 99 |
|
| 100 | /**
|
| 101 | * Test the handleCommand() method, overriding the initial reply code and text
|
| 102 | */
|
| 103 | public void testHandleCommand_OverrideInitialReplyCodeAndText() throws Exception {
|
| 104 |
|
| 105 | final int OVERRIDE_REPLY_CODE = 333;
|
| 106 | final String OVERRIDE_REPLY_TEXT = "reply text";
|
| 107 |
|
| 108 | session.sendReply(OVERRIDE_REPLY_CODE, OVERRIDE_REPLY_TEXT);
|
| 109 | session.openDataConnection();
|
| 110 | session.closeDataConnection();
|
| 111 | session.sendReply(226, REPLY_TEXT226);
|
| 112 | replay(session);
|
| 113 |
|
| 114 | commandHandler.setPreliminaryReplyCode(OVERRIDE_REPLY_CODE);
|
| 115 | commandHandler.setPreliminaryReplyText(OVERRIDE_REPLY_TEXT);
|
| 116 | commandHandler.setReplyTextBundle(replyTextBundle);
|
| 117 | commandHandler.handleCommand(COMMAND, session, INVOCATION_RECORD);
|
| 118 |
|
| 119 | verify(session);
|
| 120 | }
|
| 121 |
|
| 122 | /**
|
| 123 | * Test the setPreliminaryReplyCode() method, passing in an invalid value
|
| 124 | */
|
| 125 | public void testSetPreliminaryReplyCode_Invalid() {
|
| 126 | try {
|
| 127 | commandHandler.setPreliminaryReplyCode(0);
|
| 128 | fail("Expected AssertFailedException");
|
| 129 | }
|
| 130 | catch (AssertFailedException expected) {
|
| 131 | LOG.info("Expected: " + expected);
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | /**
|
| 136 | * Test the setFinalReplyCode() method, passing in an invalid value
|
| 137 | */
|
| 138 | public void testSetFinalReplyCode_Invalid() {
|
| 139 | try {
|
| 140 | commandHandler.setFinalReplyCode(0);
|
| 141 | fail("Expected AssertFailedException");
|
| 142 | }
|
| 143 | catch (AssertFailedException expected) {
|
| 144 | LOG.info("Expected: " + expected);
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | //-------------------------------------------------------------------------
|
| 149 | // Test setup
|
| 150 | //-------------------------------------------------------------------------
|
| 151 |
|
| 152 | /**
|
| 153 | * Perform initialization before each test
|
| 154 | *
|
| 155 | * @see org.mockftpserver.test.AbstractTestCase#setUp()
|
| 156 | */
|
| 157 | protected void setUp() throws Exception {
|
| 158 | super.setUp();
|
| 159 | session = (Session) createMock(Session.class);
|
| 160 | replyTextBundle = new ListResourceBundle() {
|
| 161 | protected Object[][] getContents() {
|
| 162 | return new Object[][] {
|
| 163 | { Integer.toString(150), REPLY_TEXT150 },
|
| 164 | { Integer.toString(222), REPLY_TEXT222 },
|
| 165 | { Integer.toString(226), REPLY_TEXT226 },
|
| 166 | { Integer.toString(333), REPLY_TEXT333 },
|
| 167 | { Integer.toString(444), REPLY_TEXT444 },
|
| 168 | };
|
| 169 | }
|
| 170 | };
|
| 171 | commandHandler = new AbstractStubDataCommandHandler() {
|
| 172 | protected void processData(Command c, Session s, InvocationRecord ir) {
|
| 173 | }
|
| 174 | };
|
| 175 | }
|
| 176 |
|
| 177 | }
|