
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 hidden column will get task execution error.
 * <p>
 * <b>Test description</b>
 * <p>
 * This test makes sure that data in hidden column can be exported without error
 * 
 */

public class Regression_140269 extends EngineCase
{

	private final static String INPUT = "regression_140269.rptdesign"; //$NON-NLS-1$
	private final static String OUTPUT = "regression_140269.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_140269( )
	{
		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!",
					"table", 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!", 1, metaData
					.getColumnCount( ) );
			assertEquals( "Export columns count are incorrect!", "code",
					metaData.getColumnName( 0 ) );
			int rowCount = 0;
			while ( dataIterator.next( ) )
			{
				rowCount++;
				assertEquals( "Export wrong data.", String.valueOf( rowCount ),
						dataIterator.getValue( "code" ) );
			}
			assertEquals( "Export wrong data rows.", 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( ) );
		}

	}

}
