Author: jochen Date: Sun Jul 9 13:22:56 2006 New Revision: 420345 URL: http://svn.apache.org/viewvc?rev=420345&view=rev Log: Added some test cases for proper exception handling.
Added: jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/StreamingTest.java - copied, changed from r420333, jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/SizesTest.java Removed: jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/SizesTest.java Modified: jakarta/commons/proper/fileupload/branches/streaming/maven.xml jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadBase.java jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadException.java jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/MockHttpServletRequest.java jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/TestAll.java Modified: jakarta/commons/proper/fileupload/branches/streaming/maven.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/branches/streaming/maven.xml?rev=420345&r1=420344&r2=420345&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/branches/streaming/maven.xml (original) +++ jakarta/commons/proper/fileupload/branches/streaming/maven.xml Sun Jul 9 13:22:56 2006 @@ -14,7 +14,7 @@ limitations under the License. --> -<project default="java:jar" +<project default="jar:jar" xmlns:ant="jelly:ant"> <!-- ================================================================== --> Modified: jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadBase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadBase.java?rev=420345&r1=420344&r2=420345&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadBase.java (original) +++ jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadBase.java Sun Jul 9 13:22:56 2006 @@ -317,9 +317,9 @@ } catch (FileUploadIOException e) { throw (FileUploadException) e.getCause(); } catch (IOException e) { - throw new FileUploadException( + throw new IOFileUploadException( "Processing of " + MULTIPART_FORM_DATA - + " request failed. " + e.getMessage()); + + " request failed. " + e.getMessage(), e); } items.add(fileItem); } @@ -792,6 +792,25 @@ } } + /** + * Thrown to indicate an IOException. + */ + public static class IOFileUploadException extends FileUploadException { + private static final long serialVersionUID = 1749796615868477269L; + private final IOException cause; + + /** + * Creates a new instance with the given cause. + */ + public IOFileUploadException(String pMsg, IOException pException) { + super(pMsg); + cause = pException; + } + + public Throwable getCause() { + return cause; + } + } /** * Thrown to indicate that the request size exceeds the configured maximum. Modified: jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadException.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadException.java?rev=420345&r1=420344&r2=420345&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadException.java (original) +++ jakarta/commons/proper/fileupload/branches/streaming/src/java/org/apache/commons/fileupload/FileUploadException.java Sun Jul 9 13:22:56 2006 @@ -23,7 +23,7 @@ */ public class FileUploadException extends Exception { - + /** * Constructs a new <code>FileUploadException</code> without message. */ @@ -39,4 +39,6 @@ public FileUploadException(final String msg) { super(msg); } + + } Modified: jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/MockHttpServletRequest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/MockHttpServletRequest.java?rev=420345&r1=420344&r2=420345&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/MockHttpServletRequest.java (original) +++ jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/MockHttpServletRequest.java Sun Jul 9 13:22:56 2006 @@ -18,6 +18,7 @@ import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.security.Principal; import java.util.Enumeration; @@ -41,15 +42,34 @@ class MockHttpServletRequest implements HttpServletRequest { - private byte[] m_requestData; + private final InputStream m_requestData; + private final int length; private String m_strContentType; private Map m_headers = new java.util.HashMap(); + /** + * Creates a new instance with the given request data + * and content type. + */ public MockHttpServletRequest( final byte[] requestData, final String strContentType) { + this(new ByteArrayInputStream(requestData), + requestData.length, strContentType); + } + + /** + * Creates a new instance with the given request data + * and content type. + */ + public MockHttpServletRequest( + final InputStream requestData, + final int requestLength, + final String strContentType) + { m_requestData = requestData; + length = requestLength; m_strContentType = strContentType; m_headers.put(FileUploadBase.CONTENT_TYPE, strContentType); } @@ -302,7 +322,7 @@ } else { - iLength = m_requestData.length; + iLength = length; } return iLength; } @@ -476,16 +496,20 @@ private static class MyServletInputStream extends javax.servlet.ServletInputStream { - private ByteArrayInputStream m_bais; + private final InputStream in; - public MyServletInputStream(byte[] data) + /** + * Creates a new instance, which returns the given + * streams data. + */ + public MyServletInputStream(InputStream pStream) { - m_bais = new ByteArrayInputStream(data); + in = pStream; } - public int read() + public int read() throws IOException { - return m_bais.read(); + return in.read(); } } } Copied: jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/StreamingTest.java (from r420333, jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/SizesTest.java) URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/StreamingTest.java?p2=jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/StreamingTest.java&p1=jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/SizesTest.java&r1=420333&r2=420345&rev=420345&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/SizesTest.java (original) +++ jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/StreamingTest.java Sun Jul 9 13:22:56 2006 @@ -15,48 +15,44 @@ */ package org.apache.commons.fileupload; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.FilterInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.fileupload.FileUploadBase.FileUploadIOException; +import org.apache.commons.fileupload.FileUploadBase.IOFileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.fileupload.servlet.ServletRequestContext; + import junit.framework.TestCase; /** * Unit test for items with varying sizes. */ -public class SizesTest extends TestCase +public class StreamingTest extends TestCase { + /** + * Tests a file upload with varying file sizes. + */ public void testFileUpload() throws IOException, FileUploadException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] request = newRequest(); + List fileItems = parseUpload(request); + Iterator fileIter = fileItems.iterator(); int add = 16; int num = 0; for (int i = 0; i < 16384; i += add) { if (++add == 32) { add = 16; } - String header = "-----1234\r\n" - + "Content-Disposition: form-data; name=\"field" + (num++) + "\"\r\n" - + "\r\n"; - baos.write(header.getBytes("US-ASCII")); - for (int j = 0; j < i; j++) { - baos.write((byte) j); - } - baos.write("\r\n".getBytes("US-ASCII")); - } - baos.write("-----1234--\r\n".getBytes("US-ASCII")); - - List fileItems = parseUpload(baos.toByteArray()); - Iterator fileIter = fileItems.iterator(); - add = 16; - num = 0; - for (int i = 0; i < 16384; i += add) { - if (++add == 32) { - add = 16; - } FileItem item = (FileItem) fileIter.next(); assertEquals("field" + (num++), item.getFieldName()); byte[] bytes = item.get(); @@ -68,14 +64,93 @@ assertTrue(!fileIter.hasNext()); } + + /** + * Tests, whether an invalid request throws a proper + * exception. + */ + public void testFileUploadException() + throws IOException, FileUploadException { + byte[] request = newRequest(); + byte[] invalidRequest = new byte[request.length-11]; + System.arraycopy(request, 0, invalidRequest, 0, request.length-11); + try { + parseUpload(invalidRequest); + fail("Expected EndOfStreamException"); + } catch (IOFileUploadException e) { + assertTrue(e.getCause() instanceof MultipartStream.MalformedStreamException); + } + } + + /** + * Tests, whether an IOException is properly delegated. + */ + public void testIOException() + throws IOException, FileUploadException { + byte[] request = newRequest(); + InputStream stream = new FilterInputStream(new ByteArrayInputStream(request)){ + private int num; + public int read() throws IOException { + if (++num > 123) { + throw new IOException("123"); + } + return super.read(); + } + public int read(byte[] pB, int pOff, int pLen) + throws IOException { + for (int i = 0; i < pLen; i++) { + int res = read(); + if (res == -1) { + return i == 0 ? -1 : i; + } + pB[pOff+i] = (byte) res; + } + return pLen; + } + }; + try { + parseUpload(stream, request.length); + } catch (IOFileUploadException e) { + assertTrue(e.getCause() instanceof IOException); + assertEquals("123", e.getCause().getMessage()); + } + } + private List parseUpload(byte[] bytes) throws FileUploadException { + return parseUpload(new ByteArrayInputStream(bytes), bytes.length); + } + + private List parseUpload(InputStream pStream, int pLength) + throws FileUploadException { String contentType = "multipart/form-data; boundary=---1234"; - FileUploadBase upload = new DiskFileUpload(); - HttpServletRequest request = new MockHttpServletRequest(bytes, contentType); + FileUploadBase upload = new ServletFileUpload(); + upload.setFileItemFactory(new DiskFileItemFactory()); + HttpServletRequest request = new MockHttpServletRequest(pStream, + pLength, contentType); - List fileItems = upload.parseRequest(request); + List fileItems = upload.parseRequest(new ServletRequestContext(request)); return fileItems; } + private byte[] newRequest() throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int add = 16; + int num = 0; + for (int i = 0; i < 16384; i += add) { + if (++add == 32) { + add = 16; + } + String header = "-----1234\r\n" + + "Content-Disposition: form-data; name=\"field" + (num++) + "\"\r\n" + + "\r\n"; + baos.write(header.getBytes("US-ASCII")); + for (int j = 0; j < i; j++) { + baos.write((byte) j); + } + baos.write("\r\n".getBytes("US-ASCII")); + } + baos.write("-----1234--\r\n".getBytes("US-ASCII")); + return baos.toByteArray(); + } } Modified: jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/TestAll.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/TestAll.java?rev=420345&r1=420344&r2=420345&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/TestAll.java (original) +++ jakarta/commons/proper/fileupload/branches/streaming/src/test/org/apache/commons/fileupload/TestAll.java Sun Jul 9 13:22:56 2006 @@ -23,20 +23,30 @@ * */ public class TestAll extends TestCase { - + /** + * Creates a new instance. + */ public TestAll(String testName) { super(testName); } + /** + * Runs the test suite (all other test cases). + */ public static Test suite() { TestSuite suite = new TestSuite(); + suite.addTest(new TestSuite(DefaultFileItemTest.class)); + suite.addTest(new TestSuite(DiskFileItemSerializeTest.class)); suite.addTest(new TestSuite(ParameterParserTest.class)); suite.addTest(new TestSuite(MultipartStreamTest.class)); suite.addTest(new TestSuite(ServletFileUploadTest.class)); - suite.addTest(new TestSuite(DefaultFileItemTest.class)); + suite.addTest(new TestSuite(StreamingTest.class)); return suite; } + /** + * Command line interface, which invokes all tests. + */ public static void main(String args[]) { String[] testCaseName = { TestAll.class.getName() }; junit.textui.TestRunner.main(testCaseName); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]