/*******************************************************************************
 * 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 java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

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.WebResponse;

/**
 * <b>Bug Description:</b>
 * <p>
 * When generating the url with bookmark, current url will be http://xxxxxx#XXX.
 * This is a standard html bookmark,which only works with http GET method. When
 * using POST method, most of app server can not recognize bookmark. They simply
 * treat it as part of parameter value. In web viewer, all requets are posted as
 * soap message exception 1st url and exporting data request.
 * <p>
 * Therefore, bookmark in birt is not bookmark in standard html. Expected url to
 * be generated as http://xxxxxxxxxxx&bookmark=XXX.
 * <p>
 * <b>Test Description:</b>
 * <p>
 * This test maily ensure that the html hyperlink tag is rendered as the format
 * of http://xxxxxxxxxxx&bookmark=XXX.
 */
public class Regression_121174 extends BaseViewerTestCase
{
	private final static String REPORT = "report/Regression_121174.rptdesign"; //$NON-NLS-1$

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

	public void begin_regression_121174( WebRequest webRequest )
	{
		// viewer "/frameset" pattern

		webRequest.setURL( null, null, SERVLET_PATH_FRAMESET, null, null );
		webRequest.addParameter( PARAM_REPORT, REPORT );
		webRequest.addParameter( PARAM_LOCALE, "en_US" ); //$NON-NLS-1$
	}

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

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

	public void end_regression_121174( WebResponse webResponse ) throws SAXException,
			UnsupportedEncodingException
	{
		String hyperlink = webResponse.getLinkWith( "aaa" ) //$NON-NLS-1$
				.getURLString( );
		assertEquals( getRuntimeFolder( )
				+ "report/Regression_121174b.rptdesign", URLDecoder.decode( //$NON-NLS-1$
				getReportParam( hyperlink ), "UTF-8" ) ); //$NON-NLS-1$
		assertEquals( "html", getFormatParam( hyperlink ).substring( 0, 4 ) );//$NON-NLS-1$
		assertEquals( "en_US", getLocaleParam( hyperlink ) );//$NON-NLS-1$
		assertEquals( "true", getMasterpageParam( hyperlink ).substring( 0, 4 ) );//$NON-NLS-1$
		assertEquals("bookmark", getBookmarkParam( hyperlink )); //$NON-NLS-1$
	}
	private String getBookmarkParam( String hyperlink )
	{
		return hyperlink.substring( hyperlink.indexOf( "__bookmark" )+ 11 ); //$NON-NLS-1$
	}

}
