001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.jsf;
018
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022
023import javax.el.ELContext;
024import javax.el.ELException;
025import javax.el.ExpressionFactory;
026import javax.faces.application.Application;
027import javax.faces.application.FacesMessage;
028import javax.faces.application.FacesMessage.Severity;
029import javax.faces.component.UIViewRoot;
030import javax.faces.context.ExternalContext;
031import javax.faces.context.FacesContext;
032import javax.faces.context.ResponseStream;
033import javax.faces.context.ResponseWriter;
034import javax.faces.render.RenderKit;
035
036import org.nuxeo.ecm.platform.el.ExpressionContext;
037
038/**
039 * Mock faces context that can be used to resolve expressions in tests.
040 * <p>
041 * Usage:
042 *
043 * <pre>
044 * MockFacesContext facesContext = new MockFacesContext() {
045 *     public Object evaluateExpressionGet(FacesContext context, String expression, Class expectedType) throws ELException {
046 *         if (&quot;#{myTestExpression}&quot;.equals(expression)) {
047 *             return myTestResult;
048 *         }
049 *         return null;
050 *     }
051 * };
052 * facesContext.setCurrent();
053 * assertNotNull(FacesContext.getCurrentInstance());
054 * </pre>
055 *
056 * @author Anahide Tchertchian
057 */
058public class MockFacesContext extends FacesContext {
059
060    protected Application app = new MockApplication();
061
062    protected Map<String, Object> variables = new HashMap<String, Object>();
063
064    protected Map<String, Object> expressions = new HashMap<String, Object>();
065
066    public void mapVariable(String key, Object value) {
067        variables.put(key, value);
068    }
069
070    public void resetVariables() {
071        variables.clear();
072    }
073
074    public void mapExpression(String expr, Object res) {
075        expressions.put(expr, res);
076    }
077
078    public void resetExpressions() {
079        expressions.clear();
080    }
081
082    public void setCurrent() {
083        setCurrentInstance(this);
084    }
085
086    public void relieveCurrent() {
087        setCurrentInstance(null);
088    }
089
090    @Override
091    public Application getApplication() {
092        return app;
093    }
094
095    @Override
096    public ELContext getELContext() {
097        ELContext ctx = new ExpressionContext();
098        ExpressionFactory ef = getApplication().getExpressionFactory();
099        for (Map.Entry<String, Object> var : variables.entrySet()) {
100            ctx.getVariableMapper().setVariable(var.getKey(), ef.createValueExpression(var.getValue(), Object.class));
101        }
102        return ctx;
103    }
104
105    protected Object evaluateExpression(FacesContext context, String expression) {
106        if (expressions.containsKey(expression)) {
107            return expressions.get(expression);
108        } else {
109            ExpressionFactory ef = context.getApplication().getExpressionFactory();
110            ELContext elCtx = getELContext();
111            return ef.createValueExpression(elCtx, expression, Object.class).getValue(elCtx);
112        }
113    }
114
115    @Deprecated
116    @SuppressWarnings("rawtypes")
117    public Object evaluateExpressionGet(FacesContext context, String expression, Class expectedType) throws ELException {
118        return evaluateExpression(context, expression);
119    }
120
121    @Override
122    public void addMessage(String clientId, FacesMessage message) {
123    }
124
125    @Override
126    public Iterator<String> getClientIdsWithMessages() {
127        return null;
128    }
129
130    @Override
131    public ExternalContext getExternalContext() {
132        return null;
133    }
134
135    @Override
136    public Severity getMaximumSeverity() {
137        return null;
138    }
139
140    @Override
141    public Iterator<FacesMessage> getMessages() {
142        return null;
143    }
144
145    @Override
146    public Iterator<FacesMessage> getMessages(String clientId) {
147        return null;
148    }
149
150    @Override
151    public RenderKit getRenderKit() {
152        return null;
153    }
154
155    @Override
156    public boolean getRenderResponse() {
157        return false;
158    }
159
160    @Override
161    public boolean getResponseComplete() {
162        return false;
163    }
164
165    @Override
166    public ResponseStream getResponseStream() {
167        return null;
168    }
169
170    @Override
171    public ResponseWriter getResponseWriter() {
172        return null;
173    }
174
175    @Override
176    public UIViewRoot getViewRoot() {
177        return null;
178    }
179
180    @Override
181    public void release() {
182    }
183
184    @Override
185    public void renderResponse() {
186    }
187
188    @Override
189    public void responseComplete() {
190    }
191
192    @Override
193    public void setResponseStream(ResponseStream responseStream) {
194    }
195
196    @Override
197    public void setResponseWriter(ResponseWriter responseWriter) {
198    }
199
200    @Override
201    public void setViewRoot(UIViewRoot root) {
202    }
203
204}