private UploadedFile getMultipartFile() { return new UploadedFile(multipartHeadersPart.getName(), multipartHeadersPart.getFileName(), currentFile); }
private void freeUploadedUnprocessedFiles(final Collection<UploadedFile> uploadedFiles) { for (UploadedFile uploadedFile : uploadedFiles) { uploadedFile.destroy(); } } }
@Test public void shouldDeleteFileDestory() throws IOException { File file = new File(tempPath + "uploadfile.pdf"); try { if (!file.createNewFile()) { fail("Unable to create new file."); } assertThat(file.exists(), is(true)); UploadedFile uploadedFile = new UploadedFile("myfile", "myfile.pdf", file); if (!uploadedFile.destroy()) { fail("Unable to destroy uploaded file."); } assertThat(uploadedFile.getFile().exists(), is(false)); assertThat(uploadedFile.getFileName(), is("myfile.pdf")); assertThat(uploadedFile.getPostFieldName(), is("myfile")); } finally { cleanupFile(file); } }
@Test public void shouldHandleDestroyWhenFileDeletedManually() throws IOException { File file = new File(tempPath + "uploadfile.pdf"); try { if (!file.createNewFile()) { fail("Unable to create new file."); } assertThat(file.exists(), is(true)); UploadedFile uploadedFile = new UploadedFile("myfile", "myfile.pdf", file); if (!file.delete()) { fail("Unable to delete file."); } if (uploadedFile.destroy()) { fail("File should be already deleted."); } assertThat(uploadedFile.getFile().exists(), is(false)); } finally { cleanupFile(file); } }
@Test public void shouldHandleFileUploadForEmptyFile() throws MalformedInputException { String data = new MultipartInputBuilder(BOUNDARY) .withField("field_1", "A123") .withFile("FIELDNAME_00", "FILE.PDF", "application/pdf", "") .withFile("FIELDNAME_01", "FILE.PDF", "application/pdf", "ABCD") .build(); MultipartRequestHandler mrh = new MultipartRequestHandler(parser, getStreamOutOfString(data), data.length(), BOUNDARY, TEMPORARY_UPLOADS_DIRECTORY, 2048); try { mrh.handle(); } catch (IOException e) { fail("Should not throw IOException: " + e.getMessage()); } assertThat(mrh.getPost().size(), is(1)); assertThat(mrh.getPost().get("field_1"), is("A123")); assertThat(mrh.getUploadedFiles().size(), is(2)); Iterator<UploadedFile> uploadedFiles = mrh.getUploadedFiles().iterator(); assertThat(uploadedFiles.next().getFile().length(), is(0L)); assertThat(uploadedFiles.next().getFile().length(), is(4l)); // TODO Check file content }
@Test public void shouldNotDeleteFileThatWasMoved() throws IOException { File file = new File(tempPath + "uploadfile.pdf"); try { if (!file.createNewFile()) { throw new IOException("Unable to create " + file.getAbsolutePath()); } assertThat(file.exists(), is(true)); UploadedFile uploadedFile = new UploadedFile("myfile", "myfile.pdf", file); File movedFile = new File(tempPath + "uploadfile123.pdf"); if (movedFile.exists() && !movedFile.delete()) { throw new IOException("Unable to delete " + movedFile.getAbsolutePath()); } assertThat(movedFile.exists(), is(false)); assertThat(uploadedFile.getFile().renameTo(movedFile), is(true)); assertThat(uploadedFile.getFile().exists(), is(false)); if (uploadedFile.destroy()) { fail("File should be already moved."); } assertThat(movedFile.exists(), is(true)); if (!movedFile.delete()) { throw new IOException("Unable to delete " + movedFile.getAbsolutePath()); } } finally { cleanupFile(file); } }
@Test public void shouldHandleBasicFileUpload() throws MalformedInputException { String data = new MultipartInputBuilder(BOUNDARY) .withField("field_1", "A123") .withFile("FIELDNAME", "FILE.PDF", "application/pdf", "ABCD") .build(); MultipartRequestHandler mrh = new MultipartRequestHandler(parser, getStreamOutOfString(data), data.length(), BOUNDARY, TEMPORARY_UPLOADS_DIRECTORY, 2048); try { mrh.handle(); } catch (IOException e) { fail("Should not throw IOException: " + e.getMessage()); } assertThat(mrh.getPost().size(), is(1)); assertThat(mrh.getPost().get("field_1"), is("A123")); assertThat(mrh.getUploadedFiles().size(), is(1)); assertThat(mrh.getUploadedFiles().iterator().next().getFile().length(), is(4l)); // TODO Check file content }