01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.markup.html.form.upload;
18:
19: import java.io.InputStream;
20: import java.lang.reflect.Field;
21: import java.util.List;
22:
23: import org.apache.wicket.WicketTestCase;
24: import org.apache.wicket.util.upload.DiskFileItemFactory;
25: import org.apache.wicket.util.upload.FileItem;
26:
27: /**
28: * Test of FileUpload
29: *
30: * @author Frank Bille (billen)
31: */
32: public class FileUploadTest extends WicketTestCase {
33:
34: /**
35: * Construct.
36: */
37: public FileUploadTest() {
38: super ("Test of FileUpload");
39: }
40:
41: /**
42: * Test that when getting an input stream a new input stream is returned every time.
43: *
44: * Also test that the inputstream is saved internally for later closing.
45: *
46: * @throws Exception
47: */
48: public void testGetInputStream() throws Exception {
49: FileItem fileItem = new DiskFileItemFactory().createItem(
50: "dummyFieldName", "text/java", false,
51: "FileUploadTest.java");
52: // Initialize the upload
53: fileItem.getOutputStream();
54:
55: // Get the internal list out
56: Field inputStreamsField = FileUpload.class
57: .getDeclaredField("inputStreamsToClose");
58: inputStreamsField.setAccessible(true);
59:
60: FileUpload fileUpload = new FileUpload(fileItem);
61:
62: List inputStreams = (List) inputStreamsField.get(fileUpload);
63:
64: assertNull(inputStreams);
65:
66: InputStream is1 = fileUpload.getInputStream();
67: inputStreams = (List) inputStreamsField.get(fileUpload);
68:
69: assertEquals(1, inputStreams.size());
70:
71: InputStream is2 = fileUpload.getInputStream();
72: inputStreams = (List) inputStreamsField.get(fileUpload);
73:
74: assertEquals(2, inputStreams.size());
75:
76: assertNotSame(is1, is2);
77:
78: // Ok lets close all the streams
79: try {
80: fileUpload.closeStreams();
81: } catch (Exception e) {
82: fail();
83: }
84:
85: inputStreams = (List) inputStreamsField.get(fileUpload);
86:
87: assertNull(inputStreams);
88: }
89:
90: }
|