{
final MockPageWithFormAndUploadField page = new MockPageWithFormAndUploadField();
tester.startPage(page);
File tmp = null;
try
{
// Write out a large text file. We need to make this file reasonably sizable,
// because things get handled using input streams, and we want to check to make
// sure they're closed properly if we abort mid-request.
// We create a temp file because we don't want to depend on a file we might not
// know the path of (e.g. the big DTD this test used previously). This enables
// us to run the test out of a JAR file if need be, and also with an unknown
// running directory (e.g. when run from wicket-parent).
tmp = writeTestFile(1000);
// Let's upload the dtd file. It's large enough to avoid being in memory.
FormTester formtester = tester.newFormTester("form");
formtester.setFile("upload", tmp, "text/plain");
formtester.submit();
// Get the file upload
FileUpload fileUpload = page.getFileUpload();
assertNotNull(fileUpload);
// Get an input stream from the file upload
InputStream is = fileUpload.getInputStream();
// We should be able to read a byte
assertTrue(is.read() != -1);
fileUpload.closeStreams();
// The input stream should be closed so we shouldn't be able to read any more bytes
try
{
is.read();
fail("The input stream should be closed so we shouldn't be able to read any more bytes");
}
catch (IOException e)
{
// Expected
}
catch (Exception e)
{
fail();
}
}
finally
{
if (tmp != null && tmp.exists())
{
tmp.delete();
}
}
}