@Test public void optionTagNotNestedWithinSelectTag() throws Exception { try { tag.setParent(null); tag.setValue("foo"); tag.doStartTag(); fail("Must throw an IllegalStateException when not nested within a <select/> tag."); } catch (IllegalStateException ex) { // expected } }
@Test public void withoutContext() throws Exception { this.tag.setParent(null); this.tag.setValue("foo"); this.tag.setLabel("Foo"); try { tag.doStartTag(); fail("Must not be able to use <option> tag without exposed context."); } catch (IllegalStateException ex) { // expected } }
@Test @SuppressWarnings("rawtypes") public void withJavaEnum() throws Exception { GenericBean testBean = new GenericBean(); testBean.setCustomEnum(CustomEnum.VALUE_1); getPageContext().getRequest().setAttribute("testBean", testBean); String selectName = "testBean.customEnum"; getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false)); this.tag.setValue("VALUE_1"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getWriter().toString(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "VALUE_1"); assertContainsAttribute(output, "selected", "selected"); }
@Test public void withCustomObjectAndEditorSelected() throws Exception { final PropertyEditor floatEditor = new SimpleFloatEditor(); floatEditor.setValue(new Float("12.34")); String selectName = "testBean.someNumber"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) { @Override public PropertyEditor getEditor() { return floatEditor; } }; getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new Float(12.34)); this.tag.setLabel("12.34f"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, "12.34f"); }
@Test public void withCustomObjectSelected() throws Exception { String selectName = "testBean.someNumber"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new Float(12.34)); this.tag.setLabel("GBP 12.34"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "12.34"); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, "GBP 12.34"); }
@Test public void withCustomObjectNotSelected() throws Exception { String selectName = "testBean.someNumber"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new Float(12.35)); this.tag.setLabel("GBP 12.35"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "12.35"); assertAttributeNotPresent(output, "selected"); assertBlockTagContains(output, "GBP 12.35"); }
@Test public void withCustomObjectAndEditorNotSelected() throws Exception { final PropertyEditor floatEditor = new SimpleFloatEditor(); String selectName = "testBean.someNumber"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) { @Override public PropertyEditor getEditor() { return floatEditor; } }; getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new Float(12.35)); this.tag.setLabel("12.35f"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertAttributeNotPresent(output, "selected"); assertBlockTagContains(output, "12.35f"); }
@Test public void renderNotSelected() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("bar"); this.tag.setLabel("Bar"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "bar"); assertBlockTagContains(output, "Bar"); }
@Test public void multiBind() 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()); }
@Test public void asBodyTagSelected() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); String bodyContent = "some content"; this.tag.setValue("Rob Harrop"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertBlockTagContains(output, bodyContent); }
@Test public void asBodyTagWithEditor() throws Exception { String selectName = "testBean.stringArray"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) { @Override public PropertyEditor getEditor() { return new RulesVariantEditor(); } }; getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); RulesVariant rulesVariant = new RulesVariant("someRules", "someVariant"); this.tag.setValue(rulesVariant); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); assertEquals(rulesVariant, getPageContext().getAttribute("value")); assertEquals(rulesVariant.toId(), getPageContext().getAttribute("displayValue")); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); }
@Test public void withPropertyEditor() throws Exception { String selectName = "testBean.stringArray"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) { @Override public PropertyEditor getEditor() { return new StringArrayPropertyEditor(); } }; getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(ARRAY_SOURCE); this.tag.setLabel("someArray"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", ARRAY_SOURCE); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, "someArray"); }
@Test public void asBodyTagCollapsed() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); String bodyContent = "some content"; this.tag.setValue(bodyContent); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", bodyContent); assertBlockTagContains(output, bodyContent); }
@Test public void asBodyTag() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); String bodyContent = "some content"; this.tag.setValue("foo"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, bodyContent); }
@Test public void canBeDisabledEvenWhenSelected() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("bar"); this.tag.setLabel("Bar"); this.tag.setDisabled(true); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "bar"); assertContainsAttribute(output, "disabled", "disabled"); assertBlockTagContains(output, "Bar"); }
@Test public void withNoLabel() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("bar"); this.tag.setCssClass("myClass"); this.tag.setOnclick("CLICK"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "bar"); assertContainsAttribute(output, "class", "myClass"); assertContainsAttribute(output, "onclick", "CLICK"); assertBlockTagContains(output, "bar"); }
@Test public void renderSelected() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setId("myOption"); this.tag.setValue("foo"); this.tag.setLabel("Foo"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "id", "myOption"); assertContainsAttribute(output, "value", "foo"); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, "Foo"); }
@Test public void withPropertyEditorStringComparison() throws Exception { final PropertyEditor testBeanEditor = new TestBeanPropertyEditor(); testBeanEditor.setValue(new TestBean("Sally")); String selectName = "testBean.spouse"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) { @Override public PropertyEditor getEditor() { return testBeanEditor; } }; getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("Sally"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "Sally"); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, "Sally"); }
@Test public void renderWithDynamicAttributes() throws Exception { String dynamicAttribute1 = "attr1"; String dynamicAttribute2 = "attr2"; String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("bar"); this.tag.setLabel("Bar"); this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1); this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "bar"); assertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1); assertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2); assertBlockTagContains(output, "Bar"); }
private Tag createSelectTag( String expression, Map<String, String> attributes ) { // Select tag SelectTag select = new SelectTag(); // Empty option (if needed) if ( WidgetBuilderUtils.needsEmptyLookupItem( attributes ) ) { OptionTag emptyOption = new OptionTag(); emptyOption.setValue( "" ); JspUtils.addDeferredChild( select, emptyOption ); } // Options tag OptionsTag optionsTag = new OptionsTag(); optionsTag.setItems( expression ); // Optional itemValue and itemLabel String itemValue = attributes.get( SPRING_LOOKUP_ITEM_VALUE ); if ( itemValue != null ) { optionsTag.setItemValue( itemValue ); } String itemLabel = attributes.get( SPRING_LOOKUP_ITEM_LABEL ); if ( itemLabel != null ) { optionsTag.setItemLabel( itemLabel ); } JspUtils.addDeferredChild( select, optionsTag ); return select; }