/*******************************************************************************
 * 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.engine.executor;

import java.util.HashMap;

import junit.framework.TestCase;

import org.eclipse.birt.core.script.ScriptContext;
import org.eclipse.birt.report.engine.api.impl.ParameterAttribute;

public class ScriptableParametersTest extends TestCase
{

	ScriptContext context;

	public void setUp( )
	{
		context = new ScriptContext( );
		HashMap params = new HashMap( );
		params.put( "string", new ParameterAttribute( "abc", "STRING VALUE" ) );
		context.registerBean( "params", new ScriptableParameters( params,
				context.getScope( ) ) );
	}

	public void treadDown( )
	{
		context.exit( );
	}

	public void testAssign( )
	{
		// change to a exits parameters
		context.eval( "params['string'] = 'abcd'" );
		assertEquals( "abcd", context.eval( "params['string']" ) );
		context.eval( "params['string'].value = 'abcde'" );
		assertEquals( "abcde", context.eval( "params['string']" ) );
		context.eval( "params['string'].displayText = 'display'" );
		assertEquals( "display", context.eval( "params['string'].displayText" ) );

		// assign to a none exist parameter will create a new entry
		// automatcially
		context.eval( "params['new param'] = 'abc'" );
		assertEquals( "abc", context.eval( "params['new param']" ) );

	}

	public void testReterive( )
	{
		// access the none exist paramter will return null directl
		assertEquals( Boolean.TRUE, context
				.eval( "params['none exsit'] == null" ) );

		// access the paramters from value
		assertEquals( "abc", context.eval( "params['string'].value" ) );
		assertEquals( "bbc", context
				.eval( "params['string'].value.replace('a', 'b')" ) );
		// access the paramters from the display text
		assertEquals( "STRING VALUE", context
				.eval( "params['string'].displayText" ) );

		// access the paramters from default value
		assertEquals( "abc", context.eval( "params['string']" ) );
		assertEquals( "bbc", context.eval( "var value = params['string'];"
				+ "value.replace('a', 'b')" ) );
	}

}
