Skip to content
Permalink
Browse files
FILEUPLOAD-336 - WhitespaceAround
  • Loading branch information
arturobernalg committed May 16, 2021
1 parent 5b5e8a6 commit b5c9fd79506f237da028c274eef2d9f9bb3d41bf
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 31 deletions.
@@ -104,7 +104,7 @@ private Base64Decoder() {
*/
public static int decode(final byte[] data, final OutputStream out) throws IOException {
int outLen = 0;
final byte [] cache = new byte[INPUT_BYTES_PER_CHUNK];
final byte[ ] cache = new byte[INPUT_BYTES_PER_CHUNK];
int cachedBytes = 0;

for (final byte b : data) {
@@ -120,7 +120,7 @@ public void testBelowThreshold() {
final OutputStream os = item.getOutputStream();
os.write(testFieldValueBytes);
os.close();
} catch(final IOException e) {
} catch (final IOException e) {
fail("Unexpected IOException");
}
assertTrue(item.isInMemory());
@@ -183,7 +183,7 @@ public void doTestAboveThreshold(final File repository) {
final OutputStream os = item.getOutputStream();
os.write(testFieldValueBytes);
os.close();
} catch(final IOException e) {
} catch (final IOException e) {
fail("Unexpected IOException");
}
assertFalse(item.isInMemory());
@@ -194,7 +194,7 @@ private byte[] createContentBytes(final int size) {
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;
@@ -220,7 +220,7 @@ private FileItem createFileItem(final byte[] contentBytes, final File repository
final OutputStream os = item.getOutputStream();
os.write(contentBytes);
os.close();
} catch(final IOException e) {
} catch (final IOException e) {
fail("Unexpected IOException" + e);
}

@@ -16,7 +16,6 @@
*/
package org.apache.commons.fileupload2;

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.nio.charset.StandardCharsets;
@@ -29,6 +28,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Test for {@link DiskFileUpload}. Remove when deprecated class is removed.
*
@@ -40,7 +40,7 @@ public void testParsing() {
assertEquals("stuff; stuff", params.get("test2"));
assertEquals("\"stuff", params.get("test3"));

params = parser.parse(s, new char[] { ',', ';' });
params = parser.parse(s, new char[] {',', ';' });
assertNull(params.get("test"));
assertEquals("stuff", params.get("test1"));
assertEquals("stuff; stuff", params.get("test2"));
@@ -95,15 +95,15 @@ public void testParsingEscapedChars() {
public void testFileUpload139() {
final ParameterParser parser = new ParameterParser();
String s = "Content-type: multipart/form-data , boundary=AaB03x";
Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
Map<String, String> params = parser.parse(s, new char[] {',', ';' });
assertEquals("AaB03x", params.get("boundary"));

s = "Content-type: multipart/form-data, boundary=AaB03x";
params = parser.parse(s, new char[] { ';', ',' });
params = parser.parse(s, new char[] {';', ',' });
assertEquals("AaB03x", params.get("boundary"));

s = "Content-type: multipart/mixed, boundary=BbC04y";
params = parser.parse(s, new char[] { ',', ';' });
params = parser.parse(s, new char[] {',', ';' });
assertEquals("BbC04y", params.get("boundary"));
}

@@ -140,17 +140,17 @@ public void testFileUpload274() {

// Should not decode if '*' is not at the end of param-name
s = "Content-Disposition: form-data; name=\"file\"; file*name=UTF-8\'\'%61%62%63\r\n";
params = parser.parse(s, new char[] { ',', ';' });
params = parser.parse(s, new char[] {',', ';' });
assertEquals("UTF-8\'\'%61%62%63", params.get("file*name"));

// Should not decode if param-value does not follow <charset>'<lang>'<encoded>
s = "Content-Disposition: form-data; name=\"file\"; filename*=a\'bc\r\n";
params = parser.parse(s, new char[] { ',', ';' });
params = parser.parse(s, new char[] {',', ';' });
assertEquals("a\'bc", params.get("filename"));

// Should not decode if param-name doesn't have '*' at end
s = "Content-Disposition: form-data; name=\"file\"; filename=a\'b\'c\r\n";
params = parser.parse(s, new char[] { ',', ';' });
params = parser.parse(s, new char[] {',', ';' });
assertEquals("a\'b\'c", params.get("filename"));
}

@@ -60,7 +60,7 @@ public void update(final long pBytesRead, final long pContentLength, final int p
items = new Integer(pItems);
}

void checkFinished(){
void checkFinished() {
assertEquals(expectedContentLength, bytesRead.longValue());
assertEquals(expectedItems, items.intValue());
}
@@ -72,14 +72,14 @@ void checkFinished(){
*/
@Test
public void testProgressListener() throws Exception {
final int numItems = 512;
final int NUM_ITEMS = 512;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int i = 0; i < numItems; i++) {
for (int i = 0; i < NUM_ITEMS; i++) {
final String header = "-----1234\r\n"
+ "Content-Disposition: form-data; name=\"field" + (i+1) + "\"\r\n"
+ "Content-Disposition: form-data; name=\"field" + (i + 1) + "\"\r\n"
+ "\r\n";
baos.write(header.getBytes(StandardCharsets.US_ASCII));
for (int j = 0; j < 16384+i; j++) {
for (int j = 0; j < 16384 + i; j++) {
baos.write((byte) j);
}
baos.write("\r\n".getBytes(StandardCharsets.US_ASCII));
@@ -88,25 +88,25 @@ public void testProgressListener() throws Exception {
final byte[] contents = baos.toByteArray();

MockHttpServletRequest request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE);
runTest(numItems, contents.length, request);
request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE){
runTest(NUM_ITEMS, contents.length, request);
request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE) {
@Override
public int getContentLength() {
return -1;
}
};
runTest(numItems, contents.length, request);
runTest(NUM_ITEMS, contents.length, request);
}

private void runTest(final int numItems, final long pContentLength, final MockHttpServletRequest request) throws FileUploadException, IOException {
private void runTest(final int NUM_ITEMS, final long pContentLength, final MockHttpServletRequest request) throws FileUploadException, IOException {
final ServletFileUpload upload = new ServletFileUpload();
final ProgressListenerImpl listener = new ProgressListenerImpl(pContentLength, numItems);
final ProgressListenerImpl listener = new ProgressListenerImpl(pContentLength, NUM_ITEMS);
upload.setProgressListener(listener);
final FileItemIterator iter = upload.getItemIterator(request);
for (int i = 0; i < numItems; i++) {
for (int i = 0; i < NUM_ITEMS; i++) {
final FileItemStream stream = iter.next();
final InputStream istream = stream.openStream();
for (int j = 0; j < 16384+i; j++) {
for (int j = 0; j < 16384 + i; j++) {
/**
* This used to be
* assertEquals((byte) j, (byte) istream.read());
@@ -76,8 +76,8 @@ public void testFileUpload()
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");
@@ -93,7 +93,7 @@ public void testFileUploadException()
public void testIOException()
throws IOException {
final byte[] request = newRequest();
final InputStream stream = new FilterInputStream(new ByteArrayInputStream(request)){
final InputStream stream = new FilterInputStream(new ByteArrayInputStream(request)) {
private int num;
@Override
public int read() throws IOException {
@@ -110,7 +110,7 @@ public int read(final byte[] pB, final int pOff, final int pLen)
if (res == -1) {
return i == 0 ? -1 : i;
}
pB[pOff+i] = (byte) res;
pB[pOff + i] = (byte) res;
}
return pLen;
}
@@ -135,8 +135,8 @@ public void badLength() throws Exception {
// The non-ASCII characters should just be ignored
@Test
public void nonASCIIcharacter() throws Exception {
assertEncoded("f","Zg=À="); // A-grave
assertEncoded("f","Zg=\u0100=");
assertEncoded("f", "Zg=À="); // A-grave
assertEncoded("f", "Zg=\u0100=");
}

private static void assertEncoded(final String clearText, final String encoded) throws Exception {

0 comments on commit b5c9fd7

Please sign in to comment.