org.springframework.web.servlet.support.BindStatus Java Examples

The following examples show how to use org.springframework.web.servlet.support.BindStatus. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OptionTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #2
Source File: OptionTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #3
Source File: OptionTagEnumTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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");
}
 
Example #4
Source File: SelectedValueComparator.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private static boolean exhaustiveCollectionCompare(
		Collection<?> collection, Object candidateValue, BindStatus bindStatus) {

	Map<PropertyEditor, Object> convertedValueCache = new HashMap<PropertyEditor, Object>(1);
	PropertyEditor editor = null;
	boolean candidateIsString = (candidateValue instanceof String);
	if (!candidateIsString) {
		editor = bindStatus.findEditor(candidateValue.getClass());
	}
	for (Object element : collection) {
		if (editor == null && element != null && candidateIsString) {
			editor = bindStatus.findEditor(element.getClass());
		}
		if (exhaustiveCompare(element, candidateValue, editor, convertedValueCache)) {
			return true;
		}
	}
	return false;
}
 
Example #5
Source File: BindTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bindTagWithoutErrors() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", status.getExpression() == null);
	assertTrue("Correct value", status.getValue() == null);
	assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
	assertTrue("Correct isError", !status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 0);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 0);
	assertTrue("Correct errorCode", "".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(",")));
}
 
Example #6
Source File: OptionTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #7
Source File: OptionTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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");
}
 
Example #8
Source File: OptionTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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");
}
 
Example #9
Source File: OptionTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void renderNotSelected() throws Exception {
	String selectName = "testBean.name";
	getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, 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 = getOutput();

	assertOptionTagOpened(output);
	assertOptionTagClosed(output);
	assertContainsAttribute(output, "value", "bar");
	assertBlockTagContains(output, "Bar");
}
 
Example #10
Source File: OptionTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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");
}
 
Example #11
Source File: OptionTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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");
}
 
Example #12
Source File: OptionTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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");
}
 
Example #13
Source File: OptionTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void renderWithDynamicAttributes() throws Exception {
	String dynamicAttribute1 = "attr1";
	String dynamicAttribute2 = "attr2";

	String selectName = "testBean.name";
	getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
	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");
}
 
Example #14
Source File: OptionTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #15
Source File: OptionTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #16
Source File: OptionTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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");
}
 
Example #17
Source File: OptionTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #18
Source File: SelectTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void withNestedOptions() throws Exception {
	this.tag.setPath("country");
	int result = this.tag.doStartTag();
	assertEquals(Tag.EVAL_BODY_INCLUDE, result);

	BindStatus value = (BindStatus) getPageContext().getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE);
	assertEquals("Selected country not exposed in page context", "UK", value.getValue());

	result = this.tag.doEndTag();
	assertEquals(Tag.EVAL_PAGE, result);
	this.tag.doFinally();

	String output = getOutput();
	assertTrue(output.startsWith("<select "));
	assertTrue(output.endsWith("</select>"));
	assertContainsAttribute(output, "name", "country");
}
 
Example #19
Source File: OptionTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void renderSelected() throws Exception {
	String selectName = "testBean.name";
	getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
	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");
}
 
Example #20
Source File: SelectedValueComparator.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static boolean exhaustiveCollectionCompare(
		Collection<?> collection, Object candidateValue, BindStatus bindStatus) {

	Map<PropertyEditor, Object> convertedValueCache = new HashMap<>();
	PropertyEditor editor = null;
	boolean candidateIsString = (candidateValue instanceof String);
	if (!candidateIsString) {
		editor = bindStatus.findEditor(candidateValue.getClass());
	}
	for (Object element : collection) {
		if (editor == null && element != null && candidateIsString) {
			editor = bindStatus.findEditor(element.getClass());
		}
		if (exhaustiveCompare(element, candidateValue, editor, convertedValueCache)) {
			return true;
		}
	}
	return false;
}
 
Example #21
Source File: OptionsTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void withItemsNullReference() throws Exception {
	getPageContext().setAttribute(
			SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.country", false));

	this.tag.setItems(Collections.emptyList());
	this.tag.setItemValue("isoCode");
	this.tag.setItemLabel("name");
	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", 0, children.size());
}
 
Example #22
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bindTagWithoutErrors() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", status.getExpression() == null);
	assertTrue("Correct value", status.getValue() == null);
	assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
	assertTrue("Correct isError", !status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 0);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 0);
	assertTrue("Correct errorCode", "".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(",")));
}
 
