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

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import org.apache.cactus.HttpSessionCookie;
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>Bug description</b>
 * <p>
 * Serious issue with viewerserlet
 * </p>
 * <b>Steps to reproduce:</b>
 * <p>
 * Using the /preview and /frameset the system seem to build a
 * *reportname.rptdocument** file in order to cache it.
 * 
 * Seems that calling the report with different parameters returns and older
 * version, with wrong data.
 * 
 * Looks that several people running the same report will always get the first
 * generated version, no matter of the parameters.
 * 
 * Changing the parameters in the dialog (run report) seems to re-generate the
 * report, but not changing the params in the url.
 * 
 * I'm passing the report parameters in the URL, and trying to get rid of the
 * params screen (I have own jsp's for the parameters).
 * 
 * 
 * 
 * /run works fine, as it does not cache anything into a file..
 * </p>
 * <b>Test description:</b>
 * <p>
 * This test maily ensure that viewer create two different folder for two
 * session .
 * </p>
 */
public class Regression_129089 extends BaseViewerTestCase
{

	private String REPORT = "report/regression_129089.rptdesign"; //$NON-NLS-1$
	private static int count = 0;
	private static boolean t1 = false, t = false;
	private static File docDir = new File( CATALINAHOME + "/webapps/" + RUNTIME
			+ "/documents/" );

	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_Regression_129089( WebRequest webRequest )
			throws Exception
	{
		assertNotNull( docDir );
		if ( !docDir.exists( ) )
		{
			count = 0;
		}
		else
		{
			count = docDir.list( ).length;
		}
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		webRequest.addParameter( PARAM_REPORT, REPORT );
		webRequest.addHeader( "request-type", "SOAP" );//$NON-NLS-1$
		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_129089( )
	{
		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_129089( WebResponse webResponse )
			throws Exception
	{
		assertEquals( 200, webResponse.getResponseCode( ) );
		t = true;
	}

	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_Regression_129089_1( 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$
		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_129089_1( )
	{
		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_129089_1( WebResponse webResponse )
			throws Exception
	{
		assertEquals( 200, webResponse.getResponseCode( ) );
		t1 = true;
	}

	public void begin_Regression_129089_2( WebRequest webRequest )
			throws Exception
	{
		assertTrue( t && t1 );
		assertEquals( count + 2, docDir.list( ).length );
	}

	public void test_Regression_129089_2( )
	{
	}

	/* 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( ) );
	}

}