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 java.util.Arrays; 19 20 21import org.apache.log4j.Logger; 22import org.easymock.ArgumentsMatcher; 23import org.mockftpserver.core.command.Command; 24import org.mockftpserver.core.command.CommandNames; 25import org.mockftpserver.core.command.ReplyCodes; 26import org.mockftpserver.core.util.AssertFailedException; 27import org.mockftpserver.stub.command.FileRetrCommandHandler; 28 29/** 30 * Tests for the FileRetrCommandHandler class 31 * 32 * @version $Revision$ - $Date$ 33 * 34 * @author Chris Mair 35 */ 36public final class FileRetrCommandHandlerTest extends AbstractCommandHandlerTest { 37 38 private static final Logger LOG = Logger.getLogger(FileRetrCommandHandlerTest.class); 39 private static final byte BYTE1 = (byte)7; 40 private static final byte BYTE2 = (byte)21; 41 42 private FileRetrCommandHandler commandHandler; 43 44 /** 45 * Test the constructor that takes a String, passing in a null 46 */ 47 public void testConstructor_String_Null() { 48 try { 49 new FileRetrCommandHandler((String)null); 50 fail("Expected AssertFailedException"); 51 } 52 catch (AssertFailedException expected) { 53 LOG.info("Expected: " + expected); 54 } 55 } 56 57 /** 58 * Test the setFile(String) method, passing in a null 59 */ 60 public void testSetFile_Null() { 61 try { 62 commandHandler.setFile(null); 63 fail("Expected AssertFailedException"); 64 } 65 catch (AssertFailedException expected) { 66 LOG.info("Expected: " + expected); 67 } 68 } 69 70 /** 71 * Test the handleCommand(Command,Session) method. Create a temporary (binary) file, and 72 * make sure its contents are written back 73 * @throws Exception 74 */ 75 public void testHandleCommand() throws Exception { 76 77 final byte[] BUFFER = new byte[FileRetrCommandHandler.BUFFER_SIZE]; 78 Arrays.fill(BUFFER, BYTE1); 79 80 session.sendReply(ReplyCodes.SEND_DATA_INITIAL_OK, replyTextFor(ReplyCodes.SEND_DATA_INITIAL_OK)); 81 session.openDataConnection(); 82 83 ArgumentsMatcher matcher = new ArgumentsMatcher() { 84 int counter = -1; // will increment for each invocation 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 } 94 else { 95 // TODO Got two invocations here; only expected one 96 //assertEquals("length for invocation #" + counter, expectedLength, actualLength); 97 assertEquals("buffer[0]", BYTE2, buffer[0]); 98 assertEquals("buffer[1]", BYTE2, buffer[1]); 99 assertEquals("buffer[2]", BYTE2, buffer[2]); 100 } 101 return true; 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.SEND_DATA_FINAL_OK, replyTextFor(ReplyCodes.SEND_DATA_FINAL_OK)); 118 replay(session); 119 120 commandHandler.setFile("Sample.data"); 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 * @see org.mockftpserver.stub.command.AbstractCommandHandlerTest#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.data"; 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} 180