Repository: wicket Updated Branches: refs/heads/wicket-6.x 7d164c618 -> 3a6eb04ca
WICKET-6270 check name of uploaded file is not empty Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/3a6eb04c Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/3a6eb04c Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/3a6eb04c Branch: refs/heads/wicket-6.x Commit: 3a6eb04ca17789b859e6efea067ea4bfddafa2c8 Parents: 7d164c6 Author: Sven Meier <svenme...@apache.org> Authored: Mon Nov 7 19:20:07 2016 +0100 Committer: Sven Meier <svenme...@apache.org> Committed: Mon Nov 7 19:27:47 2016 +0100 ---------------------------------------------------------------------- .../html/form/upload/FileUploadField.java | 6 ++- .../http/mock/MockHttpServletRequest.java | 51 +++++++++++--------- .../apache/wicket/util/tester/FormTester.java | 2 +- .../html/form/upload/FileUploadFieldTest.java | 41 +++++++++++++--- 4 files changed, 68 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/3a6eb04c/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java index 07f81f3..512e472 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java @@ -26,6 +26,7 @@ import org.apache.wicket.model.IModel; import org.apache.wicket.protocol.http.IMultipartWebRequest; import org.apache.wicket.request.Request; import org.apache.wicket.util.convert.ConversionException; +import org.apache.wicket.util.string.Strings; import org.apache.wicket.util.upload.FileItem; /** @@ -110,7 +111,10 @@ public class FileUploadField extends FormComponent<List<FileUpload>> { for (FileItem item : fileItems) { - fileUploads.add(new FileUpload(item)); + // WICKET-6270 detect empty field by missing file name + if (Strings.isEmpty(item.getName()) == false) { + fileUploads.add(new FileUpload(item)); + } } } } http://git-wip-us.apache.org/repos/asf/wicket/blob/3a6eb04c/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java index bc014f6..989c7f7 100755 --- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java +++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java @@ -56,7 +56,6 @@ import org.apache.wicket.util.encoding.UrlDecoder; import org.apache.wicket.util.encoding.UrlEncoder; import org.apache.wicket.util.file.File; import org.apache.wicket.util.io.IOUtils; -import org.apache.wicket.util.lang.Args; import org.apache.wicket.util.string.StringValue; import org.apache.wicket.util.string.Strings; import org.apache.wicket.util.upload.FileUploadBase; @@ -245,19 +244,19 @@ public class MockHttpServletRequest implements HttpServletRequest */ public void addFile(String fieldName, File file, String contentType) { - Args.notNull(file, "file"); - - if (file.exists() == false) - { - throw new IllegalArgumentException( - "File does not exists. You must provide an existing file: " + - file.getAbsolutePath()); - } + if (file != null) { + if (file.exists() == false) + { + throw new IllegalArgumentException( + "File does not exists. You must provide an existing file: " + + file.getAbsolutePath()); + } - if (file.isFile() == false) - { - throw new IllegalArgumentException( - "You can only add a File, which is not a directory. Only files can be uploaded."); + if (file.isFile() == false) + { + throw new IllegalArgumentException( + "You can only add a File, which is not a directory. Only files can be uploaded."); + } } if (uploadedFiles == null) @@ -1652,7 +1651,9 @@ public class MockHttpServletRequest implements HttpServletRequest out.write("; name=\"".getBytes()); out.write(fieldName.getBytes()); out.write("\"; filename=\"".getBytes()); - out.write(uf.getFile().getName().getBytes()); + if (uf.getFile() != null) { + out.write(uf.getFile().getName().getBytes()); + } out.write("\"".getBytes()); out.write(crlf.getBytes()); out.write("Content-Type: ".getBytes()); @@ -1660,16 +1661,18 @@ public class MockHttpServletRequest implements HttpServletRequest out.write(crlf.getBytes()); out.write(crlf.getBytes()); - // Load the file and put it into the the inputstream - FileInputStream fis = new FileInputStream(uf.getFile()); - - try - { - IOUtils.copy(fis, out); - } - finally - { - fis.close(); + if (uf.getFile() != null) { + // Load the file and put it into the the inputstream + FileInputStream fis = new FileInputStream(uf.getFile()); + + try + { + IOUtils.copy(fis, out); + } + finally + { + fis.close(); + } } out.write(crlf.getBytes()); } http://git-wip-us.apache.org/repos/asf/wicket/blob/3a6eb04c/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java b/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java index 1257648..f14b8d0 100644 --- a/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java +++ b/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java @@ -657,7 +657,7 @@ public class FormTester * <code>FormComponent</code>. The <code>FormComponent</code> must be of a type * <code>FileUploadField</code>. * @param file - * the <code>File</code> to upload. + * the <code>File</code> to upload or {@code null} for an empty input * @param contentType * the content type of the file. Must be a valid mime type. * @return This http://git-wip-us.apache.org/repos/asf/wicket/blob/3a6eb04c/wicket-core/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java index a9f6c51..d1f06b7 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/upload/FileUploadFieldTest.java @@ -128,22 +128,51 @@ public class FileUploadFieldTest extends WicketTestCase /** * https://issues.apache.org/jira/browse/WICKET-5691 - * - * */ + */ @Test public void testEmptyField() throws Exception { tester.startPage(TestValidationPage.class); + FileUploadField uploadField = (FileUploadField)tester.getComponentFromLastRenderedPage("form:upload"); + uploadField.add(new IValidator<List<FileUpload>>() { + @Override + public void validate(IValidatable<List<FileUpload>> validatable) + { + // must check during validation, since the uploads are nullified on detach + assertEquals(0, validatable.getValue().size()); + } + }); FormTester formtester = tester.newFormTester("form"); formtester.submit(); - FileUploadField fileUploadField = (FileUploadField)tester.getComponentFromLastRenderedPage("form:upload"); - - assertEquals(0, fileUploadField.getFileUploads().size()); + tester.assertNoErrorMessage(); + } + + /** + * https://issues.apache.org/jira/browse/WICKET-6270 + */ + @Test + public void testEmptyFieldAsBrowserSendsIt() throws Exception + { + tester.startPage(TestValidationPage.class); + + FileUploadField uploadField = (FileUploadField)tester.getComponentFromLastRenderedPage("form:upload"); + uploadField.add(new IValidator<List<FileUpload>>() { + @Override + public void validate(IValidatable<List<FileUpload>> validatable) + { + // must check during validation, since the uploads are nullified on detach + assertEquals(0, validatable.getValue().size()); + } + }); + FormTester formtester = tester.newFormTester("form"); + formtester.setFile("upload", null, "text/xml"); + formtester.submit(); + + tester.assertNoErrorMessage(); } - public static class TestValidationPage extends MockPageWithFormAndUploadField { /** */