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

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

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * <b>Regression test case description:</b>
 * <p>
 * The function Total.percentile() works out the wrong result.
 * </p>
 * <b>Test description:</b>
 * <p>
 * Creating a Table with two INT columns A and B
 * </p>
 * <p>
 * Then import "Total.percentile(dataSetRow.A,0.3)" to the column binding,
 * compare the result set with golen file.
 * </p>
 */

public class Regression_131668 extends APITestCase
{

	String callsTableName;

	protected void setUp( ) throws Exception
	{
		super.setUp( );

		prepareDataSet( new DataSourceInfo( ConfigText
				.getString( "Api.TestData4.TableName" ), ConfigText
				.getString( "Api.TestData4.TableSQL" ), ConfigText
				.getString( "Api.TestData4.TestDataFileName" ) ) );

		callsTableName = ConfigText.getString( "Api.TestData4.TableName" );
	}

	/*
	 * @see org.eclipse.birt.data.engine.api.APITestCase#getDataSourceInfo()
	 */
	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestData4.TableName" ), ConfigText
				.getString( "Api.TestData4.TableSQL" ), ConfigText
				.getString( "Api.TestData4.TestDataFileName" ) );
	}

	public void testWeightedAve( ) throws Exception
	{
		String testSQL = "select " + callsTableName + ".A " + ", "
				+ callsTableName + ".B " + "from " + callsTableName;

		( (OdaDataSetDesign) this.dataSet ).setQueryText( testSQL );
		String value = "16";

		String[] ccName = new String[]{"Expected", "Actual"};
		String[] ccExpr = new String[]{value,
				"Total.percentile(dataSetRow.A, 0.3)"};
		for ( int i = 0; i < ccName.length; i++ )
		{
			ComputedColumn computedColumn = new ComputedColumn( ccName[i],
					ccExpr[i], DataType.ANY_TYPE );
			( (BaseDataSetDesign) this.dataSet )
					.addComputedColumn( computedColumn );
		}

		String[] bindingNameRow = new String[4];
		bindingNameRow[0] = "ROW_A";
		bindingNameRow[1] = "ROW_B";
		bindingNameRow[2] = "ROW_ccc";
		bindingNameRow[3] = "ROW_NUM1";
		ScriptExpression[] bindingExprRow = new ScriptExpression[]{

		new ScriptExpression( "dataSetRow.A" ),
				new ScriptExpression( "dataSetRow.B" ),
				new ScriptExpression( "dataSetRow.Expected" ),
				new ScriptExpression( "dataSetRow.Actual" )};

		IResultIterator resultIt = this.executeQuery( this.createQuery( null,
				null, null, null, null, null, null, null, null, bindingNameRow,
				bindingExprRow ) );

		printResult( resultIt, bindingNameRow, bindingExprRow );

		checkOutputFile( );

	}

	private void printResult( IResultIterator resultIt,
			String[] bindingNameRow, ScriptExpression[] bindingExprRow )
			throws Exception
	{
		// output column name
		String str = "";
		for ( int i = 0; i < bindingNameRow.length; i++ )
		{
			str += bindingExprRow[i].getText( ).replaceAll(
					"dataSetRow\\Q.\\E", "" );
			str += "  ";
		}
		testPrintln( str );

		// output row data
		while ( resultIt.next( ) )
		{
			str = "";
			for ( int i = 0; i < bindingNameRow.length; i++ )
			{
				str += " ";
				if ( resultIt.getValue( bindingNameRow[i] ) == null )
					str += "<null>";
				else
					str += (int) Double.parseDouble( resultIt.getValue(
							bindingNameRow[i] ).toString( ) );
				str += "    ";
			}
			testPrintln( str );
		}
	}
}