001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.markup.html.form.upload;
018:
019: import java.io.BufferedOutputStream;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024:
025: import org.apache.wicket.Page;
026: import org.apache.wicket.RequestCycle;
027: import org.apache.wicket.WicketTestCase;
028: import org.apache.wicket.protocol.http.MockHttpServletRequest;
029: import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
030: import org.apache.wicket.util.file.File;
031: import org.apache.wicket.util.lang.Bytes;
032: import org.apache.wicket.util.tester.ITestPageSource;
033:
034: /**
035: * Test of FileUploadField
036: *
037: * @author Frank Bille (billen)
038: */
039: public class FileUploadFieldTest extends WicketTestCase {
040:
041: /**
042: * Construct.
043: */
044: public FileUploadFieldTest() {
045: super ("Test of FileUploadField");
046: }
047:
048: /**
049: * Test that detach closes the streams
050: *
051: * @throws Exception
052: */
053: public void testInternalDetach() throws Exception {
054: final MockPageWithFormAndUploadField page = new MockPageWithFormAndUploadField();
055:
056: FileUploadField field = new FileUploadField("upload");
057: page.getForm().add(field);
058:
059: tester.startPage(new ITestPageSource() {
060: private static final long serialVersionUID = 1L;
061:
062: public Page getTestPage() {
063: return page;
064: }
065: });
066:
067: // Setup the request. It should be a IMultipartWebRequest
068: RequestCycle requestCycle = tester.createRequestCycle();
069: MockHttpServletRequest servletRequest = tester
070: .getServletRequest();
071: servletRequest.setMethod("POST");
072: servletRequest.setParameter("form2:hf:fs", "");
073: servletRequest.setParameter("wicketState", "");
074:
075: File tmp = null;
076: try {
077: // Write out a large text file. We need to make this file reasonably sizable,
078: // because things get handled using input streams, and we want to check to make
079: // sure they're closed properly if we abort mid-request.
080:
081: // We create a temp file because we don't want to depend on a file we might not
082: // know the path of (e.g. the big DTD this test used previously). This enables
083: // us to run the test out of a JAR file if need be, and also with an unknown
084: // running directory (e.g. when run from wicket-parent).
085: tmp = new File(java.io.File.createTempFile(this .getClass()
086: .getName(), ".txt"));
087: OutputStream os = new BufferedOutputStream(
088: new FileOutputStream(tmp));
089: for (int i = 0; i < 1000; i++) {
090: os.write("test test test test test\n".getBytes());
091: }
092: os.close();
093:
094: // Let's upload the dtd file. It's large enough to avoid being in memory.
095: servletRequest.addFile("upload", tmp, "text/plain");
096:
097: requestCycle.setRequest(new MultipartServletWebRequest(
098: servletRequest, Bytes.MAX));
099:
100: // attach manually for the test
101: field.attach();
102:
103: // Get the file upload
104: FileUpload fileUpload = field.getFileUpload();
105:
106: assertNotNull(fileUpload);
107:
108: // Get an input stream from the file upload
109: InputStream is = fileUpload.getInputStream();
110:
111: // We should be able to read a byte
112: assertTrue(is.read() != -1);
113:
114: field.detach();
115:
116: // The input stream should be closed so we shouldn't be able to read any more bytes
117: try {
118: is.read();
119: fail();
120: } catch (IOException e) {
121: // Expected
122: } catch (Exception e) {
123: fail();
124: }
125: } finally {
126: if (tmp != null && tmp.exists()) {
127: tmp.delete();
128: }
129: }
130: }
131: }
|