コード例 #1
0
  // 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());
  }
コード例 #2
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);
    }
  }