/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.model.script;

import junit.framework.TestCase;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.script.CoreJavaScriptInitializer;
import org.eclipse.birt.core.script.CoreJavaScriptWrapper;
import org.eclipse.birt.core.script.IJavascriptWrapper;
import org.eclipse.birt.core.script.ScriptContext;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.WrapFactory;

import com.ibm.icu.util.ULocale;

/**
 * The base test case for testing scripting object.
 */

abstract class ScriptTestCase extends TestCase
{

	protected ScriptContext context = null;

	protected Scriptable scope = null;

	protected SessionHandle sessionHandle = null;

	protected ReportDesignHandle designHandle = null;

	/*
	 * @see TestCase#setUp()
	 */

	protected void setUp( ) throws Exception
	{
		super.setUp( );
		Platform.startup( null );
		sessionHandle = DesignEngine.newSession( (ULocale) null );
		designHandle = sessionHandle.createDesign( );

		Context cx = Context.enter( );
		ScriptableObject rootScope = cx.initStandardObjects( );
		Context.exit( );

		this.context = new ScriptContext( rootScope );
		this.scope = context.getScope( );

		context.getContext( ).setWrapFactory( new WrapFactory( ) {

			protected IJavascriptWrapper modelWrapper = new ModelJavaScriptWrapper( );
			protected IJavascriptWrapper coreWrapper = new CoreJavaScriptWrapper( );

			/**
			 * wrapper an java object to javascript object.
			 */
			public Object wrap( Context cx, Scriptable scope, Object obj,
					Class staticType )
			{
				Object object = modelWrapper.wrap( cx, scope, obj, staticType );
				if ( object != obj )
				{
					return object;
				}
				object = coreWrapper.wrap( cx, scope, obj, staticType );
				if ( object != obj )
				{
					return object;
				}
				return super.wrap( cx, scope, obj, staticType );
			}
		} );

		new ModelJavaScriptInitializer( ).initialize( cx, scope );
		new CoreJavaScriptInitializer( ).initialize( cx, scope );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	public void tearDown( )
	{
		context.exit( );
	}

	/**
	 * Evaluates a JavaScript source string.
	 * 
	 * @param script
	 *            the JavaScript to evaluate
	 * @return the evaluation result
	 * @throws Exception
	 *             if any exception.
	 */

	protected Object eval( String script ) throws Exception
	{
		return context.eval( script );
	}

}