Skip to content

Commit b3b4c7a

Browse files
committed
fixed checkboxes comparison for multi-list (SPR-6505)
1 parent cdee538 commit b3b4c7a

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/CheckboxesTagTests.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,15 +18,16 @@
1818

1919
import java.beans.PropertyEditorSupport;
2020
import java.io.StringReader;
21+
import java.text.ParseException;
2122
import java.util.ArrayList;
2223
import java.util.Calendar;
2324
import java.util.Date;
2425
import java.util.HashSet;
2526
import java.util.LinkedHashMap;
2627
import java.util.List;
28+
import java.util.Locale;
2729
import java.util.Map;
2830
import java.util.Set;
29-
3031
import javax.servlet.jsp.tagext.Tag;
3132

3233
import org.dom4j.Document;
@@ -37,6 +38,8 @@
3738
import org.springframework.beans.Pet;
3839
import org.springframework.beans.TestBean;
3940
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
41+
import org.springframework.format.Formatter;
42+
import org.springframework.format.support.FormattingConversionService;
4043
import org.springframework.util.ObjectUtils;
4144
import org.springframework.validation.BeanPropertyBindingResult;
4245
import org.springframework.validation.BindingResult;
@@ -414,6 +417,55 @@ public void testWithMultiValueWithReverseEditor() throws Exception {
414417
assertEquals("BAZ", checkboxElement3.attribute("value").getValue());
415418
}
416419

420+
public void testWithMultiValueWithFormatter() throws Exception {
421+
this.tag.setPath("stringArray");
422+
this.tag.setItems(new Object[] {" foo", " bar", " baz"});
423+
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
424+
FormattingConversionService cs = new FormattingConversionService();
425+
cs.addFormatterForFieldType(String.class, new Formatter<String>() {
426+
public String print(String object, Locale locale) {
427+
return object;
428+
}
429+
public String parse(String text, Locale locale) throws ParseException {
430+
return text.trim();
431+
}
432+
});
433+
bindingResult.initConversion(cs);
434+
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
435+
436+
int result = this.tag.doStartTag();
437+
assertEquals(Tag.SKIP_BODY, result);
438+
439+
String output = getOutput();
440+
441+
// wrap the output so it is valid XML
442+
output = "<doc>" + output + "</doc>";
443+
444+
SAXReader reader = new SAXReader();
445+
Document document = reader.read(new StringReader(output));
446+
Element spanElement1 = (Element) document.getRootElement().elements().get(0);
447+
Element checkboxElement1 = (Element) spanElement1.elements().get(0);
448+
assertEquals("input", checkboxElement1.getName());
449+
assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
450+
assertEquals("stringArray", checkboxElement1.attribute("name").getValue());
451+
assertEquals("checked", checkboxElement1.attribute("checked").getValue());
452+
assertEquals(" foo", checkboxElement1.attribute("value").getValue());
453+
Element spanElement2 = (Element) document.getRootElement().elements().get(1);
454+
Element checkboxElement2 = (Element) spanElement2.elements().get(0);
455+
assertEquals("input", checkboxElement2.getName());
456+
assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
457+
assertEquals("stringArray", checkboxElement2.attribute("name").getValue());
458+
assertEquals("checked", checkboxElement2.attribute("checked").getValue());
459+
assertEquals(" bar", checkboxElement2.attribute("value").getValue());
460+
Element spanElement3 = (Element) document.getRootElement().elements().get(2);
461+
Element checkboxElement3 = (Element) spanElement3.elements().get(0);
462+
assertEquals("input", checkboxElement3.getName());
463+
assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
464+
assertEquals("stringArray", checkboxElement3.attribute("name").getValue());
465+
assertNull("not checked", checkboxElement3.attribute("checked"));
466+
assertEquals(" baz", checkboxElement3.attribute("value").getValue());
467+
}
468+
417469
public void testCollectionOfPets() throws Exception {
418470
this.tag.setPath("pets");
419471
List allPets = new ArrayList();

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -463,7 +463,7 @@ public void testWithMultiList() throws Exception {
463463
assertEquals("Austria(AT)", e.getText());
464464
}
465465

466-
public void testWithElementConverter() throws Exception {
466+
public void testWithElementFormatter() throws Exception {
467467
this.bean.setRealCountry(Country.COUNTRY_UK);
468468

469469
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
@@ -505,7 +505,7 @@ public Country parse(String text, Locale locale) throws ParseException {
505505
assertEquals("United Kingdom", e.getText());
506506
}
507507

508-
public void testWithMultiListAndElementConverter() throws Exception {
508+
public void testWithMultiListAndElementFormatter() throws Exception {
509509
List list = new ArrayList();
510510
list.add(Country.COUNTRY_UK);
511511
list.add(Country.COUNTRY_AT);

0 commit comments

Comments
 (0)