Пример #1
0
  protected void setupNewValue(UIInput input) {

    input.setSubmittedValue("foo");
    UISelectItem si = new UISelectItem();
    si.setItemValue("foo");
    si.setItemLabel("foo label");
    input.getChildren().add(si);
  }
Пример #2
0
 @Test
 public void testSaveState() {
   FacesContext context = EasyMock.createMock(FacesContext.class);
   UIInput input = new UIInput();
   replay(context);
   assertNotNull(input.saveState(context));
   verify(context);
 }
Пример #3
0
 @Test
 public void testRestoreState() {
   FacesContext context = EasyMock.createMock(FacesContext.class);
   UIInput input = new UIInput();
   replay(context);
   input.restoreState(context, null);
   verify(context);
 }
Пример #4
0
 @Test(expected = NullPointerException.class)
 public void testRestoreState2() {
   FacesContext context = EasyMock.createMock(FacesContext.class);
   UIInput input = new UIInput();
   replay(context);
   input.restoreState(null, null);
   verify(context);
 }
Пример #5
0
 @Test
 public void testRestoreState3() {
   FacesContext context = EasyMock.createMock(FacesContext.class);
   UIInput input = new UIInput();
   replay(context);
   Object state = input.saveState(context);
   assertNotNull(state);
   input.restoreState(context, state);
   verify(context);
 }
Пример #6
0
  /**
   * <span class="changed_modified_2_0">In</span> addition to the standard validation behavior
   * inherited from {@link UIInput}, ensure that any specified value is equal to one of the
   * available options. Before comparing each option, coerce the option value type to the type of
   * this component's value following the Expression Language coercion rules. If the specified value
   * is not equal to any of the options, enqueue an error message and set the <code>valid</code>
   * property to <code>false</code>.
   *
   * <p class="changed_added_2_0">If {@link #isRequired} returns <code>true</code>, and the current
   * value is equal to the value of an inner {@link UISelectItem} whose {@link
   * UISelectItem#isNoSelectionOption} method returns <code>true</code>, enqueue an error message
   * and set the <code>valid</code> property to <code>false</code>.
   *
   * @param context The {@link FacesContext} for the current request
   * @param value The converted value to test for membership.
   * @throws NullPointerException if <code>context</code> is <code>null</code>
   */
  protected void validateValue(FacesContext context, Object value) {

    // Skip validation if it is not necessary
    super.validateValue(context, value);

    if (!isValid() || (value == null)) {
      return;
    }

    // Ensure that the value matches one of the available options
    boolean found =
        SelectUtils.matchValue(
            getFacesContext(), this, value, new SelectItemsIterator(context, this), getConverter());

    boolean isNoSelection =
        SelectUtils.valueIsNoSelectionOption(
            getFacesContext(), this, value, new SelectItemsIterator(context, this), getConverter());

    // Enqueue an error message if an invalid value was specified
    if ((!found) || (isRequired() && isNoSelection)) {
      FacesMessage message =
          MessageFactory.getMessage(
              context, INVALID_MESSAGE_ID, MessageFactory.getLabel(context, this));
      context.addMessage(getClientId(context), message);
      setValid(false);
    }
  }
Пример #7
0
  protected List<SelectItem> getSelectItems(FacesContext context, UIInput component) {
    List<SelectItem> selectItems = new ArrayList<SelectItem>();

    for (UIComponent child : component.getChildren()) {
      if (child instanceof UISelectItem) {
        UISelectItem uiSelectItem = (UISelectItem) child;
        Object selectItemValue = uiSelectItem.getValue();

        if (selectItemValue == null) {
          selectItems.add(
              new SelectItem(
                  uiSelectItem.getItemValue(),
                  uiSelectItem.getItemLabel(),
                  uiSelectItem.getItemDescription(),
                  uiSelectItem.isItemDisabled(),
                  uiSelectItem.isItemEscaped(),
                  uiSelectItem.isNoSelectionOption()));
        } else {
          selectItems.add((SelectItem) selectItemValue);
        }

      } else if (child instanceof UISelectItems) {
        UISelectItems uiSelectItems = ((UISelectItems) child);
        Object value = uiSelectItems.getValue();

        if (value != null) {
          if (value instanceof SelectItem) {
            selectItems.add((SelectItem) value);
          } else if (value.getClass().isArray()) {
            for (int i = 0; i < Array.getLength(value); i++) {
              Object item = Array.get(value, i);

              if (item instanceof SelectItem) selectItems.add((SelectItem) item);
              else selectItems.add(createSelectItem(context, uiSelectItems, item));
            }
          } else if (value instanceof Map) {
            Map map = (Map) value;

            for (Iterator it = map.keySet().iterator(); it.hasNext(); ) {
              Object key = it.next();

              selectItems.add(
                  createSelectItem(context, uiSelectItems, String.valueOf(key), map.get(key)));
            }
          } else if (value instanceof Collection) {
            Collection collection = (Collection) value;

            for (Iterator it = collection.iterator(); it.hasNext(); ) {
              Object item = it.next();
              if (item instanceof SelectItem) selectItems.add((SelectItem) item);
              else selectItems.add(createSelectItem(context, uiSelectItems, item));
            }
          }
        }
      }
    }

    return selectItems;
  }
