KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > mocks > jsf > MockFacesContext


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.test.mocks.jsf;
6
7 import java.util.* ;
8 import java.io.IOException JavaDoc ;
9 import java.io.PrintWriter JavaDoc ;
10
11 import javax.faces.render.* ;
12 import javax.faces.application.* ;
13 import javax.faces.context.* ;
14 import javax.faces.event.FacesEvent;
15 import javax.faces.component.UIViewRoot ;
16 import javax.servlet.http.* ;
17
18 public class MockFacesContext extends FacesContext {
19     
20   protected MockExternalContext externalContext_;
21   protected Locale locale_ = null;
22   protected UIViewRoot viewRoot_ = null;
23   protected List facesEvents_ = null;
24   protected Map facesMessages_ = null;
25   protected int maximumSeverity_ = 0;
26   protected ResponseStream responseStream_ = null;
27   protected ResponseWriter responseWriter_ = null;
28   protected boolean renderResponse_ = false;
29   protected boolean responseComplete_ = false;
30   protected Application application_ ;
31   protected boolean renderPhase_ ;
32  
33   private FacesContext portalFacesContext;
34
35   public MockFacesContext() {
36      externalContext_ = new MockExternalContext();
37   }
38   
39   public MockFacesContext(HttpServletRequest req, HttpServletResponse res ) throws IOException JavaDoc {
40     init(req, res) ;
41   }
42   
43   public void init(HttpServletRequest req, HttpServletResponse res) throws IOException JavaDoc {
44     renderPhase_ = false ;
45     responseWriter_ = null ;
46     externalContext_.init(req, res) ;
47     PrintWriter JavaDoc writer = res.getWriter() ;
48     responseWriter_ = new MockResponseWriter(writer) ;
49     renderResponse_ = false;
50   }
51   
52   public void reset() { externalContext_.reset() ; }
53   
54   public void release() {
55   }
56
57   public ExternalContext getExternalContext() { return externalContext_; }
58
59   public Locale getLocale() {
60     if (locale_ == null) {
61       locale_ = getExternalContext().getRequestLocale();
62     }
63     return locale_;
64   }
65
66   public void setLocale( Locale locale ) { locale_ = locale; }
67
68   public UIViewRoot getViewRoot() { return viewRoot_ ; }
69   
70   public void setViewRoot(UIViewRoot viewRoot) { viewRoot_ = viewRoot; }
71
72
73   public void addMessage(String JavaDoc clientId, FacesMessage message) {
74   }
75
76   public Iterator getMessages() { return null ; }
77
78
79   public Iterator getMessages(String JavaDoc clientId) { return null ; }
80
81   public Iterator getFacesEvents() { return null ; }
82
83   public void addFacesEvent( FacesEvent event ) {
84     if (facesEvents_ == null) facesEvents_ = new ArrayList();
85     facesEvents_.add( event );
86   }
87
88   public ResponseStream getResponseStream() {
89     return responseStream_;
90   }
91
92   public void setResponseStream( ResponseStream responseStream ) {
93     responseStream_ = responseStream;
94   }
95
96   public ResponseWriter getResponseWriter() {
97     return responseWriter_;
98   }
99
100   public void setResponseWriter( ResponseWriter responseWriter ) {
101     responseWriter_ = responseWriter;
102   }
103
104
105   public FacesMessage.Severity getMaximumSeverity() {
106     return null ;
107   }
108
109   public RenderKit getRenderKit() { return null ; }
110
111   public Iterator getClientIdsWithMessages() {
112     return null ;
113   }
114
115   public Application getApplication() {
116     if (application_ == null ) application_ = new MockApplication() ;
117     return application_ ;
118   }
119   
120   public void renderResponse() { renderResponse_ = true; }
121
122   public boolean getRenderResponse() { return renderResponse_; }
123
124   public void responseComplete() { responseComplete_ = true; }
125
126   public boolean getResponseComplete() { return responseComplete_; }
127
128   public FacesContext getPortalFacesContext() { return portalFacesContext ; }
129
130   public boolean isPortletRenderPhase() { return renderPhase_ ; }
131   
132   public static MockFacesContext getMockFacesContextCurrentInstance() {
133     MockFacesContext context = (MockFacesContext)FacesContext.getCurrentInstance() ;
134     if(context == null) {
135         context= new MockFacesContext() ;
136         FacesContext.setCurrentInstance(context) ;
137     }
138     return context ;
139   }
140 }
141
Popular Tags