gt: 		queryDefn.setDataSetName( this.dataSet.getName( ) );
context: /*******************************************************************************
   * Copyright (c) 2004 Actuate Corporation.
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the Eclipse Public License v1.0
   * which accompanies this distribution, and is available at
   * http://www.eclipse.org/legal/epl-v10.html
   *
   * Contributors:
   *  Actuate Corporation  - initial API and implementation
   *******************************************************************************/
  
  package org.eclipse.birt.data.engine.api;
  
  import java.util.Map;
  
  import org.eclipse.birt.core.archive.IDocArchiveReader;
  import org.eclipse.birt.core.archive.IDocArchiveWriter;
  import org.eclipse.birt.data.engine.api.querydefn.BaseDataSetDesign;
  import org.eclipse.birt.data.engine.api.querydefn.BaseDataSourceDesign;
  import org.eclipse.birt.data.engine.api.querydefn.BaseExpression;
  import org.eclipse.birt.data.engine.api.querydefn.FilterDefinition;
  import org.eclipse.birt.data.engine.api.querydefn.GroupDefinition;
  import org.eclipse.birt.data.engine.api.querydefn.OdaDataSetDesign;
  import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
  import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;
  import org.eclipse.birt.data.engine.api.querydefn.SortDefinition;
  import org.eclipse.birt.data.engine.api.querydefn.SubqueryDefinition;
  
  import testutil.BaseTestCase;
  import testutil.JDBCDataSource;
  import testutil.JDBCOdaDataSource;
  import testutil.TestDataSource;
  
  import org.junit.After;
  import org.junit.Before;
  
  /**
   * Base class for test cases that work with Data Engine public API
   */
  abstract public class APITestCase extends BaseTestCase
  {
  	/** connection property */
  	protected String DriverClass;
  	protected String URL;
  	protected String User;
  	protected String Password;
  
  	/** test table and util */
  	private String tableName;
  	protected TestDataSource dataSourceInstance;
  	
  	/** instance of DataEngine */
  	protected DataEngine dataEngine;	
  	/**
  	 * Every test case might have one datasource and dataset. They are defined
  	 * in base class for convinience to use.
  	 */
  	protected BaseDataSourceDesign dataSource;
  	protected BaseDataSetDesign dataSet;
  	
  	/*
  	 * @see TestCase#setUp()
  	 */
  	@Before
      public void apiSetUp() throws Exception
  	{
  		DataEngineContext context = DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION,
  				this.scriptContext,
  				(IDocArchiveReader) null,
  				(IDocArchiveWriter) null,
  				(ClassLoader) null );
  		context.setTmpdir( this.getTempDir() );
  		dataEngine = DataEngine.newDataEngine( context );
  		prepareDataSource( );
  	}
  	
  	/*
  	 * @see TestCase#tearDown()
  	 */
  	@After
      public void apiTearDown() throws Exception
  	{
  		dataEngine.shutdown( );
  		closeDataSource( );
  	}
  	
  	/**
  	 * Prepare test table and oda data source and data set design. In most case,
  	 * only one data source needs to be defined.
  	 * 
  	 * @throws Exception
  	 */
  	private void prepareDataSource( ) throws Exception
  	{
  		DataSourceInfo dataSourceInfo = getDataSourceInfo( );
  		if ( dataSourceInfo != null )
  		{
  			// derived class might want to use other data base rather than derby
  			prepareDataSet( dataSourceInfo );
  
  			dataSource = this.dataSourceInstance.getOdaDataSourceDesign( );
  			dataSet = this.dataSourceInstance.getOdaDataSetDesign( );
  			( (OdaDataSetDesign) dataSet ).setQueryText( "select * from "
  					+ dataSourceInfo.tableName );
  			dataEngine.defineDataSource( this.dataSource );
  			dataEngine.defineDataSet( this.dataSet );
  		}
  	}
  	
  	protected Map getAppContext( )
  	{
  		// TODO Auto-generated method stub
  		return null;
  	}
  	
  	/**
  	 * Prepare data source connection property, these properties will be used in
  	 * test table preparation and oda data source preparation.
  	 */
  /*	private void prepareDataSourceProperty()
  	{
  		if ( DriverClass == null )
  			DriverClass = "org.apache.derby.jdbc.EmbeddedDriver";
  		if ( URL == null )
  			URL = JDBCDataSourceUtil.getURL();
  		if ( User == null )
  			User = "user";
  		if ( Password == null )
  			Password = "password";		
  		System.setProperty( "DTETest.driver", DriverClass );		
  		System.setProperty( "DTETest.url", URL );
  		System.setProperty( "DTETest.user",User);
  		System.setProperty( "DTETest.password",Password);	
  	}
  	*/
  	/**
  	 * Prepare test table. This method is defined separatelly since in some test
  	 * cases, they might use more than one data set, although they share the
  	 * same data source.
  	 * 
  	 * @param dataSourceInfo
  	 * @throws Exception
  	 */
  	protected void prepareDataSet( DataSourceInfo dataSourceInfo )
  			throws Exception
  	{
  		if ( dataSourceInfo != null
  				&& dataSourceInfo.tableName != null
  				&& dataSourceInfo.createSql != null
  				&& dataSourceInfo.dataFileName != null )
  		{
  			this.tableName = dataSourceInfo.tableName;
  			
  			this.prepareTestTable( dataSourceInfo.tableName,
  					dataSourceInfo.createSql,
  					dataSourceInfo.dataFileName );
  		}
  	}
  	
  	/**
  	 * Create test table and populate data into table, currently only derby data
  	 * base is used.
  	 * 
  	 * @param tableName
  	 * @param createSql
  	 * @param dataFileName
  	 * @throws Exception
  	 */
  	private void prepareTestTable( String tableName, String createSql,
  			String dataFileName ) throws Exception
  	{		
  		if ( dataSourceInstance == null )
  			dataSourceInstance = JDBCDataSource.newInstance( );
  		
  		// create table
  		this.dataSourceInstance.createTable( tableName,
  				createSql,
  				true );
  		
  		// insert data into table
  		this.dataSourceInstance.populateTable( tableName,
  				getInputFolder(dataFileName) );
  	}
  	
  	/**
  	 * Normally, an API test case will be based on a particular data source, so
  	 * before test begins, test case provides which data source will be used and
  	 * then its data will be prepared and meantime its data source and data set
  	 * will be defined in DataEngine. If the return value is null, there is no
  	 * any datasource and dataset is defined in dataEngine, which means other
  	 * methods such as newDataSet and newReportQuery can not be used.
  	 * 
  	 * @return which data source will be used in this test case
  	 */
  	protected abstract DataSourceInfo getDataSourceInfo( );
  	
  	/**
  	 * Wrap the info for the preparation of data source, when derby is used, it
  	 * needs to provide the createSql and dataFileName to create a table for
  	 * test.
  	 */
  	public class DataSourceInfo
  	{
  		private String tableName;
  		private String createSql;
  		private String dataFileName;
  
  		/**
  		 * @param tableName, used table name
  		 * @param createSql, sql to create table
  		 * @param dataFileName, data to insert table
  		 */
  		public DataSourceInfo( String tableName, String createSql,
  				String dataFileName )
  		{
  			this.tableName = tableName;
  			this.createSql = createSql;
  			this.dataFileName = dataFileName;
  		}
  	}
  	
  	/**
  	 * @throws Exception
  	 */
  	protected void closeDataSource( ) throws Exception
  	{
  		if ( this.dataSourceInstance != null )
  		{
  			if ( tableName != null )
  				dataSourceInstance.dropTable( tableName );
  			this.dataSourceInstance.close( true );
  			this.dataSourceInstance = null;
  		}
  	}
  	
  	/**
  	 * @return test table name
  	 */
  	protected String getTestTableName()
  	{
  		return this.tableName;
  	}
  	
  	/**
  	 * new a JDBC dataset with specified datasetname and querytext
  	 * 
  	 * @param datasetName
  	 * @param queryText
  	 * @return dataset
  	 * @throws Exception
  	 */
  	protected OdaDataSetDesign newDataSet( String datasetName, String queryText )
  			throws Exception
  	{
  		OdaDataSetDesign dset = new OdaDataSetDesign( datasetName );
  		dset.setDataSource( this.dataSource.getName( ) );
  		dset.setQueryText( queryText );
  		dset.setExtensionID( JDBCOdaDataSource.DATA_SET_TYPE );
  		dataEngine.defineDataSet( dset );
  		
  		return dset;
  	}
  	
  	/**
  	 * new a default query, which only has data set information
  	 * 
  	 * @return queryDefn QueryDefinition
  	 */
  	protected QueryDefinition newReportQuery( )
  	{
  		QueryDefinition queryDefn = new QueryDefinition( );
  