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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.birt.report.tests.engine.EngineCase;

/**
 * <b>Bug description</b>
 * <p>
 * Image has not been display on HTML
 * 
 * <p>
 * <b>Test description</b>
 * <p>
 * This test make sure that the image from dataset is saved in image folder when
 * generate HTML
 * 
 */
public class Regression_120513 extends EngineCase
{

	final static String INPUT = "regression_120513";

	public void test_Regression_120513( ) throws Exception
	{

		runAndThenRender( INPUT );
		testCompare( INPUT );
	}

	/**
	 * generate report document and HTML
	 * 
	 * @param fileName
	 * @throws Exception
	 */
	protected void runAndThenRender( String fileName ) throws Exception
	{
		String inputDesignFile = fileName + ".rptdesign";
		String outputHTMLFile = fileName + ".html";
		runAndThenRender( inputDesignFile, outputHTMLFile, "All", "HTML" );
	}

	/**
	 * compare with golden file
	 * 
	 * @param fileHTMLName
	 * @param goldImage
	 * @throws IOException
	 */
	protected void testCompare( String fileHTMLName ) throws IOException
	{

		String fileHTMLFile = getClassFolder( ) + "/" + OUTPUT_FOLDER + "/"
				+ fileHTMLName + ".html";
		BufferedReader reader = new BufferedReader( new FileReader( new File(
				fileHTMLFile ) ) );
		String line = null;
		String imageName = null;
		int startNumber = 0;
		int endNumber = 0;
		while ( ( line = reader.readLine( ) ) != null )
		{
			if ( line.indexOf( "src=\"image\\" ) > 0 )
			{
				startNumber = line.trim( ).indexOf( "image\\" ) + 6;
				endNumber = line.trim( ).lastIndexOf( "alt=" ) - 2;
				imageName = line.trim( ).substring( startNumber, endNumber );
				break;
			}
		}
		if ( imageName == null )
			fail( "Can not find the image name, test fail!" );

		String goldenImageFile = getClassFolder( ) + "/" + OUTPUT_FOLDER + "/"
				+ "image" + "/" + imageName;
		File inputFile = new File( goldenImageFile );
		if ( inputFile.isFile( ) )
		{
			long len = inputFile.length( );
			assertTrue( imageName + " is not empty and test pass.", len > 0 );
			assertEquals( imageName + " file is incorrect!", 40023, len );
		}
		else
			fail( imageName + " file is not exist, test fail!" );

	}

}
