Skip to content

Commit

Permalink
Merge b9b5ca2 into 09d6305
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed May 16, 2021
2 parents 09d6305 + b9b5ca2 commit de9c024
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
}

Expand All @@ -128,7 +128,7 @@ public void testFileUpload274() {

// Should parse a UTF-8 charset
String s = "Content-Disposition: form-data; name=\"file\"; filename*=UTF-8\'\'%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF\r\n";
Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
Map<String, String> params = parser.parse(s, new char[] {',', ';' });
assertEquals("\u3053\u3093\u306B\u3061\u306F", params.get("filename")); //filename = "こんにちは" in japanese

// Should parse ISO-8859-1 charset
Expand All @@ -138,17 +138,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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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));
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit de9c024

Please sign in to comment.