/*******************************************************************************
 * 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.data.engine.executor.transform.group;
import junit.framework.TestCase;

import org.eclipse.birt.data.engine.api.IGroupDefinition;
import org.eclipse.birt.data.engine.api.querydefn.GroupDefinition;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.odi.IQuery;

/**
 * JUnit test for GroupByNumRange
 */

public class GroupByNumberRangeTest extends TestCase
{
	protected GroupDefinition groupDefn;
	protected GroupBy groupBy;
	protected Object currentGroupKey;
	protected Object previousGroupKey;
	
	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp( ) throws Exception
	{
		groupDefn = new GroupDefinition();
		groupDefn.setInterval(IGroupDefinition.NUMERIC_INTERVAL);
		groupDefn.setIntervalRange(3);
		super.setUp( );
	}

	/*
	 * @see TestCase#tearDown()
	 */
	protected void tearDown( ) throws Exception
	{
		super.tearDown( );
	}
	
	/**
	 * Test GroupByNumRange#isInSameGroup when the type of group object is Integer
	 * @throws DataException
	 *
	 */
	public void testIntegerIsInSameGroup( ) throws DataException
	{
		currentGroupKey = new Integer( 3 );
		previousGroupKey = new Integer( 2 );
		groupBy = this.getInstance( groupDefn );
		assertFalse(groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = new Integer( 5 );
		previousGroupKey = new Integer( 2 );
		groupBy = this.getInstance( groupDefn );
		assertFalse( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );
		
		currentGroupKey = new Integer( 5 );
		previousGroupKey = new Integer( 5 );
		groupBy = this.getInstance( groupDefn );
		assertTrue( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );
		
		currentGroupKey = new Integer( -2 );
		previousGroupKey = new Integer( -3 );
		groupBy = this.getInstance( groupDefn );
		assertTrue(groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );
		currentGroupKey = new Integer( 4 );
		previousGroupKey = new Integer( 4 );
		assertTrue(groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );
		currentGroupKey = new Integer( -3 );
		previousGroupKey = new Integer( 5 );
		assertFalse(groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );
	}



	/**
	 * 
	 * @param groupDefn
	 * @return
	 * @throws DataException
	 */
	protected GroupBy getInstance( GroupDefinition groupDefn ) throws DataException
	{
		String groupKey = groupDefn.getKeyColumn();
		IQuery.GroupSpec groupSpec = new IQuery.GroupSpec( groupKey );
		groupSpec.setName( groupDefn.getName() );
		groupSpec.setInterval( groupDefn.getInterval());
		groupSpec.setIntervalRange( groupDefn.getIntervalRange());
		groupSpec.setIntervalStart( groupDefn.getIntervalStart());
		groupSpec.setSortDirection( groupDefn.getSortDirection());
		
		return GroupBy.newInstance( groupSpec, 0, null, Integer.class );
	}
	
}