001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kew.test.web; 017 018import java.io.File; 019import java.io.FileInputStream; 020import java.io.FileNotFoundException; 021import java.io.IOException; 022import java.io.InputStream; 023 024import org.apache.struts.upload.FormFile; 025import org.springframework.util.FileCopyUtils; 026 027/** 028 * A mock FormFile which is constructed directly from a File on disk 029 * @author Kuali Rice Team (rice.collab@kuali.org) 030 */ 031public class MockFormFile implements FormFile { 032 private File file; 033 private String fileName; 034 private int fileSize; 035 private String contentType = "application/octet-stream"; 036 037 public MockFormFile(File file) { 038 this.file = file; 039 this.fileName = file.getName(); 040 this.fileSize = (int) file.length(); 041 } 042 043 public String getContentType() { 044 return contentType; 045 } 046 047 public void setContentType(String contentType) { 048 this.contentType = contentType; 049 } 050 051 public byte[] getFileData() throws FileNotFoundException, IOException { 052 return FileCopyUtils.copyToByteArray(file); 053 } 054 055 public String getFileName() { 056 return fileName; 057 } 058 059 public void setFileName(String fileName) { 060 this.fileName = fileName; 061 } 062 063 public int getFileSize() { 064 return fileSize; 065 } 066 067 public void setFileSize(int fileSize) { 068 this.fileSize = fileSize; 069 } 070 071 public InputStream getInputStream() throws FileNotFoundException, IOException { 072 return new FileInputStream(file); 073 } 074 075 public void destroy() { 076 } 077 078 public String toString() { 079 return "[MockFormFile: " + file + "]"; 080 } 081}