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