@Test public void testMandatorySpecificToUser() { UIContext uic1 = new UIContextImpl(); UIContext uic2 = new UIContextImpl(); List<Diagnostic> diags = new ArrayList<>(); // uic 1 has mandatory check // uic 2 does not. WTextField textField = new WTextField(); textField.setLocked(true); setActiveContext(uic1); textField.setMandatory(true); textField.validate(diags); Assert.assertEquals("UIC 1 should have a validation error", 1, diags.size()); diags.clear(); setActiveContext(uic2); textField.validate(diags); Assert.assertEquals("UIC 2 should not have a validation error", 0, diags.size()); } }
/** * Test validate - where isDisableable and isDisabled. */ @Test public void testValidateIfIsDisableable() { // instanceOf Disableable and isDisabled implies abort validate() WTextField date = new WTextField(); date.addValidator(new MyValidator()); date.setLocked(true); setActiveContext(createUIContext()); date.setDisabled(true); List<Diagnostic> diags = new ArrayList<>(); date.validate(diags); // this calls validate(uic,diags) in WComponent parent class - the one being tested Assert.assertTrue("diags list should be empty", diags.isEmpty()); resetContext(); date.validate(diags); Assert.assertFalse("Diags list should not be empty", diags.isEmpty()); }
@Test public void testValidateMinLength() { WTextField field = new WTextField(); field.setLocked(true);
@Test public void testValidateMaxLength() { WTextField field = new WTextField(); field.setLocked(true);
@Test public void testValidatePattern() { WTextField field = new WTextField(); field.setLocked(true);
@Test public void testGetValue() { WTextField field = new WTextField(); field.setLocked(true); setActiveContext(createUIContext()); // Set data as a null value field.setData(null); Assert.assertNull("getValue should return null when data is null", field.getValue()); // Set data as a empty string field.setData(""); Assert.assertNull("getValue should return null when data is an empty string", field. getValue()); // Set data as a String value field.setData("A"); Assert.assertEquals("getValue returned the incorrect value for the data", "A", field. getValue()); // Set data as an Object Object object = new Date(); field.setData(object); Assert. assertEquals("getValue should return the string value of the data", object. toString(), field.getValue()); }
@Test public void testGetRequestValue() { WTextField field = new WTextField(); field.setLocked(true); setActiveContext(createUIContext()); // Set current value field.setText("A"); // Empty Request (not present, should return current value) MockRequest request = new MockRequest(); Assert. assertEquals( "Current value of the field should have been returned for empty request", "A", field.getRequestValue(request)); // Request with "empty" value (should return null as an empty value on the request is treated as null) request = new MockRequest(); request.setParameter(field.getId(), ""); Assert .assertNull("Null should have been returned for request with empty value", field. getRequestValue(request)); // Request with value (should return the value on the request) request = new MockRequest(); request.setParameter(field.getId(), "X"); Assert.assertEquals("Value from the request should have been returned", "X", field. getRequestValue(request)); }
@Test public void testDoHandleRequest() { WTextField field = new WTextField(); field.setLocked(true);