@ -23,6 +23,7 @@ import java.util.ArrayList;
@@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Collections ;
import java.util.Comparator ;
import java.util.HashMap ;
import java.util.LinkedHashMap ;
import java.util.List ;
import java.util.Locale ;
import java.util.Map ;
@ -54,6 +55,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -54,6 +55,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Juergen Hoeller
* @author Jeremy Grelle
* @author Dave Syer
* @author Sam Brannen
* /
@SuppressWarnings ( { "rawtypes" , "unchecked" } )
public class SelectTagTests extends AbstractFormTagTests {
@ -78,6 +80,18 @@ public class SelectTagTests extends AbstractFormTagTests {
@@ -78,6 +80,18 @@ public class SelectTagTests extends AbstractFormTagTests {
this . tag . setPageContext ( getPageContext ( ) ) ;
}
@Override
protected TestBean createTestBean ( ) {
this . bean = new TestBeanWithRealCountry ( ) ;
this . bean . setName ( "Rob" ) ;
this . bean . setCountry ( "UK" ) ;
this . bean . setSex ( "M" ) ;
this . bean . setMyFloat ( Float . valueOf ( "12.34" ) ) ;
this . bean . setSomeIntegerArray ( new Integer [ ] { 12 , 34 } ) ;
return this . bean ;
}
@Test
void dynamicAttributes ( ) throws JspException {
String dynamicAttribute1 = "attr1" ;
@ -133,6 +147,78 @@ public class SelectTagTests extends AbstractFormTagTests {
@@ -133,6 +147,78 @@ public class SelectTagTests extends AbstractFormTagTests {
assertList ( true ) ;
}
@Test // gh-33023
void withListWithHtmlEscapingInPath ( ) throws Exception {
this . tag . setPath ( "favoriteCafé" ) ;
this . tag . setItems ( List . of ( "Cup of Joe" , "Jane's Coffee Shop" ) ) ;
this . tag . setSize ( "2" ) ;
var expectedOutput = "" "
< select id = "favoriteCafé" name = "favoriteCafé" size = "2" >
< option value = "Cup of Joe" > Cup of Joe < / option >
< option value = "Jane's Coffee Shop" > Jane & # 39 ; s Coffee Shop < / option >
< / select >
"" ".replace(" \ n ", " " ) ;
assertThat ( this . tag . doStartTag ( ) ) . isEqualTo ( Tag . SKIP_BODY ) ;
assertThat ( getOutput ( ) ) . isEqualTo ( expectedOutput ) ;
}
@Test // gh-33023
void withListWithHtmlEscapingAndCharacterEncodingInPath ( ) throws Exception {
this . getPageContext ( ) . getResponse ( ) . setCharacterEncoding ( "UTF-8" ) ;
this . tag . setPath ( "favoriteCafé" ) ;
this . tag . setItems ( List . of ( "Cup of Joe" , "Jane's Coffee Shop" ) ) ;
this . tag . setSize ( "2" ) ;
var expectedOutput = "" "
< select id = "favoriteCafé" name = "favoriteCafé" size = "2" >
< option value = "Cup of Joe" > Cup of Joe < / option >
< option value = "Jane's Coffee Shop" > Jane & # 39 ; s Coffee Shop < / option >
< / select >
"" ".replace(" \ n ", " " ) ;
assertThat ( this . tag . doStartTag ( ) ) . isEqualTo ( Tag . SKIP_BODY ) ;
assertThat ( getOutput ( ) ) . isEqualTo ( expectedOutput ) ;
}
@Test // gh-35783
void withListWithHtmlEscapingInOptions ( ) throws Exception {
this . tag . setPath ( "name" ) ;
this . tag . setItems ( List . of ( "café" , "Jane \"I Love Cafés\" Smith" ) ) ;
this . tag . setSize ( "2" ) ;
var expectedOutput = "" "
< select id = "name" name = "name" size = "2" >
< option value = "café" > caf & eacute ; < / option >
< option value = "Jane "I Love Cafés" Smith" > Jane & quot ; I Love Caf & eacute ; s & quot ; Smith < / option >
< / select >
"" ".replace(" \ n ", " " ) ;
assertThat ( this . tag . doStartTag ( ) ) . isEqualTo ( Tag . SKIP_BODY ) ;
assertThat ( getOutput ( ) ) . isEqualTo ( expectedOutput ) ;
}
@Test // gh-35783
void withListWithHtmlEscapingAndCharacterEncodingInOptions ( ) throws Exception {
this . getPageContext ( ) . getResponse ( ) . setCharacterEncoding ( "UTF-8" ) ;
this . tag . setPath ( "name" ) ;
this . tag . setItems ( List . of ( "café" , "Jane \"I Love Cafés\" Smith" ) ) ;
this . tag . setSize ( "2" ) ;
var expectedOutput = "" "
< select id = "name" name = "name" size = "2" >
< option value = "café" > café < / option >
< option value = "Jane "I Love Cafés" Smith" > Jane & quot ; I Love Cafés & quot ; Smith < / option >
< / select >
"" ".replace(" \ n ", " " ) ;
assertThat ( this . tag . doStartTag ( ) ) . isEqualTo ( Tag . SKIP_BODY ) ;
assertThat ( getOutput ( ) ) . isEqualTo ( expectedOutput ) ;
}
@Test
void withResolvedList ( ) throws Exception {
this . tag . setPath ( "country" ) ;
@ -336,8 +422,58 @@ public class SelectTagTests extends AbstractFormTagTests {
@@ -336,8 +422,58 @@ public class SelectTagTests extends AbstractFormTagTests {
void withMap ( ) throws Exception {
this . tag . setPath ( "sex" ) ;
this . tag . setItems ( getSexes ( ) ) ;
int result = this . tag . doStartTag ( ) ;
assertThat ( result ) . isEqualTo ( Tag . SKIP_BODY ) ;
var expectedOutput = "" "
< select id = "sex" name = "sex" >
< option value = "F" > Female < / option >
< option value = "M" selected = "selected" > Male < / option >
< / select >
"" ".replace(" \ n ", " " ) ;
assertThat ( this . tag . doStartTag ( ) ) . isEqualTo ( Tag . SKIP_BODY ) ;
assertThat ( getOutput ( ) ) . isEqualTo ( expectedOutput ) ;
}
@Test // gh-35783
void withMapWithHtmlEscapingInOptions ( ) throws Exception {
var map = new LinkedHashMap < String , String > ( ) ;
map . put ( "F" , "Jane \"I Love Cafés\" Smith" ) ;
map . put ( "M" , "Joe Café" ) ;
this . tag . setPath ( "sex" ) ;
this . tag . setItems ( map ) ;
var expectedOutput = "" "
< select id = "sex" name = "sex" >
< option value = "F" > Jane & quot ; I Love Caf & eacute ; s & quot ; Smith < / option >
< option value = "M" selected = "selected" > Joe Caf & eacute ; < / option >
< / select >
"" ".replace(" \ n ", " " ) ;
assertThat ( this . tag . doStartTag ( ) ) . isEqualTo ( Tag . SKIP_BODY ) ;
assertThat ( getOutput ( ) ) . isEqualTo ( expectedOutput ) ;
}
@Test // gh-35783
void withMapWithHtmlEscapingAndCharacterEncodingInOptions ( ) throws Exception {
this . getPageContext ( ) . getResponse ( ) . setCharacterEncoding ( "UTF-8" ) ;
var map = new LinkedHashMap < String , String > ( ) ;
map . put ( "F" , "Jane \"I Love Cafés\" Smith" ) ;
map . put ( "M" , "Joe Café" ) ;
this . tag . setPath ( "sex" ) ;
this . tag . setItems ( map ) ;
var expectedOutput = "" "
< select id = "sex" name = "sex" >
< option value = "F" > Jane & quot ; I Love Cafés & quot ; Smith < / option >
< option value = "M" selected = "selected" > Joe Café < / option >
< / select >
"" ".replace(" \ n ", " " ) ;
assertThat ( this . tag . doStartTag ( ) ) . isEqualTo ( Tag . SKIP_BODY ) ;
assertThat ( getOutput ( ) ) . isEqualTo ( expectedOutput ) ;
}
@Test
@ -959,7 +1095,7 @@ public class SelectTagTests extends AbstractFormTagTests {
@@ -959,7 +1095,7 @@ public class SelectTagTests extends AbstractFormTagTests {
}
private Map getSexes ( ) {
Map < String , String > sexes = new HashMap < > ( ) ;
Map < String , String > sexes = new Linked HashMap< > ( ) ;
sexes . put ( "F" , "Female" ) ;
sexes . put ( "M" , "Male" ) ;
return sexes ;
@ -997,17 +1133,6 @@ public class SelectTagTests extends AbstractFormTagTests {
@@ -997,17 +1133,6 @@ public class SelectTagTests extends AbstractFormTagTests {
}
}
@Override
protected TestBean createTestBean ( ) {
this . bean = new TestBeanWithRealCountry ( ) ;
this . bean . setName ( "Rob" ) ;
this . bean . setCountry ( "UK" ) ;
this . bean . setSex ( "M" ) ;
this . bean . setMyFloat ( Float . valueOf ( "12.34" ) ) ;
this . bean . setSomeIntegerArray ( new Integer [ ] { 12 , 34 } ) ;
return this . bean ;
}
private TestBean getTestBean ( ) {
return ( TestBean ) getPageContext ( ) . getRequest ( ) . getAttribute ( COMMAND_NAME ) ;
}