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

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.ParserConfigurationException;

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

import utility.HTMLDecoder;

import com.meterware.httpunit.WebResponse;

/**
 * <b>Description:</b>
 * <p>
 * [Smoke][Regression]:Preivew in Web Viewer, image can not be displayed
 * <p>
 * <b>Steps to reproduce:</b>
 * <ol>
 * <li>Insert a image into layout
 * <li>Preview in Web Viewer Actual Results: 1.Image can not be displayed
 * </ol>
 * <b>Expected Results:</b>Image can be displayed
 * <p>
 * <b>Test description:</b> View the report, get the response, make sure the
 * image source link is correct and the catched image is generated under correct
 * path.
 */
public class Regression_143891 extends BaseViewerTestCase
{

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

	/**
	 * @param webRequest
	 * @throws Exception
	 */
	public void begin_regression_143891( WebRequest webRequest )
	{
		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.addHeader( "SOAPAction", "''" ); //$NON-NLS-1$//$NON-NLS-2$
		webRequest.setUserData( getSoapStream( ) );
		webRequest.addParameter( PARAM_LOCALE, "en_US" ); //$NON-NLS-1$

	}

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

	/**
	 * test_regression_143891
	 */
	public void test_regression_143891( )
	{
		ViewerServlet servlet = new ViewerServlet( );

		try
		{
			servlet.init( config );
			servlet.doPost( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * @param webResponse
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws Exception
	 */
	public void end_regression_143891( WebResponse webResponse )
			throws IOException, SAXException, ParserConfigurationException
	{
		String text = HTMLDecoder.decode( webResponse.getText( ) );
		System.out.println( text );

		Pattern pattern = Pattern.compile( "<img id=\"bk1\".*>" ); //$NON-NLS-1$
		Matcher matcher = pattern.matcher( text );

		// locate first match of: <img id="bk1"
		// src="/WebViewerExample_0823/run?__imageID=file3.jpg" alt="">

		boolean find = matcher.find( );
		assertTrue( find );

		String line = matcher.group( );
		System.out.println( line );
		int start = line.indexOf( "src=\"" ); //$NON-NLS-1$

		String src = line
				.substring( start + 5, line.indexOf( "\"", start + 5 ) ); //$NON-NLS-1$
		System.out.println( src );

		String imageName = src.substring( src.indexOf( "=" ) + 1 ); //$NON-NLS-1$
		System.out.println( imageName );

		// make sure the temparary image is generated under default image
		// folder.

		String imageFilePath = getRuntimeFolder( ) + IMAGE_DIR + "/" //$NON-NLS-1$
				+ imageName;
		System.out.println( imageFilePath );

		File imageFile = new File( imageFilePath );
		assertTrue( imageFile.exists( ) );
	}

}