TreeFrogFramework  2.8
tftest.h
Go to the documentation of this file.
1 #pragma once
2 #include "tkvsdatabasepool.h"
3 #include "turlroute.h"
4 #include <QByteArray>
5 #include <QEventLoop>
6 #include <QObject>
7 #include <QtTest/QtTest>
8 #include <TAppSettings>
9 #include <TSystemGlobal>
10 #include <TStdErrSystemLogger>
11 #include <TWebApplication>
12 #ifdef QT_SQL_LIB
13 #include "tsqldatabasepool.h"
14 #include <TActionThread>
15 #endif
16 #ifdef Q_OS_LINUX
17 #include <TMultiplexingServer>
18 #endif
19 
20 
21 #define TF_TEST_MAIN(TestObject) TF_TEST_SQL_MAIN(TestObject, true);
22 
23 #if QT_VERSION < 0x060000
24 #include <QTextCodec>
25 
26 #define TF_TEST_SQL_MAIN(TestObject, EnableTransactions) \
27  int main(int argc, char *argv[]) \
28  { \
29  class Thread : public TActionThread { \
30  public: \
31  Thread() : TActionThread(0), returnCode(0) { } \
32  volatile int returnCode; \
33  \
34  protected: \
35  virtual void run() \
36  { \
37  setTransactionEnabled(EnableTransactions); \
38  TestObject obj; \
39  returnCode = QTest::qExec(&obj, QCoreApplication::arguments()); \
40  commitTransactions(); \
41  for (QMap<int, TSqlTransaction>::iterator it = sqlDatabases.begin(); it != sqlDatabases.end(); ++it) { \
42  it.value().database().close(); /* close SQL database */ \
43  } \
44  for (QMap<int, TKvsDatabase>::iterator it = kvsDatabases.begin(); it != kvsDatabases.end(); ++it) { \
45  it.value().close(); /* close KVS database */ \
46  } \
47  QEventLoop eventLoop; \
48  while (eventLoop.processEvents()) { \
49  } \
50  } \
51  }; \
52  TWebApplication app(argc, argv); \
53  QByteArray codecName = Tf::appSettings()->value(Tf::InternalEncoding).toByteArray(); \
54  QTextCodec *codec = QTextCodec::codecForName(codecName); \
55  QTextCodec::setCodecForLocale(codec); \
56  Tf::setupSystemLogger(new TStdErrSystemLogger); \
57  app.setDatabaseEnvironment("test"); \
58  TUrlRoute::instance(); \
59  TSqlDatabasePool::instance(); \
60  TKvsDatabasePool::instance(); \
61  INIT_MULTIPLEXING_SERVER(); \
62  Thread thread; \
63  thread.start(); \
64  thread.wait(); \
65  _exit(thread.returnCode); \
66  return thread.returnCode; \
67  }
68 
69 #else
70 
71 #define TF_TEST_SQL_MAIN(TestObject, EnableTransactions) \
72  int main(int argc, char *argv[]) \
73  { \
74  class Thread : public TActionThread { \
75  public: \
76  Thread() : TActionThread(0), returnCode(0) { } \
77  volatile int returnCode; \
78  \
79  protected: \
80  virtual void run() \
81  { \
82  setTransactionEnabled(EnableTransactions); \
83  TestObject obj; \
84  returnCode = QTest::qExec(&obj, QCoreApplication::arguments()); \
85  commitTransactions(); \
86  for (QMap<int, TSqlTransaction>::iterator it = sqlDatabases.begin(); it != sqlDatabases.end(); ++it) { \
87  it.value().database().close(); /* close SQL database */ \
88  } \
89  for (QMap<int, TKvsDatabase>::iterator it = kvsDatabases.begin(); it != kvsDatabases.end(); ++it) { \
90  it.value().close(); /* close KVS database */ \
91  } \
92  QEventLoop eventLoop; \
93  while (eventLoop.processEvents()) { \
94  } \
95  } \
96  }; \
97  TWebApplication app(argc, argv); \
98  Tf::setupSystemLogger(new TStdErrSystemLogger); \
99  app.setDatabaseEnvironment("test"); \
100  TUrlRoute::instance(); \
101  TSqlDatabasePool::instance(); \
102  TKvsDatabasePool::instance(); \
103  INIT_MULTIPLEXING_SERVER(); \
104  Thread thread; \
105  thread.start(); \
106  thread.wait(); \
107  _exit(thread.returnCode); \
108  return thread.returnCode; \
109  }
110 #endif
111 
112 
113 #if QT_VERSION < 0x060000
114 
115 #define TF_TEST_SQLLESS_MAIN(TestObject) \
116  int main(int argc, char *argv[]) \
117  { \
118  TWebApplication app(argc, argv); \
119  Tf::setupSystemLogger(new TStdErrSystemLogger); \
120  QByteArray codecName = Tf::appSettings()->value(Tf::InternalEncoding).toByteArray(); \
121  QTextCodec *codec = QTextCodec::codecForName(codecName); \
122  QTextCodec::setCodecForLocale(codec); \
123  TestObject tc; \
124  return QTest::qExec(&tc, argc, argv); \
125  }
126 
127 #else
128 
129 #define TF_TEST_SQLLESS_MAIN(TestObject) \
130  int main(int argc, char *argv[]) \
131  { \
132  TWebApplication app(argc, argv); \
133  Tf::setupSystemLogger(new TStdErrSystemLogger); \
134  TestObject tc; \
135  return QTest::qExec(&tc, argc, argv); \
136  }
137 
138 #endif
139 
140 
141 #ifdef Q_OS_LINUX
142 #define INIT_MULTIPLEXING_SERVER() TMultiplexingServer::instantiate(0)
143 #else
144 #define INIT_MULTIPLEXING_SERVER()
145 #endif