/* * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * "The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations under * the License. * * The Original Code is ICEfaces 1.5 open source software code, released * November 5, 2006. The Initial Developer of the Original Code is ICEsoft * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C) * 2004-2010 ICEsoft Technologies Canada, Corp. All Rights Reserved. * * Contributor(s): _____________________. * * Alternatively, the contents of this file may be used under the terms of * the GNU Lesser General Public License Version 2.1 or later (the "LGPL" * License), in which case the provisions of the LGPL License are * applicable instead of those above. If you wish to allow use of your * version of this file only under the terms of the LGPL License and not to * allow others to use your version of this file under the MPL, indicate * your decision by deleting the provisions above and replace them with * the notice and other provisions required by the LGPL License. If you do * not delete the provisions above, a recipient may use your version of * this file under either the MPL or the LGPL License." */ /* * limited scope mock objects test */ package com.icesoft.faces.mock.test.container; import com.icesoft.faces.metadata.ICECompsListHelper; import com.sun.faces.config.WebConfiguration; import com.sun.faces.mock.MockApplication; import com.icesoft.faces.mock.test.container.MockExternalContext; import com.sun.faces.mock.MockFacesContext; import com.sun.faces.mock.MockHttpServletRequest; import com.sun.faces.mock.MockHttpServletResponse; import com.sun.faces.mock.MockHttpSession; import com.sun.faces.mock.MockLifecycle; import com.sun.faces.mock.MockRenderKit; import com.sun.faces.mock.MockViewHandler; import com.sun.rave.jsfmeta.beans.RendererBean; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.faces.FactoryFinder; import javax.faces.application.ApplicationFactory; import javax.faces.application.ViewHandler; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.lifecycle.Lifecycle; import javax.faces.render.RenderKit; import javax.faces.render.RenderKitFactory; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import junit.framework.TestCase; public class MockTestCase extends TestCase { //Mock Container private MockApplication application; private MockViewHandler viewHandler; private MockServletConfig servletConfig; private MockExternalContext externalContext; private MockFacesContext facesContext; private Lifecycle lifecycle; private HttpServletRequest httpServletRequest; private HttpServletResponse httpServletResponse; private MockServletContext servletContext; private HttpSession httpSession; //TODO private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } protected void defaultMockContainer() { //Servlet API 2.4 servletContext = new MockServletContext(); servletConfig = new MockServletConfig(servletContext); httpSession = new MockHttpSession(servletContext); httpServletRequest = new MockHttpServletRequest(httpSession); httpServletResponse = new MockHttpServletResponse(); String appFactoryName = "com.sun.faces.mock.MockApplicationFactory"; FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, appFactoryName); String renderKitFactoryName = "com.sun.faces.mock.MockRenderKitFactory"; FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY, renderKitFactoryName); externalContext = new MockExternalContext(servletContext, httpServletRequest, httpServletResponse); Map myRequestParameterMap = new HashMap(); myRequestParameterMap.put("clientid_inputText","event"); externalContext.setRequestParameterMap(myRequestParameterMap); lifecycle = new MockLifecycle(); facesContext = new MockFacesContext(externalContext, lifecycle); ApplicationFactory applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); application = (MockApplication) applicationFactory.getApplication(); facesContext.setApplication(application); //TODO: WebConfiguration webConfig = WebConfiguration.getInstance(servletContext); viewHandler = new MockViewHandler(); application.setViewHandler(viewHandler); //TODO: RenderKitFactory renderKitFactory = (RenderKitFactory)FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); RenderKit renderkit = new MockRenderKit(); renderKitFactory.addRenderKit(renderKitFactoryName, renderkit); renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, renderkit); } @Override protected void setUp() throws Exception { defaultMockContainer(); } @Override protected void tearDown() throws Exception { application = null; viewHandler = null; servletConfig = null; externalContext = null; facesContext = null; lifecycle = null; httpServletRequest = null; httpServletResponse = null; servletContext = null; httpSession = null; properties = null; } public ViewHandler getViewHandler() { return viewHandler; } protected FacesContext getFacesContext() { return facesContext; } protected Object invokePrivateMethod(String methodName, Class[] params, Object[] args, Class className, Object invocationTarget) { try { Method method = className.getDeclaredMethod(methodName, params); method.setAccessible(true); return method.invoke(invocationTarget, args); } catch (IllegalArgumentException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } return null; } protected Object getPrivateFieldValue(Object instance, Class className, String fieldName){ try { Field field = className.getDeclaredField(fieldName); field.setAccessible(true); return field.get(instance); } catch (NoSuchFieldException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, className.getName()+" "+ex.getMessage(), ex); } catch (SecurityException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } return null; } protected void setPrivateFieldValue(Object instance, Class className, String fieldName, Object value){ try { Field field = className.getDeclaredField(fieldName); field.setAccessible(true); field.set(instance, value); } catch (NoSuchFieldException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, className.getName()+" "+ex.getMessage(), ex); } catch (SecurityException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(MockTestCase.class.getName()).log(Level.SEVERE, null, ex); } } public UIComponent[] getUIComponents(){ return ICECompsListHelper.getComponents(); } public RendererBean getRendererBean(String componentFamily, String rendererType){ return ICECompsListHelper.getRenderer(componentFamily, rendererType); } }