2020import java .nio .charset .StandardCharsets ;
2121import java .util .ArrayList ;
2222import java .util .Arrays ;
23+ import java .util .HashMap ;
2324import java .util .List ;
25+ import java .util .Map ;
2426import java .util .concurrent .ExecutorService ;
2527import java .util .concurrent .Executors ;
2628import java .util .concurrent .Future ;
3436import org .springframework .beans .DirectFieldAccessor ;
3537import org .springframework .context .ApplicationContextException ;
3638import org .springframework .context .support .StaticApplicationContext ;
39+ import org .springframework .http .HttpHeaders ;
40+ import org .springframework .http .MediaType ;
41+ import org .springframework .mock .web .test .MockHttpServletRequest ;
42+ import org .springframework .mock .web .test .MockHttpServletResponse ;
43+ import org .springframework .mock .web .test .MockServletContext ;
44+ import org .springframework .web .context .support .StaticWebApplicationContext ;
45+ import org .springframework .web .servlet .DispatcherServlet ;
3746
3847import static org .hamcrest .Matchers .*;
3948import static org .junit .Assert .*;
@@ -51,18 +60,18 @@ public class ScriptTemplateViewTests {
5160
5261 private ScriptTemplateConfigurer configurer ;
5362
54- private StaticApplicationContext applicationContext ;
63+ private StaticWebApplicationContext wac ;
5564
5665 private static final String RESOURCE_LOADER_PATH = "classpath:org/springframework/web/servlet/view/script/" ;
5766
5867
5968 @ Before
6069 public void setup () {
6170 this .configurer = new ScriptTemplateConfigurer ();
62- this .applicationContext = new StaticApplicationContext ();
63- this .applicationContext .getBeanFactory ().registerSingleton ("scriptTemplateConfigurer" , this .configurer );
71+ this .wac = new StaticWebApplicationContext ();
72+ this .wac .getBeanFactory ().registerSingleton ("scriptTemplateConfigurer" , this .configurer );
6473 this .view = new ScriptTemplateView ();
65- this .view .setUrl ("sampleView " );
74+ this .view .setUrl (RESOURCE_LOADER_PATH + "empty.txt " );
6675 }
6776
6877 @ Test
@@ -83,30 +92,34 @@ public void detectScriptTemplateConfigWithEngine() {
8392 this .configurer .setEngine (engine );
8493 this .configurer .setRenderObject ("Template" );
8594 this .configurer .setRenderFunction ("render" );
95+ this .configurer .setContentType (MediaType .TEXT_PLAIN_VALUE );
8696 this .configurer .setCharset (StandardCharsets .ISO_8859_1 );
97+ this .configurer .setSharedEngine (true );
98+
8799 DirectFieldAccessor accessor = new DirectFieldAccessor (this .view );
88- this .view .setApplicationContext (this .applicationContext );
100+ this .view .setApplicationContext (this .wac );
89101 assertEquals (engine , accessor .getPropertyValue ("engine" ));
90102 assertEquals ("Template" , accessor .getPropertyValue ("renderObject" ));
91103 assertEquals ("render" , accessor .getPropertyValue ("renderFunction" ));
104+ assertEquals (MediaType .TEXT_PLAIN_VALUE , accessor .getPropertyValue ("contentType" ));
92105 assertEquals (StandardCharsets .ISO_8859_1 , accessor .getPropertyValue ("charset" ));
106+ assertEquals (true , accessor .getPropertyValue ("sharedEngine" ));
93107 }
94108
95109 @ Test
96110 public void detectScriptTemplateConfigWithEngineName () {
97111 this .configurer .setEngineName ("nashorn" );
98112 this .configurer .setRenderObject ("Template" );
99113 this .configurer .setRenderFunction ("render" );
100- this .configurer .setCharset (StandardCharsets .ISO_8859_1 );
101- this .configurer .setSharedEngine (true );
114+
102115 DirectFieldAccessor accessor = new DirectFieldAccessor (this .view );
103- this .view .setApplicationContext (this .applicationContext );
116+ this .view .setApplicationContext (this .wac );
104117 assertEquals ("nashorn" , accessor .getPropertyValue ("engineName" ));
105118 assertNotNull (accessor .getPropertyValue ("engine" ));
106119 assertEquals ("Template" , accessor .getPropertyValue ("renderObject" ));
107120 assertEquals ("render" , accessor .getPropertyValue ("renderFunction" ));
108- assertEquals (StandardCharsets . ISO_8859_1 , accessor .getPropertyValue ("charset " ));
109- assertEquals (true , accessor .getPropertyValue ("sharedEngine " ));
121+ assertEquals (MediaType . TEXT_HTML_VALUE , accessor .getPropertyValue ("contentType " ));
122+ assertEquals (StandardCharsets . UTF_8 , accessor .getPropertyValue ("charset " ));
110123 }
111124
112125 @ Test
@@ -115,7 +128,7 @@ public void customEngineAndRenderFunction() throws Exception {
115128 given (engine .get ("key" )).willReturn ("value" );
116129 this .view .setEngine (engine );
117130 this .view .setRenderFunction ("render" );
118- this .view .setApplicationContext (this .applicationContext );
131+ this .view .setApplicationContext (this .wac );
119132 engine = this .view .getEngine ();
120133 assertNotNull (engine );
121134 assertEquals ("value" , engine .get ("key" ));
@@ -131,7 +144,7 @@ public void nonSharedEngine() throws Exception {
131144 this .view .setEngineName ("nashorn" );
132145 this .view .setRenderFunction ("render" );
133146 this .view .setSharedEngine (false );
134- this .view .setApplicationContext (this .applicationContext );
147+ this .view .setApplicationContext (this .wac );
135148 ExecutorService executor = Executors .newFixedThreadPool (4 );
136149 List <Future <Boolean >> results = new ArrayList <>();
137150 for (int i = 0 ; i < iterations ; i ++) {
@@ -160,7 +173,7 @@ public void nonInvocableScriptEngine() throws Exception {
160173 public void noRenderFunctionDefined () {
161174 this .view .setEngine (mock (InvocableScriptEngine .class ));
162175 try {
163- this .view .setApplicationContext (this .applicationContext );
176+ this .view .setApplicationContext (this .wac );
164177 fail ("Should have thrown IllegalArgumentException" );
165178 }
166179 catch (IllegalArgumentException ex ) {
@@ -174,7 +187,7 @@ public void engineAndEngineNameBothDefined() {
174187 this .view .setEngineName ("test" );
175188 this .view .setRenderFunction ("render" );
176189 try {
177- this .view .setApplicationContext (this .applicationContext );
190+ this .view .setApplicationContext (this .wac );
178191 fail ("Should have thrown IllegalArgumentException" );
179192 }
180193 catch (IllegalArgumentException ex ) {
@@ -188,7 +201,7 @@ public void engineSetterAndNonSharedEngine() {
188201 this .view .setRenderFunction ("render" );
189202 this .view .setSharedEngine (false );
190203 try {
191- this .view .setApplicationContext (this .applicationContext );
204+ this .view .setApplicationContext (this .wac );
192205 fail ("Should have thrown IllegalArgumentException" );
193206 }
194207 catch (IllegalArgumentException ex ) {
@@ -203,7 +216,7 @@ public void parentLoader() {
203216 this .view .setEngine (mock (InvocableScriptEngine .class ));
204217 this .view .setRenderFunction ("render" );
205218 this .view .setResourceLoaderPath (RESOURCE_LOADER_PATH );
206- this .view .setApplicationContext (this .applicationContext );
219+ this .view .setApplicationContext (this .wac );
207220 ClassLoader classLoader = this .view .createClassLoader ();
208221 assertNotNull (classLoader );
209222 URLClassLoader urlClassLoader = (URLClassLoader ) classLoader ;
@@ -219,6 +232,38 @@ public void parentLoader() {
219232 assertThat (Arrays .asList (urlClassLoader .getURLs ()).get (1 ).toString (), Matchers .endsWith ("org/springframework/web/servlet/view/" ));
220233 }
221234
235+ @ Test // SPR-13379
236+ public void contentType () throws Exception {
237+ MockServletContext servletContext = new MockServletContext ();
238+ this .wac .setServletContext (servletContext );
239+ this .wac .refresh ();
240+ MockHttpServletRequest request = new MockHttpServletRequest ();
241+ request .setAttribute (DispatcherServlet .WEB_APPLICATION_CONTEXT_ATTRIBUTE , this .wac );
242+ MockHttpServletResponse response = new MockHttpServletResponse ();
243+ Map <String , Object > model = new HashMap <String , Object >();
244+ this .view .setEngine (mock (InvocableScriptEngine .class ));
245+ this .view .setRenderFunction ("render" );
246+ this .view .setResourceLoaderPath (RESOURCE_LOADER_PATH );
247+ this .view .setApplicationContext (this .wac );
248+
249+ this .view .render (model , request , response );
250+ assertEquals (MediaType .TEXT_HTML_VALUE + ";charset=" +
251+ StandardCharsets .UTF_8 , response .getHeader (HttpHeaders .CONTENT_TYPE ));
252+
253+ response = new MockHttpServletResponse ();
254+ this .view .setContentType (MediaType .TEXT_PLAIN_VALUE );
255+ this .view .render (model , request , response );
256+ assertEquals (MediaType .TEXT_PLAIN_VALUE + ";charset=" +
257+ StandardCharsets .UTF_8 , response .getHeader (HttpHeaders .CONTENT_TYPE ));
258+
259+ response = new MockHttpServletResponse ();
260+ this .view .setCharset (StandardCharsets .ISO_8859_1 );
261+ this .view .render (model , request , response );
262+ assertEquals (MediaType .TEXT_PLAIN_VALUE + ";charset=" +
263+ StandardCharsets .ISO_8859_1 , response .getHeader (HttpHeaders .CONTENT_TYPE ));
264+
265+ }
266+
222267
223268 private interface InvocableScriptEngine extends ScriptEngine , Invocable {
224269 }
0 commit comments