public void testWithCustomBinder() throws Exception {
    this.tag.setPath("myFloat");

    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.rob, COMMAND_NAME);
    errors.getPropertyAccessor().registerCustomEditor(Float.class, new SimpleFloatEditor());
    exposeBindingResult(errors);

    assertEquals(Tag.SKIP_BODY, this.tag.doStartTag());

    String output = getOutput();
    assertTagOpened(output);
    assertTagClosed(output);

    assertContainsAttribute(output, "type", getType());
    assertValueAttribute(output, "12.34f");
  }
Example #2
0
  public void testMultiBind() throws Exception {
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(new TestBean(), "testBean");
    result
        .getPropertyAccessor()
        .registerCustomEditor(TestBean.class, "friends", new FriendEditor());
    exposeBindingResult(result);

    BindStatus bindStatus = new BindStatus(getRequestContext(), "testBean.friends", false);

    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    this.tag.setValue(new TestBean("foo"));
    this.tag.doStartTag();
    this.tag.doEndTag();

    assertEquals("<option value=\"foo\">foo</option>", getOutput());
  }
  public void testWithCollectionAndCustomEditor() throws Exception {
    PropertyEditor propertyEditor = new SimpleFloatEditor();

    TestBean target = new TestBean();
    target.setMyFloat(new Float("12.34"));

    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
    errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor);
    exposeBindingResult(errors);

    getPageContext()
        .setAttribute(
            SelectTag.LIST_VALUE_PAGE_ATTRIBUTE,
            new BindStatus(getRequestContext(), "testBean.myFloat", false));

    this.tag.setItems("${floats}");
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    String output = getOutput();
    output = "<doc>" + output + "</doc>";

    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element rootElement = document.getRootElement();

    List children = rootElement.elements();
    assertEquals("Incorrect number of children", 6, children.size());

    Element element = (Element) rootElement.selectSingleNode("option[text() = '12.34f']");
    assertNotNull("Option node should not be null", element);
    assertEquals("12.34 node not selected", "selected", element.attribute("selected").getValue());
    assertNull("No id rendered", element.attribute("id"));

    element = (Element) rootElement.selectSingleNode("option[text() = '12.35f']");
    assertNotNull("Option node should not be null", element);
    assertNull("12.35 node incorrectly selected", element.attribute("selected"));
    assertNull("No id rendered", element.attribute("id"));
  }
  public void testWithFloatCustom() throws Exception {
    PropertyEditor propertyEditor = new SimpleFloatEditor();
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME);
    errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor);
    exposeBindingResult(errors);

    this.tag.setPath("myFloat");

    Float[] array =
        new Float[] {
          new Float("12.30"), new Float("12.32"), new Float("12.34"), new Float("12.36"),
          new Float("12.38"), new Float("12.40"), new Float("12.42"), new Float("12.44"),
          new Float("12.46"), new Float("12.48")
        };

    this.tag.setItems(array);
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);

    String output = getWriter().toString();
    assertTrue(output.startsWith("<select "));
    assertTrue(output.endsWith("</select>"));

    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element rootElement = document.getRootElement();
    assertEquals("select", rootElement.getName());
    assertEquals("myFloat", rootElement.attribute("name").getValue());
    List children = rootElement.elements();
    assertEquals("Incorrect number of children", array.length, children.size());

    Element e = (Element) rootElement.selectSingleNode("option[text() = '12.34f']");
    assertEquals("'12.34' node not selected", "selected", e.attribute("selected").getValue());

    e = (Element) rootElement.selectSingleNode("option[text() = '12.32f']");
    assertNull("'12.32' node incorrectly selected", e.attribute("selected"));
  }