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.commons.fileupload;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.FilterInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.util.Iterator;
025: import java.util.List;
026: import javax.servlet.http.HttpServletRequest;
027:
028: import org.apache.commons.fileupload.FileUploadBase.FileUploadIOException;
029: import org.apache.commons.fileupload.FileUploadBase.IOFileUploadException;
030: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
031: import org.apache.commons.fileupload.servlet.ServletFileUpload;
032: import org.apache.commons.fileupload.servlet.ServletRequestContext;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * Unit test for items with varying sizes.
038: */
039: public class StreamingTest extends TestCase {
040: /**
041: * Tests a file upload with varying file sizes.
042: */
043: public void testFileUpload() throws IOException,
044: FileUploadException {
045: byte[] request = newRequest();
046: List fileItems = parseUpload(request);
047: Iterator fileIter = fileItems.iterator();
048: int add = 16;
049: int num = 0;
050: for (int i = 0; i < 16384; i += add) {
051: if (++add == 32) {
052: add = 16;
053: }
054: FileItem item = (FileItem) fileIter.next();
055: assertEquals("field" + (num++), item.getFieldName());
056: byte[] bytes = item.get();
057: assertEquals(i, bytes.length);
058: for (int j = 0; j < i; j++) {
059: assertEquals((byte) j, bytes[j]);
060: }
061: }
062: assertTrue(!fileIter.hasNext());
063: }
064:
065: /**
066: * Tests, whether an invalid request throws a proper
067: * exception.
068: */
069: public void testFileUploadException() throws IOException,
070: FileUploadException {
071: byte[] request = newRequest();
072: byte[] invalidRequest = new byte[request.length - 11];
073: System.arraycopy(request, 0, invalidRequest, 0,
074: request.length - 11);
075: try {
076: parseUpload(invalidRequest);
077: fail("Expected EndOfStreamException");
078: } catch (IOFileUploadException e) {
079: assertTrue(e.getCause() instanceof MultipartStream.MalformedStreamException);
080: }
081: }
082:
083: /**
084: * Tests, whether an IOException is properly delegated.
085: */
086: public void testIOException() throws IOException,
087: FileUploadException {
088: byte[] request = newRequest();
089: InputStream stream = new FilterInputStream(
090: new ByteArrayInputStream(request)) {
091: private int num;
092:
093: public int read() throws IOException {
094: if (++num > 123) {
095: throw new IOException("123");
096: }
097: return super .read();
098: }
099:
100: public int read(byte[] pB, int pOff, int pLen)
101: throws IOException {
102: for (int i = 0; i < pLen; i++) {
103: int res = read();
104: if (res == -1) {
105: return i == 0 ? -1 : i;
106: }
107: pB[pOff + i] = (byte) res;
108: }
109: return pLen;
110: }
111: };
112: try {
113: parseUpload(stream, request.length);
114: } catch (IOFileUploadException e) {
115: assertTrue(e.getCause() instanceof IOException);
116: assertEquals("123", e.getCause().getMessage());
117: }
118: }
119:
120: private List parseUpload(byte[] bytes) throws FileUploadException {
121: return parseUpload(new ByteArrayInputStream(bytes),
122: bytes.length);
123: }
124:
125: private List parseUpload(InputStream pStream, int pLength)
126: throws FileUploadException {
127: String contentType = "multipart/form-data; boundary=---1234";
128:
129: FileUploadBase upload = new ServletFileUpload();
130: upload.setFileItemFactory(new DiskFileItemFactory());
131: HttpServletRequest request = new MockHttpServletRequest(
132: pStream, pLength, contentType);
133:
134: List fileItems = upload.parseRequest(new ServletRequestContext(
135: request));
136: return fileItems;
137: }
138:
139: private byte[] newRequest() throws IOException {
140: ByteArrayOutputStream baos = new ByteArrayOutputStream();
141: int add = 16;
142: int num = 0;
143: for (int i = 0; i < 16384; i += add) {
144: if (++add == 32) {
145: add = 16;
146: }
147: String header = "-----1234\r\n"
148: + "Content-Disposition: form-data; name=\"field"
149: + (num++) + "\"\r\n" + "\r\n";
150: baos.write(header.getBytes("US-ASCII"));
151: for (int j = 0; j < i; j++) {
152: baos.write((byte) j);
153: }
154: baos.write("\r\n".getBytes("US-ASCII"));
155: }
156: baos.write("-----1234--\r\n".getBytes("US-ASCII"));
157: return baos.toByteArray();
158: }
159: }
|