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

import org.eclipse.birt.data.engine.api.IResultIterator;
import org.eclipse.birt.data.engine.api.querydefn.ConditionalExpression;
import org.eclipse.birt.data.engine.api.querydefn.FilterDefinition;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * <b>Bug description</b>
 * <p>
 * Description:
 * <p>
 * when input a long value such as '36555667' in the input field of 'Top N',
 * throw out an exception.
 * <p>
 * <ol>
 * Steps to reproduce:
 * <li>new a report
 * <li>new a datasource and a data set
 * 
 * <li>create a bar chart and use the data set
 * <li>click the 'Filter' button, and set a filter with 'Top N' and input
 * '36555667' as the input value
 * <li>Preview
 * </ol>
 * <p>
 * Actual result:
 * <p>
 * throw up an exception: out of memory
 * <p>
 * Expect result:
 * <p>
 * The chart can be displayed.
 * <p>
 * <b>Test description</b>
 * <li>Set a filt in dataset with expression "row.AMOUNT TopN 36555667"
 * <li>An exception is expected
 * <li>Catch and confim it.
 * 
 */
public class Regression_124448 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 testRegression124448_InvalidTopBottomNDefinition( )
			throws Exception
	{

		FilterDefinition filterDefn = new FilterDefinition(
				new ConditionalExpression( "row.AMOUNT",
						ConditionalExpression.OP_TOP_N, "36555667" ) );
		this.dataSet.addFilter( filterDefn );

		QueryDefinition queryDefn = newReportQuery( );
		// column mapping
		String[] name = new String[]{"Country", "City", "SALE_DATE"};
		ScriptExpression[] se = new ScriptExpression[name.length];
		se[0] = new ScriptExpression( "dataSetRow.COUNTRY" );
		se[1] = new ScriptExpression( "dataSetRow.CITY" );
		se[2] = new ScriptExpression( "dataSetRow.SALE_DATE" );
		for ( int i = 0; i < name.length; i++ )
			queryDefn.addResultSetExpression( name[i], se[i] );

		try
		{
			IResultIterator ri = executeQuery( queryDefn );
			fail( );
		}
		catch ( DataException e )
		{
			String errorCode = e.getErrorCode( );
			assertEquals( ResourceConstants.INVALID_DEFINITION_IN_FILTER,
					errorCode );
		}
	}

}