update credits
[LibreOffice.git] / sal / cppunittester / cppunittester.cxx
blobb637fb62cb2dcd34aa0792c86e0e7a8d831b5279
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifdef WNT
21 #include <windows.h>
22 #endif
24 #include <cstdlib>
25 #include <iostream>
26 #include <string>
27 #include <sal/types.h>
28 #include "cppunittester/protectorfactory.hxx"
29 #include "osl/module.h"
30 #include "osl/module.hxx"
31 #include "osl/thread.h"
32 #include "rtl/process.h"
33 #include "rtl/string.h"
34 #include "rtl/string.hxx"
35 #include "rtl/textcvt.h"
36 #include "rtl/ustring.hxx"
37 #include "sal/main.h"
39 #include "cppunit/CompilerOutputter.h"
40 #include "cppunit/Exception.h"
41 #include "cppunit/TestFailure.h"
42 #include "cppunit/TestResult.h"
43 #include "cppunit/TestResultCollector.h"
44 #include "cppunit/TestRunner.h"
45 #include "cppunit/extensions/TestFactoryRegistry.h"
46 #include "cppunit/plugin/PlugInManager.h"
47 #include "cppunit/plugin/DynamicLibraryManagerException.h"
48 #include "cppunit/portability/Stream.h"
50 #include "boost/noncopyable.hpp"
51 #include <boost/scoped_array.hpp>
52 #include <boost/algorithm/string.hpp>
54 #include <algorithm>
56 namespace {
58 void usageFailure() {
59 std::cerr
60 << ("Usage: cppunittester (--protector <shared-library-path>"
61 " <function-symbol>)* <shared-library-path>")
62 << std::endl;
63 std::exit(EXIT_FAILURE);
66 rtl::OUString getArgument(sal_Int32 index) {
67 rtl::OUString arg;
68 rtl_getAppCommandArg(index, &arg.pData);
69 return arg;
72 std::string convertLazy(rtl::OUString const & s16) {
73 rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
74 static_assert(sizeof (sal_Int32) <= sizeof (std::string::size_type), "must be at least the same size");
75 // ensure following cast is legitimate
76 return std::string(
77 s8.getStr(), static_cast< std::string::size_type >(s8.getLength()));
80 #if defined TIMETESTS
81 //Output how long each test took
82 class TimingListener
83 : public CppUnit::TestListener
84 , private boost::noncopyable
86 public:
87 void startTest( CppUnit::Test *) SAL_OVERRIDE
89 m_nStartTime = osl_getGlobalTimer();
92 void endTest( CppUnit::Test *test ) SAL_OVERRIDE
94 sal_uInt32 nEndTime = osl_getGlobalTimer();
95 std::cout << test->getName() << ": " << nEndTime-m_nStartTime
96 << "ms" << std::endl;
99 private:
100 sal_uInt32 m_nStartTime;
102 #endif
104 #ifdef UNX
105 #include <stdlib.h>
106 // Setup an env variable so that temp file (or other) can
107 // have a useful value to identify the source
108 class EyecatcherListener
109 : public CppUnit::TestListener
110 , private boost::noncopyable
112 public:
113 void startTest( CppUnit::Test* test) SAL_OVERRIDE
115 boost::scoped_array<char> tn(new char [ test->getName().length() + 2 ]);
116 strcpy(tn.get(), test->getName().c_str());
117 int len = strlen(tn.get());
118 for(int i = 0; i < len; i++)
120 if(!isalnum(tn[i]))
122 tn[i] = '_';
125 tn[len] = '_';
126 tn[len + 1] = 0;
127 setenv("LO_TESTNAME", tn.get(), true);
130 void endTest( CppUnit::Test* /* test */ ) SAL_OVERRIDE
134 #endif
136 class LogFailuresAsTheyHappen : public CppUnit::TestListener
138 public:
139 virtual void addFailure( const CppUnit::TestFailure &failure ) SAL_OVERRIDE
141 printFailureLocation( failure.sourceLine() );
142 printFailedTestName( failure );
143 printFailureMessage( failure );
146 private:
147 static void printFailureLocation( const CppUnit::SourceLine &sourceLine )
149 if ( !sourceLine.isValid() )
150 std::cerr << "unknown:0:";
151 else
152 std::cerr << sourceLine.fileName() << ":" << sourceLine.lineNumber() << ":";
155 static void printFailedTestName( const CppUnit::TestFailure &failure )
157 std::cerr << failure.failedTestName() << std::endl;
160 static void printFailureMessage( const CppUnit::TestFailure &failure )
162 std::cerr << failure.thrownException()->message().shortDescription() << std::endl;
163 std::cerr << failure.thrownException()->message().details() << std::endl;
167 namespace {
169 void addRecursiveTests(const std::vector<std::string>& test_names, CppUnit::Test* pTest, CppUnit::TestRunner& rRunner)
171 for (int i = 0; i < pTest->getChildTestCount(); ++i)
173 CppUnit::Test* pNewTest = pTest->getChildTestAt(i);
174 addRecursiveTests(test_names, pNewTest, rRunner);
175 if (std::find(test_names.begin(), test_names.end(), pNewTest->getName()) != test_names.end())
176 rRunner.addTest(pNewTest);
182 //Allow the whole uniting testing framework to be run inside a "Protector"
183 //which knows about uno exceptions, so it can print the content of the
184 //exception before falling over and dying
185 class CPPUNIT_API ProtectedFixtureFunctor
186 : public CppUnit::Functor
187 , private boost::noncopyable
189 private:
190 const std::string &testlib;
191 const std::string &args;
192 std::vector<CppUnit::Protector *> &protectors;
193 CppUnit::TestResult &result;
194 public:
195 ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
196 : testlib(testlib_)
197 , args(args_)
198 , protectors(protectors_)
199 , result(result_)
202 bool run() const
204 #ifdef DISABLE_DYNLOADING
206 // NOTE: Running cppunit unit tests on iOS was something I did
207 // only very early (several years ago) when starting porting
208 // this stuff to iOS. The complicated mechanisms to do build
209 // such unit test single executables have surely largely
210 // bit-rotted or been semi-intentionally broken since. This
211 // stuff here left for information only. --tml 2014.
213 // For iOS cppunit plugins aren't really "plugins" (shared
214 // libraries), but just static archives. In the real main
215 // program of a cppunit app, which calls the lo_main() that
216 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
217 // call the initialize methods of the CppUnitTestPlugIns that
218 // we statically link to the app executable.
219 #else
220 CppUnit::PlugInManager manager;
221 try {
222 manager.load(testlib, args);
223 } catch (const CppUnit::DynamicLibraryManagerException &e) {
224 std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
225 #ifdef WIN32
226 const char *pPath = getenv ("PATH");
227 if (pPath && strlen (pPath) > 256)
229 std::cerr << "Windows has significant build problems with long PATH variables ";
230 std::cerr << "please check your PATH variable and re-autogen.\n";
232 #endif
233 std::cerr << "Path is '" << getenv("PATH") << "'\n";
234 return false;
236 #endif
238 for (size_t i = 0; i < protectors.size(); ++i)
239 result.pushProtector(protectors[i]);
241 bool success;
243 CppUnit::TestResultCollector collector;
244 result.addListener(&collector);
246 LogFailuresAsTheyHappen logger;
247 result.addListener(&logger);
249 #ifdef TIMETESTS
250 TimingListener timer;
251 result.addListener(&timer);
252 #endif
254 #ifdef UNX
255 EyecatcherListener eye;
256 result.addListener(&eye);
257 // set this to track down files created before first test method
258 std::string lib(testlib.substr(testlib.rfind('/')+1));
259 setenv("LO_TESTNAME", lib.c_str(), true);
260 #endif
262 const char* pVal = getenv("CPPUNIT_TEST_NAME");
264 CppUnit::TestRunner runner;
265 if (pVal)
267 std::vector<std::string> test_names;
268 boost::split(test_names, pVal, boost::is_any_of("\t "));
269 CppUnit::Test* pTest = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
270 addRecursiveTests(test_names, pTest, runner);
272 else
274 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
276 runner.run(result);
278 CppUnit::CompilerOutputter outputter(&collector, CppUnit::stdCErr());
279 outputter.setNoWrap();
280 outputter.write();
281 success = collector.wasSuccessful();
284 for (size_t i = 0; i < protectors.size(); ++i)
285 result.popProtector();
287 return success;
289 virtual bool operator()() const SAL_OVERRIDE
291 return run();
297 SAL_IMPLEMENT_MAIN()
299 bool ok = false;
302 #ifdef WNT
303 //Disable Dr-Watson in order to crash simply without popup dialogs under
304 //windows
305 DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
306 SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
307 #ifdef _DEBUG // These functions are present only in the debgging runtime
308 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
309 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
310 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
311 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
312 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
313 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
314 #endif
315 #endif
317 std::vector<CppUnit::Protector *> protectors;
318 CppUnit::TestResult result;
319 std::string args;
320 std::string testlib;
321 sal_uInt32 index = 0;
322 while (index < rtl_getAppCommandArgCount())
324 rtl::OUString arg = getArgument(index);
325 if ( arg != "--protector" )
327 if (testlib.empty())
329 testlib = rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
330 args += testlib;
332 else
334 args += ' ';
335 args += rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
337 ++index;
338 continue;
340 if (rtl_getAppCommandArgCount() - index < 3) {
341 usageFailure();
343 rtl::OUString lib(getArgument(index + 1));
344 rtl::OUString sym(getArgument(index + 2));
345 #ifndef DISABLE_DYNLOADING
346 osl::Module mod(lib, SAL_LOADMODULE_GLOBAL);
347 oslGenericFunction fn = mod.getFunctionSymbol(sym);
348 mod.release();
349 #else
350 oslGenericFunction fn = 0;
351 if (sym == "unoexceptionprotector")
352 fn = (oslGenericFunction) unoexceptionprotector;
353 else if (sym == "unobootstrapprotector")
354 fn = (oslGenericFunction) unobootstrapprotector;
355 else if (sym == "vclbootstrapprotector")
356 fn = (oslGenericFunction) vclbootstrapprotector;
357 else
359 std::cerr
360 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
361 << std::endl;
362 std::exit(EXIT_FAILURE);
364 #endif
365 CppUnit::Protector *protector = fn == 0
367 : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
368 if (protector == 0) {
369 std::cerr
370 << "Failure instantiating protector \"" << convertLazy(lib)
371 << "\", \"" << convertLazy(sym) << '"' << std::endl;
372 std::exit(EXIT_FAILURE);
374 protectors.push_back(protector);
375 index+=3;
378 ProtectedFixtureFunctor tests(testlib, args, protectors, result);
379 ok = tests.run();
381 catch (const std::exception& e)
383 SAL_WARN("vcl.app", "Fatal exception: " << e.what());
386 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */