|
18 | 18 | */
|
19 | 19 | package org.apache.struts2.dispatcher.multipart;
|
20 | 20 |
|
| 21 | +import org.apache.commons.fileupload2.core.DiskFileItem; |
| 22 | +import org.apache.struts2.dispatcher.LocalizedMessage; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.IOException; |
| 27 | +import java.lang.reflect.Field; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.apache.commons.lang3.StringUtils.normalizeSpace; |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
21 | 34 | public class JakartaMultiPartRequestTest extends AbstractMultiPartRequestTest {
|
22 | 35 |
|
23 | 36 | @Override
|
24 | 37 | protected AbstractMultiPartRequest createMultipartRequest() {
|
25 | 38 | return new JakartaMultiPartRequest();
|
26 | 39 | }
|
27 | 40 |
|
| 41 | + @Test |
| 42 | + public void temporaryFileCleanupForInMemoryUploads() throws IOException, NoSuchFieldException, IllegalAccessException { |
| 43 | + // given - small files that will be in-memory |
| 44 | + String content = formFile("file1", "test1.csv", "a,b,c,d") + |
| 45 | + formFile("file2", "test2.csv", "1,2,3,4") + |
| 46 | + endline + "--" + boundary + "--"; |
| 47 | + |
| 48 | + mockRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); |
| 49 | + |
| 50 | + // when |
| 51 | + multiPart.parse(mockRequest, tempDir); |
| 52 | + |
| 53 | + // Access private field to verify temporary files are tracked |
| 54 | + Field tempFilesField = JakartaMultiPartRequest.class.getDeclaredField("temporaryFiles"); |
| 55 | + tempFilesField.setAccessible(true); |
| 56 | + @SuppressWarnings("unchecked") |
| 57 | + List<File> temporaryFiles = (List<File>) tempFilesField.get(multiPart); |
| 58 | + |
| 59 | + // Store file paths before cleanup for verification |
| 60 | + List<String> tempFilePaths = temporaryFiles.stream() |
| 61 | + .map(File::getAbsolutePath) |
| 62 | + .toList(); |
| 63 | + |
| 64 | + // Verify temporary files exist before cleanup |
| 65 | + assertThat(temporaryFiles).isNotEmpty(); |
| 66 | + for (File tempFile : temporaryFiles) { |
| 67 | + assertThat(tempFile).exists(); |
| 68 | + } |
| 69 | + |
| 70 | + // when - cleanup |
| 71 | + multiPart.cleanUp(); |
| 72 | + |
| 73 | + // then - verify files are deleted and tracking list is cleared |
| 74 | + for (String tempFilePath : tempFilePaths) { |
| 75 | + assertThat(new File(tempFilePath)).doesNotExist(); |
| 76 | + } |
| 77 | + assertThat(temporaryFiles).isEmpty(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void cleanupMethodsCanBeOverridden() { |
| 82 | + // Create a custom implementation to test extensibility |
| 83 | + class CustomJakartaMultiPartRequest extends JakartaMultiPartRequest { |
| 84 | + boolean diskFileItemsCleanedUp = false; |
| 85 | + boolean temporaryFilesCleanedUp = false; |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void cleanUpDiskFileItems() { |
| 89 | + diskFileItemsCleanedUp = true; |
| 90 | + super.cleanUpDiskFileItems(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void cleanUpTemporaryFiles() { |
| 95 | + temporaryFilesCleanedUp = true; |
| 96 | + super.cleanUpTemporaryFiles(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + CustomJakartaMultiPartRequest customMultiPart = new CustomJakartaMultiPartRequest(); |
| 101 | + |
| 102 | + // when |
| 103 | + customMultiPart.cleanUp(); |
| 104 | + |
| 105 | + // then |
| 106 | + assertThat(customMultiPart.diskFileItemsCleanedUp).isTrue(); |
| 107 | + assertThat(customMultiPart.temporaryFilesCleanedUp).isTrue(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void temporaryFileCreationFailureAddsError() throws IOException { |
| 112 | + // Create a custom implementation that simulates temp file creation failure |
| 113 | + class FaultyJakartaMultiPartRequest extends JakartaMultiPartRequest { |
| 114 | + @Override |
| 115 | + protected void processFileField(DiskFileItem item) { |
| 116 | + // Simulate in-memory upload that fails to create temp file |
| 117 | + if (item.isInMemory()) { |
| 118 | + try { |
| 119 | + // Simulate IOException during temp file creation |
| 120 | + throw new IOException("Simulated temp file creation failure"); |
| 121 | + } catch (IOException e) { |
| 122 | + // Add the error to the errors list for proper user feedback |
| 123 | + LocalizedMessage errorMessage = buildErrorMessage(e.getClass(), e.getMessage(), |
| 124 | + new Object[]{item.getName()}); |
| 125 | + if (!errors.contains(errorMessage)) { |
| 126 | + errors.add(errorMessage); |
| 127 | + } |
| 128 | + } |
| 129 | + } else { |
| 130 | + super.processFileField(item); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + FaultyJakartaMultiPartRequest faultyMultiPart = new FaultyJakartaMultiPartRequest(); |
| 136 | + |
| 137 | + // given - small file that would normally be in-memory |
| 138 | + String content = formFile("file1", "test1.csv", "a,b") + |
| 139 | + endline + "--" + boundary + "--"; |
| 140 | + |
| 141 | + mockRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); |
| 142 | + |
| 143 | + // when |
| 144 | + faultyMultiPart.parse(mockRequest, tempDir); |
| 145 | + |
| 146 | + // then - verify error is properly captured |
| 147 | + assertThat(faultyMultiPart.getErrors()) |
| 148 | + .hasSize(1) |
| 149 | + .first() |
| 150 | + .extracting(LocalizedMessage::getTextKey) |
| 151 | + .isEqualTo("struts.messages.upload.error.IOException"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void temporaryFileCreationErrorsAreNotDuplicated() throws IOException { |
| 156 | + // Test that duplicate errors are not added to the errors list |
| 157 | + JakartaMultiPartRequest multiPartWithDuplicateErrors = new JakartaMultiPartRequest(); |
| 158 | + |
| 159 | + // Simulate adding the same error twice |
| 160 | + IOException testException = new IOException("Test exception"); |
| 161 | + LocalizedMessage errorMessage = multiPartWithDuplicateErrors.buildErrorMessage( |
| 162 | + testException.getClass(), testException.getMessage(), new Object[]{"test.csv"}); |
| 163 | + |
| 164 | + // when - add same error twice |
| 165 | + multiPartWithDuplicateErrors.errors.add(errorMessage); |
| 166 | + if (!multiPartWithDuplicateErrors.errors.contains(errorMessage)) { |
| 167 | + multiPartWithDuplicateErrors.errors.add(errorMessage); |
| 168 | + } |
| 169 | + |
| 170 | + // then - only one error should be present |
| 171 | + assertThat(multiPartWithDuplicateErrors.getErrors()).hasSize(1); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void cleanupIsIdempotent() throws IOException { |
| 176 | + // given - process some files |
| 177 | + String content = formFile("file1", "test1.csv", "1,2,3,4") + |
| 178 | + endline + "--" + boundary + "--"; |
| 179 | + |
| 180 | + mockRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); |
| 181 | + multiPart.parse(mockRequest, tempDir); |
| 182 | + |
| 183 | + // when - call cleanup multiple times |
| 184 | + multiPart.cleanUp(); |
| 185 | + multiPart.cleanUp(); |
| 186 | + multiPart.cleanUp(); |
| 187 | + |
| 188 | + // then - should not throw exceptions and should be safe |
| 189 | + assertThat(multiPart.uploadedFiles).isEmpty(); |
| 190 | + assertThat(multiPart.parameters).isEmpty(); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void endToEndMultipartProcessingWithCleanup() throws IOException { |
| 195 | + // Test complete multipart processing lifecycle |
| 196 | + String content = formFile("file1", "test1.csv", "1,2,3,4") + |
| 197 | + formField("param1", "value1") + |
| 198 | + formFile("file2", "test2.csv", "5,6,7,8") + |
| 199 | + formField("param2", "value2") + |
| 200 | + endline + "--" + boundary + "--"; |
| 201 | + |
| 202 | + mockRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); |
| 203 | + |
| 204 | + // when - full processing |
| 205 | + multiPart.parse(mockRequest, tempDir); |
| 206 | + |
| 207 | + // then - verify everything was processed |
| 208 | + assertThat(multiPart.getErrors()).isEmpty(); |
| 209 | + assertThat(multiPart.getFile("file1")).hasSize(1); |
| 210 | + assertThat(multiPart.getFile("file2")).hasSize(1); |
| 211 | + assertThat(multiPart.getParameter("param1")).isEqualTo("value1"); |
| 212 | + assertThat(multiPart.getParameter("param2")).isEqualTo("value2"); |
| 213 | + |
| 214 | + // when - cleanup |
| 215 | + multiPart.cleanUp(); |
| 216 | + |
| 217 | + // then - verify complete cleanup |
| 218 | + assertThat(multiPart.uploadedFiles).isEmpty(); |
| 219 | + assertThat(multiPart.parameters).isEmpty(); |
| 220 | + } |
| 221 | + |
28 | 222 | }
|
0 commit comments