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

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.tests.engine.EngineCase;

/**
 * <b>Bug description</b>
 * <p>
 * Error messages in preview shouldn't show "null"
 * <p>
 * <b>Test description</b>
 * <p>
 * This test makes sure that error messages display correctly.
 * 
 * 
 */
public class Regression_120163 extends EngineCase
{

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

	/**
	 * @throws Exception
	 */

	public void test_Regression_120163( ) throws Exception
	{
		IReportRunnable runnable = engine.openReportDesign( inputPath + INPUT );
		HTMLRenderOption options = new HTMLRenderOption( );
		options.setOutputFormat( "html" );
		options.setOutputFileName( outputPath + OUTPUT );
		HTMLRenderContext renderContext = new HTMLRenderContext( );
		renderContext.setImageDirectory( "image" );
		HashMap appContext = new HashMap( );
		appContext.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
				renderContext );

		IRunAndRenderTask task = engine.createRunAndRenderTask( runnable );
		task.setAppContext( appContext );
		task.setRenderOption( options );
		task.run( );
		assertTrue( task.getErrors( ).size( ) > 0 );
		task.close( );
		checkChartError( );
		this.removeFile( outputPath + OUTPUT );
	}

	/**
	 * Check the error message marked out chart id instead of null.
	 * 
	 * @throws IOException
	 */
	private void checkChartError( ) throws IOException
	{
		BufferedReader reader = new BufferedReader( new FileReader( outputPath
				+ OUTPUT ) );
		String line = null;
		boolean found = false;
		while ( ( line = reader.readLine( ) ) != null )
		{
			if ( line.indexOf( "Chart (id = 11)" ) != -1 )
			{
				found = true;
				break;
			}
		}
		assertTrue( found );
	}

}
