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

/**
 * JUnit test for GroupByStrRange
 */

public class GroupByStringRangeTest extends TestCase
{
	protected GroupBy groupBy;
	
	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp( ) throws Exception
	{
		GroupDefinition groupDefn = new GroupDefinition();
		groupDefn.setInterval(IGroupDefinition.STRING_PREFIX_INTERVAL);
		groupDefn.setIntervalRange(3);
		groupBy = this.getInstance( groupDefn );
		super.setUp( );
	}

	/*
	 * @see TestCase#tearDown()
	 */
	protected void tearDown( ) throws Exception
	{
		super.tearDown( );
	}
	
	/**
	 * Test GroupByStrRange#IsInSameGroup
	 * @throws DataException
	 *
	 */
	public void testIsInSameGroup() throws DataException {
		String currentGroupKey;
		String previousGroupKey;
		
		currentGroupKey = "";
		previousGroupKey = "a";
		assertFalse( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "a";
		previousGroupKey = "a";
		assertTrue( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "a";
		previousGroupKey = "ab";
		assertFalse( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "aa";
		previousGroupKey = "ab";
		assertFalse( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "ab";
		previousGroupKey = "ab";
		assertTrue( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "ab";
		previousGroupKey = "abb";
		assertFalse( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "aba";
		previousGroupKey = "abb";
		assertFalse( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "abb";
		previousGroupKey = "abb";
		assertTrue( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "abbc";
		previousGroupKey = "abbd";
		assertTrue( groupBy.isInSameGroup( currentGroupKey, previousGroupKey ) );

		currentGroupKey = "abbcd";
		previousGroupKey = "abbdg";
		assertTrue( 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, String.class );
	}	


}
