1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.fileupload;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 import org.apache.commons.fileupload.FileItem;
33 import org.apache.commons.fileupload.disk.DiskFileItem;
34 import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
35 import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
36 import org.apache.myfaces.test.utils.HtmlRenderedAttr;
37 import org.apache.shale.test.mock.MockResponseWriter;
38 import org.easymock.MockControl;
39
40 public class HtmlFileUploadRendererTest extends AbstractTomahawkViewControllerTestCase
41 {
42 private HtmlInputFileUpload fileUpload;
43
44 public HtmlFileUploadRendererTest(String name)
45 {
46 super(name);
47 }
48
49 public static Test suite()
50 {
51 return new TestSuite(HtmlFileUploadRendererTest.class);
52 }
53
54
55
56 public void setUp() throws Exception
57 {
58 super.setUp();
59 fileUpload = new HtmlInputFileUpload();
60 }
61
62 public void tearDown() throws Exception
63 {
64 super.tearDown();
65 fileUpload = null;
66 }
67
68 public void testHtmlPropertyPassTru() throws Exception
69 {
70 HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicAttrs();
71
72 MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
73 HtmlCheckAttributesUtil.checkRenderedAttributes(
74 fileUpload, facesContext, writer, attrs);
75 if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
76 fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
77 }
78 }
79
80 public void testUploadedFileDefaultFileImplSerializable() throws Exception
81 {
82 String fieldName = "inputFile";
83 String contentType = "someType";
84 boolean isFormField = true;
85 String fileName = "tempFile";
86 int sizeThreshold = 10000;
87 File repository = new File(System.getProperty("java.io.tmpdir"));
88 DiskFileItem item = new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, repository);
89
90 UploadedFileDefaultFileImpl original = new UploadedFileDefaultFileImpl(item);
91
92 ByteArrayOutputStream out = new ByteArrayOutputStream();
93 ObjectOutputStream oos = new ObjectOutputStream(out);
94 oos.writeObject(original);
95 oos.close();
96
97 byte[] serializedArray = out.toByteArray();
98 InputStream in = new ByteArrayInputStream(serializedArray);
99 ObjectInputStream ois = new ObjectInputStream(in);
100 UploadedFileDefaultFileImpl copy = (UploadedFileDefaultFileImpl) ois.readObject();
101
102 assertEquals(original.getName(), copy.getName());
103 assertEquals(original.getContentType(), copy.getContentType());
104 assertEquals(copy.getSize(),0);
105
106 copy.getStorageStrategy().deleteFileContents();
107 }
108
109 public void testUploadedFileDefaultMemoryImplSerializable() throws Exception
110 {
111 String fieldName = "inputFile";
112 String contentType = "someType";
113
114 MockControl control = MockControl.createControl(FileItem.class);
115 FileItem item = (FileItem) control.getMock();
116
117 item.getName();
118 control.setReturnValue(fieldName,1);
119 item.getContentType();
120 control.setReturnValue(contentType,1);
121 item.getSize();
122 control.setReturnValue(0,1);
123 item.getInputStream();
124 control.setReturnValue(new InputStream()
125 {
126 public int read() throws IOException
127 {
128 return -1;
129 }
130 },1);
131
132 item.delete();
133 control.setVoidCallable(1);
134
135 control.replay();
136
137 UploadedFileDefaultMemoryImpl original = new UploadedFileDefaultMemoryImpl(item);
138
139 ByteArrayOutputStream out = new ByteArrayOutputStream();
140 ObjectOutputStream oos = new ObjectOutputStream(out);
141 oos.writeObject(original);
142 oos.close();
143
144 byte[] serializedArray = out.toByteArray();
145 InputStream in = new ByteArrayInputStream(serializedArray);
146 ObjectInputStream ois = new ObjectInputStream(in);
147 UploadedFileDefaultMemoryImpl copy = (UploadedFileDefaultMemoryImpl) ois.readObject();
148
149 assertEquals(original.getName(), copy.getName());
150 assertEquals(original.getContentType(), copy.getContentType());
151 assertEquals(copy.getSize(),0);
152
153 copy.getStorageStrategy().deleteFileContents();
154 }
155
156 }