/*!
  @header BDBAddObjectTEST
  @abstract Module of BDB
 
  @availability OS X, GNUstep
  @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

  19.07.05 ola     initial version
  23.08.06 ola     license changed
  -------------------------------------------------------------------------
  </pre>
*/

#include <BDBAddObjectTEST.h>

@interface ASimpleObject : NSObject <NSCoding> {
  @private
    NSString *simpleString;
    NSMutableArray *simpleArray;
    
}

- init;
- (void) dealloc;

- (id) initWithCoder: (NSCoder *) coder;
- (void)encodeWithCoder:(NSCoder *) encoder;

- (BOOL) checkState;
@end


@implementation ASimpleObject

- init {
  self = [super init];
  
  self->simpleString = [[NSString alloc] 
    initWithFormat: @"Hello from ASimpleObject" ];
    
  self->simpleArray = [[NSMutableArray alloc] init];
  
  [self->simpleArray addObject: @"A string entry" ];
  [self->simpleArray addObject: [[NSNumber alloc] initWithInt: 23]];
  
  return self;
}


- (void) dealloc {
  [self->simpleString release];
  [self->simpleArray release];
  
  [super dealloc];
}


- (id) initWithCoder: (NSCoder *) coder {
  self = [super init];
  
  NSLog( @"ASimpleObject::initWithCoder...." );
  
  self->simpleString = [[coder decodeObject] retain];
  self->simpleArray = [[coder decodeObject] retain];
  
  return self;
}


- (void)encodeWithCoder:(NSCoder *) encoder {
  NSLog( @"ASimpleObject::encodeWithCoder...." );
  
  [encoder encodeObject: self->simpleString];
  [encoder encodeObject: self->simpleArray];
}


- (BOOL) checkState {
  BOOL stateOK = YES;
  
  if( nil == self->simpleString || nil == self->simpleArray ) {
    return NO;
  }
  
  if( ![self->simpleString isEqualToString: @"Hello from ASimpleObject"] ) {
    return NO;
  }
  
  NSLog( @"simple string reloaded!" );
  
  if( [self->simpleArray count] < 2 ) {
    return NO;
  }
  
  NSLog( @"Array with at least 2 elements reloaded!" );
  
  if( ![[self->simpleArray objectAtIndex:0] 
    isEqualToString: @"A string entry"] ) {
    return NO;
  }
  
  NSLog( @"First element correctly reloaded" );
  if( [[self->simpleArray objectAtIndex: 1] intValue] != 23 ) {
    return NO;
  }
  
  NSLog( @"Second element correctly reloaded.\nState completely reloaded!" );  
  return stateOK;
}
@end


@implementation BDBAddObjectTEST

- testAddObject {
  BDBDatabaseEntry *keyEntry, *dataEntry;
  ASimpleObject *obj;
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  
  
  NSLog( @"BDBAddObjectTEST::testAddObject: BEGINNING TESTS..." );
  
  obj = [[[ASimpleObject alloc] init] autorelease];
  keyEntry = [ [[BDBDatabaseEntry alloc] 
    initWithObject: @"ASimpleObject1"] autorelease];
  dataEntry = [ [[BDBDatabaseEntry alloc] initWithObject: obj ] autorelease ];

  [ database putEntryWithTransaction: nil key: keyEntry value: dataEntry ];
  keyEntry = [ [[BDBDatabaseEntry alloc] 
    initWithObject: @"ASimpleObject1"] autorelease];
  dataEntry = [[BDBDatabaseEntry alloc] init];
  
  [database getEntryWithTransaction: nil key:keyEntry data: dataEntry];
  
  obj = (ASimpleObject *) ([dataEntry object]);
  
  ECAssertTrue( [obj checkState], @"Object not correctly reloaded!" );

  NSLog( @"BDBAddObjectTEST::testAddObject: TESTS FINISHED..." );
  [pool release];
  return self;
}
@end
