Search in sources :

Example 6 with MockRequest

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

Example 7 with MockRequest

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());
}
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 8 with MockRequest

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);
}
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 9 with MockRequest

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());
}
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 10 with MockRequest

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());
}
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

MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)250 Test (org.junit.Test)216 UIContext (com.github.bordertech.wcomponents.UIContext)22 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)17 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)16 ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)13 PrintWriter (java.io.PrintWriter)13 Request (com.github.bordertech.wcomponents.Request)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)8 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)7 StringWriter (java.io.StringWriter)7 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 SubUIContext (com.github.bordertech.wcomponents.WRepeater.SubUIContext)6 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)5 BigDecimal (java.math.BigDecimal)5 Escape (com.github.bordertech.wcomponents.Escape)4 WTextField (com.github.bordertech.wcomponents.WTextField)4 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3