// Test setting properties to valid values
  public void testPropertiesValid() throws Exception {

    super.testPropertiesValid();
    UISelectItem selectItem = (UISelectItem) component;

    // value
    SelectItem item = new SelectItem("foo");
    selectItem.setValue(item);
    assertEquals("expected value", item, selectItem.getValue());
    selectItem.setValue(null);
    assertNull("erased value", selectItem.getValue());

    selectItem.setItemDescription("foo");
    assertEquals("foo", selectItem.getItemDescription());
    selectItem.setItemDescription(null);
    assertNull(selectItem.getItemDescription());

    selectItem.setItemDisabled(false);
    assertFalse(selectItem.isItemDisabled());
    selectItem.setItemDisabled(true);
    assertTrue(selectItem.isItemDisabled());

    selectItem.setItemLabel("foo");
    assertEquals("foo", selectItem.getItemLabel());
    selectItem.setItemLabel(null);
    assertNull(selectItem.getItemLabel());

    selectItem.setItemValue("foo");
    assertEquals("foo", selectItem.getItemValue());
    selectItem.setItemValue(null);
    assertNull(selectItem.getItemValue());
  }
  // Check that the properties on the specified components are equal
  protected void checkProperties(UIComponent comp1, UIComponent comp2) {

    super.checkProperties(comp1, comp2);
    UISelectItem si1 = (UISelectItem) comp1;
    UISelectItem si2 = (UISelectItem) comp2;
    assertEquals(si1.getItemDescription(), si2.getItemDescription());
    assertEquals(si1.isItemDisabled(), si2.isItemDisabled());
    assertEquals(si1.getItemLabel(), si2.getItemLabel());
    assertEquals(si1.getItemValue(), si2.getItemValue());
  }
Example #3
0
  public static List<SelectItem> createSelectItems(UIComponent component) {
    List<SelectItem> items = new ArrayList<SelectItem>();
    Iterator<UIComponent> children = component.getChildren().iterator();

    while (children.hasNext()) {
      UIComponent child = children.next();

      if (child instanceof UISelectItem) {
        UISelectItem selectItem = (UISelectItem) child;

        items.add(new SelectItem(selectItem.getItemValue(), selectItem.getItemLabel()));
      } else if (child instanceof UISelectItems) {
        Object selectItems = ((UISelectItems) child).getValue();

        if (selectItems instanceof SelectItem[]) {
          SelectItem[] itemsArray = (SelectItem[]) selectItems;

          for (SelectItem item : itemsArray)
            items.add(new SelectItem(item.getValue(), item.getLabel()));

        } else if (selectItems instanceof Collection) {
          Collection<SelectItem> collection = (Collection<SelectItem>) selectItems;

          for (SelectItem item : collection)
            items.add(new SelectItem(item.getValue(), item.getLabel()));
        }
      }
    }

    return items;
  }
