/*!
  @header BDBRecnoAccessTEST
  @abstract Module of 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


 <pre>
  -------------------------------------------------------------------------
  Modification history

  21.03.05 ola     initial version
  -------------------------------------------------------------------------
  </pre>
*/

#include <Encore/Encore.h>
#include <BDBRecnoAccessTEST.h>


@implementation BDBRecnoAccessTEST

- appendValue: (NSString *) strToAppend expectedRecordNr: (u_int32_t) recno 
  withDatabase: (BDBDatabase *) database {
  BDBDatabaseRecordNumber *nr;
  BDBOperationStatus status;
  BDBDatabaseEntry *value;
  u_int32_t appendNr;
  NSString *msg;
  
  EC_AUTORELEASEPOOL_BEGIN
  
  nr = [[BDBDatabaseRecordNumber alloc] init];
  [nr autorelease];
  
  value = [[BDBDatabaseEntry alloc] initWithObject: strToAppend ];
  [value autorelease];
  
  status = [database appendEntryWithTransaction: nil 
    entryToAppend: value
    resultingRecordNr: nr];
  
  msg = [[[NSString alloc]
      initWithFormat: @"BDBRecnoAccessTEST::testRecNoAccess: Appending of "\
      "string \"%@\" FAILED with status=%d", strToAppend, (int) status]
      autorelease];
      
  ECAssertTrue( status == BDB_STATUS_SUCCESS, msg );
  
  appendNr = [nr entryNumber];
  NSLog( @"BDBRecnoAccessTEST::testRecNoAccess: Added record nr=%u",
    recno );

  msg = [[[NSString alloc]
    initWithFormat: @"BDBRecnoAccessTEST::testRecNoAccess: Nr of "\
    "created record does not equal %u. Instead: %u", recno, appendNr ]
    autorelease];
    
  ECAssertTrue( recno == appendNr,  msg );
    
  EC_AUTORELEASEPOOL_END
  
  return self;
}


- testRecNoAccess {
  BDBDatabaseConfig *dbConfig;
  BDBDatabase *database;
  NSMutableArray *values;
  int i;
  BDBDatabaseRecordNumber *recNo;
  
  EC_AUTORELEASEPOOL_BEGIN
  
  values = [[NSMutableArray alloc] init];
  [values autorelease];
  
  NSLog( @"BDBRecnoAccessTEST::testRecNoAccess: BEGIN..." );
  dbConfig = [[[BDBDatabaseConfig alloc] init] autorelease];
  [dbConfig setDatabaseType: BDB_RECNO];
  [dbConfig setAllowCreate: YES];

  NSLog( @"Creating the database..." );
  database = [BDBDatabase 
    initWithFilename: @"TESTrecno.db" 
    databaseName: nil
	databaseConfig: dbConfig];

  [database autorelease];
  
  [values insertObject: @"Item 1" atIndex: 0 ];
  [values insertObject: @"Item 2" atIndex: 1 ];
  [values insertObject: @"Item 3" atIndex: 2 ];
  
  NSLog( @"BDBRecnoAccessTEST::testRecNoAccess: Adding records of diverse "\
    "sizes..." );
  [self appendValue: [values objectAtIndex: 0] 
    expectedRecordNr: 1 withDatabase: database];
  [self appendValue: [values objectAtIndex: 1 ] 
    expectedRecordNr: 2 withDatabase: database];
  [self appendValue: [values objectAtIndex: 2] 
    expectedRecordNr: 3 withDatabase: database];
  
  NSLog( @"BDBRecnoAccessTEST::testRecNoAccess: Checking insertions now..." );
  for( i = 1; i < 4; i++ ) {
    BDBDatabaseEntry *data;
    NSString *message;
    NSString *retrievedStr;
    
    recNo = [[[BDBDatabaseRecordNumber alloc] initWithEntryNumber: i ] 
	  autorelease];
    data = [[[BDBDatabaseEntry alloc] init] autorelease];
    NSAssert( BDB_STATUS_SUCCESS == [database getEntryWithTransaction:nil 
      recordNumber:recNo data: data], @"Fetching record number=1 FAILED!" );
      
    message = [[[NSString alloc] initWithFormat:
      @"BDBRecnoAccessTEST::testRecNoAccess: Insertion nr=%u does not contain"\
      " expected value", i ] autorelease];
    retrievedStr = [data object];

    ECAssertTrue( [retrievedStr isEqualToString: [values objectAtIndex: i-1 ]],
      message );
      
    message = [[[NSString alloc]initWithFormat:
      @"BDBRecnoAccessTEST::testRecNoAccess: Insertion at%u is OK", i ] 
      autorelease];
    NSLog( message );
  }
  
  [database close];
  NSLog( @"BDBRecnoAccessTEST::testRecNoAccess: FINISHED" );
  
  EC_AUTORELEASEPOOL_END
  return self;
}
@end
