/*******************************************************************************
 * 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 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.executor.transform.group.GroupBy;
import org.eclipse.birt.data.engine.executor.transform.group.GroupUtil;
import org.eclipse.birt.data.engine.odi.IQuery;

import junit.framework.TestCase;

/**
 * Unit Test of GroupByPositionRange 
 */
public class GroupByPositionRangeTest extends TestCase
{	
	/**
	 * Test GroupByPositionRange#isSameGroup
	 * 
	 * @throws DataException
	 */
	public void testIsSameGroup( ) throws DataException
	{
		GroupDefinition groupDefn = new GroupDefinition();
		GroupBy groupBy;
		
		// inverval value is 0
		groupDefn = new GroupDefinition();
		groupDefn.setInterval(IGroupDefinition.NUMERIC_INTERVAL);
		groupDefn.setIntervalRange(0);
		
		/**
		 * null,
		 * null, not same
		 * null, not same
		 * null, not same
		 */
		groupBy = getInstance( groupDefn );
		assertFalse( groupBy.isInSameGroup( null, null, new Integer(0), 1 ) );
		assertFalse( groupBy.isInSameGroup( null, null, new Integer(0), 2 ) );
		assertFalse( groupBy.isInSameGroup( null, null, new Integer(0), 3 ) );
		
		// inverval value is 1	
		groupDefn.setInterval(IGroupDefinition.NUMERIC_INTERVAL);
		groupDefn.setIntervalRange(1);
		
		/**
		 * null,
		 * null, not same
		 * null, not same
		 * null, not same
		 */
		groupBy = getInstance( groupDefn );
		assertFalse( groupBy.isInSameGroup( null, null, new Integer(0), 1 ) );
		assertFalse( groupBy.isInSameGroup( null, null, new Integer(0), 2 ) );
		assertFalse( groupBy.isInSameGroup( null, null, new Integer(0), 3 ) );
		
		// inverval value is 2
		groupDefn.setInterval(IGroupDefinition.NUMERIC_INTERVAL);
		groupDefn.setIntervalRange(2);
		
		/**
		 * null,
		 * null, same group
		 * null,
		 * null, same group
		 */
		groupBy = getInstance( groupDefn );
		assertTrue( groupBy.isInSameGroup( null, null, new Integer(0), 1 ) );
		assertFalse( groupBy.isInSameGroup( null, null, new Integer(0), 2 ) );
		assertTrue( groupBy.isInSameGroup( null, null, new Integer(0), 3 ) );
		
		// inverval value is 3	
		groupDefn.setInterval(IGroupDefinition.NUMERIC_INTERVAL);
		groupDefn.setIntervalRange(3);

		/**
		 * null,
		 * null, not same
		 * null, same group
		 * null, not same
		 */
		groupBy = getInstance( groupDefn );
		assertTrue( groupBy.isInSameGroup( null, null, new Integer(0), 1 ) );
		assertTrue( groupBy.isInSameGroup( null, null, new Integer(0), 2 ) );
		assertFalse( groupBy.isInSameGroup( null, null, new Integer(0), 3 ) );
	}

	/**
	 * return GroupBy instance constructed from GroupDefinition instance
	 * 
	 * @param groupDefn
	 * @return GroupBy instance
	 * @throws DataException
	 */
	private static GroupBy getInstance( GroupDefinition groupDefn ) throws DataException
	{
		IQuery.GroupSpec groupSpec = new IQuery.GroupSpec( GroupUtil.rowidName );
		groupSpec.setName( groupDefn.getName() );
		groupSpec.setInterval( groupDefn.getInterval());
		groupSpec.setIntervalRange( groupDefn.getIntervalRange());
		groupSpec.setIntervalStart( groupDefn.getIntervalStart());
		groupSpec.setSortDirection( groupDefn.getSortDirection());
		
		return GroupBy.newInstanceForRowID( groupSpec );
	}
	
}
