/******************************************************************************
   BDBCursorTEST
   BDB
 
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA


  -------------------------------------------------------------------------
  Modification history

  09.01.2005 ola     initial version
  -------------------------------------------------------------------------
******************************************************************************/
#include "BDBCursorTEST.h"


@implementation BDBCursorTEST

#define __CursorTESTKey @"BDBCursorTESTKey"
#define __CursorTESTData1 @"BDBCursorTESTValue-1"
#define __CursorTESTData2 @"BDBCursorTESTValue-2"
#define __CursorTESTData3 @"BDBCursorTESTValue-3"

- setup {
  [BDBAbstractBTreeTEST deleteDatabase];
  return [ super setup ];
}

- testCursorOperations {
  NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
  BDBCursorConfig *cursorConfig;
  BDBCursor *cursor;
  BDBDatabaseEntry *key, *data, *data1, *data2, *data3;
  BDBLockMode *lockMode; 
  
  lockMode = [[[BDBLockMode alloc] init] autorelease];
  
  NSLog( @"BDBCursorTest::testCursorOperations - BEGIN..." );
  
  NSLog( @"Entering database entries with the same key..." );
  key = [[[BDBDatabaseEntry alloc] initWithObject: __CursorTESTKey ]
    autorelease];
	
  data1 = [[[BDBDatabaseEntry alloc] initWithObject: __CursorTESTData1 ]
    autorelease];	
  [ database putEntryWithTransaction: nil key:key value: data1 ];
  
  data2 = [[[BDBDatabaseEntry alloc] initWithObject: __CursorTESTData2 ]
    autorelease];
  [ database putEntryWithTransaction: nil key:key value: data2 ];
  
  data3 = [[[BDBDatabaseEntry alloc] initWithObject: __CursorTESTData3 ]
    autorelease];
  [ database putEntryWithTransaction: nil key:key value: data3 ];
  
  cursorConfig = [[[BDBCursorConfig alloc] init] autorelease];
  cursor = [database openCursor: nil cursorConfig: cursorConfig ];
  NSLog( @"Now performing read tests..." );

  // retrieving first entry
  data = [[[BDBDatabaseEntry alloc] init] autorelease];
  ECAssertTrue( BDB_STATUS_SUCCESS == [ cursor firstWithKey: key 
    value: data lockMode:lockMode], 
	@"Loading first: Key NOT found althought being added!" );

  ECAssertTrue( [[data object] isEqual: [data1 object]], 
    @"Loading first: Retrieved data incorrect!" );
	
  // fetching next (second) entry
  ECAssertTrue( BDB_STATUS_SUCCESS == [ cursor nextWithKey: key 
    value: data lockMode:lockMode], 
	@"Loading second: Key NOT found althought being added!" );
  
  ECAssertTrue( [[data object] isEqual: [data2 object]], 
    @"Loading second: Retrieved data incorrect!" );
  
  // fetching next (third) entry
  ECAssertTrue( BDB_STATUS_SUCCESS == [ cursor nextWithKey: key 
    value: data lockMode:lockMode], 
	@"Loading third: Key NOT found althought being added!" );
  
  ECAssertTrue( [[data object] isEqual: [data3 object]], 
    @"Loading third: Retrieved data incorrect!" );
	
  // fetching previous->previous (first) entry
  ECAssertTrue( BDB_STATUS_SUCCESS == [ cursor previousWithKey: key 
    value: data lockMode:lockMode], 
	@"Loading second(per previous): Key NOT found althought being added!" );

  ECAssertTrue( BDB_STATUS_SUCCESS == [ cursor previousWithKey: key 
    value: data lockMode:lockMode], 
	@"Loading first(per previous): Key NOT found althought being added!" );
  
  ECAssertTrue( [[data object] isEqual: [data1 object]], 
    @"Loading first (per PREV->PREV): Retrieved data incorrect!" );

  // performing PREVIOUS and therefor reading not existent entry
  ECAssertTrue( BDB_STATUS_NOTFOUND == [ cursor previousWithKey: key 
    value: data lockMode:lockMode], 
	@"Loading non-existent entry did not return expected result!" );
  	
  [pool release];
  NSLog( @"BDBCursorTest::testCursorOperations - FINISHED SUCCESSFULLY!" );
  return self;
}
@end
