
package org.eclipse.birt.tests.data.engine.regression;

import org.eclipse.birt.core.data.DataType;
import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IGroupDefinition;
import org.eclipse.birt.data.engine.api.querydefn.BaseDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.ComputedColumn;
import org.eclipse.birt.data.engine.api.querydefn.GroupDefinition;
import org.eclipse.birt.data.engine.api.querydefn.OdaDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * <b>Regression test case description:</b>
 * <p>
 * Group on computed column defined with expression as following failed: if
 * (row["account_id"]%2==0)1;else 2;
 * </p>
 * <b>Test description:</b>
 * <p>
 * Construct a script expression and set the value as above, to identify the
 * result table with two groups(1 and 2).
 * </p>
 */

public class Regression_124065 extends APITestCase
{

	String callsTableName;

	protected void setUp( ) throws Exception
	{
		super.setUp( );

		prepareDataSet( new DataSourceInfo( ConfigText
				.getString( "Api.TestData0.TableName" ), ConfigText
				.getString( "Api.TestData0.TableSQL" ), ConfigText
				.getString( "Api.TestData0.TestDataFileName" ) ) );

		callsTableName = ConfigText.getString( "Api.TestData0.TableName" );
	}

	/*
	 * @see org.eclipse.birt.data.engine.api.APITestCase#getDataSourceInfo()
	 */
	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestData.TableName" ), ConfigText
				.getString( "Api.TestData.TableSQL" ), ConfigText
				.getString( "Api.TestData.TestDataFileName" ) );
	}

	public void testScriptExpressionOnCompCol( ) throws Exception
	{
		String testSQL = "select " + callsTableName + ".AMOUNT1 " + "from "
				+ callsTableName;

		( (OdaDataSetDesign) this.dataSet ).setQueryText( testSQL );

		String[] ccName = new String[]{"ccc"};
		String[] ccExpr = new String[]{"if (row.AMOUNT1%2==0) 1;else 2;"};

		ComputedColumn computedColumn = new ComputedColumn( ccName[0],
				ccExpr[0], DataType.INTEGER_TYPE );
		( (BaseDataSetDesign) this.dataSet ).addComputedColumn( computedColumn );

		String[] bindingNameGroup = new String[1];
		bindingNameGroup[0] = "GROUP_GROUP1";
		IBaseExpression[] bindingExprGroup = new IBaseExpression[1];
		bindingExprGroup[0] = new ScriptExpression( "dataSetRow.ccc" );
		GroupDefinition[] groupDefn = new GroupDefinition[]{new GroupDefinition(
				"group1" )};
		groupDefn[0].setInterval( IGroupDefinition.NUMERIC_INTERVAL );
		groupDefn[0].setKeyExpression( "row.GROUP_GROUP1" );

		String[] bindingNameRow = new String[2];
		bindingNameRow[0] = "ROW_AMOUNT1";

		bindingNameRow[1] = "ROW_ccc";
		ScriptExpression[] bindingExprRow = new ScriptExpression[]{
				new ScriptExpression( "dataSetRow.AMOUNT1" ),

				new ScriptExpression( "dataSetRow.ccc" )};

		QueryDefinition qd = this.createQuery( bindingNameGroup,
				bindingExprGroup, groupDefn, null, null, null, null, null,
				null, bindingNameRow, bindingExprRow );

		String[] columnStr = new String[]{"id", "amount1"};
		// printResult(resultIt, bindingNameRow, bindingExprRow);
		String outputStr = getOutputStrForGroupTest( 30, qd, groupDefn.length,
				bindingNameRow, columnStr );
		// use DtE output framework
		testPrint( outputStr );
		// golden file compare
		this.checkOutputFile( );
		checkOutputFile( );

	}
}