/**
 * 
 */
package org.ajax4jsf.portlet.context;

import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import org.ajax4jsf.portlet.MockActionRequest;
import org.ajax4jsf.portlet.MockActionResponse;
import org.ajax4jsf.portlet.MockPortletContext;
import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
import junit.framework.TestCase;

/**
 * @author asmirnov
 * 
 */
public class ContextFactoryTest extends AbstractAjax4JsfTestCase {
    /**
         * @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.ajax4jsf.portlet.context.FacesContextFactoryImpl#FacesContextFactoryImpl(javax.faces.context.FacesContextFactory)}.
         */
    public void testFacesContextFactoryImpl() {
	FacesContextFactoryImpl factory = new FacesContextFactoryImpl();
	FacesContext context = factory.getFacesContext(servletContext, request,
		response, lifecycle);
	assertTrue((context instanceof FacesContextImpl));
	assertTrue((context.getExternalContext() instanceof ServletContextImpl));
    }

    /**
         * Test method for
         * {@link org.ajax4jsf.portlet.context.FacesContextFactoryImpl#getFacesContext(java.lang.Object, java.lang.Object, java.lang.Object, javax.faces.lifecycle.Lifecycle)}.
         */
    public void testGetFacesContextPortlet() {
	FacesContextFactoryImpl factory = new FacesContextFactoryImpl();
	MockPortletContext portletContext = new MockPortletContext();
	MockActionRequest portletRequest = new MockActionRequest();
	MockActionResponse portletResponse = new MockActionResponse();
	FacesContext context = factory.getFacesContext(portletContext,
		portletRequest, portletResponse, lifecycle);
	assertTrue((context instanceof FacesContextImpl));
	assertTrue((context.getExternalContext() instanceof PortletContextImpl));
	assertSame(FacesContext.getCurrentInstance(),context);
    }

    public void testIllegalEnvironment() throws Exception {
	FacesContextFactoryImpl factory = new FacesContextFactoryImpl();
	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
	}
    }
}
