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

import java.io.File;

import org.apache.cactus.WebRequest;
import org.eclipse.birt.report.servlet.ViewerServlet;
import org.eclipse.birt.report.tests.viewer.BaseViewerTestCase;

import com.meterware.httpunit.WebResponse;

/**
 * <b>Steps to reproduce:</b>
 * <ol>
 * <li>Create a report and add a chart
 * <li>Set TOC for the chart
 * <li>Preview and click on it in TOC
 * </ol>
 * <b>Actual result:</b>
 * <p>
 * It did not link to dedicated chart
 * <p>
 * <b>Expected result:</b>
 * <p>
 * It links to dedicated chart
 * </p>
 * <b>Test description:</b>
 * <p>
 * Verify that TOC can be located
 * </p>
 */
public class Regression_122594 extends BaseViewerTestCase
{

	private final static String REPORT = "report/regression_122594.rptdesign";
	private final static String REPORTDOCUMENT = CATALINAHOME
			+ "/webapps/" + RUNTIME + "/report/regression_122594.rptdocument";

	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_regression_122594( WebRequest webRequest )
			throws Exception
	{
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		webRequest.addParameter( PARAM_REPORT, REPORT );
		webRequest.addParameter( PARAM_REPORT_DOCUMENT, REPORTDOCUMENT );
		webRequest.addHeader( "request-type", "SOAP" );
		webRequest.setContentType( "text/xml; charset=utf-8" );
		webRequest.addHeader( "SOAPAction", "''" );
		webRequest.setUserData( getTOCSoapMessage( "chart" ) );
	}

	/**
	 * test_regression_122594
	 */
	public void test_regression_122594( )
	{
		ViewerServlet servlet = new ViewerServlet( );
		try
		{
			servlet.init( config );
			servlet.doPost( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * @param webResponse
	 */
	public void end_regression_122594( WebResponse webResponse )
			throws Exception
	{
		String content = webResponse.getText( );

		String imageSRC = "__imageID=";
		String imageEXT = ".png";
		int sIndex = content.indexOf( imageSRC ) + imageSRC.length( );
		int eIndex = content.indexOf( imageEXT ) + imageEXT.length( );
		String imageDirectory = CATALINAHOME
				+ "/webapps/" + RUNTIME + "/report/images/";
		String imageFileName = imageDirectory
				+ content.substring( sIndex, eIndex );
		File imageFile = new File( imageFileName );
		assertTrue( imageFile.exists( ) );
		if ( imageFile.length( ) <= 0 )
			fail( "image with error" );

		sIndex = 0;
		eIndex = 0;

		String tag = "<PageNumber>2</PageNumber>";
		sIndex = content.indexOf( tag );
		eIndex = sIndex + tag.length( );
		assertEquals( tag, content.substring( sIndex, eIndex ) );
	}

}