|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2008 the original author or authors. |
| 2 | + * Copyright 2002-2010 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.beans.PropertyEditorSupport;
|
20 | 20 | import java.io.StringReader;
|
| 21 | +import java.text.ParseException; |
21 | 22 | import java.util.ArrayList;
|
22 | 23 | import java.util.Calendar;
|
23 | 24 | import java.util.Date;
|
24 | 25 | import java.util.HashSet;
|
25 | 26 | import java.util.LinkedHashMap;
|
26 | 27 | import java.util.List;
|
| 28 | +import java.util.Locale; |
27 | 29 | import java.util.Map;
|
28 | 30 | import java.util.Set;
|
29 |
| - |
30 | 31 | import javax.servlet.jsp.tagext.Tag;
|
31 | 32 |
|
32 | 33 | import org.dom4j.Document;
|
|
37 | 38 | import org.springframework.beans.Pet;
|
38 | 39 | import org.springframework.beans.TestBean;
|
39 | 40 | import org.springframework.beans.propertyeditors.StringTrimmerEditor;
|
| 41 | +import org.springframework.format.Formatter; |
| 42 | +import org.springframework.format.support.FormattingConversionService; |
40 | 43 | import org.springframework.util.ObjectUtils;
|
41 | 44 | import org.springframework.validation.BeanPropertyBindingResult;
|
42 | 45 | import org.springframework.validation.BindingResult;
|
@@ -414,6 +417,55 @@ public void testWithMultiValueWithReverseEditor() throws Exception {
|
414 | 417 | assertEquals("BAZ", checkboxElement3.attribute("value").getValue());
|
415 | 418 | }
|
416 | 419 |
|
| 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 | + |
417 | 469 | public void testCollectionOfPets() throws Exception {
|
418 | 470 | this.tag.setPath("pets");
|
419 | 471 | List allPets = new ArrayList();
|
|
0 commit comments