2020import java .util .ArrayList ;
2121import java .util .HashMap ;
2222import java .util .List ;
23+ import java .util .Locale ;
2324import java .util .Map ;
2425import java .util .concurrent .ExecutorService ;
2526import java .util .concurrent .Executors ;
2829import javax .script .ScriptEngine ;
2930
3031import org .junit .Before ;
32+ import org .junit .Rule ;
3133import org .junit .Test ;
34+ import org .junit .rules .ExpectedException ;
3235
3336import org .springframework .beans .DirectFieldAccessor ;
3437import org .springframework .context .ApplicationContextException ;
@@ -58,6 +61,8 @@ public class ScriptTemplateViewTests {
5861
5962 private StaticWebApplicationContext wac ;
6063
64+ @ Rule
65+ public ExpectedException expectedException = ExpectedException .none ();
6166
6267 @ Before
6368 public void setup () {
@@ -68,15 +73,24 @@ public void setup() {
6873 }
6974
7075
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+
7189 @ Test
7290 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" ));
8094 }
8195
8296 @ Test
@@ -152,53 +166,37 @@ public void nonSharedEngine() throws Exception {
152166
153167 @ Test
154168 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" ));
162172 }
163173
164174 @ Test
165175 public void noRenderFunctionDefined () {
166176 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" ));
174180 }
175181
176182 @ Test
177183 public void engineAndEngineNameBothDefined () {
178184 this .view .setEngine (mock (InvocableScriptEngine .class ));
179185 this .view .setEngineName ("test" );
180186 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'" ));
188190 }
189191
190192 @ Test
191193 public void engineSetterAndNonSharedEngine () {
192194 this .view .setEngine (mock (InvocableScriptEngine .class ));
193195 this .view .setRenderFunction ("render" );
194196 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" ));
202200 }
203201
204202 @ Test // SPR-14210
0 commit comments