use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.
the class WCheckBoxRenderer method doRender.
/**
* Paints the given WCheckBox.
*
* @param component the WCheckBox to paint.
* @param renderContext the RenderContext to paint to.
*/
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
WCheckBox checkBox = (WCheckBox) component;
XmlStringBuilder xml = renderContext.getWriter();
boolean readOnly = checkBox.isReadOnly();
xml.appendTagOpen(TAG_NAME);
xml.appendAttribute("id", component.getId());
xml.appendOptionalAttribute("class", component.getHtmlClass());
xml.appendOptionalAttribute("track", component.isTracking(), "true");
xml.appendOptionalAttribute("hidden", checkBox.isHidden(), "true");
xml.appendOptionalAttribute("selected", checkBox.isSelected(), "true");
if (readOnly) {
xml.appendAttribute("readOnly", "true");
xml.appendEnd();
return;
}
WComponent submitControl = checkBox.getDefaultSubmitButton();
String submitControlId = submitControl == null ? null : submitControl.getId();
WComponentGroup<WCheckBox> group = checkBox.getGroup();
String groupName = group == null ? null : group.getId();
xml.appendOptionalAttribute("groupName", groupName);
xml.appendOptionalAttribute("disabled", checkBox.isDisabled(), "true");
xml.appendOptionalAttribute("required", checkBox.isMandatory(), "true");
xml.appendOptionalAttribute("submitOnChange", checkBox.isSubmitOnChange(), "true");
xml.appendOptionalAttribute("toolTip", checkBox.getToolTip());
xml.appendOptionalAttribute("accessibleText", checkBox.getAccessibleText());
xml.appendOptionalAttribute("buttonId", submitControlId);
List<Diagnostic> diags = checkBox.getDiagnostics(Diagnostic.ERROR);
if (diags == null || diags.isEmpty()) {
xml.appendEnd();
return;
}
xml.appendClose();
DiagnosticRenderUtil.renderDiagnostics(checkBox, renderContext);
xml.appendEndTag(TAG_NAME);
}
use of com.github.bordertech.wcomponents.validation.Diagnostic 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.validation.Diagnostic 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.validation.Diagnostic 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.validation.Diagnostic 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