Source

mana-core-sgcomps / test / ProxyProviderSvc_test.cxx

The default branch has multiple heads

Full commit
/** @file ProxyProviderSvc_test.cxx
 * @brief unit test for the proxy provider mechanism
 * @author ATLAS Collaboration
 ***************************************************************************/

// $Id: ProxyProviderSvc_test.cxx,v 1.2 2007-06-22 23:54:59 calaf Exp $

//<<<<<< INCLUDES                                                       >>>>>>

#include "../src/ProxyProviderSvc.h"

#include <cassert>
#include <iostream>
#include <string>

#include "TestTools/initGaudi.h"
#include "TestTools/SGassert.h"
#include "ToyConversion/FooBar.h"
#include "ToyConversion/ToyConversionSvc.h"

#include "SGTools/TransientAddress.h"
#include "SGTools/DataProxy.h"

#include "StoreGate/StoreGateSvc.h"
#include "CLIDSvc/tools/ClassID_traits.h"
#include "AthenaKernel/IAddressProvider.h"
#include "AthenaKernel/StoreID.h"

#include "GaudiKernel/GenericAddress.h"
#include "GaudiKernel/ISvcLocator.h"
#include "GaudiKernel/StatusCode.h"
#include "GaudiKernel/ClassID.h"


#include <string>

using namespace Athena_test;
using std::cerr;
using std::cout;
using std::endl;
using std::string;
using SG::DataProxy;
using SG::TransientAddress;

class FooBar {};
#include "CLIDSvc/CLASS_DEF.h"
CLASS_DEF(FooBar, 8109, 0)

template <typename PROXIED>
class TestProvider : public IAddressProvider {
public:
  TestProvider(string key) :
    m_ID(ClassID_traits<PROXIED>::ID()), m_key(key) 
  {
    m_IOA = new GenericAddress(ToyConversionSvc::storageType(),
			       m_ID,m_key);
  }
  virtual ~TestProvider() {}
  ///get all addresses that the provider wants to preload in SG maps

  virtual StatusCode loadAddresses(StoreID::type /*id*/, tadList& /* tList */) 
  {return StatusCode::SUCCESS;}

  virtual StatusCode preLoadAddresses(StoreID::type /*id*/, tadList& tList) 
  {
    TransientAddress* tad = new TransientAddress(m_ID, m_key, m_IOA);
    tList.push_back(tad);
    return StatusCode::SUCCESS;
  }

  ///get a specific address, plus all others  the provider wants to load in SG maps
  virtual StatusCode updateAddress(TransientAddress* tad)
  { 
    
    if ((tad->clID() != m_ID) || (tad->name() != m_key)) {
//        cout << "m_ID: " << m_ID.clID() << " " << m_ID.key() << endl;
//        cout << "id: " << id.clID() << " " << id.key() << endl;
      return StatusCode::FAILURE;
    } else {
      tad->setAddress(m_IOA);
    }
      return StatusCode::SUCCESS;
  }
      
private:
  CLID m_ID;
  std::string m_key;
  IOpaqueAddress* m_IOA;
};


int main() {
  ISvcLocator* pSvcLoc(0);
  if (!initGaudi("ProxyProviderSvc_test.txt", pSvcLoc)) {
    cerr << "This test can not be run" << endl;
    return 0;
  }  
  assert( pSvcLoc );

  StoreGateSvc* pStore(0);
  static const bool CREATE(true);
  assert( (pSvcLoc->service("StoreGateSvc", pStore, CREATE)).isSuccess() );
  assert( pStore );
  IProxyProviderSvc* pIPPSvc;
  assert( (pSvcLoc->service("ProxyProviderSvc", pIPPSvc, CREATE)).isSuccess() );
  assert( pIPPSvc );

  ProxyProviderSvc* pPPSvc = dynamic_cast<ProxyProviderSvc*>(pIPPSvc);
  assert( pPPSvc );

  pPPSvc->addProvider(new TestProvider<Foo>("aFoo"));
  pPPSvc->addProvider(new TestProvider<Bar>("aBar"));
  pPPSvc->addProvider(new TestProvider<FooBar>("aFooBar"));


  DataHandle<Bar> hBar;
  assert( (pStore->bind(hBar, "aBar")).isSuccess() );
  
  assert( hBar.ID() == "aBar" );

  assert( hBar.cptr() );

  cout << pStore->dump() << endl;

  assert( !(pStore->transientContains<Foo>("aFoo")) );
  assert( pStore->contains<Foo>("aFoo") );

  const Foo* pFoo(0);
  assert( (pStore->retrieve(pFoo, "aFoo")).isSuccess() );
  assert( pFoo );


  const FooBar* pFooBar(0);
  SGASSERTERROR( (pStore->retrieve(pFooBar, "aFooBar")).isSuccess() );
  assert( 0 == pFooBar );

  cout << "*** ProxyProviderSvc_test OK ***" <<endl;
  return 0;
}