01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.spring;
16:
17: import javax.servlet.ServletContext;
18:
19: import org.apache.tapestry.test.TapestryTestCase;
20: import org.springframework.web.context.WebApplicationContext;
21: import org.testng.annotations.Test;
22:
23: public class TapestrySpringFilterTest extends TapestryTestCase {
24: @Test
25: public void no_web_application_context_in_servlet_context()
26: throws Exception {
27: ServletContext context = mockServletContext();
28:
29: expect(
30: context
31: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
32: .andReturn(null);
33:
34: replay();
35:
36: TapestrySpringFilter filter = new TapestrySpringFilter();
37:
38: try {
39: filter.provideExtraModuleDefs(context);
40: unreachable();
41: } catch (RuntimeException ex) {
42: assertEquals(
43: ex.getMessage(),
44: "The Spring WebApplicationContext is not present. "
45: + "The likely cause is that the org.springframework.web.context.ContextLoaderListener listener was not declared "
46: + "inside the application\'s web.xml deployment descriptor.");
47: }
48:
49: verify();
50: }
51:
52: protected final ServletContext mockServletContext() {
53: return newMock(ServletContext.class);
54: }
55:
56: @Test
57: public void failure_obtaining_context() throws Exception {
58: ServletContext context = mockServletContext();
59: Throwable t = new RuntimeException("Failure.");
60:
61: expect(
62: context
63: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
64: .andThrow(t);
65:
66: replay();
67:
68: TapestrySpringFilter filter = new TapestrySpringFilter();
69:
70: try {
71: filter.provideExtraModuleDefs(context);
72: unreachable();
73: } catch (RuntimeException ex) {
74: assertEquals(ex.getMessage(),
75: "An exception occurred obtaining the Spring WebApplicationContext: Failure.");
76:
77: assertSame(ex.getCause(), t);
78: }
79:
80: verify();
81: }
82: }
|