public void testSelectItemsIterator() { // sub test 1 : non-selectItem at end UISelectOne selectOne = (UISelectOne) component; selectOne.getChildren().add(new UISelectItemSub("orr", null, null)); selectOne.getChildren().add(new UISelectItemSub("esposito", null, null)); UIParameter param = new UIParameter(); param.setName("param"); param.setValue("paramValue"); selectOne.getChildren().add(param); Iterator iter = new SelectItemsIterator(selectOne); while (iter.hasNext()) { Object object = iter.next(); assertTrue(object instanceof javax.faces.model.SelectItem); assertTrue( (((SelectItem) object).getValue().equals("orr")) || (((SelectItem) object).getValue().equals("esposito"))); } // sub test 2: non-selectitem in middle selectOne = new UISelectOne(); selectOne.getChildren().add(new UISelectItemSub("gretsky", null, null)); selectOne.getChildren().add(param); selectOne.getChildren().add(new UISelectItemSub("howe", null, null)); iter = new SelectItemsIterator(selectOne); while (iter.hasNext()) { Object object = iter.next(); assertTrue(object instanceof javax.faces.model.SelectItem); assertTrue( (((SelectItem) object).getValue().equals("gretsky")) || (((SelectItem) object).getValue().equals("howe"))); } }
/* * (non-Javadoc) * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, * javax.faces.component.UIComponent, java.lang.Object) */ @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { UISelectOne combo = (UISelectOne) component; String message = combo.getValidatorMessage(); if (null == message || "".equals(message.trim())) { ResourceBundle bundle = context.getApplication().getResourceBundle(context, "messageBundle"); String messageFromKey = bundle.getString("message.generic.field.required"); message = MessageFormat.format(messageFromKey, combo.getId()); } if (null == value || value.toString().equals("0") || "".equals(value.toString().trim())) { FacesMessage msg = new FacesMessage(message, message); msg.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(msg); } }
protected void setProperties(UIComponent component) { super.setProperties(component); javax.faces.component.UISelectOne selectone = null; try { selectone = (javax.faces.component.UISelectOne) component; } catch (ClassCastException cce) { throw new IllegalStateException( "Component " + component.toString() + " not expected type. Expected: javax.faces.component.UISelectOne. Perhaps you're missing a tag?"); } if (converter != null) { if (!converter.isLiteralText()) { selectone.setValueExpression("converter", converter); } else { Converter conv = FacesContext.getCurrentInstance() .getApplication() .createConverter(converter.getExpressionString()); selectone.setConverter(conv); } } if (converterMessage != null) { selectone.setValueExpression("converterMessage", converterMessage); } if (immediate != null) { selectone.setValueExpression("immediate", immediate); } if (required != null) { selectone.setValueExpression("required", required); } if (requiredMessage != null) { selectone.setValueExpression("requiredMessage", requiredMessage); } if (validator != null) { selectone.addValidator(new MethodExpressionValidator(validator)); } if (validatorMessage != null) { selectone.setValueExpression("validatorMessage", validatorMessage); } if (value != null) { selectone.setValueExpression("value", value); } if (valueChangeListener != null) { selectone.addValueChangeListener( new MethodExpressionValueChangeListener(valueChangeListener)); } if (accesskey != null) { selectone.setValueExpression("accesskey", accesskey); } if (dir != null) { selectone.setValueExpression("dir", dir); } if (disabled != null) { selectone.setValueExpression("disabled", disabled); } if (disabledClass != null) { selectone.setValueExpression("disabledClass", disabledClass); } if (enabledClass != null) { selectone.setValueExpression("enabledClass", enabledClass); } if (label != null) { selectone.setValueExpression("label", label); } if (lang != null) { selectone.setValueExpression("lang", lang); } if (onblur != null) { selectone.setValueExpression("onblur", onblur); } if (onchange != null) { selectone.setValueExpression("onchange", onchange); } if (onclick != null) { selectone.setValueExpression("onclick", onclick); } if (ondblclick != null) { selectone.setValueExpression("ondblclick", ondblclick); } if (onfocus != null) { selectone.setValueExpression("onfocus", onfocus); } if (onkeydown != null) { selectone.setValueExpression("onkeydown", onkeydown); } if (onkeypress != null) { selectone.setValueExpression("onkeypress", onkeypress); } if (onkeyup != null) { selectone.setValueExpression("onkeyup", onkeyup); } if (onmousedown != null) { selectone.setValueExpression("onmousedown", onmousedown); } if (onmousemove != null) { selectone.setValueExpression("onmousemove", onmousemove); } if (onmouseout != null) { selectone.setValueExpression("onmouseout", onmouseout); } if (onmouseover != null) { selectone.setValueExpression("onmouseover", onmouseover); } if (onmouseup != null) { selectone.setValueExpression("onmouseup", onmouseup); } if (onselect != null) { selectone.setValueExpression("onselect", onselect); } if (readonly != null) { selectone.setValueExpression("readonly", readonly); } if (style != null) { selectone.setValueExpression("style", style); } if (styleClass != null) { selectone.setValueExpression("styleClass", styleClass); } if (tabindex != null) { selectone.setValueExpression("tabindex", tabindex); } if (title != null) { selectone.setValueExpression("title", title); } }
// Test that appropriate properties are value binding enabled public void testValueBindings() { super.testValueBindings(); UISelectOne test = (UISelectOne) component; // "value" property request.setAttribute("foo", "bar"); test.setValue(null); assertNull(test.getValue()); test.setValueBinding("value", application.createValueBinding("#{foo}")); assertNotNull(test.getValueBinding("value")); assertEquals("bar", test.getValue()); test.setValue("baz"); assertEquals("baz", test.getValue()); test.setValue(null); assertEquals("bar", test.getValue()); test.setValueBinding("value", null); assertNull(test.getValueBinding("value")); assertNull(test.getValue()); }
// Test validation of a required field public void testValidateRequired() throws Exception { UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null); root.getChildren().add(component); UISelectOne selectOne = (UISelectOne) component; selectOne.getChildren().add(new UISelectItemSub("foo", null, null)); selectOne.getChildren().add(new UISelectItemSub("bar", null, null)); selectOne.getChildren().add(new UISelectItemSub("baz", null, null)); selectOne.setRequired(true); checkMessages(0); selectOne.setValid(true); selectOne.setSubmittedValue("foo"); selectOne.validate(facesContext); checkMessages(0); assertTrue(selectOne.isValid()); selectOne.setValid(true); selectOne.setSubmittedValue(""); selectOne.validate(facesContext); checkMessages(1); assertTrue(!selectOne.isValid()); selectOne.setValid(true); selectOne.setSubmittedValue(null); // awiner: see UIInputTestCase selectOne.validate(facesContext); checkMessages(1); assertTrue(selectOne.isValid()); }
// Test validation against a nested list of available options public void testValidateNested() throws Exception { // Set up UISelectOne with nested UISelectItems UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null); root.getChildren().add(component); UISelectOne selectOne = (UISelectOne) component; UISelectItems selectItems = new UISelectItems(); selectItems.setValue(setupOptions()); selectOne.getChildren().add(selectItems); selectOne.setRequired(true); checkMessages(0); // Verify that all legal values will validate for (int i = 0; i < legalValues.length; i++) { selectOne.setValid(true); selectOne.setSubmittedValue(legalValues[i]); selectOne.validate(facesContext); assertTrue("Value '" + legalValues[i] + "' found", selectOne.isValid()); checkMessages(0); } // Verify that illegal values will not validate for (int i = 0; i < illegalValues.length; i++) { selectOne.setValid(true); selectOne.setSubmittedValue(illegalValues[i]); selectOne.validate(facesContext); assertTrue("Value '" + illegalValues[i] + "' not found", !selectOne.isValid()); checkMessages(i + 1); } }
// Test validation of component with UISelectItems pointing to map public void testValidation2() throws Exception { // Put our component under test in a tree under a UIViewRoot UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null); root.getChildren().add(component); // Add valid options to the component under test Map map = new HashMap(); map.put("key_foo", "foo"); map.put("key_bar", "bar"); map.put("key_baz", "baz"); UISelectItems items = new UISelectItems(); items.setValue(map); UISelectOne selectOne = (UISelectOne) component; selectOne.getChildren().add(items); selectOne.setValid(true); selectOne.setSubmittedValue("foo"); selectOne.validate(facesContext); assertTrue(selectOne.isValid()); // Validate one value on the list and one not on the list selectOne.setValid(true); selectOne.setSubmittedValue("car"); selectOne.setRendererType(null); // We don't have any renderers selectOne.validate(facesContext); assertTrue(!selectOne.isValid()); }
// Test validation of value against the valid list public void testValidation() throws Exception { // Put our component under test in a tree under a UIViewRoot UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null); root.getChildren().add(component); // Add valid options to the component under test UISelectOne selectOne = (UISelectOne) component; selectOne.getChildren().add(new UISelectItemSub("foo", null, null)); selectOne.getChildren().add(new UISelectItemSub("bar", null, null)); selectOne.getChildren().add(new UISelectItemSub("baz", null, null)); // Validate a value that is on the list selectOne.setValid(true); selectOne.setSubmittedValue("bar"); selectOne.setRendererType(null); // We don't have any renderers selectOne.validate(facesContext); assertTrue(selectOne.isValid()); // Validate a value that is not on the list selectOne.getAttributes().put("label", "mylabel"); selectOne.setValid(true); selectOne.setSubmittedValue("bop"); selectOne.validate(facesContext); assertTrue(!selectOne.isValid()); Iterator messages = facesContext.getMessages(); while (messages.hasNext()) { FacesMessage message = (FacesMessage) messages.next(); assertTrue(message.getSummary().indexOf("mylabel") >= 0); } }
@Override protected String getSubmitParam(FacesContext context, UISelectOne selectOne) { return selectOne.getClientId(context) + "_input"; }