📄 optiontagtests.java
字号:
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.tags.form;
import org.springframework.beans.Colour;
import org.springframework.beans.TestBean;
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.AssertThrows;
import org.springframework.util.StringUtils;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.web.servlet.support.BindStatus;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Unit tests for the {@link OptionTag} class.
*
* @author Rob Harrop
* @author Rick Evans
*/
public final class OptionTagTests extends AbstractHtmlElementTagTests {
private static final String ARRAY_SOURCE = "abc,123,def";
private static final String[] ARRAY = StringUtils.commaDelimitedListToStringArray(ARRAY_SOURCE);
private OptionTag tag;
protected void onSetUp() {
this.tag = new OptionTag() {
protected TagWriter createTagWriter() {
return new TagWriter(getWriter());
}
};
this.tag.setParent(new SelectTag());
this.tag.setPageContext(getPageContext());
}
public void testCanBeDisabledEvenWhenSelected() throws Exception {
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.name", false));
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 = getWriter().toString();
assertOptionTagOpened(output);
assertOptionTagClosed(output);
assertContainsAttribute(output, "value", "bar");
assertContainsAttribute(output, "disabled", "disabled");
assertBlockTagContains(output, "Bar");
}
public void testRenderNotSelected() throws Exception {
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.name", false));
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 = getWriter().toString();
assertOptionTagOpened(output);
assertOptionTagClosed(output);
assertContainsAttribute(output, "value", "bar");
assertBlockTagContains(output, "Bar");
}
public void testRenderSelected() throws Exception {
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.name", false));
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 = getWriter().toString();
assertOptionTagOpened(output);
assertOptionTagClosed(output);
assertContainsAttribute(output, "value", "foo");
assertContainsAttribute(output, "selected", "selected");
assertBlockTagContains(output, "Foo");
}
public void testWithNoLabel() throws Exception {
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.name", false));
this.tag.setValue("bar");
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", "bar");
assertBlockTagContains(output, "bar");
}
public void testWithoutContext() throws Exception {
this.tag.setParent(null);
this.tag.setValue("foo");
this.tag.setLabel("Foo");
new AssertThrows(IllegalStateException.class,
"Must not be able to use <option> tag without exposed context.") {
public void test() throws Exception {
tag.doStartTag();
}
}.runTest();
}
public void testWithEnum() throws Exception {
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.favouriteColour", false));
String value = Colour.GREEN.getCode().toString();
String label = Colour.GREEN.getLabel();
this.tag.setValue(value);
this.tag.setLabel(label);
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);
assertContainsAttribute(output, "selected", "selected");
assertBlockTagContains(output, label);
}
public void testWithEnumNotSelected() throws Exception {
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.favouriteColour", false));
String value = Colour.BLUE.getCode().toString();
String label = Colour.BLUE.getLabel();
this.tag.setValue(value);
this.tag.setLabel(label);
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);
assertAttributeNotPresent(output, "selected");
assertBlockTagContains(output, label);
}
public void testWithPropertyEditor() throws Exception {
BindStatus bindStatus = new BindStatus(getRequestContext(), "testBean.stringArray", false) {
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 = getWriter().toString();
assertOptionTagOpened(output);
assertOptionTagClosed(output);
assertContainsAttribute(output, "value", ARRAY_SOURCE);
assertContainsAttribute(output, "selected", "selected");
assertBlockTagContains(output, "someArray");
}
public void testWithPropertyEditorStringComparison() throws Exception {
final PropertyEditor testBeanEditor = new TestBeanPropertyEditor();
testBeanEditor.setValue(new TestBean("Sally"));
BindStatus bindStatus = new BindStatus(getRequestContext(), "testBean.spouse", false) {
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 = getWriter().toString();
assertOptionTagOpened(output);
assertOptionTagClosed(output);
assertContainsAttribute(output, "value", "Sally");
assertContainsAttribute(output, "selected", "selected");
assertBlockTagContains(output, "Sally");
}
public void testWithCustomObjectSelected() throws Exception {
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.someNumber", false));
this.tag.setValue("${myNumber}");
this.tag.setLabel("GBP ${myNumber}");
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", "12.34");
assertContainsAttribute(output, "selected", "selected");
assertBlockTagContains(output, "GBP 12.34");
}
public void testWithCustomObjectNotSelected() throws Exception {
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.someNumber", false));
this.tag.setValue("${myOtherNumber}");
this.tag.setLabel("GBP ${myOtherNumber}");
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", "12.35");
assertAttributeNotPresent(output, "selected");
assertBlockTagContains(output, "GBP 12.35");
}
public void testWithCustomObjectAndEditorSelected() throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -