WMultiTextField inputs1 = new WMultiTextField(); inputs1.setToolTip("My tooltip for #1"); layout.addField("Dynamic mult-input " + ++i, inputs1); WMultiTextField inputs2 = new WMultiTextField(); inputs2.setColumns(30); inputs2.setMaxLength(30); layout.addField("Dynamic mult-input " + ++i, inputs2); WMultiTextField inputs3 = new WMultiTextField(); inputs3.setColumns(30); inputs3.setMaxLength(30); inputs3.setMaxInputs(5); inputs3.setPlaceholder("Maximum of 5 inputs"); WMultiTextField inputs4 = new WMultiTextField(); inputs4.setColumns(30); inputs4.setMaxLength(30); inputs4.setDisabled(true); layout.addField("Dynamic mult-input " + ++i, inputs4); WMultiTextField inputs4a = new WMultiTextField(); inputs4a.setReadOnly(true); layout.addField("Dynamic mult-input " + ++i, inputs4a); WMultiTextField mtfMandatory = new WMultiTextField(); mtfMandatory.setMandatory(true); layout.addField("Mandatory", mtfMandatory);
@Test public void testSetData() { WMultiTextField field = new WMultiTextField(); field.setLocked(true); setActiveContext(createUIContext()); field.setData(ENTERED_1_2); Assert.assertTrue("Wrong value returned for data", Arrays.equals(ENTERED_1_2, (Object[]) field.getData())); // Empty array field.setData(ENTERED_EMPTY); Assert.assertNull("Null should be returned for empty array", field.getData()); // Empty options field.setData(new String[]{"", TEXT1, "", TEXT2, ""}); Assert.assertTrue("Empty options should have been removed", Arrays.equals(ENTERED_1_2, (Object[]) field.getData())); // Max options exceeded field.setMaxInputs(1); field.setData(ENTERED_1_2); Assert.assertTrue("Should only return the first option", Arrays.equals(ENTERED_1, (Object[]) field.getData())); }
/** * {@inheritDoc} */ @Override public String[] getRequestValue(final Request request) { if (isPresent(request)) { String[] paramValues = request.getParameterValues(getId()); return removeEmptyStrings(paramValues); } else { return getValue(); } }
/** * Set the inputs based on the incoming request. The text input values are set as an array of strings on the * parameter with this name {@link #getName()}. Any empty strings will be ignored. * * @param request the current request. * @return true if the inputs have changed, otherwise return false */ @Override protected boolean doHandleRequest(final Request request) { String[] values = getRequestValue(request); String[] current = getValue(); boolean changed = !Arrays.equals(values, current); if (changed) { setData(values); } return changed; }
/** * {@inheritDoc} */ @Override public String[] getValue() { Object data = getData(); if (data == null) { return null; } String[] array = null; // Array data if (data instanceof String[]) { array = (String[]) data; } else if (data instanceof List) { // List data List<?> list = (List<?>) data; array = new String[list.size()]; for (int i = 0; i < list.size(); i++) { Object item = list.get(i); array[i] = item == null ? "" : item.toString(); } } else { // Object array = new String[]{data.toString()}; } return removeEmptyStrings(array); }
@Test public void testDoPaintOptions() throws IOException, SAXException, XpathException { WMultiTextField field = new WMultiTextField(); field.setTextInputs(new String[]{"a", "b"}); assertXpathEvaluatesTo(field.getId(), "//ui:multitextfield/@id", field); field.setDisabled(true); assertSchemaMatch(field); assertXpathEvaluatesTo("true", "//ui:multitextfield/@disabled", field); field.setMandatory(true); assertSchemaMatch(field); assertXpathEvaluatesTo("true", "//ui:multitextfield/@required", field); field.setToolTip("tooltip"); assertSchemaMatch(field); assertXpathEvaluatesTo(field.getToolTip(), "//ui:multitextfield/@toolTip", field); field.setAccessibleText("accessible"); assertSchemaMatch(field); assertXpathEvaluatesTo(field.getAccessibleText(), "//ui:multitextfield/@accessibleText", field); field.setColumns(40); assertSchemaMatch(field); assertXpathEvaluatesTo("40", "//ui:multitextfield/@size", field); field.setMinLength(45); assertSchemaMatch(field);
XmlStringBuilder xml = renderContext.getWriter(); boolean readOnly = textField.isReadOnly(); String[] values = textField.getTextInputs(); xml.appendOptionalAttribute("class", component.getHtmlClass()); xml.appendOptionalAttribute("track", component.isTracking(), "true"); xml.appendOptionalAttribute("hidden", textField.isHidden(), "true"); int cols = textField.getColumns(); int minLength = textField.getMinLength(); int maxLength = textField.getMaxLength(); int maxInputs = textField.getMaxInputs(); String pattern = textField.getPattern(); xml.appendOptionalAttribute("disabled", textField.isDisabled(), "true"); xml.appendOptionalAttribute("required", textField.isMandatory(), "true"); xml.appendOptionalAttribute("toolTip", textField.getToolTip()); xml.appendOptionalAttribute("accessibleText", textField.getAccessibleText()); xml.appendOptionalAttribute("size", cols > 0, cols); xml.appendOptionalAttribute("minLength", minLength > 0, minLength); xml.appendOptionalAttribute("pattern", !Util.empty(pattern), pattern); String placeholder = textField.getPlaceholder(); xml.appendOptionalAttribute("placeholder", !Util.empty(placeholder), placeholder); xml.appendOptionalAttribute("title", I18nUtilities.format(null, InternalMessages.DEFAULT_MULTITEXTFIELD_TIP));
@Test public void testDoHandleRequest() { WMultiTextField field = new WMultiTextField(); field.setMaxInputs(4); request.setParameter(field.getId(), ""); boolean changed = field.doHandleRequest(request); Assert.assertFalse("Should not change for empty request", changed); request.setParameter(field.getId(), ENTERED_1_2); changed = field.doHandleRequest(request); field.getTextInputs())); changed = field.doHandleRequest(request); Assert.assertFalse("Should have not changed for request with same text entries", changed); Assert .assertTrue("Unexpected inputs set on WMultiTextField", Arrays.equals(ENTERED_1_2, field.getTextInputs())); request.setParameter(field.getId(), ""); changed = field.doHandleRequest(request); Assert.assertTrue("Should have changed for empty request", changed); Assert.assertNull("Text inputs should be null", field.getTextInputs());
@Test public void testGetValue() { WMultiTextField field = new WMultiTextField(); field.setBeanProperty("."); field.setLocked(true); field.setBean(null); Assert.assertNull("Value should be null", field.getValue()); field.setBean(ENTERED_EMPTY); Assert.assertNull("Value should be null for empty array", field.getValue()); field.setBean(ENTERED_1_2); Assert.assertTrue("Value should be an array with the two entrie in the array", Arrays.equals(ENTERED_1_2, field.getValue())); field.setBean(Arrays.asList(ENTERED_1_2)); Assert.assertTrue("Value should be an array with the two entries in the list", Arrays.equals(ENTERED_1_2, field.getValue())); field.setBean(data); Assert.assertTrue( "Value should be an array with the two entries in the list without the empty entry", Arrays.equals(ENTERED_1_2, field.getValue())); field.setBean(TEXT1); Assert.assertTrue("Value should be an array with the text item", Arrays.equals(ENTERED_1, field.getValue()));
@Test public void testGetValueAsString() { WMultiTextField field = new WMultiTextField(); field.setLocked(true); setActiveContext(createUIContext()); // Null Assert.assertNull("Value as String should be null", field.getValueAsString()); // Entered text1 field.setData(ENTERED_1); Assert.assertEquals("Value as String should be text1", TEXT1, field.getValueAsString()); // Entered text1 and text2 field.setData(ENTERED_1_2); Assert .assertEquals("Value as String should be text1 and text2", TEXT1 + ", " + TEXT2, field.getValueAsString()); }
@Test public void testValidateMaxLength() { String text = "test"; WMultiTextField field = new WMultiTextField(); field.setLocked(true); setActiveContext(createUIContext()); field.setTextInputs(null); field.validate(diags); Assert.assertTrue("Null text with no maximum set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{""}); field.validate(diags); Assert.assertTrue("Empty text with no maximum set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{text}); field.validate(diags); Assert.assertTrue("Text with no maximum set should be valid", diags.isEmpty()); field.setMaxLength(1); field.setTextInputs(null); field.validate(diags); Assert.assertTrue("Null text with maximum set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{""}); field.validate(diags); Assert.assertTrue("Empty text with maximum set should be valid", diags.isEmpty());
@Test public void testValidatePattern() { WMultiTextField field = new WMultiTextField(); field.setLocked(true); field.setTextInputs(null); field.validate(diags); Assert.assertTrue("Null text with no pattern set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{""}); field.validate(diags); Assert.assertTrue("Empty text with no pattern set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{text}); field.validate(diags); Assert.assertTrue("Text with no pattern set should be valid", diags.isEmpty()); field.setPattern(pattern); field.setTextInputs(null); field.validate(diags); Assert.assertTrue("Null text with pattern set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{""}); field.validate(diags); Assert.assertTrue("Empty text with pattern set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{text}); field.validate(diags);
@Test public void testGetRequestValue() { WMultiTextField field = new WMultiTextField(); setActiveContext(createUIContext()); field.setTextInputs(ENTERED_1); // Empty Request - return current value MockRequest request = new MockRequest(); Assert.assertTrue("Request value should be the current value for an empty request", Arrays.equals(ENTERED_1, field.getRequestValue(request))); // Empty values on the request String[] value = new String[]{"", null}; request = new MockRequest(); request.setParameter(field.getId(), value); Assert.assertNull("Request value should be null for empty values on request", field. getRequestValue(request)); // Values on the request request = new MockRequest(); request.setParameter(field.getId(), ENTERED_1_2); Assert.assertTrue("Request value should be the values on the request", Arrays.equals(ENTERED_1_2, field.getRequestValue(request))); // An empty string or null in the array should be removed String[] inputs2 = new String[]{"", "x", "y", "", "", null, "z"}; request.setParameter(field.getId(), inputs2); Assert.assertTrue( "Request value should be the values on the request without the null and empty entries", Arrays.equals(new String[]{"x", "y", "z"}, field.getRequestValue(request))); }
super.validateComponent(diags); String[] values = getValue(); int max = getMaxLength(); int min = getMinLength(); Pattern pattern = getComponentModel().pattern; diags.add(createErrorDiagnostic( InternalMessages.DEFAULT_VALIDATION_ERROR_MIN_LENGTH, this, String.valueOf(min))); diags.add(createErrorDiagnostic( InternalMessages.DEFAULT_VALIDATION_ERROR_MAX_LENGTH, this, String.valueOf(max))); Matcher matcher = pattern.matcher(value); if (!matcher.matches()) { diags.add(createErrorDiagnostic( InternalMessages.DEFAULT_VALIDATION_ERROR_INVALID_PATTERN, this)); invalid = true;
@Test public void testRendererCorrectlyConfigured() { WMultiTextField wmtf = new WMultiTextField(); Assert.assertTrue("Incorrect renderer supplied", getWebXmlRenderer(wmtf) instanceof WMultiTextFieldRenderer); }
@Test public void testXssEscaping() throws IOException, SAXException, XpathException { WMultiTextField field = new WMultiTextField(); field.setTextInputs(new String[]{getInvalidCharSequence(), getMaliciousContent()}); assertSafeContent(field); field.setToolTip(getMaliciousAttribute("ui:multitextfield")); assertSafeContent(field); field.setAccessibleText(getMaliciousAttribute("ui:multitextfield")); assertSafeContent(field); }
@Test public void testValidateMinLength() { String text = "test"; WMultiTextField field = new WMultiTextField(); field.setTextInputs(null); field.validate(diags); Assert.assertTrue("Null text with no minimum set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{""}); field.validate(diags); Assert.assertTrue("Empty text with no minimum set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{text}); field.validate(diags); Assert.assertTrue("Text with no minimum set should be valid", diags.isEmpty()); field.setMinLength(1); field.setTextInputs(null); field.validate(diags); Assert.assertTrue("Null text with minimum set should be valid", diags.isEmpty()); field.setTextInputs(new String[]{""}); field.validate(diags); Assert.assertTrue("Empty text with minimum set should be valid", diags.isEmpty()); diags.clear(); field.setTextInputs(new String[]{text});
@Test public void testConstructors() { // Constructor 1 WMultiTextField field = new WMultiTextField(); Assert.assertNull("Text should be null", field.getTextInputs()); // Constructor 2 field = new WMultiTextField(ENTERED_1_2); Assert.assertTrue("Wrong text values returned", Arrays.equals(ENTERED_1_2, field. getTextInputs())); }
@Test(expected = PatternSyntaxException.class) public void testSetPatternInvalid() { WMultiTextField field = new WMultiTextField(); field.setPattern("[foo"); }
@Test public void testReadOnly() throws IOException, SAXException, XpathException { WMultiTextField field = new WMultiTextField(); field.setReadOnly(true); assertSchemaMatch(field); assertXpathEvaluatesTo("true", "//ui:multitextfield/@readOnly", field); }