Ejemplo n.º 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());
  }
Ejemplo n.º 2
0
  // 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());
  }
Ejemplo n.º 3
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);
    }
  }
Ejemplo n.º 4
0
  // 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);
    }
  }