/*******************************************************************************
 * 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 org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;

import testutil.APITestCase;

/**
 * <b>Bug description</b>
 * <p>
 * Description:
 * <p>
 * In the second time use of scripted data set, the result is null
 * <p>
 * <ol>
 * Steps to reproduce:
 * <li>Create a scripted data set
 * <li>Drag this data set to report design to create a table
 * <li>Do step 4 again to create the second table
 * <li>Preview report, the first table has result but the second table is null
 * </ol>
 * Expected: the first table and the second table have the same result
 * <p>
 * <b>Test description</b>
 * <p>
 * As Lin Zhu's bug resolve comments:
 * <P>
 * A column binding dependency cycle checker and group name dependency checker
 * so that the following conditions would lead to an exception.
 * <P>
 * Define two column binding dependency cycle. Catch the exception and comfirm
 * whether it course by binding dependency cycle problem.
 * 
 * 
 */

public class Regression_136259 extends APITestCase
{

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

		dset.setOpenScript( "count=0;" );
		dset.setFetchScript( "if ( ++count > 10 ) return false;"
				+ "row.Data1 = count;" + "row.Data2 = count+100;"
				+ "return true;" );

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

		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] ),
				new ScriptExpression( "row[\"Col2\"]" ),
				new ScriptExpression( "row[\"Col1\"]" )};

		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( );
			fail( );
		}
		catch ( DataException e )
		{
			String errorCode = e.getErrorCode( );
			assertEquals( ResourceConstants.COLUMN_BINDING_CYCLE, errorCode );

		}

		dataEngine.shutdown( );
	}

	protected DataSourceInfo getDataSourceInfo( )
	{
		return null;
	}

}
