/*******************************************************************************
 * 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>
 * Open the attached report. The data item in the second column is bound to an
 * invalid expression row["junk"] (no binding by the name junk exists). No error
 * is generated when this report is run. The 2nd column simply shows empty
 * values.
 * <P>
 * Expected:
 * <P>
 * erroneous "row" references must be reported as error.
 * <p>
 * <b>Test description</b>
 * <p>
 * 
 * <li>Create a new scripted dataset.
 * <li>Create a binding with invalid expression row["junk"]
 * <li>Catch the error and confirm it.
 * 
 */

public class Regression_143105 extends APITestCase
{

	public void testRegression143105_BindingnotExistRow( ) 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*11+count;" + "return true;" );

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

		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[\"junk\"]" ),
		// new ScriptExpression( "Total.rank(row.Data1,true)" )
		};

		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 );
		IResultIterator ri = qr.getResultIterator( );
		ri.getValue( scriptColumnNames[0] );

		try
		{
			ri.getValue( scriptColumnNames[1] );
			fail( "Should not get null value" );
		}

		catch ( DataException e )
		{
			String errorCode = e.getErrorCode( );
			assertEquals( ResourceConstants.INAVLID_FIELD_NAME, errorCode );
		}
	}

	protected DataSourceInfo getDataSourceInfo( )
	{
		return null;
	}

}
