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

import java.io.IOException;

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 with a start value is not correct.
 * </p>
 * <b>Test description:</b>
 * <p>
 * New a group and set its interval 3 and start value 2 on a computed column.
 * Compare it with golden.
 * </p>
 */

public class Regression_123930 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.TestData0.TableName" ), ConfigText
				.getString( "Api.TestData0.TableSQL" ), ConfigText
				.getString( "Api.TestData0.TestDataFileName" ) );
	}

	public void testGroupOnStartvalue2( ) throws Exception
	{
		groupOnNumber( 3, new Integer( 5 ) );

	}

	private void groupOnNumber( double intervalValue, Object startValue )
			throws Exception, IOException
	{
		String testSQL = "select " + callsTableName + ".AMOUNT1 " + "from "
				+ callsTableName;

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

		String[] ccName = new String[]{"ccc"};
		String[] ccExpr = new String[]{"row.AMOUNT1-1"};

		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" );
		groupDefn[0].setInterval( IGroupDefinition.NUMERIC_INTERVAL );

		if ( startValue != null )
			groupDefn[0].setIntervalStart( startValue );
		// set interval value
		groupDefn[0].setIntervalRange( intervalValue );

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

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

		};

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

		String[] columnStr = new String[]{"AMOUNT1", "CC"};

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

		checkOutputFile( );

	}
}
