/*******************************************************************************
 * 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.tests.data.engine.regression;

import java.util.ArrayList;

import org.eclipse.birt.core.data.DataType;
import org.eclipse.birt.data.engine.api.IPreparedQuery;
import org.eclipse.birt.data.engine.api.IQueryResults;
import org.eclipse.birt.data.engine.api.IResultIterator;
import org.eclipse.birt.data.engine.api.IScriptExpression;
import org.eclipse.birt.data.engine.api.querydefn.ColumnDefinition;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.ScriptDataSourceDesign;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;

/**
 * <b>Bug description</b>
 * <p>
 * In the fetch "method" of a data set, the "method" I am supposed to return
 * either true or false. Based on my knowledge of Javascript, the proper syntax
 * for returning a value out of a method is to use return (value to return).
 * 
 * So I would expect the following code to work in the fetch method:
 * <P>
 * if (count < 10){count++; return true; } else { return false; }
 * <P>
 * <b>Test description</b>
 * <P>
 * Add a script with "return" into the scripted data set fetch method. try and
 * cathch to see wheather there are some errors to throw out.
 * 
 * 
 */

public class Regression_96025 extends APITestCase
{

	public void testExecuteScript_return( ) throws Exception
	{
		ScriptDataSourceDesign dsource = new ScriptDataSourceDesign(
				"ScriptedDataSource" );
		ScriptDataSetDesign dset = new ScriptDataSetDesign( "ScriptedDataSet" );

		dset.setOpenScript( "count=0;" );
		dset.setFetchScript( "if ( count < 10 ) {count++; return false;}"
				+ "else return true;" );

		String[] scriptColumnNames = new String[]{"Data1", "Data2"};

		ArrayList list = new ArrayList( );
		for ( int i = 0; i < scriptColumnNames.length; i++ )
		{
			ColumnDefinition col = new ColumnDefinition( scriptColumnNames[i] );
			col.setDataType( DataType.ANY_TYPE );

			list.add( col );
		};

		dset.getResultSetHints( ).addAll( list );

		dset.setDataSource( dsource.getName( ) );
		dataEngine.defineDataSource( dsource );
		dataEngine.defineDataSet( dset );

		IScriptExpression[] scriptExprs = new IScriptExpression[]{
				new ScriptExpression( "dataSetRow." + scriptColumnNames[0] ),
				new ScriptExpression( "dataSetRow." + scriptColumnNames[1] )};

		QueryDefinition rqDefn = new QueryDefinition( );
		// add expressions

		rqDefn.setDataSetName( "ScriptedDataSet" );
		for ( int i = 0; i < scriptExprs.length; i++ )
		{
			rqDefn
					.addResultSetExpression( scriptColumnNames[i],
							scriptExprs[i] );
		}

		IPreparedQuery pq = dataEngine.prepare( rqDefn );

		IQueryResults qr = pq.execute( jsScope );
		try
		{
			IResultIterator ri = qr.getResultIterator( );
		}
		catch ( Exception DataException )
		{
			fail( "Excute the script Error" );
		}
	}

	protected DataSourceInfo getDataSourceInfo( )
	{
		return null;
	}

}
