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

import java.util.Iterator;

import junit.framework.TestCase;

import org.eclipse.birt.data.engine.api.IResultMetaData;
import org.eclipse.birt.report.data.adapter.api.DataRequestSession;
import org.eclipse.birt.report.data.adapter.api.DataSessionContext;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DataSourceHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.ResultSetColumnHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.elements.structures.ColumnHint;
import org.eclipse.birt.report.model.elements.DataSet;

import com.ibm.icu.util.ULocale;
/**
 * <b>Regression test case description:</b>
 * <p>            
 * Column alias name can not take effect</p>
 * <b>Test description:</b>
 * <p>
 * Through refreshMetaData() to refresh the cachedMetaData structure, 
 * get the column name from the result set to identify its name has been changed by alias name.
 * </p>
 */
public class Regression_145508 extends TestCase
{

	public void testAlias( ) throws Exception
	{
		SessionHandle session = new DesignEngine( new DesignConfig( )).newSessionHandle( ULocale.ENGLISH );
		ReportDesignHandle designHandle = session.createDesign( );
		ElementFactory factory = designHandle.getElementFactory( );
		DataSourceHandle datasource = factory.newScriptDataSource( "dsource" );
		DataSetHandle dataset = factory.newScriptDataSet( "dset" ) ;
		dataset.setDataSource( "dsource" );

		designHandle.getDataSources( ).add( datasource );
		designHandle.getDataSets( ).add( dataset );

		ColumnHint columnHint = StructureFactory.createColumnHint( );
		columnHint.setProperty( ColumnHint.ALIAS_MEMBER, "bbb" );
		columnHint.setProperty( ColumnHint.COLUMN_NAME_MEMBER, "aaa" );
		PropertyHandle propHandle = dataset
				.getPropertyHandle( DataSet.COLUMN_HINTS_PROP );
		propHandle.addItem( columnHint );
		assertEquals( "bbb", columnHint.getProperty( designHandle.getModule( ),
				ColumnHint.ALIAS_MEMBER ) );

		DataSessionContext dsc = new DataSessionContext(3, designHandle);
		DataRequestSession drsi = DataRequestSession.newSession( dsc );
		IResultMetaData meta = drsi.refreshMetaData(dataset);
		
		
		Iterator it = dataset.getCachedMetaDataHandle( ).getResultSet( ).iterator( );
		
		while( it.hasNext())
		{
			ResultSetColumnHandle handle = (ResultSetColumnHandle)it.next( );
			assertEquals(handle.getColumnName( ),"bbb");
		}
	}
	



}