LCOV - code coverage report
Current view: top level - libreoffice/sal/cppunittester - cppunittester.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 53 69 76.8 %
Date: 2012-12-27 Functions: 8 12 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #ifdef WNT
      21             : #include <windows.h>
      22             : #endif
      23             : 
      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"
      38             : #include "sal/types.h"
      39             : 
      40             : #include "cppunit/CompilerOutputter.h"
      41             : #include "cppunit/TestResult.h"
      42             : #include "cppunit/TestResultCollector.h"
      43             : #include "cppunit/TestRunner.h"
      44             : #include "cppunit/extensions/TestFactoryRegistry.h"
      45             : #include "cppunit/plugin/PlugInManager.h"
      46             : #include "cppunit/plugin/DynamicLibraryManagerException.h"
      47             : #include "cppunit/portability/Stream.h"
      48             : 
      49             : #include "boost/noncopyable.hpp"
      50             : #include "boost/ptr_container/ptr_vector.hpp"
      51             : #include "boost/static_assert.hpp"
      52             : 
      53             : namespace {
      54             : 
      55           0 : void usageFailure() {
      56             :     std::cerr
      57             :         << ("Usage: cppunittester (--protector <shared-library-path>"
      58           0 :             " <function-symbol>)* <shared-library-path>")
      59           0 :         << std::endl;
      60           0 :     std::exit(EXIT_FAILURE);
      61             : }
      62             : 
      63         394 : rtl::OUString getArgument(sal_Int32 index) {
      64         394 :     rtl::OUString arg;
      65         394 :     rtl_getAppCommandArg(index, &arg.pData);
      66         394 :     return arg;
      67             : }
      68             : 
      69           0 : std::string convertLazy(rtl::OUString const & s16) {
      70           0 :     rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
      71             :     BOOST_STATIC_ASSERT(sizeof (sal_Int32) <= sizeof (std::string::size_type));
      72             :         // ensure following cast is legitimate
      73             :     return std::string(
      74           0 :         s8.getStr(), static_cast< std::string::size_type >(s8.getLength()));
      75             : }
      76             : 
      77             : //Output how long each test took
      78             : class TimingListener
      79             :     : public CppUnit::TestListener
      80             :     , private boost::noncopyable
      81             : {
      82             : public:
      83             :     void startTest( CppUnit::Test *)
      84             :     {
      85             :         m_nStartTime = osl_getGlobalTimer();
      86             :     }
      87             : 
      88             :     void endTest( CppUnit::Test *test )
      89             :     {
      90             :         sal_uInt32 nEndTime = osl_getGlobalTimer();
      91             :         std::cout << test->getName() << ": " << nEndTime-m_nStartTime
      92             :             << "ms" << std::endl;
      93             :     }
      94             : 
      95             : private:
      96             :     sal_uInt32 m_nStartTime;
      97             : };
      98             : 
      99             : //Allow the whole uniting testing framework to be run inside a "Protector"
     100             : //which knows about uno exceptions, so it can print the content of the
     101             : //exception before falling over and dying
     102          89 : class CPPUNIT_API ProtectedFixtureFunctor
     103             :     : public CppUnit::Functor
     104             :     , private boost::noncopyable
     105             : {
     106             : private:
     107             :     const std::string &testlib;
     108             :     const std::string &args;
     109             :     std::vector<CppUnit::Protector *> &protectors;
     110             :     CppUnit::TestResult &result;
     111             : public:
     112          89 :     ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
     113             :         : testlib(testlib_)
     114             :         , args(args_)
     115             :         , protectors(protectors_)
     116          89 :         , result(result_)
     117             :     {
     118          89 :     }
     119          89 :     bool run() const
     120             :     {
     121             : #ifdef DISABLE_DYNLOADING
     122             :         // For iOS cppunit plugins aren't really "plugins" (shared
     123             :         // libraries), but just static archives. In the real main
     124             :         // program of a cppunit app, which calls the lo_main() that
     125             :         // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
     126             :         // call the initialize methods of the CppUnitTestPlugIns that
     127             :         // we statically link to the app executable.
     128             : #else
     129          89 :         CppUnit::PlugInManager manager;
     130             :         try {
     131          89 :             manager.load(testlib, args);
     132           0 :         } catch (const CppUnit::DynamicLibraryManagerException &e) {
     133           0 :             std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
     134           0 :             return false;
     135             :         }
     136             : #endif
     137          89 :         CppUnit::TestRunner runner;
     138          89 :         runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
     139             : 
     140          89 :         CppUnit::TestResultCollector collector;
     141          89 :         result.addListener(&collector);
     142             : 
     143             : #ifdef TIMETESTS
     144             :         TimingListener timer;
     145             :         result.addListener(&timer);
     146             : #endif
     147             : 
     148         161 :         for (size_t i = 0; i < protectors.size(); ++i)
     149          72 :             result.pushProtector(protectors[i]);
     150             : 
     151          89 :         runner.run(result);
     152             : 
     153         161 :         for (size_t i = 0; i < protectors.size(); ++i)
     154          72 :             result.popProtector();
     155             : 
     156          89 :         CppUnit::CompilerOutputter(&collector, CppUnit::stdCErr()).write();
     157          89 :         return collector.wasSuccessful();
     158             :     }
     159           0 :     virtual bool operator()() const
     160             :     {
     161           0 :         return run();
     162             :     }
     163             : };
     164             : 
     165             : }
     166             : 
     167         178 : SAL_IMPLEMENT_MAIN() {
     168             : #ifdef WNT
     169             :     //Disable Dr-Watson in order to crash simply without popup dialogs under
     170             :     //windows
     171             :     DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
     172             :     SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
     173             : #ifdef _DEBUG // These functions are present only in the debgging runtime
     174             :     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
     175             :     _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
     176             :     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
     177             :     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
     178             :     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
     179             :     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
     180             : #endif
     181             : #endif
     182             : 
     183          89 :     boost::ptr_vector<osl::Module> modules;
     184          89 :     std::vector<CppUnit::Protector *> protectors;
     185          89 :     CppUnit::TestResult result;
     186          89 :     std::string args;
     187          89 :     std::string testlib;
     188          89 :     sal_uInt32 index = 0;
     189         428 :     while (index < rtl_getAppCommandArgCount())
     190             :     {
     191         250 :         rtl::OUString arg = getArgument(index);
     192         250 :         if ( arg != "--protector" )
     193             :         {
     194         178 :             if (testlib.empty())
     195             :             {
     196          89 :                 testlib = rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
     197          89 :                 args += testlib;
     198             :             }
     199             :             else
     200             :             {
     201          89 :                 args += ' ';
     202          89 :                 args += rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
     203             :             }
     204         178 :             ++index;
     205         178 :             continue;
     206             :         }
     207          72 :         if (rtl_getAppCommandArgCount() - index < 3) {
     208           0 :             usageFailure();
     209             :         }
     210          72 :         rtl::OUString lib(getArgument(index + 1));
     211          72 :         rtl::OUString sym(getArgument(index + 2));
     212             : #ifndef DISABLE_DYNLOADING
     213          72 :         modules.push_back(new osl::Module(lib, SAL_LOADMODULE_GLOBAL));
     214          72 :         oslGenericFunction fn = modules.back().getFunctionSymbol(sym);
     215             : #else
     216             :         oslGenericFunction fn = 0;
     217             :         if (sym == "unoexceptionprotector")
     218             :             fn = (oslGenericFunction) unoexceptionprotector;
     219             :         else if (sym == "unobootstrapprotector")
     220             :             fn = (oslGenericFunction) unobootstrapprotector;
     221             :         else
     222             :         {
     223             :             std::cerr
     224             :                 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
     225             :                 << std::endl;
     226             :             std::exit(EXIT_FAILURE);
     227             :         }
     228             : #endif
     229             :         CppUnit::Protector *protector = fn == 0
     230             :             ? 0
     231          72 :             : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
     232          72 :         if (protector == 0) {
     233             :             std::cerr
     234           0 :                 << "Failure instantiating protector \"" << convertLazy(lib)
     235           0 :                 << "\", \"" << convertLazy(sym) << '"' << std::endl;
     236           0 :             std::exit(EXIT_FAILURE);
     237             :         }
     238          72 :         protectors.push_back(protector);
     239          72 :         index+=3;
     240          72 :     }
     241             : 
     242          89 :     ProtectedFixtureFunctor tests(testlib, args, protectors, result);
     243          89 :     bool ok = tests.run();
     244             : 
     245          89 :     return ok ? EXIT_SUCCESS : EXIT_FAILURE;
     246         267 : }
     247             : 
     248             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10