Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / sal / cppunittester / cppunittester.cxx
blobe8584c1edcabae09982117a8f48f0326250e801c
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 <limits>
27 #include <string>
28 #include <sal/types.h>
29 #include "cppunittester/protectorfactory.hxx"
30 #include "osl/module.h"
31 #include "osl/module.hxx"
32 #include "osl/thread.h"
33 #include "rtl/process.h"
34 #include "rtl/string.h"
35 #include "rtl/string.hxx"
36 #include "rtl/textcvt.h"
37 #include "rtl/ustring.hxx"
38 #include "sal/main.h"
39 #include "sal/types.h"
41 #include "cppunit/CompilerOutputter.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/ptr_container/ptr_vector.hpp"
53 namespace {
55 void usageFailure() {
56 std::cerr
57 << ("Usage: cppunittester (--protector <shared-library-path>"
58 " <function-symbol>)* <shared-library-path>")
59 << std::endl;
60 std::exit(EXIT_FAILURE);
63 rtl::OUString getArgument(sal_Int32 index) {
64 rtl::OUString arg;
65 rtl_getAppCommandArg(index, &arg.pData);
66 return arg;
69 std::string convertLazy(rtl::OUString const & s16) {
70 rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
71 return std::string(
72 s8.getStr(),
73 ((static_cast< sal_uInt32 >(s8.getLength())
74 > (std::numeric_limits< std::string::size_type >::max)())
75 ? (std::numeric_limits< std::string::size_type >::max)()
76 : static_cast< std::string::size_type >(s8.getLength())));
79 //Output how long each test took
80 class TimingListener
81 : public CppUnit::TestListener
82 , private boost::noncopyable
84 public:
85 void startTest( CppUnit::Test *)
87 m_nStartTime = osl_getGlobalTimer();
90 void endTest( CppUnit::Test *test )
92 sal_uInt32 nEndTime = osl_getGlobalTimer();
93 std::cout << test->getName() << ": " << nEndTime-m_nStartTime
94 << "ms" << std::endl;
97 private:
98 sal_uInt32 m_nStartTime;
101 //Allow the whole uniting testing framework to be run inside a "Protector"
102 //which knows about uno exceptions, so it can print the content of the
103 //exception before falling over and dying
104 class CPPUNIT_API ProtectedFixtureFunctor
105 : public CppUnit::Functor
106 , private boost::noncopyable
108 private:
109 const std::string &testlib;
110 const std::string &args;
111 std::vector<CppUnit::Protector *> &protectors;
112 CppUnit::TestResult &result;
113 public:
114 ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
115 : testlib(testlib_)
116 , args(args_)
117 , protectors(protectors_)
118 , result(result_)
121 bool run() const
123 #ifdef DISABLE_DYNLOADING
124 // For iOS cppunit plugins aren't really "plugins" (shared
125 // libraries), but just static archives. In the real main
126 // program of a cppunit app, which calls the lo_main() that
127 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
128 // call the initialize methods of the CppUnitTestPlugIns that
129 // we statically link to the app executable.
130 #else
131 CppUnit::PlugInManager manager;
132 try {
133 manager.load(testlib, args);
134 } catch (const CppUnit::DynamicLibraryManagerException &e) {
135 std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
136 return false;
138 #endif
139 CppUnit::TestRunner runner;
140 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
142 CppUnit::TestResultCollector collector;
143 result.addListener(&collector);
145 #ifdef TIMETESTS
146 TimingListener timer;
147 result.addListener(&timer);
148 #endif
150 for (size_t i = 0; i < protectors.size(); ++i)
151 result.pushProtector(protectors[i]);
153 runner.run(result);
155 for (size_t i = 0; i < protectors.size(); ++i)
156 result.popProtector();
158 CppUnit::CompilerOutputter(&collector, CppUnit::stdCErr()).write();
159 return collector.wasSuccessful();
161 virtual bool operator()() const
163 return run();
169 SAL_IMPLEMENT_MAIN() {
170 #ifdef WNT
171 //Disable Dr-Watson in order to crash simply without popup dialogs under
172 //windows
173 DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
174 SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
175 #ifdef _DEBUG // These functions are present only in the debgging runtime
176 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
177 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
178 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
179 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
180 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
181 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
182 #endif
183 #endif
185 boost::ptr_vector<osl::Module> modules;
186 std::vector<CppUnit::Protector *> protectors;
187 CppUnit::TestResult result;
188 std::string args;
189 std::string testlib;
190 sal_uInt32 index = 0;
191 while (index < rtl_getAppCommandArgCount())
193 rtl::OUString arg = getArgument(index);
194 if ( arg != "--protector" )
196 if (testlib.empty())
198 testlib = rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
199 args += testlib;
201 else
203 args += ' ';
204 args += rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
206 ++index;
207 continue;
209 if (rtl_getAppCommandArgCount() - index < 3) {
210 usageFailure();
212 rtl::OUString lib(getArgument(index + 1));
213 rtl::OUString sym(getArgument(index + 2));
214 #ifndef DISABLE_DYNLOADING
215 modules.push_back(new osl::Module(lib, SAL_LOADMODULE_GLOBAL));
216 oslGenericFunction fn = modules.back().getFunctionSymbol(sym);
217 #else
218 oslGenericFunction fn = 0;
219 if (sym == "unoexceptionprotector")
220 fn = (oslGenericFunction) unoexceptionprotector;
221 else if (sym == "unobootstrapprotector")
222 fn = (oslGenericFunction) unobootstrapprotector;
223 else
225 std::cerr
226 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
227 << std::endl;
228 std::exit(EXIT_FAILURE);
230 #endif
231 CppUnit::Protector *protector = fn == 0
233 : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
234 if (protector == 0) {
235 std::cerr
236 << "Failure instantiating protector \"" << convertLazy(lib)
237 << "\", \"" << convertLazy(sym) << '"' << std::endl;
238 std::exit(EXIT_FAILURE);
240 protectors.push_back(protector);
241 index+=3;
244 ProtectedFixtureFunctor tests(testlib, args, protectors, result);
245 bool ok = tests.run();
247 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */