TreeFrogFramework  1.18
tftest.h
Go to the documentation of this file.
1 #ifndef TFTEST_H
2 #define TFTEST_H
3 
4 #include <QtTest/QtTest>
5 #include <QObject>
6 #include <QTextCodec>
7 #include <QByteArray>
8 #include <QEventLoop>
9 #include <TWebApplication>
10 #include <TAppSettings>
11 #include "tkvsdatabasepool.h"
12 #ifdef QT_SQL_LIB
13 # include <TActionThread>
14 # include "tsqldatabasepool.h"
15 #endif
16 
17 
18 #define TF_TEST_MAIN(TestObject) \
19 int main(int argc, char *argv[]) \
20 { \
21  class Thread : public TActionThread { \
22  public: \
23  Thread() : TActionThread(0), returnCode(0) { } \
24  volatile int returnCode; \
25  protected: \
26  virtual void run() \
27  { \
28  TestObject obj; \
29  returnCode = QTest::qExec(&obj, QCoreApplication::arguments()); \
30  commitTransactions(); \
31  for (QMap<int, QSqlDatabase>::iterator it = sqlDatabases.begin(); it != sqlDatabases.end(); ++it) { \
32  it.value().close(); /* close SQL database */ \
33  } \
34  for (QMap<int, TKvsDatabase>::iterator it = kvsDatabases.begin(); it != kvsDatabases.end(); ++it) { \
35  it.value().close(); /* close KVS database */ \
36  } \
37  QEventLoop eventLoop; \
38  while (eventLoop.processEvents()) {} \
39  } \
40  }; \
41  TWebApplication app(argc, argv); \
42  QByteArray codecName = Tf::appSettings()->value(Tf::InternalEncoding, "UTF-8").toByteArray(); \
43  QTextCodec *codec = QTextCodec::codecForName(codecName); \
44  QTextCodec::setCodecForLocale(codec); \
45  TF_SET_CODEC_FOR_TR(codec); \
46  app.setDatabaseEnvironment("test"); \
47  TSqlDatabasePool::instantiate(); \
48  TKvsDatabasePool::instantiate(); \
49  Thread thread; \
50  thread.start(); \
51  thread.wait(); \
52  _exit(thread.returnCode); \
53  return thread.returnCode; \
54 }
55 
56 
57 #define TF_TEST_SQLLESS_MAIN(TestObject) \
58 int main(int argc, char *argv[]) \
59 { \
60  TWebApplication app(argc, argv); \
61  QByteArray codecName = Tf::appSettings()->value(Tf::InternalEncoding, "UTF-8").toByteArray(); \
62  QTextCodec *codec = QTextCodec::codecForName(codecName); \
63  QTextCodec::setCodecForLocale(codec); \
64  TF_SET_CODEC_FOR_TR(codec); \
65  TestObject tc; \
66  return QTest::qExec(&tc, argc, argv); \
67 }
68 
69 #endif // TFTEST_H