This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git
The following commit(s) were added to refs/heads/master by this push:
new 7d065cc Better exception handling
7d065cc is described below
commit 7d065cc0a7f78f7dc8260b3d7957ac3cab3cd632
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Apr 3 07:08:55 2023 -0400
Better exception handling
Use try-with-resources
---
.../org/apache/commons/fileupload2/FileItem.java | 4 +-
.../commons/fileupload2/disk/DiskFileItem.java | 42 ++++-----
.../fileupload2/DiskFileItemSerializeTest.java | 105 +++++++++------------
3 files changed, 63 insertions(+), 88 deletions(-)
diff --git a/src/main/java/org/apache/commons/fileupload2/FileItem.java
b/src/main/java/org/apache/commons/fileupload2/FileItem.java
index a44369e..997316f 100644
--- a/src/main/java/org/apache/commons/fileupload2/FileItem.java
+++ b/src/main/java/org/apache/commons/fileupload2/FileItem.java
@@ -197,8 +197,8 @@ public interface FileItem extends FileItemHeadersSupport {
* @param file The {@code File} into which the uploaded item should
* be stored.
*
- * @throws Exception if an error occurs.
+ * @throws IOException if an error occurs.
*/
- void write(File file) throws Exception;
+ void write(File file) throws IOException;
}
diff --git
a/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
b/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
index cf266ce..af7646b 100644
--- a/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
+++ b/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
@@ -555,27 +555,26 @@ public class DiskFileItem implements FileItem {
}
/**
- * A convenience method to write an uploaded item to disk. The client code
- * is not concerned with whether or not the item is stored in memory, or on
- * disk in a temporary location. They just want to write the uploaded item
- * to a file.
+ * Writes an uploaded item to disk.
* <p>
- * This implementation first attempts to rename the uploaded item to the
- * specified destination file, if the item was originally written to disk.
- * Otherwise, the data will be copied to the specified file.
+ * The client code is not concerned with whether or not the item is stored
in memory, or on disk in a temporary location. They just want to write the
+ * uploaded item to a file.
+ * </p>
* <p>
- * This method is only guaranteed to work <em>once</em>, the first time it
- * is invoked for a particular item. This is because, in the event that the
- * method renames a temporary file, that file will no longer be available
- * to copy or rename again at a later time.
+ * This implementation first attempts to rename the uploaded item to the
specified destination file, if the item was originally written to disk.
Otherwise,
+ * the data will be copied to the specified file.
+ * </p>
+ * <p>
+ * This method is only guaranteed to work <em>once</em>, the first time it
is invoked for a particular item. This is because, in the event that the method
+ * renames a temporary file, that file will no longer be available to copy
or rename again at a later time.
+ * </p>
*
- * @param file The {@code File} into which the uploaded item should
- * be stored.
+ * @param file The {@code File} into which the uploaded item should be
stored.
*
- * @throws Exception if an error occurs.
+ * @throws IOException if an error occurs.
*/
@Override
- public void write(final File file) throws Exception {
+ public void write(final File file) throws IOException {
if (isInMemory()) {
try (OutputStream fout = Files.newOutputStream(file.toPath())) {
fout.write(get());
@@ -586,22 +585,17 @@ public class DiskFileItem implements FileItem {
final File outputFile = getStoreLocation();
if (outputFile == null) {
/*
- * For whatever reason we cannot write the
- * file to disk.
+ * For whatever reason we cannot write the file to disk.
*/
- throw new FileUploadException(
- "Cannot write uploaded file to disk!");
+ throw new FileUploadException("Cannot write uploaded file to
disk!");
}
// Save the length of the file
size = outputFile.length();
/*
- * The uploaded file is being stored on disk
- * in a temporary location so move it to the
- * desired file.
+ * The uploaded file is being stored on disk in a temporary
location so move it to the desired file.
*/
if (file.exists() && !file.delete()) {
- throw new FileUploadException(
- "Cannot write uploaded file to disk!");
+ throw new FileUploadException("Cannot write uploaded file to
disk!");
}
FileUtils.moveFile(outputFile, file);
}
diff --git
a/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java
b/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java
index 14be63d..6bc8fab 100644
---
a/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java
+++
b/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java
@@ -38,13 +38,12 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
- * Serialization Unit tests for
- * {@link org.apache.commons.fileupload2.disk.DiskFileItem}.
+ * Serialization Unit tests for {@link
org.apache.commons.fileupload2.disk.DiskFileItem}.
*/
public class DiskFileItemSerializeTest {
- // Use a private repo to catch any files left over by tests
- private static final File REPO = new
File(System.getProperty("java.io.tmpdir"), "diskfileitemrepo");
+ // Use a private repository to catch any files left over by tests
+ private static final File REPOSITORY = new
File(System.getProperty("java.io.tmpdir"), "DiskFileItemRepo");
/**
* Content type for regular form items.
@@ -87,76 +86,64 @@ public class DiskFileItemSerializeTest {
/**
* Create a FileItem with the specfied content bytes.
*/
- private FileItem createFileItem(final byte[] contentBytes) {
- return createFileItem(contentBytes, REPO);
+ private FileItem createFileItem(final byte[] contentBytes) throws
IOException {
+ return createFileItem(contentBytes, REPOSITORY);
}
/**
* Create a FileItem with the specfied content bytes and repository.
*/
- private FileItem createFileItem(final byte[] contentBytes, final File
repository) {
+ private FileItem createFileItem(final byte[] contentBytes, final File
repository) throws IOException {
final FileItemFactory factory = new DiskFileItemFactory(THRESHOLD,
repository);
final String textFieldName = "textField";
- final FileItem item = factory.createItem(
- textFieldName,
- TEXT_CONTENT_TYPE,
- true,
- "My File Name"
- );
- try {
- final OutputStream os = item.getOutputStream();
+ final FileItem item = factory.createItem(textFieldName,
TEXT_CONTENT_TYPE, true, "My File Name");
+ try (OutputStream os = item.getOutputStream()) {
os.write(contentBytes);
- os.close();
- } catch (final IOException e) {
- fail("Unexpected IOException" + e);
}
-
return item;
-
}
/**
- * Do deserialization
+ * Do deserialization.
*/
- private Object deserialize(final ByteArrayOutputStream baos) throws
Exception {
+ private Object deserialize(final ByteArrayOutputStream baos) {
return SerializationUtils.deserialize(baos.toByteArray());
}
/**
- * Do serialization
+ * Do serialization.
*/
- private ByteArrayOutputStream serialize(final Object target) throws
Exception {
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final ObjectOutputStream oos = new ObjectOutputStream(baos);
- oos.writeObject(target);
- oos.flush();
- oos.close();
- return baos;
+ private ByteArrayOutputStream serialize(final Object target) throws
IOException {
+ try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final ObjectOutputStream oos = new ObjectOutputStream(baos)) {
+ oos.writeObject(target);
+ oos.flush();
+ return baos;
+ }
}
@BeforeEach
- public void setUp() throws Exception {
- if (REPO.exists()) {
- FileUtils.deleteDirectory(REPO);
+ public void setUp() throws IOException {
+ if (REPOSITORY.exists()) {
+ FileUtils.deleteDirectory(REPOSITORY);
}
- FileUtils.forceMkdir(REPO);
+ FileUtils.forceMkdir(REPOSITORY);
}
@AfterEach
public void tearDown() throws IOException {
- for(final File file : FileUtils.listFiles(REPO, null, true)) {
+ for (final File file : FileUtils.listFiles(REPOSITORY, null, true)) {
System.out.println("Found leftover file " + file);
}
- FileUtils.deleteDirectory(REPO);
+ FileUtils.deleteDirectory(REPOSITORY);
}
/**
- * Test creation of a field for which the amount of data falls above the
- * configured threshold.
+ * Test creation of a field for which the amount of data falls above the
configured threshold.
*/
@Test
- public void testAboveThreshold() {
+ public void testAboveThreshold() throws IOException {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD + 1);
final FileItem item = createFileItem(testFieldValueBytes);
@@ -175,11 +162,10 @@ public class DiskFileItemSerializeTest {
}
/**
- * Test creation of a field for which the amount of data falls below the
- * configured threshold.
+ * Test creation of a field for which the amount of data falls below the
configured threshold.
*/
@Test
- public void testBelowThreshold() {
+ public void testBelowThreshold() throws IOException {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD - 1);
testInMemoryObject(testFieldValueBytes);
@@ -188,14 +174,14 @@ public class DiskFileItemSerializeTest {
/**
* Helper method to test creation of a field.
*/
- private void testInMemoryObject(final byte[] testFieldValueBytes) {
- testInMemoryObject(testFieldValueBytes, REPO);
+ private void testInMemoryObject(final byte[] testFieldValueBytes) throws
IOException {
+ testInMemoryObject(testFieldValueBytes, REPOSITORY);
}
/**
* Helper method to test creation of a field when a repository is used.
*/
- public void testInMemoryObject(final byte[] testFieldValueBytes, final
File repository) {
+ public void testInMemoryObject(final byte[] testFieldValueBytes, final
File repository) throws IOException {
final FileItem item = createFileItem(testFieldValueBytes, repository);
// Check state is as expected
@@ -214,7 +200,7 @@ public class DiskFileItemSerializeTest {
* Test deserialization fails when repository is not valid.
*/
@Test
- public void testInvalidRepository() {
+ public void testInvalidRepository() throws IOException {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
final File repository = new File(System.getProperty("java.io.tmpdir"),
"file");
@@ -226,7 +212,7 @@ public class DiskFileItemSerializeTest {
* Test deserialization fails when repository contains a null character.
*/
@Test
- public void testInvalidRepositoryWithNullChar() {
+ public void testInvalidRepositoryWithNullChar() throws IOException {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
final File repository = new File(System.getProperty("java.io.tmpdir"),
"\0");
@@ -235,11 +221,10 @@ public class DiskFileItemSerializeTest {
}
/**
- * Test creation of a field for which the amount of data equals the
- * configured threshold.
+ * Test creation of a field for which the amount of data equals the
configured threshold.
*/
@Test
- public void testThreshold() {
+ public void testThreshold() throws IOException {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
testInMemoryObject(testFieldValueBytes);
@@ -249,24 +234,20 @@ public class DiskFileItemSerializeTest {
* Test serialization and deserialization when repository is not null.
*/
@Test
- public void testValidRepository() {
+ public void testValidRepository() throws IOException {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
- testInMemoryObject(testFieldValueBytes, REPO);
+ testInMemoryObject(testFieldValueBytes, REPOSITORY);
}
/**
* Helper method to test writing item contents to a file.
*/
- public void testWritingToFile(final FileItem item, final byte[]
testFieldValueBytes) {
- try {
- final File temp = File.createTempFile("fileupload", null);
- // Note that the file exists and is initially empty;
- // write() must be able to handle that.
- item.write(temp);
- compareBytes("Initial", FileUtils.readFileToByteArray(temp),
testFieldValueBytes);
- } catch (Exception e) {
- fail("Unexpected Exception", e);
- }
+ public void testWritingToFile(final FileItem item, final byte[]
testFieldValueBytes) throws IOException {
+ final File temp = File.createTempFile("fileupload", null);
+ // Note that the file exists and is initially empty;
+ // write() must be able to handle that.
+ item.write(temp);
+ compareBytes("Initial", FileUtils.readFileToByteArray(temp),
testFieldValueBytes);
}
}