/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

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

import org.apache.cactus.WebRequest;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.servlet.BirtEngineServlet;
import org.eclipse.birt.report.tests.viewer.BaseViewerTestCase;
import org.xml.sax.SAXException;

import com.ibm.icu.util.ULocale;
import com.meterware.httpunit.HTMLElement;
import com.meterware.httpunit.WebResponse;

/**
 * <b>Bug Description</b>
 * <p>
 * on birt runtime (i use tomcat) if file.rptdesign is update, birt not check
 * this update and it run the old file.rptdocument
 * <p>
 * <b>Test Description</b>
 * <p>
 * Change report design, make sure the response content is updated
 */
public class Regression_122839 extends BaseViewerTestCase
{
	private final static String REPORT = "report/Regression_122839.rptdesign"; //$NON-NLS-1$

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

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

	/**
	 * Process request
	 */

	public void test_regression_122839( )
	{
		BirtEngineServlet servlet = new BirtEngineServlet( );

		try
		{
			servlet.init( config );
			servlet.doGet( request, response );

			IDesignEngine designEngine = new DesignEngine( new DesignConfig( ) );
			SessionHandle session = designEngine
					.newSessionHandle( ULocale.ENGLISH );
			String report = CATALINAHOME + "/webapps/" + RUNTIME + "/" + REPORT; //$NON-NLS-1$//$NON-NLS-2$

			// rename the title to 'Report' and save

			ReportDesignHandle designHandle = session.openDesign( report );
			((LabelHandle)designHandle.findElement( "label" )).setText( "bbb" ); //$NON-NLS-1$ //$NON-NLS-2$
			designHandle.saveAs( report );

			// view the report again.

			servlet.doGet( request, response );

		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			fail( e.toString( ) );
		}
	}

	/**
	 * get response, make sure the title is refreshed to new value.
	 * 
	 * @param webResponse
	 * @throws SAXException
	 */

	public void end_regression_122839( WebResponse webResponse )
			throws SAXException
	{
		// make sure the title is refreshed.

		HTMLElement element = webResponse.getElementWithID( "labelbookmark" );  //$NON-NLS-1$
		assertNotNull( element );
		assertEquals("bbb", element.getText( )); //$NON-NLS-1$
	}
}
