/*******************************************************************************
 * 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.model.script;

import org.eclipse.birt.report.model.api.ScalarParameterHandle;
import org.eclipse.birt.report.model.api.ScriptDataSetHandle;
import org.eclipse.birt.report.model.api.ScriptDataSourceHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.TableHandle;

/**
 * Test cases for NativeReport.
 */

public class NativeReportTest extends ScriptTestCase
{

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */

	protected void setUp( ) throws Exception
	{
		super.setUp( );

		context.registerBean( "design", //$NON-NLS-1$
				new ReportDefinition( designHandle ) );
	}

	/**
	 * Tests the design on the report.
	 * 
	 * @throws Exception
	 */

	public void testReportDefn( ) throws Exception
	{

		designHandle.setAuthor( "BIRT v1.0" ); //$NON-NLS-1$

		TableHandle tableHandle = designHandle.getElementFactory( )
				.newTableItem( "table", 3 ); //$NON-NLS-1$
		designHandle.getBody( ).add( tableHandle );

		StyleHandle styleHandle = designHandle.getElementFactory( ).newStyle(
				"myStyle" ); //$NON-NLS-1$
		designHandle.getStyles( ).add( styleHandle );

		ScriptDataSourceHandle dataSourceHandle = designHandle
				.getElementFactory( ).newScriptDataSource( "dataSource" ); //$NON-NLS-1$
		designHandle.getDataSources( ).add( dataSourceHandle );

		ScriptDataSetHandle dataSetHandle = designHandle.getElementFactory( )
				.newScriptDataSet( "dataSet" ); //$NON-NLS-1$
		designHandle.getDataSets( ).add( dataSetHandle );

		ScalarParameterHandle parameterHandle = dataSetHandle
				.getElementFactory( ).newScalarParameter( "parameter" ); //$NON-NLS-1$
		designHandle.getParameters( ).add( parameterHandle );

		String script1 = "design"; //$NON-NLS-1$
		Object value1 = eval( script1 );
		assertTrue( value1 instanceof NativeReportDefinition ); //$NON-NLS-1$

		script1 = "design.findStyle('myStyle')"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "myStyle", ( (ElementDefinition) value1 ).getName( ) ); //$NON-NLS-1$

		script1 = "design.styles['myStyle']"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "myStyle", ( (ElementDefinition) value1 ).getName( ) ); //$NON-NLS-1$

		script1 = "design.styles.myStyle"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "myStyle", ( (ElementDefinition) value1 ).getName( ) ); //$NON-NLS-1$

		
		script1 = "design.findDataSet('dataSet') "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "dataSet", ( (ElementDefinition) value1 ).getName( ) ); //$NON-NLS-1$

		script1 = "design.dataSets['dataSet'] "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "dataSet", ( (ElementDefinition) value1 ).getName( ) ); //$NON-NLS-1$
		
		script1 = "design.parameters.parameter "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "parameter", ( (ElementDefinition) value1 ).getName( ) ); //$NON-NLS-1$

		script1 = "design.findDataSource('dataSource') "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "dataSource", ( (ElementDefinition) value1 ).getName( ) ); //$NON-NLS-1$

		script1 = "design.dataSources['dataSource']"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals("dataSource", ( (ElementDefinition) value1 ).getName( )); //$NON-NLS-1$
	
		script1 = "design.dataSources.dataSource"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals("dataSource", ( (ElementDefinition) value1 ).getName( )); //$NON-NLS-1$
		
		script1 = "design.findReportItem('table') "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "table", ( (ElementDefinition) value1 ).getName( ) ); //$NON-NLS-1$

		// test methods and members on NativeElementDefinition

		script1 = "design.name"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( null, value1 ); 

		script1 = "design.hasStyle"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "false", value1.toString( ) ); //$NON-NLS-1$

		script1 = "design.propertyDefns[0] "; //$NON-NLS-1$
		value1 = eval( script1 );
		PropertyDefinition propDefn = (PropertyDefinition) value1;
		assertEquals( "refreshRate", propDefn.getName( ) ); //$NON-NLS-1$

		script1 = "design.propertyDefns.author "; //$NON-NLS-1$
		value1 = eval( script1 );
		propDefn = (PropertyDefinition) value1;
		assertEquals( "author", propDefn.getName( ) ); //$NON-NLS-1$

		// Note: length is not supported.

		// script1 = "design.propertyDefns.length "; //$NON-NLS-1$
		// value1 = eval( script1 );
		// assertEquals( 26, ( (Number) value1 ).intValue( ) ); //$NON-NLS-1$

		script1 = "design.author "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "BIRT v1.0", value1 ); //$NON-NLS-1$

		script1 = "design.properties['author'] "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "BIRT v1.0", value1 ); //$NON-NLS-1$
	}

}