00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #pragma once
00032
00033 #include "TestToolBox/SynchronizerImpl.h"
00034 #include "TestToolBox/EnvironmentImpl.h"
00035 #include "TestToolBox/TestEventsImpl.h"
00036 #include "TestToolBox/ErrorEventsImpl.h"
00037 #include "TestToolBox/ProtocolImpl.h"
00038 #include "TestToolBox/TestTimeImpl.h"
00039 #include "TestToolBox/ExtensionsForBoostTest/BoostEventReceiverForSynchronizer.h"
00040 #include "TestToolBox/ExtensionsForBoostTest/BoostEventReceiverForTestEvents.h"
00041
00042 #include <iostream>
00043
00044 #include <boost/thread.hpp>
00045
00046 namespace TestToolBox
00047 {
00048
00049
00050
00051 class AutoSetupDefaultTestEnv
00052 {
00053
00054
00055 public:
00056
00057
00058 AutoSetupDefaultTestEnv()
00059 : m_cleanupTestEvents (true)
00060 , m_cleanupProtocol (true)
00061 {
00062 Synchronizer::Get()->
00063 ConnectWithEventReceiver(&m_eventReceiverForSynchronizer);
00064 TestEvents::Get()->
00065 ConnectWithEventReceiver(&m_eventReceiverForTestEvents);
00066 TestEvents::Get()->
00067 ConnectWithProtocol (Protocol::Get());
00068 }
00069
00070
00071
00072 AutoSetupDefaultTestEnv(
00073 IProtocol* in_pProtocol)
00074 : m_cleanupTestEvents (true)
00075 , m_cleanupProtocol (false)
00076 {
00077 Synchronizer::Get()->
00078 ConnectWithEventReceiver(&m_eventReceiverForSynchronizer);
00079 TestEvents::Get()->
00080 ConnectWithEventReceiver(&m_eventReceiverForTestEvents);
00081 if (in_pProtocol)
00082 {
00083 TestEvents::Get()->
00084 ConnectWithProtocol (in_pProtocol);
00085 }
00086 }
00087
00088 virtual ~AutoSetupDefaultTestEnv()
00089 {
00090 if (m_cleanupTestEvents)
00091 {
00092 TestEvents::Cleanup();
00093 }
00094 Synchronizer::Cleanup();
00095 Environment::Cleanup();
00096
00097 if (m_cleanupProtocol)
00098 {
00099 Protocol::Cleanup();
00100 }
00101 }
00102 protected:
00103
00104 bool m_cleanupProtocol;
00105 bool m_cleanupTestEvents;
00106
00107 BoostEventReceiverForSynchronizer m_eventReceiverForSynchronizer;
00108 BoostEventReceiverForTestEvents m_eventReceiverForTestEvents;
00109
00110
00111 };
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 typedef ErrorEvents<> MyErrorEvents;
00126
00127 __declspec(selectany) MyErrorEvents* MyErrorEvents::s_pErrorEvents = 0;
00128
00129
00130
00131
00132 class AutoSetupTestEnvWithErrors : public AutoSetupDefaultTestEnv
00133 {
00134 public:
00135
00136 AutoSetupTestEnvWithErrors()
00137 {
00138 MyErrorEvents::Get(); // assure that singleton is created before concurrent
00139 // accesses may occur
00140
00141 // TODO:
00142 // Define reactions for the severity values which should not be treated
00143 // like default (=regular error, reported as test event, test case is continued)
00144 }
00145 ~AutoSetupTestEnvWithErrors()
00146 {
00147 MyErrorEvents::Cleanup();
00148 }
00149
00150
00151 };
00152 */
00153
00154 class AutoTestCase
00155 {
00156 public:
00157 explicit AutoTestCase(
00158 std::string const & in_testCaseDescription,
00159 char const * in_file = 0,
00160 int const in_line = 0)
00161 : m_file(in_file)
00162 , m_line (in_line)
00163 {
00164 TestEvents::Get()->StartNewTestCase(in_testCaseDescription);
00165 }
00166 ~AutoTestCase(void)
00167 {
00168 TestToolBox::TestEvents::Get()->CheckForUnexpectedEvents ("~AutoTestCase", m_file,m_line);
00169 BOOST_CHECK(TestToolBox::TestEvents::Get()->CurTestIsStillOk());
00170 }
00171 private:
00172 char const * m_file;
00173 int const m_line;
00174 };
00175
00176
00177 class FinalCleanup
00178 {
00179 public:
00180 virtual ~FinalCleanup (void)
00181 {
00182 Synchronizer::Cleanup();
00183 TestEvents::Cleanup();
00184 MyErrorEvents::Cleanup();
00185 Protocol::Cleanup();
00186 TestTime::Cleanup();
00187 Environment::Cleanup();
00188 }
00189 };
00190
00191 class DefaultSettingsForBoostAndTTB : public FinalCleanup
00192 {
00193 public:
00194 DefaultSettingsForBoostAndTTB (
00195 unsigned char in_protocolMode = OutputMode::M_STDOUT | OutputMode::M_OUTFILE | OutputMode::M_RPTFILE,
00196 bool in_detailedLog = true)
00197 {
00198
00199
00200
00201 Protocol::Get(in_protocolMode);
00202
00203 Synchronizer::Get();
00204 TestEvents::Get();
00205 MyErrorEvents::Get();
00206
00207
00208 MyErrorEvents::Get()->DeleteStoredErrorsAfterReport(true);
00209
00210 if (in_protocolMode == OutputMode::M_NULL)
00211 {
00212
00213 Synchronizer::Get()->
00214 ConnectWithEventReceiver(&m_eventReceiverForSynchronizer);
00215 TestEvents::Get()->
00216 ConnectWithEventReceiver(&m_eventReceiverForTestEvents);
00217 }
00218 else
00219 {
00220 Synchronizer::Get()->
00221 ConnectWithEventReceiver(TestEvents::Get());
00222
00223
00224
00225 TestEvents::Get()->
00226 ConnectWithProtocol (Protocol::Get());
00227 }
00228
00229 TestEvents::Get()->SetDetailedLog(in_detailedLog);
00230 }
00231
00232 protected:
00233 BoostEventReceiverForSynchronizer m_eventReceiverForSynchronizer;
00234 BoostEventReceiverForTestEvents m_eventReceiverForTestEvents;
00235 };
00236
00237
00238 #define TTB_AUTO_TEST_CASE(DESC) AutoTestCase autoTestCase(DESC,__FILE__,__LINE__)
00239
00240 #define TTB_TEST_CASE(DESC) \
00241 TestToolBox::TestEvents::Get()->CheckForUnexpectedEvents ("next test case", __FILE__,__LINE__); \
00242 BOOST_CHECK(TestToolBox::TestEvents::Get()->CurTestIsStillOk()); \
00243 TestToolBox::TestEvents::Get()->StartNewTestCase(DESC);
00244
00245 #define TTB_BOOST_CHECK_FOR_TEST_EVENTS() \
00246 BOOST_CHECK(TestToolBox::TestEvents::Get()->AllTestsHaveSucceeded());
00247
00248
00249
00250 inline void DummyFunction(){}
00251
00252 class WorkAroundBoostThreadMemLeak
00253 {
00254 public:
00255 WorkAroundBoostThreadMemLeak ()
00256 {
00257 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF );
00258 _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG );
00259 _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
00260 _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_DEBUG );
00261 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
00262
00263
00264 {
00265
00266
00267 boost::thread t(DummyFunction);
00268 t.join();
00269 }
00270
00271
00272 _CrtMemCheckpoint(&memStateStartup);
00273 }
00274 ~WorkAroundBoostThreadMemLeak ()
00275 {
00276 cout << "MEMLEAK REPORT (Workaround Boost thread memleak / run with --detect_memory_leak=0)" << endl;
00277 _CrtMemCheckpoint(&memStateShutdown);
00278 #ifdef _DEBUG
00279
00280 _CrtMemState diffState;
00281 #endif
00282 if (_CrtMemDifference(&diffState,&memStateStartup,&memStateShutdown))
00283 {
00284 _CrtMemDumpStatistics(&diffState);
00285 _CrtMemDumpAllObjectsSince(&memStateStartup);
00286 cout << "MEMLEAK REPORT: End of leaks report!" << endl;
00287 }
00288 else
00289 {
00290 cout << "MEMLEAK REPORT: No leaks detected!" << endl;
00291 }
00292
00293 }
00294
00295 private:
00296 _CrtMemState memStateStartup;
00297 _CrtMemState memStateShutdown;
00298
00299 };
00300
00301 };
00302
00303
00304