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

import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.birt.core.archive.FileArchiveWriter;
import org.eclipse.birt.core.archive.IDocArchiveWriter;
import org.eclipse.birt.report.engine.api.IDataExtractionTask;
import org.eclipse.birt.report.engine.api.IDataIterator;
import org.eclipse.birt.report.engine.api.IExtractionResults;
import org.eclipse.birt.report.engine.api.IReportDocument;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IResultMetaData;
import org.eclipse.birt.report.engine.api.IResultSetItem;
import org.eclipse.birt.report.engine.api.IRunTask;
import org.eclipse.birt.report.tests.engine.EngineCase;

/**
 * <b>Bug description</b>
 * <p>
 * Export data from a table with toc definition inside will return wrong result.
 * <p>
 * <b>Test description</b>
 * <p>
 * This test makes sure that toc defined in data item inside table will not
 * affect data extraction
 * 
 */

public class Regression_122854 extends EngineCase
{

	private final static String INPUT = "regression_122854.rptdesign"; //$NON-NLS-1$
	private final static String OUTPUT = "regression_122854.rptdocument"; //$NON-NLS-1$
	private String inputPath = getClassFolder( )
			+ System.getProperty( "file.separator" ) + INPUT_FOLDER
			+ System.getProperty( "file.separator" );
	private String outputPath = getClassFolder( )
			+ System.getProperty( "file.separator" ) + OUTPUT_FOLDER
			+ System.getProperty( "file.separator" );

	/**
	 * @throws Exception
	 */
	public void test_Regression_122854( )
	{
		try
		{
			/* create report document */
			IDocArchiveWriter fArchive = new FileArchiveWriter( outputPath
					+ OUTPUT );
			IReportRunnable runnable = engine.openReportDesign( inputPath
					+ INPUT );
			IRunTask runTask = engine.createRunTask( runnable );
			runTask.setAppContext( new HashMap( ) );
			runTask.run( fArchive );

			if ( runTask.getErrors( ).size( ) > 0 )
			{
				fail( "Fail to create report document!" );
			}
			runTask.close( );

			/* Extract data */
			IReportDocument reportDoc = engine.openReportDocument( outputPath
					+ OUTPUT );
			IDataExtractionTask extractTask = engine
					.createDataExtractionTask( reportDoc );
			ArrayList resultsetList = (ArrayList) extractTask
					.getResultSetList( );
			assertEquals( "Fail to export data!", 1, resultsetList.size( ) );
			IResultSetItem resultSetItem = (IResultSetItem) resultsetList
					.get( 0 );
			String resultSetName = resultSetItem.getResultSetName( );
			assertEquals( "Return wrong result set name when export data!",
					"t1", resultSetName );
			extractTask.selectResultSet( resultSetName );
			IExtractionResults results = extractTask.extract( );
			assertNotNull( results );
			IDataIterator dataIterator = results.nextResultIterator( );
			assertNotNull( "Export null data!", dataIterator );
			IResultMetaData metaData = dataIterator.getResultMetaData( );
			assertEquals( "Export columns count are incorrect!", 2, metaData
					.getColumnCount( ) );
			assertEquals( "Export incorrect columns", "CUSTOMERNUMBER",
					metaData.getColumnName( 0 ) );
			assertEquals( "Export incorrect columns", "CREDITLIMIT", metaData
					.getColumnName( 1 ) );
			assertEquals( "Exception when export data!", 0, extractTask
					.getErrors( ).size( ) );
			extractTask.close( );
			removeFile( outputPath + OUTPUT );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( "Exception when run DataExtractionTask! "
					+ e.getLocalizedMessage( ) );
		}

	}

}
