
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.IResultIterator;
import org.eclipse.birt.data.engine.api.querydefn.GroupDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;
import org.eclipse.birt.data.engine.core.DataException;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * <b>Bug description</b>
 * <p>
 * Set a invalid interval type will cause incorrect group action
 * <p>
 * <b>Test description</b> We set a date interval type on a string type group
 * key. 
 * Try to catch the exception and assert whether it is the exception we
 * wanted.
 * <p>
 */

public class Regression_142939 extends APITestCase
{

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

	public void testInvalidIntervaltypegroup( ) throws Exception, IOException
	{
		// group binding defination
		String[] bindingNameGroup = new String[1];
		bindingNameGroup[0] = "GROUP_COUNTRY";
		IBaseExpression[] bindingExprGroup = new IBaseExpression[1];
		bindingExprGroup[0] = new ScriptExpression( "dataSetRow.COUNTRY",
				DataType.STRING_TYPE );
		GroupDefinition[] groupDefn = new GroupDefinition[]{new GroupDefinition(
				"group1" )};
		// group key
		groupDefn[0].setKeyExpression( "row.GROUP_COUNTRY" );
		// group interval type
		groupDefn[0].setInterval( IGroupDefinition.YEAR_INTERVAL );
		// set interval value
		groupDefn[0].setIntervalRange( 3 );
		// column binding
		String[] bindingNameRow = new String[2];
		bindingNameRow[0] = "ROW_ID";
		bindingNameRow[1] = "ROW_COUNTRY";
		IBaseExpression[] bindingExprRow = new IBaseExpression[2];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.ID" );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.COUNTRY" );

		// Generate a group

		try
		{
			IResultIterator resultIt = this.executeQuery( this.createQuery(
					bindingNameGroup, bindingExprGroup, groupDefn, null, null,
					null, null, null, null, bindingNameRow, bindingExprRow ) );
			fail( "Expecting exception due to Invalid interval type." );
		}
		catch ( DataException e )
		{
		
		}
	}
}
