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

import java.io.ByteArrayInputStream;
import java.io.File;
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>
 * Image can not be previewed again if the report has been previewed in
 * WebViewer. 
 * </p>
 * <b>Steps to reproduce:</b>
 * <ol>
 * <li>Create a report, insert an image, select type as URI and enter a
 * ralative path, "a.jpg" for example(the image exsit under the same directory
 * as the report)
 * <li>Preview in embedded viewer, see the image can be displayed.
 * <li>Then preview the page in WebViewer, see the image can also be displayed.
 * <li>In this state, the image can no longer be displayed in embedded viewer.
 * </ol>
 * </p>
 * <b>Test description:</b>
 * <p>
 * This test maily ensure that the url image is found in web viewer.
 * </p>
 */
public class Regression_137139 extends BaseViewerTestCase
{

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

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

	public void begin_Regression_137139( 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_137139( )
	{
		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_137139( WebResponse webResponse )
			throws Exception
	{
		assertEquals( 200, webResponse.getResponseCode( ) );
		String hyperlink = webResponse.getText( );
		hyperlink = hyperlink.substring( hyperlink.indexOf( "BOOKMARK" ) );
		String imageURL = hyperlink.substring( hyperlink.indexOf( "src" ) + 11,
				hyperlink.indexOf( "jpg" ) + 3 );
		imageURL = URLDecoder.decode( imageURL, "UTF-8" );
		String imageName=imageURL.substring( imageURL.indexOf("=")+1,imageURL.indexOf("jpg")+3 );
		imageURL = imageURL.replaceAll( "file.*.jpg", "file.jpg" );
		assertEquals( RUNTIME + "/run?__imageID=file.jpg", imageURL );
		File imageFile=new File(CATALINAHOME+"/webapps/"+RUNTIME+"/report/images/"+imageName);
		assertTrue(imageFile.exists( ));
		assertTrue(imageFile.length( )>0);

	}

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

}