/******************************************************************************
* JBoss, a division of Red Hat *
* Copyright 2006, Red Hat Middleware, LLC, and individual *
* contributors as indicated by the @authors tag. See the *
* copyright.txt in the distribution for a full listing of *
* individual contributors. *
* *
* This is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2.1 of *
* the License, or (at your option) any later version. *
* *
* This software is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this software; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
******************************************************************************/
package org.jboss.portletbridge.context;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import javax.faces.FacesException;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.context.FacesContext;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.portlet.faces.Bridge;
import javax.xml.namespace.QName;
import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
import org.jboss.portletbridge.BridgeConfig;
import org.jboss.portletbridge.BridgeRequestScope;
import org.jboss.portletbridge.BridgeStrategy;
import org.jboss.portletbridge.ExcludedRequestAttribute;
import org.jboss.portletbridge.MockActionRequest;
import org.jboss.portletbridge.MockActionResponse;
import org.jboss.portletbridge.MockPortletContext;
import org.jboss.portletbridge.RequestScopeManager;
import org.jboss.portletbridge.StateId;
/**
* @author asmirnov
*
*/
public class ContextFactoryTest extends AbstractAjax4JsfTestCase {
private static final StateId STATE_ID = new StateId("1234foo:edit:12345");
/**
* @param arg0
*/
public ContextFactoryTest(String arg0) {
super(arg0);
}
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#setUp()
*/
public void setUp() throws Exception {
super.setUp();
}
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#tearDown()
*/
public void tearDown() throws Exception {
super.tearDown();
}
/**
* Test method for
* {@link org.jboss.portletbridge.context.FacesContextFactoryImpl#FacesContextFactoryImpl(javax.faces.context.FacesContextFactory)}.
*/
public void testFacesContextFactoryImpl() {
FacesContextFactoryImpl factory = new FacesContextFactoryImpl(facesContextFactory);
FacesContext context = factory.getFacesContext(servletContext, request,
response, lifecycle);
assertFalse((context instanceof FacesContextImpl));
assertFalse((context.getExternalContext() instanceof AbstractExternalContext));
}
/**
* Test method for
* {@link org.jboss.portletbridge.context.FacesContextFactoryImpl#getFacesContext(java.lang.Object, java.lang.Object, java.lang.Object, javax.faces.lifecycle.Lifecycle)}.
*/
public void testGetFacesContextPortlet() {
FacesContextFactoryImpl factory = new FacesContextFactoryImpl(facesContextFactory);
MockPortletContext portletContext = new MockPortletContext(servletContext);
MockActionRequest portletRequest = new MockActionRequest(portletContext);
MockActionResponse portletResponse = new MockActionResponse();
RequestScopeManager portletStateHolder = RequestScopeManager.getInstance(facesContext);
BridgeRequestScope portletState = new BridgeRequestScope();
portletStateHolder.saveRequestScope(STATE_ID, portletState);
PortletBridgeContext bridgeContext = new PortletBridgeContext(getBridgeConfig());
portletRequest.setAttribute(PortletBridgeContext.REQUEST_PARAMETER_NAME, bridgeContext);
portletRequest.setAttribute(Bridge.PORTLET_LIFECYCLE_PHASE, Bridge.PortletPhase.ACTION_PHASE);
FacesContext context = factory.getFacesContext(portletContext,
portletRequest, portletResponse, lifecycle);
assertTrue((context instanceof FacesContextImpl));
assertTrue((context.getExternalContext() instanceof AbstractExternalContext));
assertSame(FacesContext.getCurrentInstance(),context);
}
public void testIllegalEnvironment() throws Exception {
FacesContextFactoryImpl factory = new FacesContextFactoryImpl(facesContextFactory);
try {
FacesContext context = factory.getFacesContext(null, request,
response, lifecycle);
assertTrue("null pointer passed", false);
} catch (NullPointerException e) {
}
try {
FacesContext context = factory.getFacesContext(facesContext,
request, response, lifecycle);
assertTrue("illegal context passed", false);
} catch (FacesException e) {
// TODO: handle exception
} catch (IllegalArgumentException e){
}
}
public Map<String, String> getErrorPages() {
return null;
}
public Lifecycle getFacesLifecycle() {
return null;
}
public BridgeConfig getBridgeConfig() {
return new BridgeConfig(){
public Map<String, String> getDefaultViewIdMap() {
HashMap<String, String> viewIdMap = new HashMap<String,String>();
viewIdMap.put("view", "/foo.xhtml");
return viewIdMap;
}
public Set<ExcludedRequestAttribute> getExcludedAttributes() {
// TODO Auto-generated method stub
return null;
}
public List<String> getFacesServletMappings() {
// TODO Auto-generated method stub
return null;
}
public String getInitParameter(String name) {
// TODO Auto-generated method stub
return null;
}
public PortletConfig getPortletConfig() {
return new PortletConfig() {
public Enumeration<Locale> getSupportedLocales() {
// TODO Auto-generated method stub
return null;
}
public ResourceBundle getResourceBundle(Locale locale) {
// TODO Auto-generated method stub
return null;
}
public Enumeration<QName> getPublishingEventQNames() {
// TODO Auto-generated method stub
return null;
}
public Enumeration<String> getPublicRenderParameterNames() {
// TODO Auto-generated method stub
return null;
}
public Enumeration<QName> getProcessingEventQNames() {
// TODO Auto-generated method stub
return null;
}
public String getPortletName() {
// TODO Auto-generated method stub
return null;
}
public PortletContext getPortletContext() {
// TODO Auto-generated method stub
return null;
}
public Enumeration<String> getInitParameterNames() {
// TODO Auto-generated method stub
return null;
}
public String getInitParameter(String name) {
// TODO Auto-generated method stub
return null;
}
public String getDefaultNamespace() {
// TODO Auto-generated method stub
return null;
}
public Map<String, String[]> getContainerRuntimeOptions() {
// TODO Auto-generated method stub
return null;
}
};
}
public String getPortletName() {
// TODO Auto-generated method stub
return null;
}
public boolean isPreserveActionParams() {
// TODO Auto-generated method stub
return false;
}
public Map<Class<? extends Throwable>, String> getErrorPages() {
// TODO Auto-generated method stub
return null;
}
public Lifecycle getFacesLifecycle() {
return null;
}
public int getNumberOfRequestScopes() {
// TODO Auto-generated method stub
return 2;
}
public FacesContext createFacesContext(Object request, Object response) {
// TODO Auto-generated method stub
return facesContext;
}
public BridgeStrategy getStrategy() {
// TODO Auto-generated method stub
return null;
}
public Map<String, String> getFilterInitParams(String className) {
// TODO Auto-generated method stub
return null;
}
};
}
public int getNumberOfRequestScopes() {
return 0;
}
public FacesContext getFacesContext(Object request, Object response) {
return null;
}
}