KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > web > TestServletWebContext


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.web;
16
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.apache.tapestry.web.ServletWebContext;
24 import org.apache.tapestry.web.WebContext;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.web.ServletWebContext}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestServletWebContext extends BaseWebTestCase
34 {
35
36     public void testGetInitParameterNames()
37     {
38         MockControl control = newControl(ServletContext JavaDoc.class);
39         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
40
41         context.getInitParameterNames();
42         control.setReturnValue(newEnumeration());
43
44         replayControls();
45
46         WebContext wc = new ServletWebContext(context);
47
48         List JavaDoc l = wc.getInitParameterNames();
49
50         checkList(l);
51
52         verifyControls();
53     }
54
55     public void testGetInitParameterValue()
56     {
57         String JavaDoc value = "William Orbit";
58
59         MockControl control = newControl(ServletContext JavaDoc.class);
60         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
61
62         context.getInitParameter("artist");
63         control.setReturnValue(value);
64
65         replayControls();
66
67         WebContext wc = new ServletWebContext(context);
68
69         assertSame(value, wc.getInitParameterValue("artist"));
70
71         verifyControls();
72     }
73
74     public void testGetAttributeNames()
75     {
76         MockControl control = newControl(ServletContext JavaDoc.class);
77         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
78
79         context.getAttributeNames();
80         control.setReturnValue(newEnumeration());
81
82         replayControls();
83
84         WebContext wc = new ServletWebContext(context);
85
86         List JavaDoc l = wc.getAttributeNames();
87
88         checkList(l);
89
90         verifyControls();
91     }
92
93     public void testGetAttribute()
94     {
95         Object JavaDoc attribute = new Object JavaDoc();
96
97         MockControl control = newControl(ServletContext JavaDoc.class);
98         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
99
100         context.getAttribute("attr");
101         control.setReturnValue(attribute);
102
103         replayControls();
104
105         WebContext wc = new ServletWebContext(context);
106
107         assertSame(attribute, wc.getAttribute("attr"));
108
109         verifyControls();
110     }
111
112     public void testSetAttribute()
113     {
114         Object JavaDoc attribute = new Object JavaDoc();
115
116         MockControl control = newControl(ServletContext JavaDoc.class);
117         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
118
119         context.setAttribute("name", attribute);
120
121         replayControls();
122
123         WebContext wc = new ServletWebContext(context);
124
125         wc.setAttribute("name", attribute);
126
127         verifyControls();
128     }
129
130     public void testSetAttributeToNull()
131     {
132         MockControl control = newControl(ServletContext JavaDoc.class);
133         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
134
135         context.removeAttribute("tonull");
136
137         replayControls();
138
139         WebContext wc = new ServletWebContext(context);
140
141         wc.setAttribute("tonull", null);
142
143         verifyControls();
144     }
145
146     public void testGetResource() throws Exception JavaDoc
147     {
148         URL JavaDoc url = new URL JavaDoc("http://jakarta.apache.org/tapestry");
149
150         MockControl control = newControl(ServletContext JavaDoc.class);
151         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
152
153         context.getResource("/tapestry");
154         control.setReturnValue(url);
155
156         replayControls();
157
158         WebContext wc = new ServletWebContext(context);
159
160         assertSame(url, wc.getResource("/tapestry"));
161
162         verifyControls();
163     }
164
165     public void testGetResourceFailure() throws Exception JavaDoc
166     {
167         Throwable JavaDoc t = new MalformedURLException JavaDoc("Like this ever happens.");
168
169         MockControl control = newControl(ServletContext JavaDoc.class);
170         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
171
172         context.getResource("/tapestry");
173         control.setThrowable(t);
174
175         replayControls();
176
177         interceptLogging(ServletWebContext.class.getName());
178
179         WebContext wc = new ServletWebContext(context);
180
181         assertNull(wc.getResource("/tapestry"));
182
183         verifyControls();
184
185         assertLoggedMessage("Error getting context resource '/tapestry': Like this ever happens.");
186     }
187 }
Popular Tags