
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.engine.api.EngineException;
import org.eclipse.birt.report.servlet.ViewerServlet;
import org.eclipse.birt.report.tests.viewer.BaseViewerTestCase;
import org.eclipse.birt.report.tests.viewer.XmlUtility;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import utility.EngineCase;
import utility.HTMLDecoder;
import utility.StringUtil;

import com.meterware.httpunit.WebResponse;

/**
 * <b>Description:</b>
 * <p>
 * HCG_BIDI: Preview tab is not mirrored
 * <p>
 * <b>Steps to reproduce:</b>
 * <p>
 * <ol>
 * <li>Start eclipse with "-dir rtl" option
 * <li>Create any report and use Template Image option to put any image to the
 * report Result: The image appear in the right corner of the report on the
 * Layout page. (Notice the when you run Eclipse in the usual mode the image
 * would appear in the left corner of the report). This is expected behavior
 * <li>Switch to Preview tab Result: The image appear in the left most corner
 * as if Eclipse was ran in regular mode (not with "-dir rtl")
 * </ol>
 * <b>Actual result:</b>
 * <p>
 * The image appear in the left most corner as if Eclipse was ran in regular
 * mode (not with "-dir rtl")
 * <p>
 * <b>Expected result:</b>
 * <p>
 * The image should appear in the rightmost corner as on the Layout tab.
 * </p>
 * <b>Test description:</b>
 * <p>
 * Specify __rtl=true, make sure that viewer support right-to-left parameters,
 * ensure that text direction is changed correctly.
 * </p>
 */

public class Regression_137421 extends BaseViewerTestCase
{

	private final static String INPUT = "regression_137421.rptdesign"; //$NON-NLS-1$

	static class RunReport extends EngineCase
	{

		/**
		 * @throws Exception
		 */

		public RunReport( ) throws Exception
		{
			this.setUp( );
		}

		void runReport( ) throws EngineException
		{
			this.run( INPUT, "regression_137421.rptdocument" ); //$NON-NLS-1$
		}
	}

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

	public void begin_regression_137421( WebRequest webRequest )
			throws Exception
	{
		// run the report, generate report document and deploy it to the app
		// server.

		RunReport runner = new RunReport( );
		runner.runReport( );

		String docpath = this.getClassFolder( ) + OUTPUT_FOLDER
				+ "regression_137421.rptdocument"; //$NON-NLS-1$
		File document = new File( docpath );
		assertTrue( document.exists( ) );

		copyFile( docpath, this.getRuntimeFolder( )
				+ "report/regression_137421.rptdocument" ); //$NON-NLS-1$

		// viewing the report document in
		// "report/regression_137421.rptdocument".

		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		webRequest.addParameter( PARAM_REPORT_DOCUMENT,
				"report/regression_137421.rptdocument" ); //$NON-NLS-1$
		webRequest.addHeader( "request-type", "SOAP" );//$NON-NLS-1$//$NON-NLS-2$
		webRequest.setContentType( "text/html; 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$
	}

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

	/**
	 * Process request
	 * 
	 * @throws EngineException
	 */

	public void test_regression_137421( )
	{
		ViewerServlet servlet = new ViewerServlet( );
		try
		{
			servlet.init( config );
			servlet.doPost( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * get response, and make sure style is right-aligned.
	 * 
	 * @param webResponse
	 * @throws SAXException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 */

	public void end_regression_137421( WebResponse webResponse )
			throws SAXException, IOException, ParserConfigurationException
	{
		assertEquals( 200, webResponse.getResponseCode( ) );

		String text = HTMLDecoder.decode( webResponse.getText( ) );
		System.out.println( text );

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

		// locate first match of: <div id=\"bookmark\" style=\"text-align:left;\">Hello world</div>

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

		String line = matcher.group( );

		Document doc = XmlUtility.readXML( line );
		Node div = doc.getElementsByTagName( "div" ).item( 0 ); //$NON-NLS-1$
		
		String style = StringUtil.trimString( div.getAttributes( )
				.getNamedItem( "style" ).getNodeValue( ) ); //$NON-NLS-1$

		// make sure style is right-aligned.
		
		assertEquals( "text-align:left;", style ); //$NON-NLS-1$
		assertEquals( "Hello world", div.getFirstChild( ).getNodeValue( )  ); //$NON-NLS-1$
	}
}