
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>
 * The error is about 2-level group result, the second group is influenced by
 * the first group.
 * <p>
 * <b>Test description</b>
 * <p>
 * Set 2-level group:
 * <p>
 * The first level group information:
 * <P>
 * Group key: row.CITY Interval type: prefix Range: 1
 * <p>
 * The second level group information:
 * <P>
 * Group key: row.ID Interval type: Interval Range: 4
 * 
 * And the result should be same as golden file
 * 
 */
public class Regression_139365 extends APITestCase
{

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

	/**
	 * Test feature of group on Number Decimal type
	 * 
	 * @throws Exception
	 */

	public void test2LevelGroupResult( ) throws Exception, IOException
	{
		// group binding defination
		String[] bindingNameGroup = new String[2];
		bindingNameGroup[0] = "GROUP_GROUP1";
		bindingNameGroup[1] = "GROUP_GROUP2";
		IBaseExpression[] bindingExprGroup = new IBaseExpression[2];

		bindingExprGroup[0] = new ScriptExpression( "dataSetRow.CITY" );
		bindingExprGroup[1] = new ScriptExpression( "dataSetRow.ID" );

		GroupDefinition[] groupDefn = new GroupDefinition[]{
				new GroupDefinition( "group1" ), new GroupDefinition( "group2" )};

		groupDefn[0].setInterval( IGroupDefinition.STRING_PREFIX_INTERVAL );
		groupDefn[0].setKeyExpression( "row.GROUP_GROUP1" );
		groupDefn[0].setIntervalRange( 1 );
		groupDefn[1].setInterval( IGroupDefinition.NUMERIC_INTERVAL );
		groupDefn[1].setKeyExpression( "row.GROUP_GROUP2" );
		groupDefn[1].setIntervalRange( 4 );

		// column binding
		String[] bindingNameRow = new String[3];
		bindingNameRow[0] = "ROW_CITY";
		bindingNameRow[1] = "ROW_ID";
		bindingNameRow[2] = "ROW_COUNTRY";
		IBaseExpression[] bindingExprRow = new IBaseExpression[3];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.CITY" );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.ID" );
		bindingExprRow[2] = new ScriptExpression( "dataSetRow.ID" );

		String[] columnStr = new String[]{"City", "ID", "ShowAllID"};

		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( );
	}
}