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.easymock.ArgumentsMatcher;
|
| 21 | import org.mockftpserver.core.command.*;
|
| 22 | import org.mockftpserver.core.command.AbstractCommandHandlerTestCase;
|
| 23 | import org.mockftpserver.core.util.AssertFailedException;
|
| 24 |
|
| 25 | import java.util.Arrays;
|
| 26 |
|
| 27 | /**
|
| 28 | * Tests for the FileRetrCommandHandler class
|
| 29 | *
|
| 30 | * @author Chris Mair
|
| 31 | * @version $Revision$ - $Date$
|
| 32 | */
|
| 33 | public final class FileRetrCommandHandlerTest extends AbstractCommandHandlerTestCase {
|
| 34 |
|
| 35 | private static final Logger LOG = LoggerFactory.getLogger(FileRetrCommandHandlerTest.class);
|
| 36 | private static final byte BYTE1 = (byte) 7;
|
| 37 | private static final byte BYTE2 = (byte) 21;
|
| 38 |
|
| 39 | private FileRetrCommandHandler commandHandler;
|
| 40 |
|
| 41 | /**
|
| 42 | * Test the constructor that takes a String, passing in a null
|
| 43 | */
|
| 44 | public void testConstructor_String_Null() {
|
| 45 | try {
|
| 46 | new FileRetrCommandHandler(null);
|
| 47 | fail("Expected AssertFailedException");
|
| 48 | }
|
| 49 | catch (AssertFailedException expected) {
|
| 50 | LOG.info("Expected: " + expected);
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | /**
|
| 55 | * Test the setFile(String) method, passing in a null
|
| 56 | */
|
| 57 | public void testSetFile_Null() {
|
| 58 | try {
|
| 59 | commandHandler.setFile(null);
|
| 60 | fail("Expected AssertFailedException");
|
| 61 | }
|
| 62 | catch (AssertFailedException expected) {
|
| 63 | LOG.info("Expected: " + expected);
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | /**
|
| 68 | * Test the handleCommand(Command,Session) method. Create a temporary (binary) file, and
|
| 69 | * make sure its contents are written back
|
| 70 | *
|
| 71 | * @throws Exception
|
| 72 | */
|
| 73 | public void testHandleCommand() throws Exception {
|
| 74 |
|
| 75 | final byte[] BUFFER = new byte[FileRetrCommandHandler.BUFFER_SIZE];
|
| 76 | Arrays.fill(BUFFER, BYTE1);
|
| 77 |
|
| 78 | session.sendReply(ReplyCodes.TRANSFER_DATA_INITIAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_INITIAL_OK));
|
| 79 | session.openDataConnection();
|
| 80 |
|
| 81 | ArgumentsMatcher matcher = new ArgumentsMatcher() {
|
| 82 | int counter = -1; // will increment for each invocation
|
| 83 |
|
| 84 | public boolean matches(Object[] expected, Object[] actual) {
|
| 85 | counter++;
|
| 86 | byte[] buffer = (byte[]) actual[0];
|
| 87 | int expectedLength = ((Integer) expected[1]).intValue();
|
| 88 | int actualLength = ((Integer) actual[1]).intValue();
|
| 89 | LOG.info("invocation #" + counter + " expected=" + expectedLength + " actualLength=" + actualLength);
|
| 90 | if (counter < 5) {
|
| 91 | assertEquals("buffer for invocation #" + counter, BUFFER, buffer);
|
| 92 | } else {
|
| 93 | // TODO Got two invocations here; only expected one
|
| 94 | //assertEquals("length for invocation #" + counter, expectedLength, actualLength);
|
| 95 | assertEquals("buffer[0]", BYTE2, buffer[0]);
|
| 96 | assertEquals("buffer[1]", BYTE2, buffer[1]);
|
| 97 | assertEquals("buffer[2]", BYTE2, buffer[2]);
|
| 98 | }
|
| 99 | return true;
|
| 100 | }
|
| 101 |
|
| 102 | public String toString(Object[] args) {
|
| 103 | return args[0].getClass().getName() + " " + args[1].toString();
|
| 104 | }
|
| 105 | };
|
| 106 |
|
| 107 | session.sendData(BUFFER, 512);
|
| 108 | control(session).setMatcher(matcher);
|
| 109 | session.sendData(BUFFER, 512);
|
| 110 | session.sendData(BUFFER, 512);
|
| 111 | session.sendData(BUFFER, 512);
|
| 112 | session.sendData(BUFFER, 512);
|
| 113 | session.sendData(BUFFER, 3);
|
| 114 |
|
| 115 | session.closeDataConnection();
|
| 116 | session.sendReply(ReplyCodes.TRANSFER_DATA_FINAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_FINAL_OK));
|
| 117 | replay(session);
|
| 118 |
|
| 119 | commandHandler.setFile("Sample.jpg");
|
| 120 | Command command = new Command(CommandNames.RETR, array(FILENAME1));
|
| 121 | commandHandler.handleCommand(command, session);
|
| 122 | verify(session);
|
| 123 |
|
| 124 | verifyNumberOfInvocations(commandHandler, 1);
|
| 125 | verifyOneDataElement(commandHandler.getInvocation(0), FileRetrCommandHandler.PATHNAME_KEY, FILENAME1);
|
| 126 | }
|
| 127 |
|
| 128 | /**
|
| 129 | * Test the handleCommand() method, when no pathname parameter has been specified
|
| 130 | */
|
| 131 | public void testHandleCommand_MissingPathnameParameter() throws Exception {
|
| 132 | commandHandler.setFile("abc.txt"); // this property must be set
|
| 133 | testHandleCommand_InvalidParameters(commandHandler, CommandNames.RETR, EMPTY);
|
| 134 | }
|
| 135 |
|
| 136 | /**
|
| 137 | * Test the HandleCommand method, when the file property has not been set
|
| 138 | */
|
| 139 | public void testHandleCommand_FileNotSet() throws Exception {
|
| 140 | try {
|
| 141 | commandHandler.handleCommand(new Command(CommandNames.RETR, EMPTY), session);
|
| 142 | fail("Expected AssertFailedException");
|
| 143 | }
|
| 144 | catch (AssertFailedException expected) {
|
| 145 | LOG.info("Expected: " + expected);
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | /**
|
| 150 | * Perform initialization before each test
|
| 151 | *
|
| 152 | * @see org.mockftpserver.core.command.AbstractCommandHandlerTestCase#setUp()
|
| 153 | */
|
| 154 | protected void setUp() throws Exception {
|
| 155 | super.setUp();
|
| 156 | commandHandler = new FileRetrCommandHandler();
|
| 157 | commandHandler.setReplyTextBundle(replyTextBundle);
|
| 158 | }
|
| 159 |
|
| 160 | // /**
|
| 161 | // * Create a sample binary file; 5 buffers full plus 3 extra bytes
|
| 162 | // */
|
| 163 | // private void createSampleFile() {
|
| 164 | // final String FILE_PATH = "test/org.mockftpserver/command/Sample.jpg";
|
| 165 | // final byte[] BUFFER = new byte[FileRetrCommandHandler.BUFFER_SIZE];
|
| 166 | // Arrays.fill(BUFFER, BYTE1);
|
| 167 | //
|
| 168 | // File file = new File(FILE_PATH);
|
| 169 | // FileOutputStream out = new FileOutputStream(file);
|
| 170 | // for (int i = 0; i < 5; i++) {
|
| 171 | // out.write(BUFFER);
|
| 172 | // }
|
| 173 | // Arrays.fill(BUFFER, BYTE2);
|
| 174 | // out.write(BUFFER, 0, 3);
|
| 175 | // out.close();
|
| 176 | // LOG.info("Created temporary file [" + FILE_PATH + "]: length=" + file.length());
|
| 177 | // }
|
| 178 |
|
| 179 | }
|