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 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.º 3
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.º 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);
    }
  }
Ejemplo n.º 5
0
 public void doTestPhaseMethodExpressionAndListener(UIViewRoot root, boolean skipping)
     throws Exception {
   PhaseSkipTestComponent comp = null;
   if (skipping) {
     comp = new PhaseSkipTestComponent();
     root.getChildren().add(comp);
     facesContext.responseComplete();
   }
   doTestPhaseMethodExpressionAndListenerWithPhaseId(root, PhaseId.APPLY_REQUEST_VALUES);
   if (skipping) {
     assertTrue(!comp.isDecodeCalled());
   }
   doTestPhaseMethodExpressionAndListenerWithPhaseId(root, PhaseId.PROCESS_VALIDATIONS);
   if (skipping) {
     assertTrue(!comp.isProcessValidatorsCalled());
   }
   doTestPhaseMethodExpressionAndListenerWithPhaseId(root, PhaseId.UPDATE_MODEL_VALUES);
   if (skipping) {
     assertTrue(!comp.isProcessUpdatesCalled());
   }
   doTestPhaseMethodExpressionAndListenerWithPhaseId(root, PhaseId.INVOKE_APPLICATION);
   doTestPhaseMethodExpressionAndListenerWithPhaseId(root, PhaseId.RENDER_RESPONSE);
   if (skipping) {
     assertTrue(!comp.isEncodeBeginCalled());
   }
 }
  public static UIViewRoot findRoot(FacesContext context, ServletRequest req, Object etag)
      throws Exception {
    if (context == null) context = FacesContext.getCurrentInstance();

    UIViewRoot root = context.getViewRoot();

    if (root == null)
      throw new NullPointerException(L.l("f:view can't find current in FacesContext"));

    Object oldETag = root.getAttributes().get("caucho.etag");

    if (oldETag != null && !oldETag.equals(etag)) {
      // clear view on JSP change

      root.getChildren().clear();
      root.getFacets().clear();
    }

    root.getAttributes().put("caucho.etag", etag);

    return root;
  }