Пример #8
0
 // 编写处理Action事件的方法
 public void processAction(ActionEvent event) {
   // 获取当前的FacesContext对象
   FacesContext context = FacesContext.getCurrentInstance();
   // 获取JSF页面中<f:view.../>元素
   UIViewRoot viewRoot = context.getViewRoot();
   // 通过ID获取<f:view.../>内的<h:form.../>子元素。
   UIComponent comp = viewRoot.findComponent("addForm");
   // 通过ID获取<h:form.../>内的第一个<h:inputText.../>子元素。
   UIInput input = (UIInput) comp.findComponent("name");
   // 通过ID获取<h:form.../>内的第二个<h:inputText.../>子元素。
   HtmlInputText price = (HtmlInputText) comp.findComponent("price");
   if (input.getValue().equals("疯狂Java讲义")) {
     price.setSize(60);
     price.setValue("99.0元");
     price.setStyle("background-color:#9999ff;" + "font-weight:bold");
   }
 }
Пример #9
0
 @Test
 public void testSaveState4() {
   FacesContext context = EasyMock.createMock(FacesContext.class);
   UIInput input = new UIInput();
   LengthValidator l1 = new LengthValidator();
   LengthValidator l2 = new LengthValidator();
   replay(context);
   input.addValidator(l1);
   input.addValidator(l2);
   l1.setMinimum(1);
   l2.setMinimum(2);
   input.markInitialState();
   assertTrue(input.initialStateMarked());
   assertTrue(l1.initialStateMarked());
   assertTrue(l2.initialStateMarked());
   Object state = input.saveState(context);
   assertNull(state);
   verify(context);
 }
Пример #10
0
 @Test(expected = NullPointerException.class)
 public void testSaveState2() {
   UIInput input = new UIInput();
   input.saveState(null);
 }
Пример #11
0
  @Test
  public void testRestoreState5() {
    FacesContext context = EasyMock.createMock(FacesContext.class);
    UIInput input = new UIInput();
    LengthValidator l1 = new LengthValidator();
    LengthValidator l2 = new LengthValidator();
    replay(context);
    input.addValidator(l1);
    input.addValidator(l2);
    l1.setMinimum(1);
    l2.setMinimum(2);
    input.markInitialState();
    l2.setMinimum(3);
    assertTrue(input.initialStateMarked());
    assertTrue(l1.initialStateMarked());
    assertTrue(!l2.initialStateMarked());
    Object state = input.saveState(context);
    assertTrue(state instanceof Object[]);
    Object[] validatorState = (Object[]) ((Object[]) state)[3];
    assertNotNull(validatorState);
    assertNull(validatorState[0]);
    assertNotNull(validatorState[1]);
    assertTrue(!(validatorState[1] instanceof StateHolderSaver));
    input = new UIInput();
    l1 = new LengthValidator();
    l2 = new LengthValidator();
    l1.setMinimum(1);
    l2.setMinimum(2);
    input.addValidator(l1);
    input.addValidator(l2);
    input.restoreState(context, state);
    assertTrue(l1.getMinimum() == 1);
    assertTrue(l2.getMinimum() == 3);
    assertTrue(input.getValidators().length == 2);

    input = new UIInput();
    l1 = new LengthValidator();
    l2 = new LengthValidator();
    input.addValidator(l1);
    input.addValidator(l2);
    l1.setMinimum(1);
    l2.setMinimum(2);
    input.markInitialState();
    LengthValidator l3 = new LengthValidator();
    l3.setMinimum(3);
    input.addValidator(l3);
    state = input.saveState(context);
    assertNotNull(validatorState);
    assertTrue(state instanceof Object[]);
    validatorState = (Object[]) ((Object[]) state)[3];
    assertNotNull(validatorState);
    assertTrue(validatorState.length == 3);
    assertNotNull(validatorState[0]);
    assertNotNull(validatorState[1]);
    assertNotNull(validatorState[2]);
    assertTrue(validatorState[0] instanceof StateHolderSaver);
    assertTrue(validatorState[1] instanceof StateHolderSaver);
    assertTrue(validatorState[2] instanceof StateHolderSaver);

    input = new UIInput();
    l1 = new LengthValidator();
    l2 = new LengthValidator();
    l3 = new LengthValidator();
    LengthValidator l4 = new LengthValidator();
    input.addValidator(l1);
    input.addValidator(l2);
    input.addValidator(l3);
    input.addValidator(l4);
    l1.setMinimum(100);
    l2.setMinimum(101);
    l3.setMinimum(102);
    l4.setMinimum(103);
    assertTrue(input.getValidators().length == 4);
    input.markInitialState();
    input.restoreState(context, state);
    assertTrue(input.getValidators().length == 3);

    Validator[] validators = input.getValidators();
    for (int i = 0, len = validators.length; i < len; i++) {
      LengthValidator v = (LengthValidator) validators[i];
      assertTrue(v.getMinimum() == (i + 1));
    }

    verify(context);
  }