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

import java.util.Iterator;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;

/**
 * @author asmirnov
 *
 */
public class FacesContextImplTest extends AbstractAjax4JsfTestCase {
    private static final String ID0 = "_id0";
    protected FacesContextImpl portalFacesContext;
    protected ServletContextImpl servletContextImpl;

    /**
     * @param arg0
     */
    public FacesContextImplTest(String arg0) {
	super(arg0);
    }

    /* (non-Javadoc)
     * @see org.ajax4jsf.tests.AbstractAjax4JsfTestCase#setUp()
     */
    public void setUp() throws Exception {
	super.setUp();
	servletContextImpl = new ServletContextImpl(servletContext,request,response);
	portalFacesContext = new FacesContextImpl(servletContextImpl,lifecycle);
	
    }

    /* (non-Javadoc)
     * @see org.ajax4jsf.tests.AbstractAjax4JsfTestCase#tearDown()
     */
    public void tearDown() throws Exception {
	super.tearDown();
//	portalFacesContext.release();
	servletContextImpl = null;
	portalFacesContext = null;
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#release()}.
     */
    public void testRelease() {
	portalFacesContext.release();
	assertNull(FacesContext.getCurrentInstance());
	try {
	    portalFacesContext.getLifecycle();
	    fail("Context not released");
	} catch (Exception e) {
	}
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#renderResponse()}.
     */
    public void testRenderResponse() {
	assertFalse(portalFacesContext.getRenderResponse());
	portalFacesContext.renderResponse();
	assertTrue(portalFacesContext.getRenderResponse());
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#responseComplete()}.
     */
    public void testResponseComplete() {
	assertFalse(portalFacesContext.getResponseComplete());
	portalFacesContext.responseComplete();
	assertTrue(portalFacesContext.getResponseComplete());
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#getELContext()}.
     */
    public void testGetELContext() {
	ELContext elContext = portalFacesContext.getELContext();
	assertSame(application.getELResolver(),elContext.getELResolver());
	assertSame(elContext,portalFacesContext.getELContext());
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#getApplication()}.
     */
    public void testGetApplication() {
	assertSame(application,portalFacesContext.getApplication());
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#getClientIdsWithMessages()}.
     */
    public void testGetClientIdsWithMessages() {
	portalFacesContext.addMessage(ID0, new FacesMessage());
	Iterator clientIdsWithMessages = portalFacesContext.getClientIdsWithMessages();
	assertTrue(clientIdsWithMessages.hasNext());
	assertEquals(ID0, clientIdsWithMessages.next());
	assertFalse(clientIdsWithMessages.hasNext());	
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#getMaximumSeverity()}.
     */
    public void testGetMaximumSeverity() {
	assertNull(portalFacesContext.getMaximumSeverity());
	portalFacesContext.addMessage(ID0, new FacesMessage());
	Severity maximumSeverity = portalFacesContext.getMaximumSeverity();
	assertEquals(0,maximumSeverity.compareTo(FacesMessage.SEVERITY_INFO));
	portalFacesContext.addMessage("_id1", new FacesMessage(FacesMessage.SEVERITY_ERROR,"error","error detail"));
	maximumSeverity = portalFacesContext.getMaximumSeverity();
	assertEquals(0,maximumSeverity.compareTo(FacesMessage.SEVERITY_ERROR));
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#getMessages()}.
     */
    public void testGetMessages() {
	FacesMessage facesMessage = new FacesMessage();
	portalFacesContext.addMessage(ID0, facesMessage);
	Iterator messages = portalFacesContext.getMessages();
	assertTrue(messages.hasNext());
	assertSame(facesMessage, messages.next());
	assertFalse(messages.hasNext());	
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#getMessages(java.lang.String)}.
     */
    public void testGetMessagesString() {
	FacesMessage facesMessage = new FacesMessage();
	portalFacesContext.addMessage(ID0, facesMessage);
	portalFacesContext.addMessage("_id1", new FacesMessage());
	Iterator messages = portalFacesContext.getMessages(ID0);
	assertTrue(messages.hasNext());
	assertSame(facesMessage, messages.next());
	assertFalse(messages.hasNext());	
    }

    /**
     * Test method for {@link org.ajax4jsf.portlet.context.FacesContextImpl#getRenderKit()}.
     */
    public void testGetRenderKit() {
	portalFacesContext.setViewRoot(null);
	assertNull(portalFacesContext.getRenderKit());
	portalFacesContext.setViewRoot(facesContext.getViewRoot());
	assertSame(renderKit, portalFacesContext.getRenderKit());
    }
}
