KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > TestPortletWebContext


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.portlet;
16
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.portlet.PortletContext;
22
23 import org.apache.tapestry.web.WebContext;
24 import org.easymock.MockControl;
25
26 /**
27  * @author Howard M. Lewis Ship
28  */

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