
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.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * <b>Bug description</b>
 * <p>
 * An error of a special data binding column with exprssion "if
 * (row.AMOUNT1%2==0) true;else false;"
 * <p>
 * <b>Test description</b>
 * <p>
 * Create a computed column with the expression, and set a group on it. Compare
 * the golden.
 */

public class Regression_136551 extends APITestCase
{

	/** computed column name */
	private String[] ccName;

	/** computed column expression */
	private String[] ccExpr;

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

	// TODO:Use scripted dataset
	public void testSpecialExpression( ) throws Exception
	{
		ccName = new String[]{"CCC"};
		ccExpr = new String[]{"if (row.AMOUNT1%2==0) true;else false;"};

		for ( int i = 0; i < ccName.length; i++ )
		{
			ComputedColumn computedColumn = new ComputedColumn( ccName[i],
					ccExpr[i], DataType.BOOLEAN_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" );
		groupDefn[0].setIntervalRange( 0 );

		String[] bindingNameRow = new String[2];
		bindingNameRow[0] = "ROW_ID";
		bindingNameRow[1] = "ROW_CCC";

		ScriptExpression[] bindingExprRow = new ScriptExpression[]{
				new ScriptExpression( "dataSetRow." + "AMOUNT1", 0 ),
				new ScriptExpression( "dataSetRow." + ccName[0], 0 ),};

		QueryDefinition qd = this.createQuery( bindingNameGroup,
				bindingExprGroup, groupDefn, null, null, null, null, null,
				null, bindingNameRow, bindingExprRow );
		String[] columnStr = new String[]{"AMOUNT1", "amount1"};

		String outputStr = getOutputStrForGroupTest( 30, qd, groupDefn.length,
				bindingNameRow, columnStr );
		// use DtE output framework
		testPrint( outputStr );
		// golden file compare

		// assert
		checkOutputFile( );

	}
}