/*!
  @header FTGraphInfoDBTEST
  @abstract Module of FT
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  Author: Oliver Langer

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

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

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

#include <FTGraphInfoDBTEST.h>

#define _maxNodes 100
#define _nodesToDelete 50

@implementation FTGraphInfoDBTEST
- testInfoDB {
  id <FTGraphManager> graphManager;
  id <FTGraph> graph;
  id <FTId> graphId;
  id <FTId> nid;
  id <FTObjectToIdMapper> objectToIdMapper;
  id <FTObjectToIdMapper> graphIdMapper;
  id <FTNode> node;
  id <FTTransaction> transaction;
  id <ECIterator> nodeIterator;
  int i;
  NSMutableSet *nodeIds;
  
  NSLog( @"FTGraphInfoDBTEST::testInfoDB: BEGINNING...." );
  
  EC_AUTORELEASEPOOL_BEGIN
  
  objectToIdMapper = [session defaultObjectToIdMapper];
  graphManager = [session graphManager];

  NSLog( @"Now creating graph..." );
  transaction = [session beginTransactionWithParent: nil
    withSettings: nil];
  
  graphId = [objectToIdMapper mapObject: @"testInfoDBGraph"];
  graph = [graphManager createGraphWithId: graphId];
  graphIdMapper = [[graph objectToIdMapper] retain];
  
  nodeIds = [[[NSMutableSet alloc] init] autorelease];
  NSLog( @"Adding %u nodes to database - this may take some time...", _maxNodes );
  for( i = 0; i < _maxNodes; i++ ) {
    NSString *s = [NSString stringWithFormat: @"node-%d", i];
    nid = [graphIdMapper mapObject: s];
    [nodeIds addObject: nid];
    node = [graph createNodeWithId: nid];
  }
  [transaction commit];
  
  NSLog( @"Written %u nodes!", [nodeIds count] );
  [graph close];
  
  graphId = [objectToIdMapper mapObject: @"testInfoDBGraph"];
  graph = [graphManager graphWithId: graphId];
  
  NSLog( @"Now reading all node ids from database - this may take some time..." );
  
  transaction = [session beginTransactionWithParent: nil
    withSettings: nil];

  nodeIterator = [graph nodeIterator];
  
  while( [nodeIterator hasNext] ) {
    node = [nodeIterator next];
    nid = [node nodeId];
    ECAssertTrue( [nodeIds containsObject: nid], @"node returned which has NOT "\
      "been added before!" );
    [nodeIds removeObject: nid];
  }

  NSLog( @"Read %u nodes!", _maxNodes - [nodeIds count] );
  [transaction commit];    
  [graph close];
    
  if( 0 != [nodeIds count] ) {
    /* some nodes have not been returned by the iterator: */
    NSLog( @"Some nodes have NOT been returned!" );
    [[[ECAssertionException alloc] 
      initWithAssertionInfo: @"FTGraphInfoDBTEST: Some nodes have NOT been "\
      "returned!" ] raise];
  }

  NSLog( @"Now deleting %u nodes...", _nodesToDelete );
  graphId = [objectToIdMapper mapObject: @"testInfoDBGraph"];
  graph = [graphManager graphWithId: graphId];
  
  transaction = [session beginTransactionWithParent: nil
    withSettings: nil];
  
  i = _nodesToDelete;
  nodeIterator = [graph nodeIterator];
  while( [nodeIterator hasNext] && i > 0) {
    node = [nodeIterator next];
    [graph removeNode: node];
    i--;
  }
  
  [transaction commit];
  [graph close];
  
  NSLog( @"Now checking existence of rest of nodes after deletion ..." );
  graphId = [objectToIdMapper mapObject: @"testInfoDBGraph"];
  graph = [graphManager graphWithId: graphId];
  
  transaction = [session beginTransactionWithParent: nil
    withSettings: nil];
  
  nodeIds = [[[NSMutableSet alloc] init] autorelease];
  nodeIterator = [graph nodeIterator];
  while( [nodeIterator hasNext] ) {
    node = [nodeIterator next];
    [nodeIds addObject: [node nodeId]];
  }
  
  NSLog( @"Found a rest of %u nodes...", [nodeIds count] );
  ECAssertTrue( [nodeIds count] == _maxNodes-_nodesToDelete,
    @"Incorrect number of nodes within graph after deletion of nodes!" );
    
  [transaction commit];
  [graph close];
  EC_AUTORELEASEPOOL_END
  
  NSLog( @"FTGraphInfoDBTEST::testInfoDB: FINISHED." );
  
  return self;
}

@end
