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