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

import java.io.IOException;

import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IGroupDefinition;
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>
 * Data can't be group well when set a group key to be decimal and interval to
 * be 1
 * <p>
 * <b>Test description</b>
 * <p>
 * This bug cover the group on decimal type data and set its interval 1.
 * 
 * 
 */
public class Regression_103152 extends APITestCase
{

	// Use database in test framework
	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestData0.TableName" ), ConfigText
				.getString( "Api.TestData0.TableSQL" ), ConfigText
				.getString( "Api.TestData0.TestDataFileName" ) );
	}

	/**
	 * Test feature of group on Number Decimal type
	 * 
	 * @throws Exception
	 */
	public void testGroupOnDecimalInterval1( ) throws Exception
	{
		groupOnNumber( 1, null );

	}

	public void testGroupOnDecimalNoInterval( ) throws Exception
	{
		// Also test no interval group result to refer
		groupOnNumber( 0, null );

	}

	private void groupOnNumber( double intervalValue, Object startValue )
			throws Exception, IOException
	{
		// group binding defination
		String[] bindingNameGroup = new String[1];
		bindingNameGroup[0] = "GROUP_NUMBER";
		IBaseExpression[] bindingExprGroup = new IBaseExpression[1];
		bindingExprGroup[0] = new ScriptExpression( "dataSetRow.ID" );
		GroupDefinition[] groupDefn = new GroupDefinition[]{new GroupDefinition(
				"group1" )};
		// group key
		groupDefn[0].setKeyExpression( "row.GROUP_NUMBER" );
		// group interval type
		groupDefn[0].setInterval( IGroupDefinition.NUMERIC_INTERVAL );

		// define startValue as base value to group, the defaul base value is 0
		if ( startValue != null )
			groupDefn[0].setIntervalStart( startValue );
		// set interval value
		groupDefn[0].setIntervalRange( intervalValue );

		// column binding
		String[] bindingNameRow = new String[3];
		bindingNameRow[0] = "ROW_ID";
		bindingNameRow[1] = "ROW_AMOUT1";
		bindingNameRow[2] = "ROW_AMOUT2";
		IBaseExpression[] bindingExprRow = new IBaseExpression[3];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.ID" );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.AMOUNT1" );
		bindingExprRow[2] = new ScriptExpression( "dataSetRow.AMOUNT2" );

		String[] columnStr = new String[]{"id", "amount1", "amount2"};

		QueryDefinition qd = this.createQuery( bindingNameGroup,
				bindingExprGroup, groupDefn, null, null, null, null, null,
				null, bindingNameRow, bindingExprRow );
		// Generate a group
		String outputStr = getOutputStrForGroupTest( 30, qd, groupDefn.length,
				bindingNameRow, columnStr );
		// use DtE output framework
		testPrint( outputStr );
		// golden file compare
		this.checkOutputFile( );
	}
}