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