2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.mock;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.faces.FactoryFinder;
27  import javax.faces.application.Application;
28  import javax.faces.application.FacesMessage;
29  import javax.faces.application.FacesMessage.Severity;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.context.ExternalContext;
32  import javax.faces.context.FacesContext;
33  import javax.faces.context.ResponseStream;
34  import javax.faces.context.ResponseWriter;
35  import javax.faces.lifecycle.Lifecycle;
36  import javax.faces.render.RenderKit;
37  import javax.faces.render.RenderKitFactory;
38  
39  
40  /***
41   * <p>Mock implementation of <code>FacesContext</code>.</p>
42   *
43   * $Id$
44   */
45  
46  public class MockFacesContext extends FacesContext {
47  
48  
49      // ------------------------------------------------------------ Constructors
50  
51  
52      public MockFacesContext() {
53          super();
54          setCurrentInstance(this);
55      }
56  
57  
58      public MockFacesContext(ExternalContext externalContext) {
59          setExternalContext(externalContext);
60          setCurrentInstance(this);
61      }
62  
63  
64      public MockFacesContext(ExternalContext externalContext, Lifecycle lifecycle) {
65          this(externalContext);
66          this.lifecycle = lifecycle;
67      }
68  
69  
70      // ----------------------------------------------------- Mock Object Methods
71  
72  
73      /***
74       * <p>Set the <code>Application</code> instance for this instance.</p>
75       *
76       * @param application The new Application
77       */
78      public void setApplication(Application application) {
79  
80          this.application = application;
81  
82      }
83  
84  
85      /***
86       * <p>Set the <code>ExternalContext</code> instance for this instance.</p>
87       *
88       * @param externalContext The new ExternalContext
89       */
90      public void setExternalContext(ExternalContext externalContext) {
91  
92          this.externalContext = externalContext;
93  
94      }
95  
96  
97      /***
98       * <p>Set the <code>FacesContext</code> instance for this instance.</p>
99       *
100      * @param facesContext The new FacesContext
101      */
102     public static void setCurrentInstance(FacesContext facesContext) {
103 
104         FacesContext.setCurrentInstance(facesContext);
105 
106     }
107 
108 
109     // ------------------------------------------------------ Instance Variables
110 
111 
112     private Application application = null;
113     private ExternalContext externalContext = null;
114     private Lifecycle lifecycle = null;
115     private Map messages = new HashMap();
116     private boolean renderResponse = false;
117     private boolean responseComplete = false;
118     private ResponseStream responseStream = null;
119     private ResponseWriter responseWriter = null;
120     private UIViewRoot viewRoot = null;
121 
122 
123     // ---------------------------------------------------- FacesContext Methods
124 
125 
126     /*** {@inheritDoc} */
127     public Application getApplication() {
128 
129         return this.application;
130 
131     }
132 
133 
134     /*** {@inheritDoc} */
135     public Iterator getClientIdsWithMessages() {
136 
137         return messages.keySet().iterator();
138 
139     }
140 
141 
142     /*** {@inheritDoc} */
143     public ExternalContext getExternalContext() {
144 
145         return this.externalContext;
146 
147     }
148 
149 
150     /*** {@inheritDoc} */
151     public Severity getMaximumSeverity() {
152 
153         Severity severity = null;
154         Iterator messages = getMessages();
155         while (messages.hasNext()) {
156             FacesMessage message = (FacesMessage) messages.next();
157             if (severity == null) {
158                 severity = message.getSeverity();
159             } else if (message.getSeverity().getOrdinal() > severity.getOrdinal()) {
160                 severity = message.getSeverity();
161             }
162         }
163         return severity;
164 
165     }
166 
167 
168     /*** {@inheritDoc} */
169     public Iterator getMessages() {
170 
171         ArrayList results = new ArrayList();
172         Iterator clientIds = messages.keySet().iterator();
173         while (clientIds.hasNext()) {
174             String clientId = (String) clientIds.next();
175             results.addAll((List) messages.get(clientId));
176         }
177         return results.iterator();
178 
179     }
180 
181 
182     /*** {@inheritDoc} */
183     public Iterator getMessages(String clientId) {
184 
185         List list = (List) messages.get(clientId);
186         if (list == null) {
187             list = new ArrayList();
188         }
189         return list.iterator();
190 
191     }
192 
193 
194     /*** {@inheritDoc} */
195     public RenderKit getRenderKit() {
196 
197         UIViewRoot vr = getViewRoot();
198         if (vr == null) {
199             return null;
200         }
201         String renderKitId = vr.getRenderKitId();
202         if (renderKitId == null) {
203             return null;
204         }
205         RenderKitFactory rkFactory = (RenderKitFactory)
206             FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
207         return rkFactory.getRenderKit(this, renderKitId);
208 
209     }
210 
211 
212     /*** {@inheritDoc} */
213     public boolean getRenderResponse() {
214 
215         return this.renderResponse;
216 
217     }
218 
219 
220     /*** {@inheritDoc} */
221     public boolean getResponseComplete() {
222 
223         return this.responseComplete;
224 
225     }
226 
227 
228     /*** {@inheritDoc} */
229     public ResponseStream getResponseStream() {
230 
231         return this.responseStream;
232 
233     }
234 
235 
236     /*** {@inheritDoc} */
237     public void setResponseStream(ResponseStream responseStream) {
238 
239         this.responseStream = responseStream;
240 
241     }
242 
243 
244     /*** {@inheritDoc} */
245     public ResponseWriter getResponseWriter() {
246 
247         return this.responseWriter;
248 
249     }
250 
251 
252     /*** {@inheritDoc} */
253     public void setResponseWriter(ResponseWriter responseWriter) {
254 
255         this.responseWriter = responseWriter;
256 
257     }
258 
259 
260     /*** {@inheritDoc} */
261     public UIViewRoot getViewRoot() {
262 
263         return this.viewRoot;
264 
265     }
266 
267 
268     /*** {@inheritDoc} */
269     public void setViewRoot(UIViewRoot viewRoot) {
270 
271         this.viewRoot = viewRoot;
272 
273     }
274 
275 
276     /*** {@inheritDoc} */
277     public void addMessage(String clientId, FacesMessage message) {
278 
279         if (message == null) {
280             throw new NullPointerException();
281         }
282         List list = (List) messages.get(clientId);
283         if (list == null) {
284             list = new ArrayList();
285             messages.put(clientId, list);
286         }
287         list.add(message);
288 
289     }
290 
291 
292     /*** {@inheritDoc} */
293     public void release() {
294 
295         application = null;
296         externalContext = null;
297         messages.clear();
298         renderResponse = false;
299         responseComplete = false;
300         responseStream = null;
301         responseWriter = null;
302         viewRoot = null;
303         setCurrentInstance(null);
304 
305     }
306 
307 
308     /*** {@inheritDoc} */
309     public void renderResponse() {
310 
311         this.renderResponse = true;
312 
313     }
314 
315 
316     /*** {@inheritDoc} */
317     public void responseComplete() {
318 
319         this.responseComplete = true;
320 
321     }
322 
323 
324 }