
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>
 * <property name="cachedRowCount">200</property>
 * <p>
 * Since the cache poperty is in the XML, this is taken during deployment also.
 * WHen i design i need only few rows (say 100 rows ) But when i deploy i need
 * all the rows Since <property name="cachedRowCount">200</property> is part of
 * the XML file, the output/pdf shows only what is specified during design
 * phase. The documentation shows, during deployment this setting will not be
 * considered, but i found the other way- during design and deployment.
 * <b>Expected result:</b>
 * <p>
 * if __designer=true, then enable the cachedRowCount, otherwise disable it.
 * Furthermore, if running in Eclipse, viewer will create URL and set __designer
 * to true always. In the deployment, by default __designer=false, neither
 * cachedRowCount disabled.
 * <p>
 * <b>Test description:</b>
 * <p>
 * In the test report data set cachedRowCount is set to 1, we add test to ensure
 * that when __designer=true, only one data row will be retrieved; otherwise if
 * __designer=false, none data will be cached.
 */

public class Regression_134013 extends BaseViewerTestCase
{

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

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

	public void begin_regression_134013( WebRequest webRequest )
	{
		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		webRequest.addParameter( PARAM_REPORT, REPORT );
		webRequest.addParameter( PARAM_LOCALE, "en_US" ); //$NON-NLS-1$

		// viewing from designer env, cachedRowCount is enabled.
		webRequest.addParameter( "__designer", "true" ); //$NON-NLS-1$//$NON-NLS-2$
	}

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

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

	public void end_regression_134013( 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$

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

		assertNotNull( bk1 );
		assertNotNull( bk2 );

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

		assertEquals( "103", bk1[0].getText( ) ); //$NON-NLS-1$
		assertEquals( "Atelier graphique", bk2[0].getText( ) ); //$NON-NLS-1$
	}

}