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

import java.io.IOException;

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

import com.meterware.httpunit.HTMLElement;
import com.meterware.httpunit.WebResponse;

/**
 * <b>Description:</b>
 * <p>
 * Support UI preference to limit report preview max rows
 * <p>
 * UI will add a new preference to limit report preview max rows, and viewer
 * should support it when preview report in designer.
 * <p>
 * <b>Test description:</b> Add "__maxrows=1" in request URL, make sure that
 * only one data row will be retrieved in the response.
 * <p>
 */

public class Regression_142673 extends BaseViewerTestCase
{

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

	/**
	 * Set request url
	 * 
	 * @param webRequest
	 */

	public void begin_regression_142673( WebRequest webRequest )
	{
		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( "__designer", "true" ); //$NON-NLS-1$//$NON-NLS-2$
		webRequest.addParameter( "__maxrows", "1" ); //$NON-NLS-1$//$NON-NLS-2$
	}

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

	/**
	 * get response, __maxrows=1 is enabled, so assert only one data rows are
	 * returned.
	 * 
	 * @param webResponse
	 * @throws SAXException
	 * @throws IOException
	 */

	public void end_regression_142673( WebResponse webResponse )
			throws SAXException, IOException
	{
		System.out.println( webResponse.getText( ) );

		HTMLElement[] bk1 = webResponse.getElementsWithAttribute( "id", "bk1" ); //$NON-NLS-1$ //$NON-NLS-2$
		HTMLElement[] bk2 = webResponse.getElementsWithAttribute( "id", "bk2" ); //$NON-NLS-1$ //$NON-NLS-2$

		// __maxrows=1 is enabled, only one data rows are returned.

		assertNotNull( bk1 );
		assertNotNull( bk2 );

		assertEquals( 1, bk1.length );
		assertEquals( 1, bk2.length );
	}

}