1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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"
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>
60 << ("Usage: cppunittester (--protector <shared-library-path>"
61 " <function-symbol>)* <shared-library-path>")
63 std::exit(EXIT_FAILURE
);
66 rtl::OUString
getArgument(sal_Int32 index
) {
68 rtl_getAppCommandArg(index
, &arg
.pData
);
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
77 s8
.getStr(), static_cast< std::string::size_type
>(s8
.getLength()));
81 //Output how long each test took
83 : public CppUnit::TestListener
84 , private boost::noncopyable
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
100 sal_uInt32 m_nStartTime
;
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
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
++)
127 setenv("LO_TESTNAME", tn
.get(), true);
130 void endTest( CppUnit::Test
* /* test */ ) SAL_OVERRIDE
136 class LogFailuresAsTheyHappen
: public CppUnit::TestListener
139 virtual void addFailure( const CppUnit::TestFailure
&failure
) SAL_OVERRIDE
141 printFailureLocation( failure
.sourceLine() );
142 printFailedTestName( failure
);
143 printFailureMessage( failure
);
147 static void printFailureLocation( const CppUnit::SourceLine
&sourceLine
)
149 if ( !sourceLine
.isValid() )
150 std::cerr
<< "unknown:0:";
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
;
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
190 const std::string
&testlib
;
191 const std::string
&args
;
192 std::vector
<CppUnit::Protector
*> &protectors
;
193 CppUnit::TestResult
&result
;
195 ProtectedFixtureFunctor(const std::string
& testlib_
, const std::string
&args_
, std::vector
<CppUnit::Protector
*> &protectors_
, CppUnit::TestResult
&result_
)
198 , protectors(protectors_
)
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.
220 CppUnit::PlugInManager manager
;
222 manager
.load(testlib
, args
);
223 } catch (const CppUnit::DynamicLibraryManagerException
&e
) {
224 std::cerr
<< "DynamicLibraryManagerException: \"" << e
.what() << "\"\n";
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";
233 std::cerr
<< "Path is '" << getenv("PATH") << "'\n";
238 for (size_t i
= 0; i
< protectors
.size(); ++i
)
239 result
.pushProtector(protectors
[i
]);
243 CppUnit::TestResultCollector collector
;
244 result
.addListener(&collector
);
246 LogFailuresAsTheyHappen logger
;
247 result
.addListener(&logger
);
250 TimingListener timer
;
251 result
.addListener(&timer
);
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);
262 const char* pVal
= getenv("CPPUNIT_TEST_NAME");
264 CppUnit::TestRunner runner
;
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
);
274 runner
.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
278 CppUnit::CompilerOutputter
outputter(&collector
, CppUnit::stdCErr());
279 outputter
.setNoWrap();
281 success
= collector
.wasSuccessful();
284 for (size_t i
= 0; i
< protectors
.size(); ++i
)
285 result
.popProtector();
289 virtual bool operator()() const SAL_OVERRIDE
303 //Disable Dr-Watson in order to crash simply without popup dialogs under
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
);
317 std::vector
<CppUnit::Protector
*> protectors
;
318 CppUnit::TestResult result
;
321 sal_uInt32 index
= 0;
322 while (index
< rtl_getAppCommandArgCount())
324 rtl::OUString arg
= getArgument(index
);
325 if ( arg
!= "--protector" )
329 testlib
= rtl::OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
335 args
+= rtl::OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
340 if (rtl_getAppCommandArgCount() - index
< 3) {
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
);
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
;
360 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
362 std::exit(EXIT_FAILURE
);
365 CppUnit::Protector
*protector
= fn
== 0
367 : (*reinterpret_cast< cppunittester::ProtectorFactory
* >(fn
))();
368 if (protector
== 0) {
370 << "Failure instantiating protector \"" << convertLazy(lib
)
371 << "\", \"" << convertLazy(sym
) << '"' << std::endl
;
372 std::exit(EXIT_FAILURE
);
374 protectors
.push_back(protector
);
378 ProtectedFixtureFunctor
tests(testlib
, args
, protectors
, result
);
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: */