Example #23
Source File: OptionTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void canBeDisabledEvenWhenSelected() throws Exception {
	String selectName = "testBean.name";
	getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, 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 = getOutput();

	assertOptionTagOpened(output);
	assertOptionTagClosed(output);
	assertContainsAttribute(output, "value", "bar");
	assertContainsAttribute(output, "disabled", "disabled");
	assertBlockTagContains(output, "Bar");
}
 
Example #24
Source File: OptionTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #25
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bindTagWithMappedProperties() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
	errors.rejectValue("map[key1]", "code1", "message1");
	errors.rejectValue("map[key1]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.map[key1]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "map[key1]".equals(status.getExpression()));
	assertTrue("Value is TestBean", status.getValue() instanceof TestBean);
	assertTrue("Correct value", "name4".equals(((TestBean) status.getValue()).getName()));
	assertTrue("Correct isError", status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
	assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
	assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
	assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0]));
	assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
}
 
Example #26
Source File: SelectTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withNestedOptions() throws Exception {
	this.tag.setPath("country");
	int result = this.tag.doStartTag();
	assertEquals(Tag.EVAL_BODY_INCLUDE, result);

	BindStatus value = (BindStatus) getPageContext().getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE);
	assertEquals("Selected country not exposed in page context", "UK", value.getValue());

	result = this.tag.doEndTag();
	assertEquals(Tag.EVAL_PAGE, result);
	this.tag.doFinally();

	String output = getOutput();
	assertTrue(output.startsWith("<select "));
	assertTrue(output.endsWith("</select>"));
	assertContainsAttribute(output, "name", "country");
}
 
Example #27
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bindTagWithToStringAndHtmlEscaping() throws JspException {
	PageContext pc = createPageContext();
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.doctor");
	tag.setHtmlEscape(true);
	TestBean tb = new TestBean("somebody", 99);
	NestedTestBean ntb = new NestedTestBean("juergen&eva");
	tb.setDoctor(ntb);
	pc.getRequest().setAttribute("tb", tb);
	tag.doStartTag();
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertEquals("doctor", status.getExpression());
	assertTrue(status.getValue() instanceof NestedTestBean);
	assertTrue(status.getDisplayValue().contains("juergen&amp;eva"));
}
 
Example #28
Source File: BindTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void bindTagWithoutErrors() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", status.getExpression() == null);
	assertTrue("Correct value", status.getValue() == null);
	assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
	assertTrue("Correct isError", !status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 0);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 0);
	assertTrue("Correct errorCode", "".equals(status.getErrorCode()));
	assertTrue("Correct errorMessage", "".equals(status.getErrorMessage()));
	assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(",")));
}
 
Example #29
Source File: VelocityMacroTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void exposeSpringMacroHelpers() throws Exception {
	VelocityView vv = new VelocityView() {
		@Override
		protected void mergeTemplate(Template template, Context context, HttpServletResponse response) {
			assertTrue(context.get(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext);
			RequestContext rc = (RequestContext) context.get(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
			BindStatus status = rc.getBindStatus("tb.name");
			assertEquals("name", status.getExpression());
			assertEquals("juergen", status.getValue());
		}
	};
	vv.setUrl(TEMPLATE_FILE);
	vv.setApplicationContext(wac);
	vv.setExposeSpringMacroHelpers(true);

	Map<String, Object> model = new HashMap<String, Object>();
	model.put("tb", new TestBean("juergen", 99));
	vv.render(model, request, response);
}
 
Example #30
Source File: BindTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void nestedPathWithBindTagWithIgnoreNestedPath() throws JspException {
	PageContext pc = createPageContext();
	Errors errors = new ServletRequestDataBinder(new TestBean(), "tb2").getBindingResult();
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb2", errors);

	NestedPathTag tag = new NestedPathTag();
	tag.setPath("tb");
	tag.setPageContext(pc);
	tag.doStartTag();

	BindTag bindTag = new BindTag();
	bindTag.setPageContext(pc);
	bindTag.setIgnoreNestedPath(true);
	bindTag.setPath("tb2.name");

	assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertEquals("tb2.name", status.getPath());
}