
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.IResultSetItem;
import org.eclipse.birt.report.engine.api.IRunTask;
import org.eclipse.birt.report.tests.engine.EngineCase;

/**
 * <b>Bug description</b>
 * <p>
 * Data in table with highlight/map setting cannot be exported
 * <p>
 * <b>Test description</b>
 * <p>
 * This test makes sure that data in table with highlight and map can be
 * exported correctly.
 * 
 */

public class Regression_121863 extends EngineCase
{

	private final static String INPUT = "regression_121863.rptdesign"; //$NON-NLS-1$
	private final static String OUTPUT = "regression_121850.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_121863( )
	{
		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 );
			int rowCount = 0;
			IDataIterator dataIterator = results.nextResultIterator( );
			assertNotNull( "Export null data!", dataIterator );
			while ( dataIterator.next( ) )
			{
				rowCount++;
				Object data = dataIterator.getValue( 0 );
				assertEquals( "Export wrong data!", String.valueOf( rowCount ),
						data.toString( ) );
			}
			assertEquals( "Export wrong datarow size!", 7, rowCount );
			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( ) );
		}

	}

}
