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
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifdef HAVE_CONFIG_H
00041 # include <config.h>
00042 #endif
00043
00044 #include <string>
00045 #include <stdexcept>
00046
00047 #include <cppunit/CompilerOutputter.h>
00048 #include <cppunit/ui/text/TestRunner.h>
00049 #include <cppunit/TestListener.h>
00050 #include <cppunit/TestResult.h>
00051 #include <cppunit/TestFailure.h>
00052 #include <cppunit/extensions/TestFactoryRegistry.h>
00053 #include <cppunit/extensions/HelperMacros.h>
00054
00055 #include <base/Log.h>
00056
00057 #include <stdlib.h>
00058 #include <stdio.h>
00059 #include "base/globalsdef.h"
00060
00061 USE_NAMESPACE
00062 #ifdef HAVE_SIGNAL_H
00063 # include <signal.h>
00064 #endif
00065
00066 using namespace std;
00067
00068 void simplifyFilename(string &filename)
00069 {
00070 size_t pos = 0;
00071 while (true) {
00072 pos = filename.find(":", pos);
00073 if (pos == filename.npos ) {
00074 break;
00075 }
00076 filename.replace(pos, 1, "_");
00077 }
00078 }
00079
00080 class ClientOutputter : public CppUnit::CompilerOutputter {
00081 public:
00082 ClientOutputter(CppUnit::TestResultCollector *result, std::ostream &stream) :
00083 CompilerOutputter(result, stream) {}
00084 void write() {
00085
00086 LOG.setLogName("test.log");
00087 CompilerOutputter::write();
00088 }
00089 };
00090
00091 class ClientListener : public CppUnit::TestListener {
00092 public:
00093 ClientListener() :
00094 m_failed(false) {
00095 #ifdef HAVE_SIGNAL_H
00096
00097
00098 const char *alarm = getenv("CLIENT_TEST_ALARM");
00099 m_alarmSeconds = alarm ? atoi(alarm) : -1;
00100
00101 struct sigaction action;
00102 memset(&action, 0, sizeof(action));
00103 action.sa_handler = alarmTriggered;
00104 action.sa_flags = SA_NODEFER;
00105 sigaction(SIGALRM, &action, NULL);
00106 #endif
00107 }
00108
00109 void addAllowedFailures(string allowedFailures) {
00110 size_t start = 0, end;
00111 while ((end = allowedFailures.find(',', start)) != allowedFailures.npos) {
00112 size_t len = end - start;
00113 if (len) {
00114 m_allowedFailures.insert(allowedFailures.substr(start, len));
00115 }
00116 start = end + 1;
00117 }
00118 if (allowedFailures.size() > start) {
00119 m_allowedFailures.insert(allowedFailures.substr(start));
00120 }
00121 }
00122
00123 void startTest (CppUnit::Test *test) {
00124 m_currentTest = test->getName();
00125 LOG.setLogName("test.log");
00126 cerr << m_currentTest;
00127 string logfile = m_currentTest + ".log";
00128 simplifyFilename(logfile);
00129 remove(logfile.c_str());
00130 LOG.setLogName(logfile.c_str());
00131 m_testFailed = false;
00132
00133 #ifdef HAVE_SIGNAL_H
00134 if (m_alarmSeconds > 0) {
00135 alarm(m_alarmSeconds);
00136 }
00137 #endif
00138 }
00139
00140 void addFailure(const CppUnit::TestFailure &failure) {
00141 m_testFailed = true;
00142 }
00143
00144 void endTest (CppUnit::Test *test) {
00145 #ifdef HAVE_SIGNAL_H
00146 if (m_alarmSeconds > 0) {
00147 alarm(0);
00148 }
00149 #endif
00150
00151 LOG.setLogName("test.log");
00152 if (m_testFailed) {
00153 if (m_allowedFailures.find(m_currentTest) == m_allowedFailures.end()) {
00154 cerr << " *** failed ***";
00155 m_failed = true;
00156 } else {
00157 cerr << " *** failure ignored ***";
00158 }
00159 }
00160 cerr << "\n";
00161 }
00162
00163 bool hasFailed() { return m_failed; }
00164 const string &getCurrentTest() const { return m_currentTest; }
00165
00166 private:
00167 set<string> m_allowedFailures;
00168 bool m_failed, m_testFailed;
00169 string m_currentTest;
00170 int m_alarmSeconds;
00171
00172 static void alarmTriggered(int signal) {
00173 CPPUNIT_ASSERT_MESSAGE("test timed out", false);
00174 }
00175 } syncListener;
00176
00177 const string &getCurrentTest() {
00178 return syncListener.getCurrentTest();
00179 }
00180
00181 static void printTests(CppUnit::Test *test, int indention)
00182 {
00183 if (!test) {
00184 return;
00185 }
00186
00187 std::string name = test->getName();
00188 printf("%*s%s\n", indention * 3, "", name.c_str());
00189 for (int i = 0; i < test->getChildTestCount(); i++) {
00190 printTests(test->getChildTestAt(i), indention+1);
00191 }
00192 }
00193
00194 int main(int argc, char* argv[])
00195 {
00196
00197 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
00198
00199 if (argc >= 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
00200 printf("usage: %s [test name]+\n\n"
00201 "Without arguments all available tests are run.\n"
00202 "Otherwise only the tests or group of tests listed are run.\n"
00203 "Here is the test hierarchy of this test program:\n",
00204 argv[0]);
00205 printTests(suite, 1);
00206 return 0;
00207 }
00208
00209
00210 CppUnit::TextUi::TestRunner runner;
00211 runner.addTest( suite );
00212
00213
00214 runner.setOutputter( new ClientOutputter( &runner.result(),
00215 std::cerr ) );
00216
00217
00218 const char *allowedFailures = getenv("CLIENT_TEST_FAILURES");
00219 if (allowedFailures) {
00220 syncListener.addAllowedFailures(allowedFailures);
00221 }
00222 runner.eventManager().addListener(&syncListener);
00223
00224 try {
00225
00226 if (argc <= 1) {
00227
00228 runner.run("", false, true, false);
00229 } else {
00230
00231 for (int test = 1; test < argc; test++) {
00232 runner.run(argv[test], false, true, false);
00233 }
00234 }
00235
00236
00237 return syncListener.hasFailed() ? 1 : 0;
00238 } catch (invalid_argument e) {
00239
00240 std::cerr << std::endl
00241 << "ERROR: " << e.what()
00242 << std::endl;
00243 return 1;
00244 }
00245 }
00246
00247
00248