/**
 * 
 */
package javax.portlet.faces;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
 * @author asmirnov
 *
 */
public class MockBridge implements Bridge {

	private boolean initialized = false;
	private boolean actionProcessed = false;
	private boolean responseProcessed = false;
	
	static int initCount = 0;
	static int actionCount = 0;
	static int responseCount = 0;
	static int destroyCount = 0;

	/* (non-Javadoc)
	 * @see javax.portlet.faces.Bridge#destroy()
	 */
	public void destroy() {
		if(this.initialized == false){
			throw new IllegalStateException("bridge not initialized");
		}
		this.initialized=false;
		destroyCount++;
	}

	/* (non-Javadoc)
	 * @see javax.portlet.faces.Bridge#doFacesRequest(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
	 */
	public void doFacesRequest(ActionRequest request, ActionResponse response)
			throws BridgeException {
		if(null == request || null == response){
			throw new NullPointerException();
		}
		if(this.initialized == false){
			throw new IllegalStateException("bridge not initialized");
		}
		if(null == request.getAttribute("javax.portlet.faces.defaultViewId")){
			throw new BridgeDefaultViewNotSpecifiedException();
		}
		this.actionProcessed=true;
		actionCount++;
	}

	/* (non-Javadoc)
	 * @see javax.portlet.faces.Bridge#doFacesRequest(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
	 */
	public void doFacesRequest(RenderRequest request, RenderResponse response)
			throws BridgeException {
		if(null == request || null == response){
			throw new NullPointerException();
		}
		if(this.initialized == false){
			throw new IllegalStateException("bridge not initialized");
		}
		if(null == request.getAttribute("javax.portlet.faces.defaultViewId")){
			throw new BridgeDefaultViewNotSpecifiedException();
		}
		this.responseProcessed=true;
		responseCount++;
	}

	/* (non-Javadoc)
	 * @see javax.portlet.faces.Bridge#init(javax.portlet.PortletConfig)
	 */
	public void init(PortletConfig config) throws PortletException {
		if(null == config){
			throw new NullPointerException();
		}
		this.initialized=true;
		initCount++;
	}

	/**
	 * @return the initialized
	 */
	protected boolean isInitialized() {
		return initialized;
	}

	/**
	 * @return the actionProcessed
	 */
	protected boolean isActionProcessed() {
		return actionProcessed;
	}

	/**
	 * @return the responseProcessed
	 */
	protected boolean isResponseProcessed() {
		return responseProcessed;
	}

}
