Search in sources :

Example 1 with Diagnostic

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);
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) WCheckBox(com.github.bordertech.wcomponents.WCheckBox) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 2 with Diagnostic

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());
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 3 with Diagnostic

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);
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 4 with Diagnostic

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());
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 5 with Diagnostic

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());
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Aggregations

Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)41 ArrayList (java.util.ArrayList)29 Test (org.junit.Test)29 DiagnosticImpl (com.github.bordertech.wcomponents.validation.DiagnosticImpl)10 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)8 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)7 WContainer (com.github.bordertech.wcomponents.WContainer)6 WComponent (com.github.bordertech.wcomponents.WComponent)5 WPanel (com.github.bordertech.wcomponents.WPanel)4 WTextField (com.github.bordertech.wcomponents.WTextField)4 WValidationErrors (com.github.bordertech.wcomponents.validation.WValidationErrors)3 WDateField (com.github.bordertech.wcomponents.WDateField)2 WFieldErrorIndicator (com.github.bordertech.wcomponents.validation.WFieldErrorIndicator)2 WFieldWarningIndicator (com.github.bordertech.wcomponents.validation.WFieldWarningIndicator)2 Date (java.util.Date)2 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)1 Diagnosable (com.github.bordertech.wcomponents.Diagnosable)1 Input (com.github.bordertech.wcomponents.Input)1 UIContext (com.github.bordertech.wcomponents.UIContext)1 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)1