/**
 * License Agreement.
 *
 * Rich Faces - Natural Ajax for Java Server Faces (JSF)
 *
 * Copyright (C) 2007 Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.richfaces.skin;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.faces.FacesException;
import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

import junit.framework.TestCase;

import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
import org.richfaces.skin.Skin;
import org.richfaces.skin.SkinFactory;
import org.richfaces.skin.SkinFactoryImpl;
import org.richfaces.skin.SkinImpl;

/**
 * Test for Skin/skin factory methods.
 * @author asmirnov@exadel.com (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.1.2.1 $ $Date: 2007/01/10 14:28:13 $
 *
 */
public class SkinTestCase extends TestCase {
	private MockControl contextControl;
	private FacesContext mockContext;

	private MockControl externalContextControl;
	private ExternalContext mockExternalContext;
	
	private TestApplicationFactory appFactory;
	
	private MockControl bindingControl;	
	private ValueBinding mockBinding;
	private MockControl bindingSkinControl;
	private ValueBinding mockSkinBinding;

	public SkinTestCase(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		contextControl = MockClassControl.createControl(FacesContext.class);
		mockContext = (FacesContext) contextControl.getMock();
		externalContextControl = MockClassControl.createControl(ExternalContext.class);
		mockExternalContext = (ExternalContext) externalContextControl.getMock();
		bindingControl = MockClassControl.createNiceControl(ValueBinding.class);
		mockBinding = (ValueBinding) bindingControl.getMock();
		bindingSkinControl = MockClassControl.createNiceControl(ValueBinding.class);
		mockSkinBinding = (ValueBinding) bindingSkinControl.getMock();
		FactoryFinder.releaseFactories();
		FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, TestApplicationFactory.class.getName());
		appFactory = (TestApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
		super.setUp();
	}

	protected void tearDown() throws Exception {
		FactoryFinder.releaseFactories();
		SkinFactory.reset();
		super.tearDown();
	}

	/*
	 * Test method for 'org.richfaces.skin.SkinFactory.getInstance()'
	 */
	public void testGetInstance() {
		SkinFactory factory = SkinFactory.getInstance();
		SkinFactory factory1 = SkinFactory.getInstance();
		assertSame(factory,factory1);
	}

	/*
	 * Test method for 'org.richfaces.skin.SkinFactory.getSkin(FacesContext)'
	 */
	public void testGetSkin() {
		SkinFactory factory = SkinFactory.getInstance();
		// setup  context mock
		mockContext.getExternalContext();
		contextControl.setReturnValue(mockExternalContext);
		contextControl.replay();
		// setup External context mock
		mockExternalContext.getInitParameter(SkinFactory.SKIN_PARAMETER);
		externalContextControl.setReturnValue("test");
		externalContextControl.replay();
		// setup Application mock
		Application app = appFactory.getApplication();
		app.createValueBinding("#{test.bean}");
		appFactory.getApplicationControl().setReturnValue(mockBinding);
		appFactory.getApplicationControl().replay();
		// setup Value binding mock.
		mockBinding.getValue(mockContext);
		bindingControl.setReturnValue("test.value");
		mockBinding.getValue(mockContext);
		bindingControl.setReturnValue("test.value1");
		bindingControl.setDefaultReturnValue("test.value");
		bindingControl.replay();
		// test call
		Skin skin = factory.getSkin(mockContext);
		// calls control
		contextControl.verify();
		externalContextControl.verify();
		appFactory.getApplicationControl().verify();
		assertNotNull("Null skin!",skin);
		// test properties
		assertEquals("string",skin.getParameter(mockContext,"string.property"));
		assertEquals("base.string",skin.getParameter(mockContext,"base.property"));
		assertEquals("test.value",skin.getParameter(mockContext,"bind.property"));
//		assertEquals("HTML_BASIC",skin.getRenderKitId(mockContext));
	}

	/*
	 * Test method for 'org.richfaces.skin.SkinFactory.getSkin(FacesContext)'
	 */
	public void testSkinReferences() {
		SkinFactory factory = SkinFactory.getInstance();
		// setup  context mock
		mockContext.getExternalContext();
		contextControl.setReturnValue(mockExternalContext);
		contextControl.replay();
		// setup External context mock
		mockExternalContext.getInitParameter(SkinFactory.SKIN_PARAMETER);
		externalContextControl.setReturnValue("test");
		externalContextControl.replay();
		// setup Application mock
		Application app = appFactory.getApplication();
		app.createValueBinding("#{test.bean}");
		appFactory.getApplicationControl().setReturnValue(mockBinding);
		appFactory.getApplicationControl().replay();
		// setup Value binding mock.
		mockBinding.getValue(mockContext);
		bindingControl.setReturnValue("test.value");
		mockBinding.getValue(mockContext);
		bindingControl.setReturnValue("test.value1");
		bindingControl.setDefaultReturnValue("test.value");
		bindingControl.replay();
		// test call
		Skin skin = factory.getSkin(mockContext);
		// calls control
		contextControl.verify();
		externalContextControl.verify();
		appFactory.getApplicationControl().verify();
		assertNotNull("Null skin!",skin);
		assertEquals("default",skin.getParameter(mockContext,"c"));
		assertEquals("yyy",skin.getParameter(mockContext,"y"));
	}

	/*
	 * Test method for 'org.richfaces.skin.SkinFactory.getSkin(FacesContext)'
	 */
	public void testCyclicSkinReferences() {
		SkinFactory factory = SkinFactory.getInstance();
		// setup  context mock
		mockContext.getExternalContext();
		contextControl.setReturnValue(mockExternalContext);
		contextControl.replay();
		// setup External context mock
		mockExternalContext.getInitParameter(SkinFactory.SKIN_PARAMETER);
		externalContextControl.setReturnValue("cyclic");
		externalContextControl.replay();
		// test call
		try {
			Skin skin = factory.getSkin(mockContext);
		} catch(FacesException e){
			return;
		}
		assertTrue(false);
	}

