1 | /*******************************************************************************
|
---|
2 | * Copyright (c) 2008 Gerhard Leonhartsberger.
|
---|
3 | * All rights reserved. This program and the accompanying materials
|
---|
4 | * are made available under the terms of the Eclipse Public License v1.0
|
---|
5 | * which accompanies this distribution, and is available at
|
---|
6 | * http://www.eclipse.org/legal/epl-v10.html
|
---|
7 | *******************************************************************************/
|
---|
8 |
|
---|
9 | #include "TestRunnerClient.h"
|
---|
10 |
|
---|
11 | #ifdef CPPUNIT_MAIN
|
---|
12 |
|
---|
13 | #include "cppunit/TestResultCollector.h"
|
---|
14 | #include "cppunit/XmlOutputter.h"
|
---|
15 | #include "cppunit/TestSuite.h"
|
---|
16 | #include "cppunit/TestResult.h"
|
---|
17 | #include "cppunit/TestFailure.h"
|
---|
18 | #include "cppunit/SourceLine.h"
|
---|
19 | #include "cppunit/Exception.h"
|
---|
20 | #include "cppunit/extensions/TestFactoryRegistry.h"
|
---|
21 | #include "cppunit/extensions/TestDecorator.h"
|
---|
22 | #include "cppunit/ui/text/TestRunner.h"
|
---|
23 |
|
---|
24 | #include <iostream>
|
---|
25 | #include <sstream>
|
---|
26 | #include <typeinfo>
|
---|
27 | #include <vector>
|
---|
28 | #include <cstring>
|
---|
29 |
|
---|
30 | #include <errno.h>
|
---|
31 | #include <unistd.h>
|
---|
32 | #include <sys/time.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | #ifdef _WIN32 // Bugzilla 40710
|
---|
37 | #include <windows.h>
|
---|
38 | #include <winbase.h>
|
---|
39 | #include <winsock.h>
|
---|
40 | #else
|
---|
41 | #include <netinet/in.h>
|
---|
42 | #include <sys/socket.h>
|
---|
43 | #include <arpa/inet.h>
|
---|
44 | #include <netdb.h>
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #define MAX_HOSTNAME_SIZE 255
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * CppUnitServer protocol constants
|
---|
51 | */
|
---|
52 | static const std::string TRACE_START = "%TRACES ";
|
---|
53 | static const std::string TRACE_END = "%TRACEE ";
|
---|
54 | static const std::string TEST_RUN_START = "%TESTC ";
|
---|
55 | static const std::string TEST_START = "%TESTS ";
|
---|
56 | static const std::string TEST_END = "%TESTE ";
|
---|
57 | static const std::string TEST_ERROR = "%ERROR ";
|
---|
58 | static const std::string TEST_FAILED = "%FAILED ";
|
---|
59 | static const std::string TEST_RUN_END = "%RUNTIME";
|
---|
60 | static const std::string TEST_STOPPED = "%TSTSTP ";
|
---|
61 | static const std::string TEST_TREE = "%TSTTREE";
|
---|
62 |
|
---|
63 | TestRunnerClient::TestRunnerClient()
|
---|
64 | {
|
---|
65 | fTestResult = 0;
|
---|
66 | fClientSocket = -1;
|
---|
67 | fPort = 0;
|
---|
68 | fKeepAlive = 0;
|
---|
69 | fDebugMode = 0;
|
---|
70 |
|
---|
71 | fHost = (char *) malloc(MAX_HOSTNAME_SIZE);
|
---|
72 | strcpy(fHost, "");
|
---|
73 | }
|
---|
74 |
|
---|
75 | TestRunnerClient::~TestRunnerClient() {
|
---|
76 |
|
---|
77 | if (fHost != NULL) {
|
---|
78 | free(fHost);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | int TestRunnerClient::Run()
|
---|
83 | {
|
---|
84 | if (fDebugMode)
|
---|
85 | {
|
---|
86 | std::cerr << "TestRunnerClient: Starting client." << std::endl;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (Connect() == -1) {
|
---|
90 | return -1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | InstallListeners();
|
---|
94 |
|
---|
95 | RunTests();
|
---|
96 |
|
---|
97 | UninstallListeners();
|
---|
98 |
|
---|
99 | if(fTestResult != NULL)
|
---|
100 | {
|
---|
101 | fTestResult->stop();
|
---|
102 | fTestResult= NULL;
|
---|
103 | }
|
---|
104 |
|
---|
105 | ShutDown();
|
---|
106 |
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | void TestRunnerClient::Init(int n, char *args[])
|
---|
111 | {
|
---|
112 | ParseCommandLine(n, args);
|
---|
113 | DefineHostName();
|
---|
114 | }
|
---|
115 |
|
---|
116 | void TestRunnerClient::ParseCommandLine(int n, char *args[])
|
---|
117 | {
|
---|
118 | // parse all arguments passed by args
|
---|
119 | for(int i = 0; i < n; i++)
|
---|
120 | {
|
---|
121 | std::string arg(args[i]);
|
---|
122 |
|
---|
123 | // port option
|
---|
124 | std::string portOption("-port=");
|
---|
125 | int pos = arg.find(portOption);
|
---|
126 | if(pos> -1)
|
---|
127 | {
|
---|
128 | std::string v = arg.substr(pos + portOption.length(), arg.length());
|
---|
129 | fPort = atoi(v.c_str());
|
---|
130 | }
|
---|
131 |
|
---|
132 | // debug option
|
---|
133 | std::string debugOption("-debug");
|
---|
134 | pos = arg.find(debugOption);
|
---|
135 | if(pos> - 1)
|
---|
136 | {
|
---|
137 | fDebugMode = 1;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | void TestRunnerClient::DefineHostName()
|
---|
143 | {
|
---|
144 | // set fHost to hostname or localhost
|
---|
145 | int ret = gethostname(fHost, MAX_HOSTNAME_SIZE);
|
---|
146 | if (ret == -1)
|
---|
147 | {
|
---|
148 | strcpy(fHost, "localhost");
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | int TestRunnerClient::Connect()
|
---|
153 | {
|
---|
154 |
|
---|
155 | #ifdef _WIN32 // Bugzilla 40710
|
---|
156 | if (fDebugMode)
|
---|
157 | {
|
---|
158 | std::cerr << "TestRunnerClient: Starting Windows Sockets WSAStartup()." << std:endl;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // start up Windows Sockets
|
---|
162 | WSADATA WSAData;
|
---|
163 | int result = WSAStartup (MAKEWORD(1, 1), &WSAData);
|
---|
164 | if (result != NO_ERROR)
|
---|
165 | {
|
---|
166 | std::cerr << "TestRunnerClient: WSAStartup() failed! Error code: " << result << std::endl;
|
---|
167 | return -1;
|
---|
168 | }
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | if (fDebugMode)
|
---|
172 | {
|
---|
173 | std::cerr << "TestRunnerClient: Trying to connect to " << fHost << ":" << fPort << std::endl;
|
---|
174 | }
|
---|
175 |
|
---|
176 | fClientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
---|
177 | if (fClientSocket == -1)
|
---|
178 | {
|
---|
179 | std::cerr << "TestRunnerClient: Socket creation failed! error code: " << fClientSocket << std::endl;
|
---|
180 | return -1;
|
---|
181 | }
|
---|
182 |
|
---|
183 | struct hostent *host = gethostbyname(fHost);
|
---|
184 | if (host == NULL)
|
---|
185 | {
|
---|
186 | std::cerr << "TestRunnerClient: Cannot find host address for " << fHost << "." << std::endl;
|
---|
187 | fClientSocket = -1;
|
---|
188 | return -1;
|
---|
189 | }
|
---|
190 |
|
---|
191 | struct sockaddr_in name;
|
---|
192 | memset((void *)&name, 0, sizeof(struct sockaddr_in));
|
---|
193 | name.sin_family = AF_INET;
|
---|
194 | name.sin_port = htons(fPort);
|
---|
195 |
|
---|
196 | memcpy(&name.sin_addr, host->h_addr, host->h_length);
|
---|
197 |
|
---|
198 | if (fDebugMode) {
|
---|
199 | std::cerr << "TestRunnerClient: Waiting for the JVM to listen ... (trying 3 times)" << std::endl;
|
---|
200 | }
|
---|
201 |
|
---|
202 | int ret = -1;
|
---|
203 | int j = 0;
|
---|
204 | while ((j < 3) && (ret == -1))
|
---|
205 | {
|
---|
206 | ret = ::connect(fClientSocket, (struct sockaddr *) &name, sizeof(struct sockaddr_in));
|
---|
207 | if (ret == -1)
|
---|
208 | {
|
---|
209 | if (fDebugMode) {
|
---|
210 | std::cerr << "TestRunnerClient: Connection request, waiting 1 second. "
|
---|
211 | << ((j-3)*-1) << " times left." << std::endl;
|
---|
212 | }
|
---|
213 | PrivateSleep(1000);
|
---|
214 | j++;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | if (ret == -1)
|
---|
218 | {
|
---|
219 | std::cerr << "TestRunnerClient: No connection established. Error code: " << errno << std::endl;
|
---|
220 | fClientSocket = -1;
|
---|
221 | return -1;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (fDebugMode) {
|
---|
225 | std::cerr << "TestRunnerClient: Connection established." << std::endl;
|
---|
226 | }
|
---|
227 | return 0;
|
---|
228 | }
|
---|
229 |
|
---|
230 | void TestRunnerClient::InstallListeners()
|
---|
231 | {
|
---|
232 | fTestResult = new CppUnit::TestResult();
|
---|
233 | fTestResult->addListener(this);
|
---|
234 | }
|
---|
235 |
|
---|
236 | void TestRunnerClient::UninstallListeners()
|
---|
237 | {
|
---|
238 | fTestResult->removeListener(this);
|
---|
239 | }
|
---|
240 |
|
---|
241 | void TestRunnerClient::RunTests()
|
---|
242 | {
|
---|
243 |
|
---|
244 | CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
|
---|
245 | CppUnit::Test *suite = registry.makeTest();
|
---|
246 | int count = suite->countTestCases();
|
---|
247 | NotifyTestRunStarted(count);
|
---|
248 |
|
---|
249 | if (count == 0)
|
---|
250 | {
|
---|
251 | NotifyTestRunEnded(0);
|
---|
252 | }
|
---|
253 |
|
---|
254 | long startTime = CurrentTimeMillis();
|
---|
255 | if (fDebugMode)
|
---|
256 | {
|
---|
257 | std::cerr <<"TestRunnerClient: Start sending test case tree ..." << std::endl;
|
---|
258 | }
|
---|
259 |
|
---|
260 | SendTestTree(suite);
|
---|
261 |
|
---|
262 | int elapsedTime = CurrentTimeMillis() - startTime;
|
---|
263 | if (fDebugMode) {
|
---|
264 | std::cerr << "TestRunnerClient: Done sending test case tree. Elapsed time is "
|
---|
265 | << elapsedTime << "ms." << std::endl;
|
---|
266 | }
|
---|
267 |
|
---|
268 | long testStartTime = CurrentTimeMillis();
|
---|
269 | if (fDebugMode) {
|
---|
270 | std::cerr << "TestRunnerClient: Test start time is " << testStartTime
|
---|
271 | << "ms." << std::endl;
|
---|
272 | }
|
---|
273 |
|
---|
274 | suite->run(fTestResult);
|
---|
275 |
|
---|
276 | if (fTestResult == NULL || fTestResult->shouldStop())
|
---|
277 | {
|
---|
278 | NotifyTestRunStopped(CurrentTimeMillis() - testStartTime);
|
---|
279 | }
|
---|
280 | else
|
---|
281 | {
|
---|
282 | NotifyTestRunEnded(CurrentTimeMillis() - testStartTime);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | void TestRunnerClient::ShutDown()
|
---|
287 | {
|
---|
288 | if (fClientSocket != -1)
|
---|
289 | {
|
---|
290 | if (fDebugMode) {
|
---|
291 | std::cerr << "TestRunnerClient: Closing connection to CppUnit sever at "
|
---|
292 | << fHost << ":" << fPort << std::endl;
|
---|
293 | }
|
---|
294 |
|
---|
295 | #ifdef _WIN32 // Bugzilla 40710
|
---|
296 | // TODO: std:err output for error return codes
|
---|
297 | closesocket(fClientSocket);
|
---|
298 | WSACleanup();
|
---|
299 | #else
|
---|
300 | int result = close(fClientSocket);
|
---|
301 | if (result != 0)
|
---|
302 | {
|
---|
303 | std::cerr << "TestRunnerClient: Close connection error: " << errno << std::endl;
|
---|
304 | }
|
---|
305 | #endif
|
---|
306 |
|
---|
307 | fClientSocket = -1;
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | void TestRunnerClient::Stop()
|
---|
312 | {
|
---|
313 | if (fTestResult != NULL)
|
---|
314 | {
|
---|
315 | fTestResult->stop();
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | void TestRunnerClient::SendTestTree(CppUnit::Test *test)
|
---|
320 | {
|
---|
321 | if (typeid(*test) == typeid(CppUnit::TestDecorator))
|
---|
322 | {
|
---|
323 | class TmpClass : public CppUnit::TestDecorator {
|
---|
324 |
|
---|
325 | public:
|
---|
326 | TmpClass(Test *t):CppUnit::TestDecorator(t)
|
---|
327 | {
|
---|
328 | // nothing to do
|
---|
329 | }
|
---|
330 |
|
---|
331 | ~TmpClass() // Bugzilla 39894
|
---|
332 | {
|
---|
333 | // nothing to do
|
---|
334 | }
|
---|
335 |
|
---|
336 | CppUnit::Test *getTest()
|
---|
337 | {
|
---|
338 | return m_test;
|
---|
339 | }
|
---|
340 | };
|
---|
341 |
|
---|
342 | TmpClass *t = (TmpClass *)test;
|
---|
343 | SendTestTree(t->getTest());
|
---|
344 | }
|
---|
345 | else if (typeid(*test) == typeid(CppUnit::TestSuite))
|
---|
346 | {
|
---|
347 | CppUnit::TestSuite *suite = (CppUnit::TestSuite *)test;
|
---|
348 | const std::vector<CppUnit::Test *> &x = suite->getTests();
|
---|
349 |
|
---|
350 | std::ostringstream os;
|
---|
351 | os << suite->getName() << ",true," << x.size();
|
---|
352 | NotifyTestTreeEntry(os.str());
|
---|
353 |
|
---|
354 | for(unsigned int i=0; i < x.size(); i++)
|
---|
355 | {
|
---|
356 | SendTestTree(x[i]);
|
---|
357 | }
|
---|
358 | }
|
---|
359 | else
|
---|
360 | {
|
---|
361 | std::ostringstream os;
|
---|
362 | os << test->getName() << ",false," << test->countTestCases();
|
---|
363 | NotifyTestTreeEntry(os.str());
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | void TestRunnerClient::SendMessage(std::string msg)
|
---|
368 | {
|
---|
369 | if (fClientSocket == -1)
|
---|
370 | {
|
---|
371 | return;
|
---|
372 | }
|
---|
373 |
|
---|
374 | #ifdef _WIN32 // Bugzilla 40710
|
---|
375 | send (fClientSocket, msg.c_str(), msg.length(), 0);
|
---|
376 | send (fClientSocket, "\n", 1, 0);
|
---|
377 | #else
|
---|
378 | write(fClientSocket, msg.c_str(), msg.length());
|
---|
379 | write(fClientSocket, "\n", 1);
|
---|
380 | #endif
|
---|
381 |
|
---|
382 | if (fDebugMode)
|
---|
383 | {
|
---|
384 | std::cerr << "TestRunnerClient: Sent message \"" << msg << "\""
|
---|
385 | << std::endl;
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | void TestRunnerClient::NotifyTestRunStarted(int testCount)
|
---|
390 | {
|
---|
391 | std::ostringstream os;
|
---|
392 | os << TEST_RUN_START << testCount;
|
---|
393 | SendMessage(os.str());
|
---|
394 | }
|
---|
395 |
|
---|
396 | void TestRunnerClient::NotifyTestRunEnded(long elapsedTime)
|
---|
397 | {
|
---|
398 | std::ostringstream os;
|
---|
399 | os << TEST_RUN_END << elapsedTime;
|
---|
400 | SendMessage(os.str());
|
---|
401 | }
|
---|
402 |
|
---|
403 | void TestRunnerClient::NotifyTestRunStopped(long elapsedTime)
|
---|
404 | {
|
---|
405 | std::ostringstream os;
|
---|
406 | os << TEST_STOPPED << elapsedTime;
|
---|
407 | SendMessage(os.str());
|
---|
408 | }
|
---|
409 |
|
---|
410 | void TestRunnerClient::NotifyTestTreeEntry(std::string treeEntry)
|
---|
411 | {
|
---|
412 | SendMessage(TEST_TREE + treeEntry);
|
---|
413 | }
|
---|
414 |
|
---|
415 | void TestRunnerClient::NotifyTestStarted(std::string testName)
|
---|
416 | {
|
---|
417 | SendMessage(TEST_START + testName);
|
---|
418 | }
|
---|
419 |
|
---|
420 | void TestRunnerClient::NotifyTestEnded(std::string testName)
|
---|
421 | {
|
---|
422 | SendMessage(TEST_END + testName);
|
---|
423 | }
|
---|
424 |
|
---|
425 | void TestRunnerClient::NotifyTestFailed(std::string status, std::string testName, std::string trace)
|
---|
426 | {
|
---|
427 | SendMessage(status + testName);
|
---|
428 | SendMessage(TRACE_START);
|
---|
429 | SendMessage(trace);
|
---|
430 | SendMessage(TRACE_END);
|
---|
431 | }
|
---|
432 |
|
---|
433 | // From TestListener
|
---|
434 | void TestRunnerClient::startTest(CppUnit::Test *test)
|
---|
435 | {
|
---|
436 | NotifyTestStarted(test->getName());
|
---|
437 | }
|
---|
438 |
|
---|
439 | // From TestListener
|
---|
440 | void TestRunnerClient::addFailure(const CppUnit::TestFailure &failure)
|
---|
441 | {
|
---|
442 | if(failure.isError())
|
---|
443 | {
|
---|
444 | NotifyTestFailed(TEST_ERROR,failure.failedTestName(),GetTrace(failure));
|
---|
445 | }
|
---|
446 | else
|
---|
447 | {
|
---|
448 | NotifyTestFailed(TEST_FAILED,failure.failedTestName(),GetTrace(failure));
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | // From TestListener
|
---|
453 | void TestRunnerClient::endTest(CppUnit::Test *test)
|
---|
454 | {
|
---|
455 | NotifyTestEnded(test->getName());
|
---|
456 | }
|
---|
457 |
|
---|
458 | std::string TestRunnerClient::GetTrace(const CppUnit::TestFailure &failure)
|
---|
459 | {
|
---|
460 | std::ostringstream os;
|
---|
461 |
|
---|
462 | CppUnit::Exception *e=failure.thrownException();
|
---|
463 | if(e->sourceLine().lineNumber()!=-1)
|
---|
464 | {
|
---|
465 | os << "File " << e->sourceLine().fileName() << ":" << e->sourceLine().lineNumber() << "\n";
|
---|
466 | }
|
---|
467 | else
|
---|
468 | {
|
---|
469 | os << "File Unknown:1\n";
|
---|
470 | }
|
---|
471 | /* TODO: expected, actual value implementation
|
---|
472 | if(typeid(*e)==typeid(CppUnit::NotEqualException))
|
---|
473 | {
|
---|
474 | CppUnit::NotEqualException *ne=(CppUnit::NotEqualException *)e;
|
---|
475 |
|
---|
476 | os << "Expected Value: " << ne->expectedValue() << "\n";
|
---|
477 | os << "Actual Value: " << ne->expectedValue() << "\n";
|
---|
478 | os << "Additional Message: " << ne->additionalMessage() << "\n";
|
---|
479 | }
|
---|
480 | else
|
---|
481 | {
|
---|
482 | End */
|
---|
483 | os << "Message: " << std::string(e->what()) << "\n";
|
---|
484 | /* } */
|
---|
485 |
|
---|
486 | return(os.str());
|
---|
487 | }
|
---|
488 |
|
---|
489 | long TestRunnerClient::CurrentTimeMillis()
|
---|
490 | {
|
---|
491 | #ifdef _WIN32 // Bugzilla 40710
|
---|
492 | unsigned long long p;
|
---|
493 | __asm__ __volatile__ ("rdtsc" : "=A" (p));
|
---|
494 | return (unsigned long)p;
|
---|
495 | #else
|
---|
496 | struct timeval tv;
|
---|
497 | gettimeofday(&tv, NULL);
|
---|
498 |
|
---|
499 | return((long)(tv.tv_sec*1000) + (tv.tv_usec/1000));
|
---|
500 | #endif
|
---|
501 | }
|
---|
502 |
|
---|
503 | void TestRunnerClient::PrivateSleep(int millisecs)
|
---|
504 | {
|
---|
505 | struct timeval delta;
|
---|
506 | delta.tv_sec = (millisecs * 1000L) / 1000000L;
|
---|
507 | delta.tv_usec = (millisecs * 1000L) % 1000000L;
|
---|
508 | select (0, NULL, NULL, NULL, &delta);
|
---|
509 | }
|
---|
510 |
|
---|
511 | /*!
|
---|
512 | * This is the main routine. The TestRunnerClient is initialized and run. The
|
---|
513 | * CppUnit tests are created, executed, and sent to the CppUnitServer.
|
---|
514 | * If no connection to the CppUnitServer was established the CppUnit tests are
|
---|
515 | * displayed on the console.
|
---|
516 | *
|
---|
517 | * @return <code>0</code> if the results of the CppUnit tests were sent to the
|
---|
518 | * CppUnitServer successfully.
|
---|
519 | * <code>-1</code> if a connection could not be established to the
|
---|
520 | * CppUnitServer.
|
---|
521 | */
|
---|
522 | int CPPUNIT_MAIN(int n, char *arg[])
|
---|
523 | {
|
---|
524 | TestRunnerClient client;
|
---|
525 |
|
---|
526 | client.Init(n, arg);
|
---|
527 | int ret = client.Run();
|
---|
528 | if (ret == -1)
|
---|
529 | {
|
---|
530 | CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
|
---|
531 | CppUnit::Test *suite = registry.makeTest();
|
---|
532 |
|
---|
533 | CppUnit::TextUi::TestRunner *runner = NULL;
|
---|
534 | runner = new CppUnit::TextUi::TestRunner();
|
---|
535 | runner->addTest(suite);
|
---|
536 | runner->run();
|
---|
537 | }
|
---|
538 |
|
---|
539 | exit(ret);
|
---|
540 | }
|
---|
541 |
|
---|
542 | #endif /*CPPUNIT_MAIN*/
|
---|