Skip to content
Permalink
Browse files
Merge pull request #87 from arturobernalg/feature/FILEUPLOAD-335
FILEUPLOAD-335 - ConstantName and Operator Order
  • Loading branch information
jochenw committed May 14, 2021
2 parents 6692f15 + 178ff6e commit 09d63053a3d295462e645feca9a67668cd19518d
Showing 7 changed files with 50 additions and 50 deletions.
@@ -28,5 +28,5 @@ public final class Constants {
*/
public static final String CONTENT_TYPE = "multipart/form-data; boundary=---1234";

private Constants() {}
private Constants() { }
}
@@ -42,17 +42,17 @@ public class DefaultFileItemTest {
/**
* Content type for regular form items.
*/
private static final String textContentType = "text/plain";
private static final String TEXT_CONTENT_TYPE = "text/plain";

/**
* Content type for file uploads.
*/
private static final String fileContentType = "application/octet-stream";
private static final String FILE_CONTENT_TYPE = "application/octet-stream";

/**
* Very low threshold for testing memory versus disk options.
*/
private static final int threshold = 16;
private static final int THRESHOLD = 16;

/**
* Test construction of a regular text field.
@@ -64,13 +64,13 @@ public void testTextFieldConstruction() {

final FileItem item = factory.createItem(
textFieldName,
textContentType,
TEXT_CONTENT_TYPE,
true,
null
);
assertNotNull(item);
assertEquals(item.getFieldName(), textFieldName);
assertEquals(item.getContentType(), textContentType);
assertEquals(item.getContentType(), TEXT_CONTENT_TYPE);
assertTrue(item.isFormField());
assertNull(item.getName());
}
@@ -86,13 +86,13 @@ public void testFileFieldConstruction() {

final FileItem item = factory.createItem(
fileFieldName,
fileContentType,
FILE_CONTENT_TYPE,
false,
fileName
);
assertNotNull(item);
assertEquals(item.getFieldName(), fileFieldName);
assertEquals(item.getContentType(), fileContentType);
assertEquals(item.getContentType(), FILE_CONTENT_TYPE);
assertFalse(item.isFormField());
assertEquals(item.getName(), fileName);
}
@@ -110,7 +110,7 @@ public void testBelowThreshold() {

final FileItem item = factory.createItem(
textFieldName,
textContentType,
TEXT_CONTENT_TYPE,
true,
null
);
@@ -173,7 +173,7 @@ public void doTestAboveThreshold(final File repository) {

final FileItem item = factory.createItem(
textFieldName,
textContentType,
TEXT_CONTENT_TYPE,
true,
null
);
@@ -219,7 +219,7 @@ public void doTestAboveThreshold(final File repository) {
* @return the new {@code FileItemFactory} instance.
*/
protected FileItemFactory createFactory(final File repository) {
return new DefaultFileItemFactory(threshold, repository);
return new DefaultFileItemFactory(THRESHOLD, repository);
}

static final String CHARSET_ISO88591 = "ISO-8859-1";
@@ -66,12 +66,12 @@ public void tearDown() throws IOException {
/**
* Content type for regular form items.
*/
private static final String textContentType = "text/plain";
private static final String TEXT_CONTENT_TYPE = "text/plain";

/**
* Very low threshold for testing memory versus disk options.
*/
private static final int threshold = 16;
private static final int THRESHOLD = 16;

/**
* Helper method to test creation of a field when a repository is used.
@@ -104,7 +104,7 @@ private void testInMemoryObject(final byte[] testFieldValueBytes) {
@Test
public void testBelowThreshold() {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(threshold - 1);
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD - 1);
testInMemoryObject(testFieldValueBytes);
}

@@ -115,7 +115,7 @@ public void testBelowThreshold() {
@Test
public void testThreshold() {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(threshold);
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
testInMemoryObject(testFieldValueBytes);
}

@@ -126,7 +126,7 @@ public void testThreshold() {
@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
@@ -147,7 +147,7 @@ public void testAboveThreshold() {
@Test
public void testValidRepository() {
// Create the FileItem
final byte[] testFieldValueBytes = createContentBytes(threshold);
final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
testInMemoryObject(testFieldValueBytes, REPO);
}

@@ -157,7 +157,7 @@ public void testValidRepository() {
@Test
public void testInvalidRepository() {
// 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);
assertThrows(IOException.class, () -> deserialize(serialize(item)));
@@ -169,7 +169,7 @@ public void testInvalidRepository() {
@Test
public void testInvalidRepositoryWithNullChar() {
// 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);
assertThrows(IOException.class, () -> deserialize(serialize(item)));
@@ -207,12 +207,12 @@ private byte[] createContentBytes(final int size) {
* 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,
TEXT_CONTENT_TYPE,
true,
"My File Name"
);
@@ -20,14 +20,14 @@

final class HttpServletRequestFactory {

static public HttpServletRequest createHttpServletRequestWithNullContentType() {
public static HttpServletRequest createHttpServletRequestWithNullContentType() {
final byte[] requestData = "foobar".getBytes();
return new MockHttpServletRequest(
requestData,
null);
}

static public HttpServletRequest createValidHttpServletRequest(
public static HttpServletRequest createValidHttpServletRequest(
final String[] strFileNames) {
// todo - provide a real implementation

@@ -34,15 +34,15 @@

public class MockHttpServletRequest implements HttpServletRequest {

private final InputStream m_requestData;
private final InputStream mmRequestData;

private long length;

private final String m_strContentType;
private final String mStrContentType;

private int readLimit = -1;

private final Map<String, String> m_headers = new java.util.HashMap<>();
private final Map<String, String> mHeaders = new java.util.HashMap<>();

/**
* Creates a new instance with the given request data
@@ -63,10 +63,10 @@ public MockHttpServletRequest(
final InputStream requestData,
final long requestLength,
final String strContentType) {
m_requestData = requestData;
mmRequestData = requestData;
length = requestLength;
m_strContentType = strContentType;
m_headers.put(FileUploadBase.CONTENT_TYPE, strContentType);
mStrContentType = strContentType;
mHeaders.put(FileUploadBase.CONTENT_TYPE, strContentType);
}

/**
@@ -98,7 +98,7 @@ public long getDateHeader(final String arg0) {
*/
@Override
public String getHeader(final String headerName) {
return m_headers.get(headerName);
return mHeaders.get(headerName);
}

/**
@@ -312,7 +312,7 @@ public void setCharacterEncoding(final String arg0)
public int getContentLength() {
int iLength;

if (null == m_requestData) {
if (null == mmRequestData) {
iLength = -1;
} else {
if (length > Integer.MAX_VALUE) {
@@ -335,15 +335,15 @@ public void setContentLength(final long length) {
*/
@Override
public String getContentType() {
return m_strContentType;
return mStrContentType;
}

/**
* @see javax.servlet.ServletRequest#getInputStream()
*/
@Override
public ServletInputStream getInputStream() throws IOException {
return new MyServletInputStream(m_requestData, readLimit);
return new MyServletInputStream(mmRequestData, readLimit);
}

/**
@@ -72,9 +72,9 @@ void checkFinished(){
*/
@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++) {
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";
@@ -88,22 +88,22 @@ public void testProgressListener() throws Exception {
final byte[] contents = baos.toByteArray();

MockHttpServletRequest request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE);
runTest(NUM_ITEMS, contents.length, request);
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);
}

