/*******************************************************************************
 * 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.data.oda.jdbc;

import junit.framework.TestCase;

/**
 * 
 * Test case for ResultSetMetaData
 * 
 */
public class ResultSetMetaDataTest extends TestCase
{

	/** Connection object, used to create statement stmt1*/
	private Connection conn1 = null;
	
	/** Connection object, used to create statement stmt2*/
	private Connection conn2 = null;

	/** Statement object, used to create ResultSet*/
	private Statement stmt1 = null;
	
	/** Statement object, used to get ResultSetMetaData rsmd_Statement*/
	private Statement stmt2 = null;

	/** ResultSet object, used to get ResultSetMetaData rsmd_ResultSet*/
	private ResultSet rs = null;

	/** the result set meta data to test get from ResultSet*/
	private ResultSetMetaData rsmd_ResultSet = null;
	
	/** the result set meta data to test get from Statement*/
	private ResultSetMetaData rsmd_Statement = null;

	/** JDBC Connection object, used to create statement jdbcStmt*/
	private java.sql.Connection jdbcConn1 = null;
	
	/** JDBC Connection object, used to create PreparedStatement jdbcPreparedStmt*/
	private java.sql.Connection jdbcConn2 = null;

	/** JDBC Statement object, used to create ResultSet jdbcRs*/
	private java.sql.Statement jdbcStmt = null;

	/** JDBC PreparedStatement object, used to create ResultSetMetaData jdbcRsmd_PreparedStmt*/
	private java.sql.PreparedStatement jdbcPreparedStmt = null;

	/** JDBC ResultSet object, used to create ResultSetMetaData jdbcRsmd_ResultSet*/
	private java.sql.ResultSet jdbcRs = null;

	/** the JDBC result set meta data to compare with ,which is get from ResultSet*/
	private java.sql.ResultSetMetaData jdbcRsmd_ResultSet = null;
	
	/** the JDBC result set meta data to compare with ,which is get from PreparedStatement*/
	private java.sql.ResultSetMetaData jdbcRsmd_PreparedStmt = null;

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp( ) throws Exception
	{
		TestUtil.createTestData( );

		conn1 = TestUtil.openConnection( );
		conn2 = TestUtil.openConnection( );
		stmt1 = (Statement) conn1.newQuery( "" );
		stmt2 = (Statement) conn2.newQuery( "" );
		String sql = "select * from " + TestUtil.TABLE_NAME;

		stmt1.prepare( sql );
		rs = (ResultSet) stmt1.executeQuery( );
		rsmd_ResultSet = (ResultSetMetaData) rs.getMetaData( );

		stmt2.prepare( sql );
		rsmd_Statement = (ResultSetMetaData) stmt2.getMetaData( );

		jdbcConn1 = TestUtil.openJDBCConnection( );
		jdbcConn2 = TestUtil.openJDBCConnection( );

		jdbcStmt = jdbcConn1.createStatement( );
		jdbcRs = jdbcStmt.executeQuery( sql );
		jdbcRsmd_ResultSet = jdbcRs.getMetaData( );

		jdbcPreparedStmt = jdbcConn2.prepareStatement( sql );
		jdbcRsmd_PreparedStmt = jdbcPreparedStmt.getMetaData( );
		super.setUp( );
	}

	/*
	 * @see TestCase#tearDown()
	 */
	protected void tearDown( ) throws Exception
	{
		rs.close( );
		stmt1.close( );
		stmt2.close( );
		conn1.close( );
		conn2.close( );

		jdbcRs.close( );
		jdbcStmt.close( );
		jdbcConn1.close( );
		jdbcConn2.close( );

		TestUtil.deleteTestData( );
		super.tearDown( );
	}

	public void testGetColumnCount_ResultSet( ) throws Exception
	{
		assertEquals( rsmd_ResultSet.getColumnCount( ), jdbcRsmd_ResultSet
				.getColumnCount( ) );
	}

	public void testGetColumnName_ResultSet( ) throws Exception
	{
		for ( int i = 1; i < rsmd_ResultSet.getColumnCount( ) + 1; i++ )
		{
			System.out.println( "i = " + i );
			System.out.println( "rsmd_ResultSet getColumnName  = "
					+ rsmd_ResultSet.getColumnName( i ) );
			assertEquals( rsmd_ResultSet.getColumnName( i ), jdbcRsmd_ResultSet
					.getColumnName( i ) );
		}
	}

