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