20
20
import java .util .ArrayList ;
21
21
import java .util .HashMap ;
22
22
import java .util .List ;
23
+ import java .util .Locale ;
23
24
import java .util .Map ;
24
25
import java .util .concurrent .ExecutorService ;
25
26
import java .util .concurrent .Executors ;
28
29
import javax .script .ScriptEngine ;
29
30
30
31
import org .junit .Before ;
32
+ import org .junit .Rule ;
31
33
import org .junit .Test ;
34
+ import org .junit .rules .ExpectedException ;
32
35
33
36
import org .springframework .beans .DirectFieldAccessor ;
34
37
import org .springframework .context .ApplicationContextException ;
@@ -58,6 +61,8 @@ public class ScriptTemplateViewTests {
58
61
59
62
private StaticWebApplicationContext wac ;
60
63
64
+ @ Rule
65
+ public ExpectedException expectedException = ExpectedException .none ();
61
66
62
67
@ Before
63
68
public void setup () {
@@ -68,15 +73,24 @@ public void setup() {
68
73
}
69
74
70
75
76
+ @ Test
77
+ public void missingTemplate () throws Exception {
78
+ MockServletContext servletContext = new MockServletContext ();
79
+ this .wac .setServletContext (servletContext );
80
+ this .wac .refresh ();
81
+ this .view .setResourceLoaderPath ("classpath:org/springframework/web/servlet/view/script/" );
82
+ this .view .setUrl ("missing.txt" );
83
+ this .view .setEngine (mock (InvocableScriptEngine .class ));
84
+ this .configurer .setRenderFunction ("render" );
85
+ this .view .setApplicationContext (this .wac );
86
+ assertFalse (this .view .checkResource (Locale .ENGLISH ));
87
+ }
88
+
71
89
@ Test
72
90
public void missingScriptTemplateConfig () throws Exception {
73
- try {
74
- this .view .setApplicationContext (new StaticApplicationContext ());
75
- fail ("Should have thrown ApplicationContextException" );
76
- }
77
- catch (ApplicationContextException ex ) {
78
- assertTrue (ex .getMessage ().contains ("ScriptTemplateConfig" ));
79
- }
91
+ this .expectedException .expect (ApplicationContextException .class );
92
+ this .view .setApplicationContext (new StaticApplicationContext ());
93
+ this .expectedException .expectMessage (contains ("ScriptTemplateConfig" ));
80
94
}
81
95
82
96
@ Test
@@ -152,53 +166,37 @@ public void nonSharedEngine() throws Exception {
152
166
153
167
@ Test
154
168
public void nonInvocableScriptEngine () throws Exception {
155
- try {
156
- this .view .setEngine (mock (ScriptEngine .class ));
157
- fail ("Should have thrown IllegalArgumentException" );
158
- }
159
- catch (IllegalArgumentException ex ) {
160
- assertThat (ex .getMessage (), containsString ("instance" ));
161
- }
169
+ this .expectedException .expect (IllegalArgumentException .class );
170
+ this .view .setEngine (mock (ScriptEngine .class ));
171
+ this .expectedException .expectMessage (contains ("instance" ));
162
172
}
163
173
164
174
@ Test
165
175
public void noRenderFunctionDefined () {
166
176
this .view .setEngine (mock (InvocableScriptEngine .class ));
167
- try {
168
- this .view .setApplicationContext (this .wac );
169
- fail ("Should have thrown IllegalArgumentException" );
170
- }
171
- catch (IllegalArgumentException ex ) {
172
- assertThat (ex .getMessage (), containsString ("renderFunction" ));
173
- }
177
+ this .expectedException .expect (IllegalArgumentException .class );
178
+ this .view .setApplicationContext (this .wac );
179
+ this .expectedException .expectMessage (contains ("renderFunction" ));
174
180
}
175
181
176
182
@ Test
177
183
public void engineAndEngineNameBothDefined () {
178
184
this .view .setEngine (mock (InvocableScriptEngine .class ));
179
185
this .view .setEngineName ("test" );
180
186
this .view .setRenderFunction ("render" );
181
- try {
182
- this .view .setApplicationContext (this .wac );
183
- fail ("Should have thrown IllegalArgumentException" );
184
- }
185
- catch (IllegalArgumentException ex ) {
186
- assertThat (ex .getMessage (), containsString ("'engine' or 'engineName'" ));
187
- }
187
+ this .expectedException .expect (IllegalArgumentException .class );
188
+ this .view .setApplicationContext (this .wac );
189
+ this .expectedException .expectMessage (contains ("'engine' or 'engineName'" ));
188
190
}
189
191
190
192
@ Test
191
193
public void engineSetterAndNonSharedEngine () {
192
194
this .view .setEngine (mock (InvocableScriptEngine .class ));
193
195
this .view .setRenderFunction ("render" );
194
196
this .view .setSharedEngine (false );
195
- try {
196
- this .view .setApplicationContext (this .wac );
197
- fail ("Should have thrown IllegalArgumentException" );
198
- }
199
- catch (IllegalArgumentException ex ) {
200
- assertThat (ex .getMessage (), containsString ("sharedEngine" ));
201
- }
197
+ this .expectedException .expect (IllegalArgumentException .class );
198
+ this .view .setApplicationContext (this .wac );
199
+ this .expectedException .expectMessage (contains ("sharedEngine" ));
202
200
}
203
201
204
202
@ Test // SPR-14210
0 commit comments