View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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   */
16  package org.kuali.rice.kew.test.web;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import org.apache.struts.upload.FormFile;
25  import org.springframework.util.FileCopyUtils;
26  
27  /**
28   * A mock FormFile which is constructed directly from a File on disk 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class MockFormFile implements FormFile {
32      private File file;
33      private String fileName;
34      private int fileSize;
35      private String contentType = "application/octet-stream";
36  
37      public MockFormFile(File file) {
38          this.file = file;
39          this.fileName = file.getName();
40          this.fileSize = (int) file.length();
41      }
42  
43      public String getContentType() {
44          return contentType;
45      }
46  
47      public void setContentType(String contentType) {
48          this.contentType = contentType;
49      }
50  
51      public byte[] getFileData() throws FileNotFoundException, IOException {
52          return FileCopyUtils.copyToByteArray(file);
53      }
54  
55      public String getFileName() {
56          return fileName;
57      }
58  
59      public void setFileName(String fileName) {
60          this.fileName = fileName;
61      }
62  
63      public int getFileSize() {
64          return fileSize;
65      }
66  
67      public void setFileSize(int fileSize) {
68          this.fileSize = fileSize;
69      }
70  
71      public InputStream getInputStream() throws FileNotFoundException, IOException {
72          return new FileInputStream(file);
73      }
74  
75      public void destroy() {
76      }
77  
78      public String toString() {
79          return "[MockFormFile: " + file + "]";
80      }
81  }