
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>
 * Cannot view report document from web viewer. 
 * <b>Steps to reproduce:</b>
 * <p>
 * Put a report document under the root folder of web viewer deployment, using
 * following URL to view it:
 * http://localhost:8888/birt_web/frameset?__document=customer1p-1kr.rptdocument
 * got error message "The design file c:\work\tomcat41\webapps\birt_web\ has
 * error and can not be run." in the browser. It seems the report document name
 * is missed in the full path of the document when web viewer wants to open it.
 * </p>
 * <b>Test description:</b>
 * <p>
 * This test maily ensure that the full path of the report document in the url
 * is correct.
 * </p>
 */
public class Regression_136770 extends BaseViewerTestCase
{

	private final static String REPORT = "report/regression_136770.rptdesign"; //$NON-NLS-1$
	private String document = CATALINAHOME + "/webapps/" + RUNTIME
			+ "/regression_136770.rptdocument";
	private static HttpSessionCookie sessionCookie;
	private static boolean flag=false;

	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_Regression_136770( WebRequest webRequest )
			throws Exception
	{
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		webRequest.addParameter( PARAM_REPORT, REPORT );
		webRequest.addParameter( PARAM_REPORT_DOCUMENT, document );
		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( ) );
		sessionCookie=webRequest.getSessionCookie( );
		webRequest.addCookie( sessionCookie );
	}

	/**
	 * 
	 */
	public void test_Regression_136770( )
	{
		session.setAttribute( "test", "ok" );
		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_136770( WebResponse webResponse )
			throws Exception
	{
		this.flag=true;
		assertEquals( 200, webResponse.getResponseCode( ) );
		File doc=new File(CATALINAHOME + "/webapps/" + RUNTIME
				+ "/regression_136770.rptdocument");
		assertNotNull(doc);
		assertTrue(doc.exists( ));
		assertTrue(doc.length( )>0);
	}

	public void begin_Regression_136770_1( WebRequest webRequest )
			throws Exception
	{
		assertTrue(flag);
		assertNotNull(sessionCookie);
		webRequest.addCookie( sessionCookie );
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		document = "regression_136770.rptdocument";
		webRequest.addParameter( PARAM_REPORT_DOCUMENT, document );
		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_136770_1( )
	{
		assertEquals("ok",session.getAttribute( "test" ));
		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_136770_1( WebResponse webResponse )
			throws Exception
	{
		assertEquals( 200, webResponse.getResponseCode( ) );
		File doc=new File(CATALINAHOME + "/webapps/" + RUNTIME
				+ "/regression_136770.rptdocument");
		doc.delete( );
	}

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

}