use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WNumberField_Test method testDoHandleRequestChanged.
@Test
public void testDoHandleRequestChanged() {
WNumberField numberField = new WNumberField();
numberField.setLocked(true);
setActiveContext(createUIContext());
// Request with Empty Value and Field is null (no change)
MockRequest request = new MockRequest();
request.setParameter(numberField.getId(), "");
boolean changed = numberField.doHandleRequest(request);
Assert.assertFalse("doHandleRequest should have returned false for empty request and field is null", changed);
// Request with valid value and Field is null (changed)
request = new MockRequest();
request.setParameter(numberField.getId(), REQUEST_VALID_NUMBER_TEXT);
changed = numberField.doHandleRequest(request);
Assert.assertTrue("doHandleRequest should have returned true for request with valid value and field is null", changed);
// Request with same value (no change)
changed = numberField.doHandleRequest(request);
Assert.assertFalse("doHandleRequest should have returned false for request with same valid value", changed);
// Request with invalid value (changed)
request = new MockRequest();
request.setParameter(numberField.getId(), REQUEST_BAD_NUMBER_TEXT);
changed = numberField.doHandleRequest(request);
Assert.assertTrue("doHandleRequest should have returned true for request with different invalid value", changed);
// Request with same invalid value (no change)
changed = numberField.doHandleRequest(request);
Assert.assertFalse("doHandleRequest should have returned false for request with same invalid value", changed);
// Request with empty value (changed)
request = new MockRequest();
request.setParameter(numberField.getId(), "");
changed = numberField.doHandleRequest(request);
Assert.assertTrue("doHandleRequest should have returned true for request going back to empty value", changed);
// Request with same empty value (no change)
changed = numberField.doHandleRequest(request);
Assert.assertFalse("doHandleRequest should have returned false for request with same empty value", changed);
}
use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WFileWidget_Test method testInvalidFileTypeAndSize.
@Test
public void testInvalidFileTypeAndSize() throws IOException {
WFileWidget widget = new WFileWidget();
setActiveContext(createUIContext());
widget.setFileTypes(Arrays.asList("image/gif", "image/jpeg"));
widget.setMaxFileSize(10);
MockRequest request = setupFileUploadRequest(widget, TEST_FILE_ITEM2);
boolean changed = widget.doHandleRequest(request);
Assert.assertEquals("File invalid, no file uploaded", changed, false);
ArrayList<Diagnostic> diags = new ArrayList<Diagnostic>();
widget.validate(diags);
Assert.assertTrue("Both file type and size invalid", diags.size() == 2);
Assert.assertFalse(widget.isFileTypeValid());
Assert.assertFalse(widget.isFileSizeValid());
// Try same request again, make sure duplicate messages are not returned
changed = widget.doHandleRequest(request);
Assert.assertEquals("File invalid, no file uploaded", changed, false);
diags.clear();
widget.validate(diags);
Assert.assertTrue(diags.size() == 2);
Assert.assertFalse(widget.isFileTypeValid());
Assert.assertFalse(widget.isFileSizeValid());
widget.setFileTypes(Collections.EMPTY_LIST);
request = setupFileUploadRequest(widget, TEST_FILE_ITEM);
changed = widget.doHandleRequest(request);
Assert.assertEquals("File valid", changed, true);
Assert.assertTrue(widget.isFileTypeValid());
Assert.assertTrue(widget.isFileSizeValid());
}
use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WFileWidget_Test method testValidateFileSize.
@Test
public void testValidateFileSize() throws IOException {
WFileWidget widget = new WFileWidget();
widget.setIdName("widgetId");
setActiveContext(createUIContext());
int maxSize = 10;
widget.setMaxFileSize(maxSize);
// Set file on the request
MockRequest request = setupFileUploadRequest(widget, TEST_FILE_ITEM);
boolean changed = widget.doHandleRequest(request);
Assert.assertEquals("File size valid, so file uploaded", changed, true);
ArrayList<Diagnostic> diags = new ArrayList<Diagnostic>();
widget.validate(diags);
Assert.assertTrue("File size valid, so no messages returned", diags.size() == 0);
Assert.assertTrue("File size valid", widget.isFileSizeValid());
// Set file on the request
request = setupFileUploadRequest(widget, TEST_FILE_ITEM2);
changed = widget.doHandleRequest(request);
Assert.assertEquals("File size invalid, but original file exists", changed, true);
widget.validate(diags);
Assert.assertTrue("File size invalid, so message returned", diags.size() == 1);
Assert.assertFalse("File size invalid", widget.isFileSizeValid());
String invalidMessage = FileUtil.getInvalidFileSizeMessage(maxSize);
Assert.assertEquals("Invalid file size message", diags.get(0).getDescription(), invalidMessage);
}
use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WFileWidget_Test method testValidateValidFileType.
@Test
public void testValidateValidFileType() throws IOException {
WFileWidget widget = new WFileWidget();
setActiveContext(createUIContext());
widget.setFileTypes(Arrays.asList("image/gif"));
// Set proper file on the request
MockRequest request = new MockRequest();
InputStream stream = getClass().getResourceAsStream("/image/x1.gif");
byte[] bytes = StreamUtil.getBytes(stream);
request.setFileContents(widget.getId(), bytes);
boolean changed = widget.doHandleRequest(request);
Assert.assertEquals("File type valid, so file uploaded", changed, true);
ArrayList<Diagnostic> diags = new ArrayList<Diagnostic>();
widget.validate(diags);
Assert.assertTrue("No validation messages exist", diags.size() == 0);
Assert.assertTrue("File type valid", widget.isFileTypeValid());
widget = new WFileWidget();
setActiveContext(createUIContext());
widget.setFileTypes(Arrays.asList("image/*"));
request = new MockRequest();
stream = getClass().getResourceAsStream("/image/x1.gif");
bytes = StreamUtil.getBytes(stream);
request.setFileContents(widget.getId(), bytes);
changed = widget.doHandleRequest(request);
Assert.assertEquals("File type valid, so file uploaded", changed, true);
widget.validate(diags);
Assert.assertTrue("No validation messages exist", diags.size() == 0);
Assert.assertTrue("File type valid", widget.isFileTypeValid());
}
use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WFileWidget_Test method testValidateAnyFileTypeAndSize.
@Test
public void testValidateAnyFileTypeAndSize() throws IOException {
WFileWidget widget = new WFileWidget();
setActiveContext(createUIContext());
// Set file on the request
MockRequest request = setupFileUploadRequest(widget, TEST_FILE_ITEM);
boolean changed = widget.doHandleRequest(request);
Assert.assertEquals("File uploaded, so file uploaded", changed, true);
ArrayList<Diagnostic> diags = new ArrayList<Diagnostic>();
widget.validate(diags);
Assert.assertTrue("No validation messages exist", diags.size() == 0);
Assert.assertTrue("No file type, then valid", widget.isFileTypeValid());
Assert.assertTrue("No file size, then valid", widget.isFileSizeValid());
}
Aggregations