This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch 1.x in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git
The following commit(s) were added to refs/heads/1.x by this push: new 67fd12ef Fix some Checkstyle issues 67fd12ef is described below commit 67fd12ef85fd2e4663b2ec9e600dc933522d3403 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Fri May 9 07:41:10 2025 -0400 Fix some Checkstyle issues --- .../org/apache/commons/fileupload/Constants.java | 3 +- .../commons/fileupload/DefaultFileItemTest.java | 167 ++++++--------------- .../fileupload/DiskFileItemSerializeTest.java | 53 +++---- .../commons/fileupload/DiskFileUploadTest.java | 6 +- .../apache/commons/fileupload/FileUploadTest.java | 10 +- .../fileupload/HttpServletRequestFactory.java | 27 ++-- .../commons/fileupload/MockHttpServletRequest.java | 29 ++-- .../commons/fileupload/MultipartStreamTest.java | 25 +-- .../commons/fileupload/ParameterParserTest.java | 4 +- .../commons/fileupload/ProgressListenerTest.java | 44 +++--- .../apache/commons/fileupload/StreamingTest.java | 26 ++-- .../java/org/apache/commons/fileupload/Util.java | 4 + .../portlet/MockPortletActionRequest.java | 4 +- .../fileupload/portlet/PortletFileUploadTest.java | 48 +++--- .../fileupload/servlet/ServletFileUploadTest.java | 5 +- .../fileupload/util/mime/MimeUtilityTestCase.java | 8 +- .../util/mime/QuotedPrintableDecoderTestCase.java | 2 +- 17 files changed, 181 insertions(+), 284 deletions(-) diff --git a/src/test/java/org/apache/commons/fileupload/Constants.java b/src/test/java/org/apache/commons/fileupload/Constants.java index 69eaa530..a0a07f47 100644 --- a/src/test/java/org/apache/commons/fileupload/Constants.java +++ b/src/test/java/org/apache/commons/fileupload/Constants.java @@ -28,5 +28,6 @@ public final class Constants { */ public static final String CONTENT_TYPE = "multipart/form-data; boundary=---1234"; - private Constants() {} + private Constants() { + } } diff --git a/src/test/java/org/apache/commons/fileupload/DefaultFileItemTest.java b/src/test/java/org/apache/commons/fileupload/DefaultFileItemTest.java index 3a3b8f66..2342303f 100644 --- a/src/test/java/org/apache/commons/fileupload/DefaultFileItemTest.java +++ b/src/test/java/org/apache/commons/fileupload/DefaultFileItemTest.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.fileupload; import static org.junit.Assert.assertEquals; @@ -25,6 +26,7 @@ import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.apache.commons.io.FileUtils; @@ -33,68 +35,60 @@ import org.junit.Test; /** * Tests for {@link org.apache.commons.fileupload.DefaultFileItem}. */ -@SuppressWarnings({"deprecation"}) // unit tests for deprecated class +@SuppressWarnings({ "deprecation" }) // unit tests for deprecated class public class DefaultFileItemTest { /** * Content type for regular form items. */ - private static final String textContentType = "text/plain"; + private static final String CONTENT_TYPE_TEXT = "text/plain"; /** * Content type for file uploads. */ - private static final String fileContentType = "application/octet-stream"; + private static final String CONTENT_TYPE_FILE = "application/octet-stream"; /** * Very low threshold for testing memory versus disk options. */ - private static final int threshold = 16; + private static final int THRESHOLD = 16; - static final String CHARSET_ISO88591 = "ISO-8859-1"; + /** Charset name. */ + static final String CHARSET_ISO_8859_1 = StandardCharsets.ISO_8859_1.name(); - static final String CHARSET_ASCII = "US-ASCII"; + /** Charset name. */ + static final String CHARSET_US_ASCII = StandardCharsets.US_ASCII.name(); - static final String CHARSET_UTF8 = "UTF-8"; + /** Charset name. */ + static final String CHARSET_UTF8 = StandardCharsets.UTF_8.name(); + /** Charset name. */ static final String CHARSET_KOI8_R = "KOI8_R"; + /** Charset name. */ static final String CHARSET_WIN1251 = "Cp1251"; - static final int SWISS_GERMAN_STUFF_UNICODE [] = { - 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 - }; - + /** Test fixture. */ + static final int[] SWISS_GERMAN_STUFF_UNICODE = { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 }; - static final int SWISS_GERMAN_STUFF_ISO8859_1 [] = { - 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 - }; + /** Test fixture. */ + static final int[] SWISS_GERMAN_STUFF_ISO8859_1 = { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 }; - static final int SWISS_GERMAN_STUFF_UTF8 [] = { - 0x47, 0x72, 0xC3, 0xBC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xC3, 0xA4, - 0x6D, 0xC3, 0xA4 - }; + /** Test fixture. */ + static final int[] SWISS_GERMAN_STUFF_UTF8 = { 0x47, 0x72, 0xC3, 0xBC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xC3, 0xA4, 0x6D, 0xC3, 0xA4 }; - static final int RUSSIAN_STUFF_UNICODE [] = { - 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, - 0x432, 0x435, 0x442 - }; + /** Test fixture. */ + static final int[] RUSSIAN_STUFF_UNICODE = { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 }; - static final int RUSSIAN_STUFF_UTF8 [] = { - 0xD0, 0x92, 0xD1, 0x81, 0xD0, 0xB5, 0xD0, 0xBC, 0x5F, - 0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, - 0xB5, 0xD1, 0x82 - }; + /** Test fixture. */ + static final int[] RUSSIAN_STUFF_UTF8 = { 0xD0, 0x92, 0xD1, 0x81, 0xD0, 0xB5, 0xD0, 0xBC, 0x5F, 0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5, + 0xD1, 0x82 }; - static final int RUSSIAN_STUFF_KOI8R [] = { - 0xF7, 0xD3, 0xC5, 0xCD, 0x5F, 0xD0, 0xD2, 0xC9, 0xD7, - 0xC5, 0xD4 - }; + /** Test fixture. */ + static final int[] RUSSIAN_STUFF_KOI8R = { 0xF7, 0xD3, 0xC5, 0xCD, 0x5F, 0xD0, 0xD2, 0xC9, 0xD7, 0xC5, 0xD4 }; - static final int RUSSIAN_STUFF_WIN1251 [] = { - 0xC2, 0xF1, 0xE5, 0xEC, 0x5F, 0xEF, 0xF0, 0xE8, 0xE2, - 0xE5, 0xF2 - }; + /** Test fixture. */ + static final int[] RUSSIAN_STUFF_WIN1251 = { 0xC2, 0xF1, 0xE5, 0xEC, 0x5F, 0xEF, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2 }; private static String constructString(final int[] unicodeChars) { final StringBuilder buffer = new StringBuilder(); @@ -107,39 +101,27 @@ public class DefaultFileItemTest { } /** - * Creates a new {@code FileItemFactory} and returns it, obscuring - * from the caller the underlying implementation of this interface. + * Creates a new {@code FileItemFactory} and returns it, obscuring from the caller the underlying implementation of this interface. * - * @param repository The directory within which temporary files will be - * created. + * @param repository The directory within which temporary files will be created. * @return the new {@code FileItemFactory} instance. */ protected FileItemFactory createFactory(final File repository) { - return new DefaultFileItemFactory(threshold, repository); + return new DefaultFileItemFactory(THRESHOLD, repository); } /** - * Common code for cases where the amount of data is above the configured - * threshold, but the ultimate destination of the data has not yet been - * determined. + * Common code for cases where the amount of data is above the configured threshold, but the ultimate destination of the data has not yet been determined. * - * @param repository The directory within which temporary files will be - * created. + * @param repository The directory within which temporary files will be created. */ private void doTestAboveThreshold(final File repository) throws IOException { final FileItemFactory factory = createFactory(repository); final String textFieldName = "textField"; final String textFieldValue = "01234567890123456789"; final byte[] testFieldValueBytes = textFieldValue.getBytes(); - - final FileItem item = factory.createItem( - textFieldName, - textContentType, - true, - null - ); + final FileItem item = factory.createItem(textFieldName, CONTENT_TYPE_TEXT, true, null); assertNotNull(item); - try (OutputStream os = item.getOutputStream()) { os.write(testFieldValueBytes); } @@ -147,24 +129,20 @@ public class DefaultFileItemTest { assertEquals(item.getSize(), testFieldValueBytes.length); assertTrue(Arrays.equals(item.get(), testFieldValueBytes)); assertEquals(item.getString(), textFieldValue); - assertTrue(item instanceof DefaultFileItem); final DefaultFileItem dfi = (DefaultFileItem) item; final File storeLocation = dfi.getStoreLocation(); assertNotNull(storeLocation); assertTrue(storeLocation.exists()); assertEquals(storeLocation.length(), testFieldValueBytes.length); - if (repository != null) { assertEquals(storeLocation.getParentFile(), repository); } - item.delete(); } /** - * Test creation of a field for which the amount of data falls above the - * configured threshold, where no specific repository is configured. + * Test creation of a field for which the amount of data falls above the configured threshold, where no specific repository is configured. */ @Test public void testAboveThresholdDefaultRepository() throws IOException { @@ -172,8 +150,7 @@ public class DefaultFileItemTest { } /** - * Test creation of a field for which the amount of data falls above the - * configured threshold, where a specific repository is configured. + * Test creation of a field for which the amount of data falls above the configured threshold, where a specific repository is configured. */ @Test public void testAboveThresholdSpecifiedRepository() throws IOException { @@ -186,8 +163,7 @@ public class DefaultFileItemTest { } /** - * 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() throws IOException { @@ -195,15 +171,8 @@ public class DefaultFileItemTest { final String textFieldName = "textField"; final String textFieldValue = "0123456789"; final byte[] testFieldValueBytes = textFieldValue.getBytes(); - - final FileItem item = factory.createItem( - textFieldName, - textContentType, - true, - null - ); + final FileItem item = factory.createItem(textFieldName, CONTENT_TYPE_TEXT, true, null); assertNotNull(item); - try (OutputStream os = item.getOutputStream()) { os.write(testFieldValueBytes); } @@ -219,64 +188,37 @@ public class DefaultFileItemTest { @Test public void testContentCharSet() throws Exception { final FileItemFactory factory = createFactory(null); - String teststr = constructString(SWISS_GERMAN_STUFF_UNICODE); - - FileItem item = factory.createItem( - "doesnotmatter", - "text/plain; charset=" + CHARSET_ISO88591, - true, - null); + FileItem item = factory.createItem("doesnotmatter", "text/plain; charset=" + CHARSET_ISO_8859_1, true, null); try (OutputStream out = item.getOutputStream()) { for (final int element : SWISS_GERMAN_STUFF_ISO8859_1) { out.write(element); } } assertEquals(teststr, teststr, item.getString()); - - item = factory.createItem( - "doesnotmatter", - "text/plain; charset=" + CHARSET_UTF8, - true, - null); + item = factory.createItem("doesnotmatter", "text/plain; charset=" + CHARSET_UTF8, true, null); try (OutputStream out = item.getOutputStream()) { for (final int element : SWISS_GERMAN_STUFF_UTF8) { out.write(element); } } assertEquals(teststr, teststr, item.getString()); - teststr = constructString(RUSSIAN_STUFF_UNICODE); - - item = factory.createItem( - "doesnotmatter", - "text/plain; charset=" + CHARSET_KOI8_R, - true, - null); + item = factory.createItem("doesnotmatter", "text/plain; charset=" + CHARSET_KOI8_R, true, null); try (OutputStream out = item.getOutputStream()) { for (final int element : RUSSIAN_STUFF_KOI8R) { out.write(element); } } assertEquals(teststr, teststr, item.getString()); - - item = factory.createItem( - "doesnotmatter", - "text/plain; charset=" + CHARSET_WIN1251, - true, - null); + item = factory.createItem("doesnotmatter", "text/plain; charset=" + CHARSET_WIN1251, true, null); try (OutputStream out = item.getOutputStream()) { for (final int element : RUSSIAN_STUFF_WIN1251) { out.write(element); } } assertEquals(teststr, teststr, item.getString()); - - item = factory.createItem( - "doesnotmatter", - "text/plain; charset=" + CHARSET_UTF8, - true, - null); + item = factory.createItem("doesnotmatter", "text/plain; charset=" + CHARSET_UTF8, true, null); try (OutputStream out = item.getOutputStream()) { for (final int element : RUSSIAN_STUFF_UTF8) { out.write(element); @@ -293,16 +235,10 @@ public class DefaultFileItemTest { final FileItemFactory factory = createFactory(null); final String fileFieldName = "fileField"; final String fileName = "originalFileName"; - - final FileItem item = factory.createItem( - fileFieldName, - fileContentType, - false, - fileName - ); + final FileItem item = factory.createItem(fileFieldName, CONTENT_TYPE_FILE, false, fileName); assertNotNull(item); assertEquals(item.getFieldName(), fileFieldName); - assertEquals(item.getContentType(), fileContentType); + assertEquals(item.getContentType(), CONTENT_TYPE_FILE); assertFalse(item.isFormField()); assertEquals(item.getName(), fileName); } @@ -314,18 +250,11 @@ public class DefaultFileItemTest { public void testTextFieldConstruction() { final FileItemFactory factory = createFactory(null); final String textFieldName = "textField"; - - final FileItem item = factory.createItem( - textFieldName, - textContentType, - true, - null - ); + final FileItem item = factory.createItem(textFieldName, CONTENT_TYPE_TEXT, true, null); assertNotNull(item); assertEquals(item.getFieldName(), textFieldName); - assertEquals(item.getContentType(), textContentType); + assertEquals(item.getContentType(), CONTENT_TYPE_TEXT); assertTrue(item.isFormField()); assertNull(item.getName()); } - } diff --git a/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java b/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java index b0be975b..a75902a6 100644 --- a/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java +++ b/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.fileupload; import static org.junit.Assert.assertEquals; @@ -38,8 +39,7 @@ import org.junit.Before; import org.junit.Test; /** - * Serialization Unit tests for - * {@link org.apache.commons.fileupload.disk.DiskFileItem}. + * Serialization Unit tests for {@link org.apache.commons.fileupload.disk.DiskFileItem}. */ public class DiskFileItemSerializeTest { @@ -49,12 +49,12 @@ public class DiskFileItemSerializeTest { /** * Content type for regular form items. */ - private static final String textContentType = "text/plain"; + private static final String CONTENT_TYPE_TEXT = "text/plain"; /** * Very low threshold for testing memory versus disk options. */ - private static final int threshold = 16; + private static final int THRESHOLD = 16; /** * Compare content bytes. @@ -75,7 +75,7 @@ public class DiskFileItemSerializeTest { final StringBuilder buffer = new StringBuilder(size); byte count = 0; for (int i = 0; i < size; i++) { - buffer.append(count+""); + buffer.append(count + ""); count++; if (count > 9) { count = 0; @@ -95,24 +95,19 @@ public class DiskFileItemSerializeTest { * Create a FileItem with the specfied content bytes and repository. */ private FileItem createFileItem(final byte[] contentBytes, final File repository) { - final FileItemFactory factory = new DiskFileItemFactory(threshold, repository); + final FileItemFactory factory = new DiskFileItemFactory(THRESHOLD, repository); final String textFieldName = "textField"; - final FileItem item = factory.createItem( - textFieldName, - textContentType, - true, - "My File Name" - ); + final FileItem item = factory.createItem(textFieldName, CONTENT_TYPE_TEXT, true, "My File Name"); try (OutputStream os = item.getOutputStream()) { os.write(contentBytes); - } catch(final IOException e) { + } catch (final IOException e) { fail("Unexpected IOException" + e); } return item; } /** - * Do deserialization + * Tests deserialization. */ private Object deserialize(final ByteArrayOutputStream baos) throws Exception { Object result = null; @@ -124,7 +119,7 @@ public class DiskFileItemSerializeTest { } /** - * Do serialization + * Tests serialization. */ private ByteArrayOutputStream serialize(final Object target) throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -145,38 +140,34 @@ public class DiskFileItemSerializeTest { @After public void tearDown() throws IOException { - for(final File file : FileUtils.listFiles(REPO, null, true)) { + for (final File file : FileUtils.listFiles(REPO, null, true)) { System.out.println("Found leftover file " + file); } FileUtils.deleteDirectory(REPO); } /** - * 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() { // Create the FileItem - final byte[] testFieldValueBytes = createContentBytes(threshold + 1); + final byte[] testFieldValueBytes = createContentBytes(THRESHOLD + 1); final FileItem item = createFileItem(testFieldValueBytes); - // Check state is as expected assertFalse("Initial: in memory", item.isInMemory()); assertEquals("Initial: size", item.getSize(), testFieldValueBytes.length); compareBytes("Initial", item.get(), testFieldValueBytes); - item.delete(); } /** - * 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() { // Create the FileItem - final byte[] testFieldValueBytes = createContentBytes(threshold - 1); + final byte[] testFieldValueBytes = createContentBytes(THRESHOLD - 1); testInMemoryObject(testFieldValueBytes); } @@ -192,7 +183,6 @@ public class DiskFileItemSerializeTest { */ private void testInMemoryObject(final byte[] testFieldValueBytes, final File repository) { final FileItem item = createFileItem(testFieldValueBytes, repository); - // Check state is as expected assertTrue("Initial: in memory", item.isInMemory()); assertEquals("Initial: size", item.getSize(), testFieldValueBytes.length); @@ -203,10 +193,10 @@ public class DiskFileItemSerializeTest { /** * Test deserialization fails when repository is not valid. */ - @Test(expected=IOException.class) + @Test(expected = IOException.class) public void testInvalidRepository() throws Exception { // Create the FileItem - final byte[] testFieldValueBytes = createContentBytes(threshold); + final byte[] testFieldValueBytes = createContentBytes(THRESHOLD); final File repository = new File(System.getProperty("java.io.tmpdir"), "file"); final FileItem item = createFileItem(testFieldValueBytes, repository); deserialize(serialize(item)); @@ -218,20 +208,19 @@ public class DiskFileItemSerializeTest { @Test(expected = InvalidPathException.class) public void testInvalidRepositoryWithNullChar() throws Exception { // Create the FileItem - final byte[] testFieldValueBytes = createContentBytes(threshold); + final byte[] testFieldValueBytes = createContentBytes(THRESHOLD); final File repository = new File(System.getProperty("java.io.tmpdir"), "\0"); final FileItem item = createFileItem(testFieldValueBytes, repository); deserialize(serialize(item)); } /** - * 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() { // Create the FileItem - final byte[] testFieldValueBytes = createContentBytes(threshold); + final byte[] testFieldValueBytes = createContentBytes(THRESHOLD); testInMemoryObject(testFieldValueBytes); } @@ -241,7 +230,7 @@ public class DiskFileItemSerializeTest { @Test public void testValidRepository() { // Create the FileItem - final byte[] testFieldValueBytes = createContentBytes(threshold); + final byte[] testFieldValueBytes = createContentBytes(THRESHOLD); testInMemoryObject(testFieldValueBytes, REPO); } } diff --git a/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java b/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java index cac6f590..73d9b5e0 100644 --- a/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java @@ -50,10 +50,10 @@ public class DiskFileUploadTest { public void testMoveFile() throws Exception { final DiskFileUpload myUpload = new DiskFileUpload(); myUpload.setSizeThreshold(0); - final String content = + final String content = "-----1234\r\n" + - "Content-Disposition: form-data; name=\"file\";" - + "filename=\"foo.tab\"\r\n" + + "Content-Disposition: form-data; name=\"file\";" + + "filename=\"foo.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "\r\n" + "This is the content of the file\n" + diff --git a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java index 17d3024a..e7af08d1 100644 --- a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java @@ -49,7 +49,7 @@ public class FileUploadTest { /** * @return {@link FileUpload} classes under test. */ - @Parameters(name="{0}") + @Parameters(name = "{0}") public static Iterable<? extends Object> data() { return Util.fileUploadImplementations(); } @@ -72,7 +72,7 @@ public class FileUploadTest { } /** - * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-239">FILEUPLOAD-239</a> + * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-239">FILEUPLOAD-239</a>. */ @Test public void testContentTypeAttachment() @@ -195,7 +195,7 @@ public class FileUploadTest { } /** - * Test case for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-130"> + * Tests <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-130">FILEUPLOAD-130</a>. */ @Test public void testFileUpload130() @@ -248,7 +248,7 @@ public class FileUploadTest { } /** - * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-62">FILEUPLOAD-62</a> + * Tests for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-62">FILEUPLOAD-62</a>. */ @Test public void testFILEUPLOAD62() throws Exception { @@ -292,7 +292,7 @@ public class FileUploadTest { } /** - * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-111">FILEUPLOAD-111</a> + * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-111">FILEUPLOAD-111</a>. */ @Test public void testFoldedHeaders() diff --git a/src/test/java/org/apache/commons/fileupload/HttpServletRequestFactory.java b/src/test/java/org/apache/commons/fileupload/HttpServletRequestFactory.java index 28dfa73a..c4dd033a 100644 --- a/src/test/java/org/apache/commons/fileupload/HttpServletRequestFactory.java +++ b/src/test/java/org/apache/commons/fileupload/HttpServletRequestFactory.java @@ -14,42 +14,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.fileupload; import javax.servlet.http.HttpServletRequest; final class HttpServletRequestFactory { - static public HttpServletRequest createHttpServletRequestWithNullContentType() { + public static HttpServletRequest createHttpServletRequestWithNullContentType() { final byte[] requestData = "foobar".getBytes(); - return new MockHttpServletRequest( - requestData, - null); + return new MockHttpServletRequest(requestData, null); } - static public HttpServletRequest createInvalidHttpServletRequest() { + public static HttpServletRequest createInvalidHttpServletRequest() { final byte[] requestData = "foobar".getBytes(); - return new MockHttpServletRequest( - requestData, - FileUploadBase.MULTIPART_FORM_DATA); + return new MockHttpServletRequest(requestData, FileUploadBase.MULTIPART_FORM_DATA); } - static public HttpServletRequest createValidHttpServletRequest( - final String[] strFileNames) { + public static HttpServletRequest createValidHttpServletRequest(final String[] strFileNames) { // TODO provide a real implementation - final StringBuilder sbRequestData = new StringBuilder(); - for (final String strFileName : strFileNames) { sbRequestData.append(strFileName); } - byte[] requestData = null; requestData = sbRequestData.toString().getBytes(); - - return new MockHttpServletRequest( - requestData, - FileUploadBase.MULTIPART_FORM_DATA); + return new MockHttpServletRequest(requestData, FileUploadBase.MULTIPART_FORM_DATA); } + private HttpServletRequestFactory() { + // empty + } } diff --git a/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java b/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java index dd29db62..e041384c 100644 --- a/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java +++ b/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.security.Principal; import java.util.Enumeration; +import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -44,7 +45,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * Creates a new instance, which returns the given * streams data. */ - public MyServletInputStream(final InputStream in, final int readLimit) { + MyServletInputStream(final InputStream in, final int readLimit) { this.in = in; this.readLimit = readLimit; } @@ -55,7 +56,7 @@ public class MockHttpServletRequest implements HttpServletRequest { } @Override - public int read(final byte b[], final int off, final int len) throws IOException { + public int read(final byte[] b, final int off, final int len) throws IOException { if (readLimit > 0) { return in.read(b, off, Math.min(readLimit, len)); } @@ -64,15 +65,15 @@ public class MockHttpServletRequest implements HttpServletRequest { } - private final InputStream m_requestData; + private final InputStream requestData; private long length; - private final String m_strContentType; + private final String strContentType; private int readLimit = -1; - private final Map<String, String> m_headers = new java.util.HashMap<>(); + private final Map<String, String> headers = new HashMap<>(); /** * Creates a new instance with the given request data @@ -91,12 +92,12 @@ public class MockHttpServletRequest implements HttpServletRequest { */ public MockHttpServletRequest( final InputStream requestData, - final long requestLength, + final long length, final String strContentType) { - m_requestData = requestData; - length = requestLength; - m_strContentType = strContentType; - m_headers.put(FileUploadBase.CONTENT_TYPE, strContentType); + this.requestData = requestData; + this.length = length; + this.strContentType = strContentType; + this.headers.put(FileUploadBase.CONTENT_TYPE, strContentType); } /** @@ -138,7 +139,7 @@ public class MockHttpServletRequest implements HttpServletRequest { public int getContentLength() { int iLength = 0; - if (null == m_requestData) { + if (null == requestData) { iLength = -1; } else { if (length > Integer.MAX_VALUE) { @@ -154,7 +155,7 @@ public class MockHttpServletRequest implements HttpServletRequest { */ @Override public String getContentType() { - return m_strContentType; + return strContentType; } /** @@ -186,7 +187,7 @@ public class MockHttpServletRequest implements HttpServletRequest { */ @Override public String getHeader(final String headerName) { - return m_headers.get(headerName); + return headers.get(headerName); } /** @@ -212,7 +213,7 @@ public class MockHttpServletRequest implements HttpServletRequest { */ @Override public ServletInputStream getInputStream() throws IOException { - return new MyServletInputStream(m_requestData, readLimit); + return new MyServletInputStream(requestData, readLimit); } /** diff --git a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java index ceb96378..6c0b71b0 100644 --- a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java +++ b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.fileupload; import static org.junit.Assert.assertNotNull; @@ -30,7 +31,7 @@ public class MultipartStreamTest { static private final String BOUNDARY_TEXT = "myboundary"; - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void testSmallBuffer() throws Exception { final String strData = "foobar"; final byte[] contents = strData.getBytes(); @@ -38,12 +39,7 @@ public class MultipartStreamTest { final byte[] boundary = BOUNDARY_TEXT.getBytes(); final int iBufSize = 1; @SuppressWarnings("unused") - final - MultipartStream unused = new MultipartStream( - input, - boundary, - iBufSize, - new MultipartStream.ProgressNotifier(null, contents.length)); + final MultipartStream unused = new MultipartStream(input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length)); } @Test @@ -52,13 +48,8 @@ public class MultipartStreamTest { final byte[] contents = strData.getBytes(); final InputStream input = new ByteArrayInputStream(contents); final byte[] boundary = BOUNDARY_TEXT.getBytes(); - final int iBufSize = - boundary.length + MultipartStream.BOUNDARY_PREFIX.length + 1; - final MultipartStream ms = new MultipartStream( - input, - boundary, - iBufSize, - new MultipartStream.ProgressNotifier(null, contents.length)); + final int iBufSize = boundary.length + MultipartStream.BOUNDARY_PREFIX.length + 1; + final MultipartStream ms = new MultipartStream(input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length)); assertNotNull(ms); } @@ -68,11 +59,7 @@ public class MultipartStreamTest { final byte[] contents = strData.getBytes(); final InputStream input = new ByteArrayInputStream(contents); final byte[] boundary = BOUNDARY_TEXT.getBytes(); - final MultipartStream ms = new MultipartStream( - input, - boundary, - new MultipartStream.ProgressNotifier(null, contents.length)); + final MultipartStream ms = new MultipartStream(input, boundary, new MultipartStream.ProgressNotifier(null, contents.length)); assertNotNull(ms); } - } diff --git a/src/test/java/org/apache/commons/fileupload/ParameterParserTest.java b/src/test/java/org/apache/commons/fileupload/ParameterParserTest.java index 08b1111a..73597ceb 100644 --- a/src/test/java/org/apache/commons/fileupload/ParameterParserTest.java +++ b/src/test/java/org/apache/commons/fileupload/ParameterParserTest.java @@ -55,7 +55,7 @@ public class ParameterParserTest { } /** - * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-199">FILEUPLOAD-199</a> + * Tests <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-199">FILEUPLOAD-199</a>. */ @Test public void testFileUpload199() { @@ -66,7 +66,7 @@ public class ParameterParserTest { } /** - * Test for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-274">FILEUPLOAD-274</a> + * Tests <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-274">FILEUPLOAD-274</a>. */ @Test public void testFileUpload274() { diff --git a/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java b/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java index 6aac067f..179e0f01 100644 --- a/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java +++ b/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.fileupload; import static org.junit.Assert.assertEquals; @@ -34,12 +35,16 @@ public class ProgressListenerTest { private static class ProgressListenerImpl implements ProgressListener { + /** Expected content length. */ private final long expectedContentLength; + /** Expected item count. */ private final int expectedItems; + /** Bytes read count. */ private Long bytesRead; + /** Item count. */ private Integer items; ProgressListenerImpl(final long expectedContentLength, final int expectedItems) { @@ -47,23 +52,21 @@ public class ProgressListenerTest { this.expectedItems = expectedItems; } - void checkFinished(){ + void checkFinished() { assertEquals(expectedContentLength, bytesRead.longValue()); assertEquals(expectedItems, items.intValue()); } @Override public void update(final long actualBytesRead, final long actualContentLength, final int actualItems) { - assertTrue(actualBytesRead >= 0 && actualBytesRead <= expectedContentLength); - assertTrue(actualContentLength == -1 || actualContentLength == expectedContentLength); - assertTrue(actualItems >= 0 && actualItems <= expectedItems); - - assertTrue(this.bytesRead == null || actualBytesRead >= this.bytesRead.longValue()); + assertTrue(actualBytesRead >= 0 && actualBytesRead <= expectedContentLength); + assertTrue(actualContentLength == -1 || actualContentLength == expectedContentLength); + assertTrue(actualItems >= 0 && actualItems <= expectedItems); + assertTrue(this.bytesRead == null || actualBytesRead >= this.bytesRead.longValue()); this.bytesRead = Long.valueOf(actualBytesRead); - assertTrue(items == null || actualItems >= items.intValue()); + assertTrue(items == null || actualItems >= items.intValue()); this.items = Integer.valueOf(actualItems); } - } private void runTest(final int numItems, final long contentLength, final MockHttpServletRequest request) throws FileUploadException, IOException { @@ -71,10 +74,11 @@ public class ProgressListenerTest { final ProgressListenerImpl listener = new ProgressListenerImpl(contentLength, numItems); upload.setProgressListener(listener); final FileItemIterator iter = upload.getItemIterator(request); - for (int i = 0; i < numItems; i++) { + for (int i = 0; i < numItems; i++) { final FileItemStream stream = iter.next(); try (InputStream istream = stream.openStream()) { - for (int j = 0; j < 16384 + i; j++) { + final int maxIn = 16384; + for (int j = 0; j < maxIn + i; j++) { /** * This used to be assertEquals((byte) j, (byte) istream.read()); but this seems to trigger a bug in JRockit, so we express the same like * this: @@ -97,30 +101,28 @@ public class ProgressListenerTest { */ @Test public void testProgressListener() throws Exception { - final int NUM_ITEMS = 512; + final int numItems = 512; final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - for (int i = 0; i < NUM_ITEMS; i++) { - final String header = "-----1234\r\n" - + "Content-Disposition: form-data; name=\"field" + (i+1) + "\"\r\n" - + "\r\n"; + for (int i = 0; i < numItems; i++) { + final String header = "-----1234\r\n" + "Content-Disposition: form-data; name=\"field" + (i + 1) + "\"\r\n" + "\r\n"; baos.write(header.getBytes("US-ASCII")); - for (int j = 0; j < 16384+i; j++) { + final int maxOut = 16384; + for (int j = 0; j < maxOut + i; j++) { baos.write((byte) j); } baos.write("\r\n".getBytes("US-ASCII")); } baos.write("-----1234--\r\n".getBytes("US-ASCII")); final byte[] contents = baos.toByteArray(); - MockHttpServletRequest request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE); - runTest(NUM_ITEMS, contents.length, request); - request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE){ + runTest(numItems, contents.length, request); + request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE) { + @Override public int getContentLength() { return -1; } }; - runTest(NUM_ITEMS, contents.length, request); + runTest(numItems, contents.length, request); } - } diff --git a/src/test/java/org/apache/commons/fileupload/StreamingTest.java b/src/test/java/org/apache/commons/fileupload/StreamingTest.java index 3a78d131..b4d0390b 100644 --- a/src/test/java/org/apache/commons/fileupload/StreamingTest.java +++ b/src/test/java/org/apache/commons/fileupload/StreamingTest.java @@ -62,7 +62,7 @@ public class StreamingTest { } @Override - public int read(final byte b[], final int off, final int len) throws IOException { + public int read(final byte[] b, final int off, final int len) throws IOException { return bais.read(b, off, Math.min(len, 3)); } @@ -125,14 +125,13 @@ public class StreamingTest { * Tests a file upload with varying file sizes. */ @Test - public void testFileUpload() - throws IOException, FileUploadException { + public void testFileUpload() throws IOException, FileUploadException { final byte[] request = newRequest(); final List<FileItem> fileItems = parseUpload(request); final Iterator<FileItem> fileIter = fileItems.iterator(); int add = 16; int num = 0; - for (int i = 0; i < 16384; i += add) { + for (int i = 0; i < 16384; i += add) { if (++add == 32) { add = 16; } @@ -140,7 +139,7 @@ public class StreamingTest { assertEquals("field" + num++, item.getFieldName()); final byte[] bytes = item.get(); assertEquals(i, bytes.length); - for (int j = 0; j < i; j++) { + for (int j = 0; j < i; j++) { assertEquals((byte) j, bytes[j]); } } @@ -148,11 +147,10 @@ public class StreamingTest { } /** - * Test for FILEUPLOAD-135 + * Test for FILEUPLOAD-135. */ @Test - public void testFILEUPLOAD135() - throws IOException, FileUploadException { + public void testFILEUPLOAD135() throws IOException, FileUploadException { final byte[] request = newShortRequest(); final ByteArrayInputStream bais = new ByteArrayInputStream(request); try (InputStream inputStream = newInputStream(bais)) { @@ -171,15 +169,13 @@ public class StreamingTest { } /** - * Tests, whether an invalid request throws a proper - * exception. + * Tests whether an invalid request throws a proper exception. */ @Test - public void testFileUploadException() - throws IOException, FileUploadException { + public void testFileUploadException() throws IOException, FileUploadException { final byte[] request = newRequest(); - final byte[] invalidRequest = new byte[request.length-11]; - System.arraycopy(request, 0, invalidRequest, 0, request.length-11); + final byte[] invalidRequest = new byte[request.length - 11]; + System.arraycopy(request, 0, invalidRequest, 0, request.length - 11); try { parseUpload(invalidRequest); fail("Expected EndOfStreamException"); @@ -189,7 +185,7 @@ public class StreamingTest { } /** - * Tests, whether an {@link InvalidFileNameException} is thrown. + * whether an {@link InvalidFileNameException} is thrown. */ @Test public void testInvalidFileNameException() throws Exception { diff --git a/src/test/java/org/apache/commons/fileupload/Util.java b/src/test/java/org/apache/commons/fileupload/Util.java index ab877695..c4a7f028 100644 --- a/src/test/java/org/apache/commons/fileupload/Util.java +++ b/src/test/java/org/apache/commons/fileupload/Util.java @@ -58,4 +58,8 @@ public class Util { final byte[] bytes = content.getBytes("US-ASCII"); return parseUpload(upload, bytes, Constants.CONTENT_TYPE); } + + private Util() { + // empty + } } diff --git a/src/test/java/org/apache/commons/fileupload/portlet/MockPortletActionRequest.java b/src/test/java/org/apache/commons/fileupload/portlet/MockPortletActionRequest.java index 4f87d880..0edf43e0 100644 --- a/src/test/java/org/apache/commons/fileupload/portlet/MockPortletActionRequest.java +++ b/src/test/java/org/apache/commons/fileupload/portlet/MockPortletActionRequest.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.fileupload.portlet; import java.io.BufferedReader; @@ -49,9 +50,7 @@ import org.apache.commons.fileupload.FileUploadBase; public class MockPortletActionRequest implements ActionRequest { private final Hashtable<String, Object> attributes = new Hashtable<>(); - private final Map<String, String> parameters = new HashMap<>(); - private String characterEncoding; private final int length; private final String contentType; @@ -267,5 +266,4 @@ public class MockPortletActionRequest implements ActionRequest { public void setCharacterEncoding(final String characterEncoding) throws UnsupportedEncodingException { this.characterEncoding = characterEncoding; } - } diff --git a/src/test/java/org/apache/commons/fileupload/portlet/PortletFileUploadTest.java b/src/test/java/org/apache/commons/fileupload/portlet/PortletFileUploadTest.java index 5eeda3ed..9b4f6415 100644 --- a/src/test/java/org/apache/commons/fileupload/portlet/PortletFileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload/portlet/PortletFileUploadTest.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.fileupload.portlet; import static org.junit.Assert.assertEquals; @@ -42,37 +43,35 @@ public class PortletFileUploadTest { private PortletFileUpload upload; @Test - public void parseParameterMap() - throws Exception { - final String text = "-----1234\r\n" + - "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + - "Content-Type: text/whatever\r\n" + - "\r\n" + - "This is the content of the file\n" + - "\r\n" + - "-----1234\r\n" + - "Content-Disposition: form-data; name=\"field\"\r\n" + - "\r\n" + - "fieldValue\r\n" + - "-----1234\r\n" + - "Content-Disposition: form-data; name=\"multi\"\r\n" + - "\r\n" + - "value1\r\n" + - "-----1234\r\n" + - "Content-Disposition: form-data; name=\"multi\"\r\n" + - "\r\n" + - "value2\r\n" + - "-----1234--\r\n"; + public void parseParameterMap() throws Exception { + // @formatter:off + final String text = "-----1234\r\n" + + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + + "Content-Type: text/whatever\r\n" + + "\r\n" + + "This is the content of the file\n" + + "\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"field\"\r\n" + + "\r\n" + + "fieldValue\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"multi\"\r\n" + + "\r\n" + + "value1\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"multi\"\r\n" + + "\r\n" + + "value2\r\n" + + "-----1234--\r\n"; + // @formatter:on final byte[] bytes = text.getBytes("US-ASCII"); final ActionRequest request = new MockPortletActionRequest(bytes, Constants.CONTENT_TYPE); - final Map<String, List<FileItem>> mappedParameters = upload.parseParameterMap(request); assertTrue(mappedParameters.containsKey("file")); assertEquals(1, mappedParameters.get("file").size()); - assertTrue(mappedParameters.containsKey("field")); assertEquals(1, mappedParameters.get("field").size()); - assertTrue(mappedParameters.containsKey("multi")); assertEquals(2, mappedParameters.get("multi").size()); } @@ -81,5 +80,4 @@ public class PortletFileUploadTest { public void setUp() { upload = new PortletFileUpload(new DiskFileItemFactory()); } - } diff --git a/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java b/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java index 4b9628f0..218c787c 100644 --- a/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java @@ -40,8 +40,7 @@ import org.junit.Test; public class ServletFileUploadTest { @Test - public void parseImpliedUtf8() - throws Exception { + public void parseImpliedUtf8() throws Exception { // utf8 encoded form-data without explicit content-type encoding final String text = "-----1234\r\n" + "Content-Disposition: form-data; name=\"utf8Html\"\r\n" + @@ -63,7 +62,7 @@ public class ServletFileUploadTest { /** - * Test case for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-210"> + * Tests <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-210">FILEUPLOAD-210</a>. */ @Test public void parseParameterMap() diff --git a/src/test/java/org/apache/commons/fileupload/util/mime/MimeUtilityTestCase.java b/src/test/java/org/apache/commons/fileupload/util/mime/MimeUtilityTestCase.java index 6ee8a10c..c00f923a 100644 --- a/src/test/java/org/apache/commons/fileupload/util/mime/MimeUtilityTestCase.java +++ b/src/test/java/org/apache/commons/fileupload/util/mime/MimeUtilityTestCase.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.fileupload.util.mime; import static org.junit.Assert.assertEquals; @@ -23,8 +24,7 @@ import java.io.UnsupportedEncodingException; import org.junit.Test; /** - * Use the online <a href="http://dogmamix.com/MimeHeadersDecoder/">MimeHeadersDecoder</a> - * to validate expected values. + * Use the online <a href="http://dogmamix.com/MimeHeadersDecoder/">MimeHeadersDecoder</a> to validate expected values. * * @since 1.3 */ @@ -34,7 +34,7 @@ public final class MimeUtilityTestCase { assertEquals(expected, MimeUtility.decodeText(encoded)); } - @Test(expected=UnsupportedEncodingException.class) + @Test(expected = UnsupportedEncodingException.class) public void decodeInvalidEncoding() throws Exception { MimeUtility.decodeText("=?invalid?B?xyz-?="); } @@ -42,7 +42,7 @@ public final class MimeUtilityTestCase { @Test public void decodeIso88591Base64Encoded() throws Exception { assertEncoded("If you can read this you understand the example.", - "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n"); + "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n"); } @Test diff --git a/src/test/java/org/apache/commons/fileupload/util/mime/QuotedPrintableDecoderTestCase.java b/src/test/java/org/apache/commons/fileupload/util/mime/QuotedPrintableDecoderTestCase.java index efe4f072..0b089124 100644 --- a/src/test/java/org/apache/commons/fileupload/util/mime/QuotedPrintableDecoderTestCase.java +++ b/src/test/java/org/apache/commons/fileupload/util/mime/QuotedPrintableDecoderTestCase.java @@ -52,7 +52,7 @@ public final class QuotedPrintableDecoderTestCase { fail("Expected IOException"); } catch (final IOException e) { final String em = e.getMessage(); - assertTrue("Expected to find " + messageText + " in '" + em + "'",em.contains(messageText)); + assertTrue("Expected to find " + messageText + " in '" + em + "'", em.contains(messageText)); } }