private void runTest(final int NUM_ITEMS, final long pContentLength, final MockHttpServletRequest request) throws FileUploadException, IOException {
private void runTest(final int numItems, final long pContentLength, final MockHttpServletRequest request) throws FileUploadException, IOException {
final ServletFileUpload upload = new ServletFileUpload();
final ProgressListenerImpl listener = new ProgressListenerImpl(pContentLength, NUM_ITEMS);
final ProgressListenerImpl listener = new ProgressListenerImpl(pContentLength, numItems);
upload.setProgressListener(listener);
final FileItemIterator iter = upload.getItemIterator(request);
for (int i = 0; i < NUM_ITEMS; i++) {
for (int i = 0; i < numItems; i++) {
final FileItemStream stream = iter.next();
final InputStream istream = stream.openStream();
for (int j = 0; j < 16384+i; j++) {
@@ -48,15 +48,15 @@

public class MockJakSrvltHttpRequest implements HttpServletRequest {

private final InputStream m_requestData;
private final InputStream mRequestData;

private long length;

private final String m_strContentType;
private final String mStrContentType;

private int readLimit = -1;

private final Map<String, String> m_headers = new java.util.HashMap<>();
private final Map<String, String> mHeaders = new java.util.HashMap<>();

/**
* Creates a new instance with the given request data
@@ -77,10 +77,10 @@ public MockJakSrvltHttpRequest(
final InputStream requestData,
final long requestLength,
final String strContentType) {
m_requestData = requestData;
mRequestData = requestData;
length = requestLength;
m_strContentType = strContentType;
m_headers.put(FileUploadBase.CONTENT_TYPE, strContentType);
mStrContentType = strContentType;
mHeaders.put(FileUploadBase.CONTENT_TYPE, strContentType);
}

/**
@@ -112,7 +112,7 @@ public long getDateHeader(final String arg0) {
*/
@Override
public String getHeader(final String headerName) {
return m_headers.get(headerName);
return mHeaders.get(headerName);
}

/**
@@ -326,7 +326,7 @@ public void setCharacterEncoding(final String arg0)
public int getContentLength() {
int iLength;

if (null == m_requestData) {
if (null == mRequestData) {
iLength = -1;
} else {
if (length > Integer.MAX_VALUE) {
@@ -349,15 +349,15 @@ public void setContentLength(final long length) {
*/
@Override
public String getContentType() {
return m_strContentType;
return mStrContentType;
}

/**
* @see javax.servlet.ServletRequest#getInputStream()
*/
@Override
public ServletInputStream getInputStream() throws IOException {
return (ServletInputStream) new MyServletInputStream(m_requestData, readLimit);
return (ServletInputStream) new MyServletInputStream(mRequestData, readLimit);
}

/**

0 comments on commit 09d6305

Please sign in to comment.