	public void testGetColumnLabel_ResultSet( ) throws Exception
	{
		for ( int i = 1; i < rsmd_ResultSet.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_ResultSet.getColumnLabel( i ),
					jdbcRsmd_ResultSet.getColumnLabel( i ) );
		}
	}

	public void testGetColumnType_ResultSet( ) throws Exception
	{
		for ( int i = 1; i < rsmd_ResultSet.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_ResultSet.getColumnType( i ), jdbcRsmd_ResultSet
					.getColumnType( i ) );
		}
	}

	public void testGetColumnTypeName_ResultSet( ) throws Exception
	{
		for ( int i = 1; i < rsmd_ResultSet.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_ResultSet.getColumnTypeName( i ),
					jdbcRsmd_ResultSet.getColumnTypeName( i ) );
		}
	}

	public void testGetColumnDisplayLength_ResultSet( ) throws Exception
	{
		for ( int i = 1; i < rsmd_ResultSet.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_ResultSet.getColumnDisplayLength( i ),
					jdbcRsmd_ResultSet.getColumnDisplaySize( i ) );
		}
	}

	public void testGetPrecision_ResultSet( ) throws Exception
	{
		for ( int i = 1; i < rsmd_ResultSet.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_ResultSet.getPrecision( i ), jdbcRsmd_ResultSet
					.getPrecision( i ) );
		}
	}

	public void testGetScale_ResultSet( ) throws Exception
	{
		for ( int i = 1; i < rsmd_ResultSet.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_ResultSet.getScale( i ), jdbcRsmd_ResultSet
					.getScale( i ) );
		}
	}

	public void testIsNullable_ResultSet( ) throws Exception
	{
		for ( int i = 1; i < rsmd_ResultSet.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_ResultSet.isNullable( i ), jdbcRsmd_ResultSet
					.isNullable( i ) );
		}
	}

	public void testGetColumnCount_Statment( ) throws Exception
	{
		assertEquals( rsmd_Statement.getColumnCount( ), jdbcRsmd_PreparedStmt
				.getColumnCount( ) );
	}

	public void testGetColumnName_Statment( ) throws Exception
	{
		for ( int i = 1; i < rsmd_Statement.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_Statement.getColumnName( i ),
					jdbcRsmd_PreparedStmt.getColumnName( i ) );
		}
	}

	public void testGetColumnLabel_Statment( ) throws Exception
	{
		for ( int i = 1; i < rsmd_Statement.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_Statement.getColumnLabel( i ),
					jdbcRsmd_PreparedStmt.getColumnLabel( i ) );
		}
	}

	public void testGetColumnType_Statment( ) throws Exception
	{
		for ( int i = 1; i < rsmd_Statement.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_Statement.getColumnType( i ),
					jdbcRsmd_PreparedStmt.getColumnType( i ) );
		}
	}

	public void testGetColumnTypeName_Statment( ) throws Exception
	{
		for ( int i = 1; i < rsmd_Statement.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_Statement.getColumnTypeName( i ),
					jdbcRsmd_PreparedStmt.getColumnTypeName( i ) );
		}
	}

	public void testGetColumnDisplayLength_Statment( ) throws Exception
	{
		for ( int i = 1; i < rsmd_Statement.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_Statement.getColumnDisplayLength( i ),
					jdbcRsmd_PreparedStmt.getColumnDisplaySize( i ) );
		}
	}

	public void testGetPrecision_Statment( ) throws Exception
	{
		for ( int i = 1; i < rsmd_Statement.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_Statement.getPrecision( i ),
					jdbcRsmd_PreparedStmt.getPrecision( i ) );
		}
	}

	public void testGetScale_Statment( ) throws Exception
	{
		for ( int i = 1; i < rsmd_Statement.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_Statement.getScale( i ), jdbcRsmd_PreparedStmt
					.getScale( i ) );
		}
	}

	public void testIsNullable_Statment( ) throws Exception
	{
		for ( int i = 1; i < rsmd_Statement.getColumnCount( ) + 1; i++ )
		{
			assertEquals( rsmd_Statement.isNullable( i ), jdbcRsmd_PreparedStmt
					.isNullable( i ) );
		}
	}
}