
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.io.UnsupportedEncodingException;
import java.net.URLDecoder;
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.BirtEngineServlet;
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.HTMLElement;
import com.meterware.httpunit.WebResponse;

/**
 * <b>Description:</b>
 * <p>
 * parameter dialog is not displayed when drill through to a PDF report that
 * contains parameter with no default value
 * <p>
 * <b>Steps to reproduce:</b>
 * <p>
 * create 1st report - add a report parameter (no default value) - create 2nd
 * report - add a label "link" - edit hyperlink property (type: drill-through,
 * target report format: PDF) - preview 1st report - click on "link" <b>Actual
 * result:</b>
 * <p>
 * "Some required parameter values are not set or set to incompatible data
 * type." was displayed instead.
 * <p>
 * <b>Expected result:</b>
 * <p>
 * parameter dialog should be displayed - target report is rendered in PDF
 * format
 * </p>
 * <b>Test description:</b>
 * <p>
 * This test ensures that parameter dialog is poped up
 * </p>
 */

public class Regression_135207 extends BaseViewerTestCase
{
	private static String REPORT = "report/Regression_135207.rptdesign"; //$NON-NLS-1$

	
	/**
	 * Set request url for Regression_135207.rptdesign
	 * 
	 * @param webRequest
	 */

	public void begin_regression_135207( WebRequest webRequest )
	{
		// viewer "/run" pattern
		
		webRequest.setURL( null, null, SERVLET_PATH_RUN, null, null );
		webRequest.addParameter( PARAM_REPORT, 
				REPORT );
		webRequest.addParameter( PARAM_LOCALE, "en_US" );  //$NON-NLS-1$
	}

	/**
	 * Process "/run" request
	 */
	public void test_regression_135207( )
	{
		BirtEngineServlet servlet = new BirtEngineServlet( );
		try
		{
			servlet.init( config );
			servlet.doGet( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * get "/run" response, and assert contents status and check the hyperlink
	 * of label.
	 * 
	 * @param webResponse
	 * @throws SAXException
	 * @throws UnsupportedEncodingException
	 */

	public void end_regression_135207( WebResponse webResponse ) throws SAXException,
			UnsupportedEncodingException
	{
		String hyperlink = webResponse.getLinkWith( "drill through link" ) //$NON-NLS-1$
				.getURLString( );
		assertEquals( CATALINAHOME + "/webapps/"+ RUNTIME //$NON-NLS-1$
				+ "/report/regression_135207b.rptdesign", URLDecoder.decode( //$NON-NLS-1$
				getReportParam( hyperlink ), "UTF-8" ) ); //$NON-NLS-1$
		assertEquals( "pdf", getFormatParam( hyperlink ) );//$NON-NLS-1$
		assertEquals( "en_US", getLocaleParam( hyperlink ) );//$NON-NLS-1$
		assertEquals( "true", getMasterpageParam( hyperlink ) );//$NON-NLS-1$
	}

	
	
	public void begin_regression_135207_1( WebRequest webRequest )
	{
		// viewer "/run" pattern
		REPORT = "report/Regression_135207b.rptdesign"; 
		webRequest.setURL( null, null, SERVLET_PATH_RUN, null, null );
		webRequest.addParameter( PARAM_REPORT, 
				REPORT );
		webRequest.addParameter( PARAM_LOCALE, "en_US" );  //$NON-NLS-1$
		webRequest.addParameter( PARAM_FORMAT, "pdf" );
	}

	/**
	 * Process "/run" request
	 */
	public void test_regression_135207_1( )
	{
		BirtEngineServlet servlet = new BirtEngineServlet( );
		try
		{
			servlet.init( config );
			servlet.doGet( request, response );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * get "/run" response, and assert contents status and check the hyperlink
	 * of label.
	 * 
	 * @param webResponse
	 * @throws SAXException
	 * @throws IOException 
	 */

	public void end_regression_135207_1( WebResponse webResponse ) throws SAXException,
			IOException
	{
		assertEquals(200,webResponse.getResponseCode( ));
		String content=webResponse.getText( );
		content=content.substring( content.indexOf( "<TITLE>" )+7,content.indexOf( "</TITLE>" ) );
		System.out.println(content );

		assertEquals("PARAMETER SELECTION PAGE",content);
	}
	
}