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