ATLAS Offline Software
Loading...
Searching...
No Matches
CppUnit_SGtestdriver.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6// This file contains the driver class for all CppUnit test classes
7// and the function to instantiate and initialize the StoreGateSvc
8// All registered CppUnit test classes will be run.
9// You can also modify the output (text, compiler, XML).
10// Authors: S.Binet<binet@cern.ch> (inst. and init. the StoreGateSvc)
11// A.Undrus<undrus@bnl.gov> (adaptation of driver class from
12// the examples of CppUnit Cookbook, generation of default
13// job options for Gaudi)
15
16#include <cppunit/extensions/TestFactoryRegistry.h>
17#include <cppunit/TextTestRunner.h>
18#include <cppunit/CompilerOutputter.h>
19#include <cppunit/TextOutputter.h>
20#include <cppunit/XmlOutputter.h>
21
22// STL includes
23#include <iostream>
24#include <cassert>
25#include <stdexcept>
26#include <string>
27#include <cstdlib>
28#include <cstdio>
29#include <fstream>
30#include <list>
31
32// Framework includes
33#ifndef NOGAUDI
34#include "TestTools/initGaudi.h"
35#endif //NOGAUDI
37#include "GaudiKernel/ISvcLocator.h"
38
42{
43 ISvcLocator * m_svcLoc;
44 StoreGateSvc * m_storeGate;
45
46 /* Search for file jobOptions.txt in ../share ../test/ ../cmt */
47 const int SZ = 3;
48 std::string FileName="";
49 std::string fn;
50 std::string filename_in[SZ] = {"../cmt/CppUnit_jobOptions.txt", "../test/CppUnit_jobOptions.txt", "../share/CppUnit_jobOptions.txt"};
51 std::ifstream inFile[SZ];
52
53 for (int i=0; i<SZ; i++)
54 {
55 fn = filename_in[i];
56 try
57 {
58 inFile[i].open(fn.c_str());
59 if (!inFile[i])
60 throw(fn);
61 FileName=fn;
62 }
63 catch(std::string fn)
64 {
65 // std::cout << "File " << filename_in[i] << "not found" << " FileName " << std::endl;
66 }
67
68 inFile[i].close();
69
70 } // for (int i<0
71
72 // std::cout << "FileFFFF " <<FileName<< std::endl;
73
74 // Build job options file if CppUnit_jobOptions.txt is unavailable
75
76 if ( FileName == "" )
77 {
78 FileName = "../run/CppUnit_jobOptions_generated.txt";
79 std::system("touch ../run/CppUnit_jobOptions_generated.txt; rm ../run/CppUnit_jobOptions_generated.txt");
80 std::ofstream jobopt("../run/CppUnit_jobOptions_generated.txt");
81
82 { // Build a default list of options.
83 std::list<std::string> opts;
84 opts.push_back("#pragma print off");
85 opts.push_back("ApplicationMgr.Dlls += { \"StoreGate\" };");
86 //opts.push_back("ApplicationMgr.Dlls += { \"CLIDSvc\" };");
87 opts.push_back("ApplicationMgr.ExtSvc += { \"ClassIDSvc\" };");
88 opts.push_back("ApplicationMgr.ExtSvc += { \"StoreGateSvc\", \"StoreGateSvc/DetectorStore\", \"StoreGateSvc/HistoryStore\" };");
89 opts.push_back("ApplicationMgr.ExtSvc += { \"ActiveStoreSvc\" };");
90 opts.push_back("AuditorSvc.Auditors += { \"AlgContextAuditor\"};");
91 opts.push_back("StoreGateSvc.OutputLevel = 2;");
92 opts.push_back("StoreGateSvc.ActivateHistory = false;");
93 //opts.push_back("CLIDSvc.OutputLevel = 2;");
94 opts.push_back("MessageSvc.useColors = false;");
95 opts.push_back("MessageSvc.OutputLevel = 3;");
96 // Build job options file from list.
97 for ( std::list<std::string>::const_iterator iopt=opts.begin();
98 iopt!=opts.end(); ++iopt )
99 {
100 jobopt << *iopt << std::endl;
101 }
102 }
103 jobopt.close();
104 } //if ( FileName == "" )
105 try {
106 std::string fileName=FileName;
107
109 m_svcLoc = 0;
110 if (!Athena_test::initGaudi(fileName, m_svcLoc)) {
111 std::cerr << "This test can not be run" << std::endl;
112 return false;
113 }
114 CPPUNIT_ASSERT( 0 != m_svcLoc);
115
116 m_storeGate = 0;
117 static const bool CREATE(true);
118 bool sc = ( m_svcLoc->service( "StoreGateSvc",
119 m_storeGate, CREATE) ).isSuccess();
120 CPPUNIT_ASSERT( sc );
121 CPPUNIT_ASSERT( 0 != m_storeGate );
122
123 if ( false == sc || 0 == m_storeGate ) {
124 std::string error = "No valid pointer to StoreGateSvc !!";
125 std::cerr << error << std::endl;
126 //throw std::runtime_error(error);
127 return false;
128 } else {
129 return true;
130 }
131 } catch ( std::exception& e ) {
132 std::cerr << "Catched : " << e.what() << std::endl;
133 return false;
134 }
135}
136
139
140
141int main( int /*argc*/, char **/* argv */)
142 {
144 if ( false == setupStoreGate() ) {
145 std::string error = "Could not setup StoreGateSvc !!";
146 throw std::runtime_error(error);
147 }
148
150 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
151
153 CppUnit::TextTestRunner runner;
154 runner.addTest( suite );
155
156 // Change the default outputter to a compiler error format outputter
157 // uncomment the following line if you need a compiler outputter.
158 //runner.setOutputter(new CppUnit::CompilerOutputter( &runner.result(),
159 // std::cerr ) );
160
161 // Change the default outputter to a xml error format outputter
162 // uncomment the following line if you need a xml outputter.
163 //runner.setOutputter( new CppUnit::XmlOutputter( &runner.result(),
164 // std::cerr ) );
165
167 // bool wasSuccessful = runner.run_spi();
168 // If you want to avoid the CppUnit typical output change the line above
169 // by the following one:
170 bool wasSuccessful = runner.run("",false,true,false);
171
172 // Return error code 1 if the one of test failed.
173 if (!wasSuccessful) return 1;
174
175 // Uncomment the next line if you want to integrate CppUnit with Oval
176 // std::cout <<"[OVAL] Cppunit-result ="<<!wasSuccessful<<"\n" ;
177
178 return 0;
179 }
bool setupStoreGate()
Simple function to instantiate and initialize the StoreGateSvc.
#define CREATE(__TYPE)
static Double_t sc
The Athena Transient Store API.
int main()
Definition hello.cxx:18
minimal gaudi initialization for AthenaServices unit testing
bool initGaudi(ISvcLocator *&pSvcLoc)
Minimal Gaudi initialization for unit testing without job options.
Definition initGaudi.cxx:24