Пример #1
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());
  }
Пример #2
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);
    }
  }