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