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

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.cactus.WebRequest;
import org.eclipse.birt.report.servlet.BirtEngineServlet;
import org.eclipse.birt.report.servlet.ViewerServlet;
import org.eclipse.birt.report.tests.viewer.BaseViewerTestCase;
import org.xml.sax.SAXException;

import com.meterware.httpunit.WebResponse;

/**
 * <b>Bug description</b>
 * <p>
 * fail to drill through to the predefined table of contents
 * <b>Steps to reproduce:</b>
 * <ol>
 * <li>preview the attached report desgin
 * <li>click on a link named drillthrough
 * </ol>
 * <b>Actual result:</b>
 * <p>
 * Error popped up
 * <p>
 * <b>Expected result:</b>
 * <p>
 * the target report is displayed
 * </p>
 * <b>Test description:</b>
 * <p>
 * This test maily ensure that the target html hyperlink tag is rendered correctly.
 * </p>
 */
public class Regression_134207 extends BaseViewerTestCase
{

	private final static String REPORT = "report/Regression_134207.rptdesign"; //$NON-NLS-1$

	/**
	 * @param webRequest
	 * @throws Exception
	 */

	public void begin_Regression_134207( WebRequest webRequest ) throws Exception
	{
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		webRequest.addParameter( PARAM_REPORT, REPORT );
		webRequest.addHeader( "request-type", "SOAP" );//$NON-NLS-1$//$NON-NLS-2$
		webRequest.setContentType( "text/xml; charset=utf-8" ); //$NON-NLS-1$
		webRequest.addParameter( PARAM_LOCALE, "en_US" );
		webRequest.addHeader( "SOAPAction", "''" ); //$NON-NLS-1$//$NON-NLS-2$
		webRequest.setUserData( getSoapStream( ) );
	}

	/**
	 * 
	 */

	public void test_Regression_134207( )
	{
		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_134207( WebResponse webResponse ) throws Exception
	{
		assertEquals( 200, webResponse.getResponseCode( ) );
		String hyperlink=webResponse.getText( );
		assertTrue(hyperlink.indexOf( "href" )!=-1);
		hyperlink=hyperlink.substring( hyperlink.indexOf( "href" )+11, hyperlink.indexOf( "target" )-7 );
		String hyperlink1=URLDecoder.decode(getReportParam( hyperlink ), "UTF-8" );
		hyperlink1=hyperlink1.substring( 0,hyperlink1.length( )-8 );
		assertEquals( CATALINAHOME + "/webapps/"+ RUNTIME //$NON-NLS-1$
				+ "/report/regression_134207b.rptdesign", hyperlink1); //$NON-NLS-1$
		
		assertTrue(hyperlink.indexOf( "__bookmark=toc" )!=-1);
		assertTrue(hyperlink.indexOf( "__format=html" )!=-1);
		assertTrue(hyperlink.indexOf( "__locale=en_US" )!=-1);
		assertTrue(hyperlink.indexOf( "__overwrite=true" )!=-1);
	}

	/* utility methods */

	private InputStream getSoapStream( )
	{
		String soapMessage = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"><Body><GetUpdatedObjects xmlns=\"http://schemas.eclipse.org/birt\"><Operation><Target><Id>Document</Id><Type>Document</Type></Target><Operator>GetPage</Operator><Oprand><Name>page</Name><Value>1</Value></Oprand><Oprand><Name>svg</Name><Value>false</Value></Oprand></Operation></GetUpdatedObjects></Body></Envelope>"; //$NON-NLS-1$
		return new ByteArrayInputStream( soapMessage.getBytes( ) );
	}

}