Example #4
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;
  }
  // Test a pristine UISelectItem instance
  public void testPristine() {

    super.testPristine();
    UISelectItem selectItem = (UISelectItem) component;

    assertNull("no value", selectItem.getValue());
    assertNull("no itemDescription", selectItem.getItemDescription());
    assertFalse("no itemDisabled", selectItem.isItemDisabled());
    assertNull("no itemLabel", selectItem.getItemLabel());
    assertNull("no itemValue", selectItem.getItemValue());
  }
  // Test attribute-property transparency
  public void testAttributesTransparency() {

    super.testAttributesTransparency();
    UISelectItem selectItem = (UISelectItem) component;

    assertEquals(selectItem.getValue(), component.getAttributes().get("value"));
    SelectItem item = new SelectItem("foo");
    selectItem.setValue(item);
    assertEquals(item, component.getAttributes().get("value"));
    selectItem.setValue(null);

    assertNull(component.getAttributes().get("value"));
    component.getAttributes().put("value", "bar");
    assertEquals("bar", selectItem.getValue());
    component.getAttributes().put("value", null);
    assertNull(selectItem.getValue());

    assertEquals(
        selectItem.getItemDescription(),
        (String) selectItem.getAttributes().get("itemDescription"));
    selectItem.setItemDescription("foo");
    assertEquals("foo", (String) selectItem.getAttributes().get("itemDescription"));
    selectItem.setItemDescription(null);
    assertNull((String) selectItem.getAttributes().get("itemDescription"));
    selectItem.getAttributes().put("itemDescription", "bar");
    assertEquals("bar", selectItem.getItemDescription());
    selectItem.getAttributes().put("itemDescription", null);
    assertNull(selectItem.getItemDescription());

    assertEquals(
        selectItem.isItemDisabled(),
        ((Boolean) selectItem.getAttributes().get("itemDisabled")).booleanValue());
    selectItem.setItemDisabled(true);
    assertTrue(((Boolean) selectItem.getAttributes().get("itemDisabled")).booleanValue());
    selectItem.setItemDisabled(false);
    assertFalse(((Boolean) selectItem.getAttributes().get("itemDisabled")).booleanValue());
    selectItem.getAttributes().put("itemDisabled", Boolean.FALSE);
    assertFalse(selectItem.isItemDisabled());
    selectItem.getAttributes().put("itemDisabled", Boolean.TRUE);
    assertTrue(selectItem.isItemDisabled());

    assertEquals(selectItem.getItemLabel(), (String) selectItem.getAttributes().get("itemLabel"));
    selectItem.setItemLabel("foo");
    assertEquals("foo", (String) selectItem.getAttributes().get("itemLabel"));
    selectItem.setItemLabel(null);
    assertNull((String) selectItem.getAttributes().get("itemLabel"));
    selectItem.getAttributes().put("itemLabel", "bar");
    assertEquals("bar", selectItem.getItemLabel());
    selectItem.getAttributes().put("itemLabel", null);
    assertNull(selectItem.getItemLabel());

    assertEquals(selectItem.getItemValue(), (String) selectItem.getAttributes().get("itemValue"));
    selectItem.setItemValue("foo");
    assertEquals("foo", (String) selectItem.getAttributes().get("itemValue"));
    selectItem.setItemValue(null);
    assertNull((String) selectItem.getAttributes().get("itemValue"));
    selectItem.getAttributes().put("itemValue", "bar");
    assertEquals("bar", selectItem.getItemValue());
    selectItem.getAttributes().put("itemValue", null);
    assertNull(selectItem.getItemValue());
  }
  public void PENDING_FIXME_testValueBindings() {

    super.testValueBindings();
    UISelectItem test = (UISelectItem) component;

    // "itemDescription" property
    request.setAttribute("foo", "bar");
    test.setItemDescription(null);
    assertNull(test.getItemDescription());
    test.setValueBinding("itemDescription", application.createValueBinding("#{foo}"));
    assertNotNull(test.getValueBinding("itemDescription"));
    assertEquals("bar", test.getItemDescription());
    test.setItemDescription("baz");
    assertEquals("baz", test.getItemDescription());
    test.setItemDescription(null);
    assertEquals("bar", test.getItemDescription());
    test.setValueBinding("itemDescription", null);
    assertNull(test.getValueBinding("itemDescription"));
    assertNull(test.getItemDescription());

    // "itemDisabled" property
    assertFalse(test.isItemDisabled());
    request.setAttribute("foo", Boolean.TRUE);
    test.setValueBinding("itemDisabled", application.createValueBinding("#{foo}"));
    assertNotNull(test.getValueBinding("itemDisabled"));
    assertTrue(test.isItemDisabled());
    test.setItemDisabled(false);
    assertFalse(test.isItemDisabled());
    test.setValueBinding("itemDisabled", null);
    assertNull(test.getValueBinding("itemDisabled"));
    assertFalse(test.isItemDisabled());

    // "itemLabel" property
    request.setAttribute("foo", "bar");
    test.setItemLabel(null);
    assertNull(test.getItemLabel());
    test.setValueBinding("itemLabel", application.createValueBinding("#{foo}"));
    assertNotNull(test.getValueBinding("itemLabel"));
    assertEquals("bar", test.getItemLabel());
    test.setItemLabel("baz");
    assertEquals("baz", test.getItemLabel());
    test.setItemLabel(null);
    assertEquals("bar", test.getItemLabel());
    test.setValueBinding("itemLabel", null);
    assertNull(test.getValueBinding("itemLabel"));
    assertNull(test.getItemLabel());

    // "itemValue" property
    request.setAttribute("foo", "bar");
    test.setItemValue(null);
    assertNull(test.getItemValue());
    test.setValueBinding("itemValue", application.createValueBinding("#{foo}"));
    assertNotNull(test.getValueBinding("itemValue"));
    assertEquals("bar", test.getItemValue());
    test.setItemValue("baz");
    assertEquals("baz", test.getItemValue());
    test.setItemValue(null);
    assertEquals("bar", test.getItemValue());
    test.setValueBinding("itemValue", null);
    assertNull(test.getValueBinding("itemValue"));
    assertNull(test.getItemValue());

    // "value" property
    request.setAttribute("foo", "bar");
    test.setValue(null);
    assertNull(test.getValue());
    test.setValueBinding("value", application.createValueBinding("#{foo}"));
    assertNotNull(test.getValueBinding("value"));
    assertEquals("bar", test.getValue());
    test.setValue("baz");
    assertEquals("baz", test.getValue());
    test.setValue(null);
    assertEquals("bar", test.getValue());
    test.setValueBinding("value", null);
    assertNull(test.getValueBinding("value"));
    assertNull(test.getValue());
  }