	/*
	 * Test method for 'org.richfaces.skin.SkinFactory.getSkin(FacesContext)'
	 */
	public void testBadSkinReferences() {
		SkinFactory factory = SkinFactory.getInstance();
		// setup  context mock
		mockContext.getExternalContext();
		contextControl.setReturnValue(mockExternalContext);
		contextControl.replay();
		// setup External context mock
		mockExternalContext.getInitParameter(SkinFactory.SKIN_PARAMETER);
		externalContextControl.setReturnValue("noref");
		externalContextControl.replay();
		// test call
		try {
			Skin skin = factory.getSkin(mockContext);
		} catch(FacesException e){
			return;
		}
		assertTrue(false);
	}
	/*
	 * Test method for 'org.richfaces.skin.SkinFactory.getSkin(FacesContext)'
	 */
	public void testGetBindedSkin() {
		SkinFactory factory = SkinFactory.getInstance();
		// setup  context mock
		mockContext.getExternalContext();
		contextControl.setReturnValue(mockExternalContext);
		mockContext.getApplication();
		contextControl.setReturnValue(appFactory.getApplication());
		contextControl.replay();
		// setup External context mock
		mockExternalContext.getInitParameter(SkinFactory.SKIN_PARAMETER);
		externalContextControl.setReturnValue("#{test.skin}");
		externalContextControl.replay();
		// setup Application mock
		Application app = appFactory.getApplication();
		app.createValueBinding("#{test.skin}");
		appFactory.getApplicationControl().setReturnValue(mockSkinBinding);
		app.createValueBinding("#{test.bean}");
		appFactory.getApplicationControl().setReturnValue(mockBinding);
		appFactory.getApplicationControl().replay();
		// setup Value binding mock.
		mockBinding.getValue(mockContext);
		bindingControl.setReturnValue("binded.test.value");
		bindingControl.replay();
		// skin EL binding.
		mockSkinBinding.getValue(mockContext);
		bindingSkinControl.setDefaultReturnValue("bindedtest");
		bindingSkinControl.replay();
		// test call
		Skin skin = factory.getSkin(mockContext);
		assertNotNull("Null skin!",skin);
		// test properties
		assertEquals("bindedstring",skin.getParameter(mockContext,"string.property"));
//		assertEquals("base.string",skin.getParameter(mockContext,"base.property"));
		assertEquals("binded.test.value",skin.getParameter(mockContext,"bind.property"));
		assertEquals("TEST",skin.getRenderKitId(mockContext));

	
		// calls control
//		contextControl.verify();
//		externalContextControl.verify();
//		appFactory.getApplicationControl().verify();
//		bindingControl.verify();
//		bindingSkinControl.verify();
}
	
	public void testSkinHash() {
		SkinFactory factory = SkinFactory.getInstance();
		// setup  context mock
		mockContext.getExternalContext();
		contextControl.setDefaultReturnValue(mockExternalContext);
		mockContext.getApplication();
		contextControl.setReturnValue(appFactory.getApplication());
		contextControl.replay();
		// setup External context mock
		mockExternalContext.getInitParameter(SkinFactory.SKIN_PARAMETER);
		externalContextControl.setReturnValue("#{test.skin}");
		Map params = new HashMap();
		mockExternalContext.getRequestMap();
		externalContextControl.setDefaultReturnValue(params);
		externalContextControl.replay();
		// setup Application mock
		Application app = appFactory.getApplication();
		app.createValueBinding("#{test.skin}");
		appFactory.getApplicationControl().setReturnValue(mockSkinBinding);
		app.createValueBinding("#{test.bean}");
		appFactory.getApplicationControl().setReturnValue(mockBinding);
		appFactory.getApplicationControl().replay();
		// setup Value binding mock.
		mockBinding.getValue(mockContext);
		bindingControl.setDefaultReturnValue("binded.test.value");
		bindingControl.replay();
		// skin EL binding.
		mockSkinBinding.getValue(mockContext);
		bindingSkinControl.setDefaultReturnValue("bindedtest");
		bindingSkinControl.replay();
		// test call
		Skin skin = factory.getSkin(mockContext);
		assertNotNull("Null skin!",skin);
		// test properties
		int hash = skin.hashCode(mockContext);
		assertTrue(params.containsKey(SkinImpl.REQUEST_HASH_CODE_PARAMETER));
		assertEquals(hash,skin.hashCode(mockContext));
		params.clear();
		assertEquals(hash,skin.hashCode(mockContext));
		// setup Value binding mock for different value - hash must differ.
		params.clear();
		bindingControl.reset();
		mockBinding.getValue(mockContext);
		bindingControl.setDefaultReturnValue("other.test.value");
		bindingControl.replay();
		assertFalse( hash==skin.hashCode(mockContext) );

	}
	/*
	 * Test method for 'org.richfaces.skin.SkinFactory.getDefaultProperties()'
	 */
	public void testGetDefaultProperties() {
		SkinFactoryImpl factory = (SkinFactoryImpl) SkinFactory.getInstance();
		Properties defaultProps = factory.getDefaultSkinProperties();
//		assertEquals("HTML_BASIC",defaultProps.getProperty("render.kit"));
		// Second default config
		assertEquals("default",defaultProps.getProperty("a"));
	}

	/*
	 * Test method for 'org.richfaces.skin.SkinFactory.getSkinName(FacesContext)'
	 */
	public void testGetSkinName() {

	}

}
