00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <cppunit/CompilerOutputter.h>
00020 #include <cppunit/extensions/TestFactoryRegistry.h>
00021 #include <cppunit/ui/text/TestRunner.h>
00022 #include <cppunit/TestListener.h>
00023 #include <cppunit/TestResult.h>
00024 #include <cppunit/TestFailure.h>
00025 #include <cppunit/extensions/HelperMacros.h>
00026
00027 #include <posix/base/posixlog.h>
00028
00029 #include <unistd.h>
00030 #include <signal.h>
00031
00032 #include <string>
00033 #include <stdexcept>
00034 using namespace std;
00035
00036 #include "Test.h"
00037
00038 class Sync4jOutputter : public CppUnit::CompilerOutputter {
00039 public:
00040 Sync4jOutputter(CppUnit::TestResultCollector *result, std::ostream &stream) :
00041 CompilerOutputter(result, stream) {}
00042 void write() {
00043
00044 setLogFile("sync.log", 0);
00045 CompilerOutputter::write();
00046 }
00047 };
00048
00049 class Sync4jListener : public CppUnit::TestListener {
00050 public:
00051 Sync4jListener() :
00052 m_failed(false) {
00053
00054
00055 const char *alarm = getenv("TEST_EVOLUTION_ALARM");
00056 m_alarmSeconds = alarm ? atoi(alarm) : -1;
00057
00058 struct sigaction action;
00059 memset(&action, 0, sizeof(action));
00060 action.sa_handler = alarmTriggered;
00061 action.sa_flags = SA_NOMASK;
00062 sigaction(SIGALRM, &action, NULL);
00063 }
00064
00065 void addAllowedFailures(string allowedFailures) {
00066 int start = 0, end;
00067 while ((end = allowedFailures.find(',', start)) != allowedFailures.npos) {
00068 int len = end - start;
00069 if (len) {
00070 m_allowedFailures.insert(allowedFailures.substr(start, len));
00071 }
00072 start = end + 1;
00073 }
00074 if (allowedFailures.size() > start) {
00075 m_allowedFailures.insert(allowedFailures.substr(start));
00076 }
00077 }
00078
00079 void startTest (CppUnit::Test *test) {
00080 m_currentTest = test->getName();
00081 setLogFile( "-", 0 );
00082 cerr << m_currentTest;
00083 string logfile = m_currentTest + ".log";
00084 simplifyFilename(logfile);
00085 remove(logfile.c_str());
00086 setLogFile( logfile.c_str(), 1 );
00087 m_testFailed = false;
00088
00089 if (m_alarmSeconds > 0) {
00090 alarm(m_alarmSeconds);
00091 }
00092 }
00093
00094 void addFailure(const CppUnit::TestFailure &failure) {
00095 m_testFailed = true;
00096 }
00097
00098 void endTest (CppUnit::Test *test) {
00099 if (m_alarmSeconds > 0) {
00100 alarm(0);
00101 }
00102
00103 setLogFile( "-", 0 );
00104 if (m_testFailed) {
00105 if (m_allowedFailures.find(m_currentTest) == m_allowedFailures.end()) {
00106 cerr << " *** failed ***";
00107 m_failed = true;
00108 } else {
00109 cerr << " *** failure ignored ***";
00110 }
00111 }
00112 cerr << "\n";
00113 }
00114
00115 bool hasFailed() { return m_failed; }
00116 const string &getCurrentTest() const { return m_currentTest; }
00117
00118 private:
00119 set<string> m_allowedFailures;
00120 bool m_failed, m_testFailed;
00121 string m_currentTest;
00122 int m_alarmSeconds;
00123
00124 static void alarmTriggered(int signal) {
00125 CPPUNIT_ASSERT_MESSAGE(false, "test timed out");
00126 }
00127 } syncListener;
00128
00129 const string &getCurrentTest() {
00130 return syncListener.getCurrentTest();
00131 }
00132
00133 void simplifyFilename(string &filename)
00134 {
00135 size_t pos = 0;
00136 while (true) {
00137 pos = filename.find(":", pos);
00138 if (pos == filename.npos ) {
00139 break;
00140 }
00141 filename.replace(pos, 1, "_");
00142 }
00143 }
00144
00145 int main(int argc, char* argv[])
00146 {
00147
00148 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
00149
00150
00151 CppUnit::TextUi::TestRunner runner;
00152 runner.addTest( suite );
00153
00154
00155 runner.setOutputter( new Sync4jOutputter( &runner.result(),
00156 std::cerr ) );
00157
00158
00159 const char *allowedFailures = getenv("TEST_EVOLUTION_FAILURES");
00160 if (allowedFailures) {
00161 syncListener.addAllowedFailures(allowedFailures);
00162 }
00163 runner.eventManager().addListener(&syncListener);
00164
00165 try {
00166
00167 if (argc <= 1) {
00168
00169 runner.run("", false, true, false);
00170 } else {
00171
00172 for (int test = 1; test < argc; test++) {
00173 runner.run(argv[test], false, true, false);
00174 }
00175 }
00176
00177
00178 return syncListener.hasFailed() ? 1 : 0;
00179 } catch (invalid_argument e) {
00180
00181 std::cerr << std::endl
00182 << "ERROR: " << e.what()
00183 << std::endl;
00184 return 1;
00185 }
00186 }