LCOV - code coverage report
Current view: top level - sal/cppunittester - cppunittester.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 70 86 81.4 %
Date: 2014-04-11 Functions: 11 16 68.8 %
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             : 
      39             : #include "cppunit/CompilerOutputter.h"
      40             : #include "cppunit/TestResult.h"
      41             : #include "cppunit/TestResultCollector.h"
      42             : #include "cppunit/TestRunner.h"
      43             : #include "cppunit/extensions/TestFactoryRegistry.h"
      44             : #include "cppunit/plugin/PlugInManager.h"
      45             : #include "cppunit/plugin/DynamicLibraryManagerException.h"
      46             : #include "cppunit/portability/Stream.h"
      47             : 
      48             : #include "boost/noncopyable.hpp"
      49             : #include "boost/ptr_container/ptr_vector.hpp"
      50             : #include "boost/static_assert.hpp"
      51             : 
      52             : namespace {
      53             : 
      54           0 : void usageFailure() {
      55             :     std::cerr
      56             :         << ("Usage: cppunittester (--protector <shared-library-path>"
      57           0 :             " <function-symbol>)* <shared-library-path>")
      58           0 :         << std::endl;
      59           0 :     std::exit(EXIT_FAILURE);
      60             : }
      61             : 
      62         787 : rtl::OUString getArgument(sal_Int32 index) {
      63         787 :     rtl::OUString arg;
      64         787 :     rtl_getAppCommandArg(index, &arg.pData);
      65         787 :     return arg;
      66             : }
      67             : 
      68           0 : std::string convertLazy(rtl::OUString const & s16) {
      69           0 :     rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
      70             :     BOOST_STATIC_ASSERT(sizeof (sal_Int32) <= sizeof (std::string::size_type));
      71             :         // ensure following cast is legitimate
      72             :     return std::string(
      73           0 :         s8.getStr(), static_cast< std::string::size_type >(s8.getLength()));
      74             : }
      75             : 
      76             : #if defined TIMETESTS
      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 *) SAL_OVERRIDE
      84             :     {
      85             :         m_nStartTime = osl_getGlobalTimer();
      86             :     }
      87             : 
      88             :     void endTest( CppUnit::Test *test ) SAL_OVERRIDE
      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             : #endif
      99             : 
     100             : #ifdef UNX
     101             : #include <stdlib.h>
     102             : // Setup an env variable so that temp file (or other) can
     103             : // have a usefull value to identify the source
     104         142 : class EyecatcherListener
     105             :     : public CppUnit::TestListener
     106             :     , private boost::noncopyable
     107             : {
     108             : public:
     109        3358 :     void startTest( CppUnit::Test* test) SAL_OVERRIDE
     110             :     {
     111        3358 :         char* tn = new char [ test->getName().length() + 2 ];
     112        3358 :         strcpy(tn, test->getName().c_str());
     113        3358 :         int len = strlen(tn);
     114      145567 :         for(int i = 0; i < len; i++)
     115             :         {
     116      142209 :             if(!isalnum(tn[i]))
     117             :             {
     118       14325 :                 tn[i] = '_';
     119             :             }
     120             :         }
     121        3358 :         tn[len] = '_';
     122        3358 :         tn[len + 1] = 0;
     123        3358 :         setenv("LO_TESTNAME", tn, true);
     124        3358 :         delete[] tn;
     125        3358 :     }
     126             : 
     127        3358 :     void endTest( CppUnit::Test* /* test */ ) SAL_OVERRIDE
     128             :     {
     129        3358 :     }
     130             : };
     131             : #endif
     132             : 
     133             : //Allow the whole uniting testing framework to be run inside a "Protector"
     134             : //which knows about uno exceptions, so it can print the content of the
     135             : //exception before falling over and dying
     136         142 : class CPPUNIT_API ProtectedFixtureFunctor
     137             :     : public CppUnit::Functor
     138             :     , private boost::noncopyable
     139             : {
     140             : private:
     141             :     const std::string &testlib;
     142             :     const std::string &args;
     143             :     std::vector<CppUnit::Protector *> &protectors;
     144             :     CppUnit::TestResult &result;
     145             : public:
     146         142 :     ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
     147             :         : testlib(testlib_)
     148             :         , args(args_)
     149             :         , protectors(protectors_)
     150         142 :         , result(result_)
     151             :     {
     152         142 :     }
     153         142 :     bool run() const
     154             :     {
     155             : #ifdef DISABLE_DYNLOADING
     156             :         // For iOS cppunit plugins aren't really "plugins" (shared
     157             :         // libraries), but just static archives. In the real main
     158             :         // program of a cppunit app, which calls the lo_main() that
     159             :         // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
     160             :         // call the initialize methods of the CppUnitTestPlugIns that
     161             :         // we statically link to the app executable.
     162             : #else
     163         142 :         CppUnit::PlugInManager manager;
     164             :         try {
     165         142 :             manager.load(testlib, args);
     166           0 :         } catch (const CppUnit::DynamicLibraryManagerException &e) {
     167           0 :             std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
     168           0 :             return false;
     169             :         }
     170             : #endif
     171         284 :         CppUnit::TestRunner runner;
     172         142 :         runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
     173             : 
     174         284 :         CppUnit::TestResultCollector collector;
     175         142 :         result.addListener(&collector);
     176             : 
     177             : #ifdef TIMETESTS
     178             :         TimingListener timer;
     179             :         result.addListener(&timer);
     180             : #endif
     181             : 
     182             : #ifdef UNX
     183         284 :         EyecatcherListener eye;
     184         142 :         result.addListener(&eye);
     185             : #endif
     186         310 :         for (size_t i = 0; i < protectors.size(); ++i)
     187         168 :             result.pushProtector(protectors[i]);
     188             : 
     189         142 :         runner.run(result);
     190             : 
     191         310 :         for (size_t i = 0; i < protectors.size(); ++i)
     192         168 :             result.popProtector();
     193             : 
     194         142 :         CppUnit::CompilerOutputter(&collector, CppUnit::stdCErr()).write();
     195         284 :         return collector.wasSuccessful();
     196             :     }
     197           0 :     virtual bool operator()() const SAL_OVERRIDE
     198             :     {
     199           0 :         return run();
     200             :     }
     201             : };
     202             : 
     203             : }
     204             : 
     205         284 : SAL_IMPLEMENT_MAIN() {
     206             : #ifdef WNT
     207             :     //Disable Dr-Watson in order to crash simply without popup dialogs under
     208             :     //windows
     209             :     DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
     210             :     SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
     211             : #ifdef _DEBUG // These functions are present only in the debgging runtime
     212             :     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
     213             :     _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
     214             :     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
     215             :     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
     216             :     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
     217             :     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
     218             : #endif
     219             : #endif
     220             : 
     221         142 :     boost::ptr_vector<osl::Module> modules;
     222         284 :     std::vector<CppUnit::Protector *> protectors;
     223         284 :     CppUnit::TestResult result;
     224         284 :     std::string args;
     225         284 :     std::string testlib;
     226         142 :     sal_uInt32 index = 0;
     227         735 :     while (index < rtl_getAppCommandArgCount())
     228             :     {
     229         451 :         rtl::OUString arg = getArgument(index);
     230         451 :         if ( arg != "--protector" )
     231             :         {
     232         283 :             if (testlib.empty())
     233             :             {
     234         142 :                 testlib = rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
     235         142 :                 args += testlib;
     236             :             }
     237             :             else
     238             :             {
     239         141 :                 args += ' ';
     240         141 :                 args += rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
     241             :             }
     242         283 :             ++index;
     243         283 :             continue;
     244             :         }
     245         168 :         if (rtl_getAppCommandArgCount() - index < 3) {
     246           0 :             usageFailure();
     247             :         }
     248         336 :         rtl::OUString lib(getArgument(index + 1));
     249         336 :         rtl::OUString sym(getArgument(index + 2));
     250             : #ifndef DISABLE_DYNLOADING
     251         168 :         modules.push_back(new osl::Module(lib, SAL_LOADMODULE_GLOBAL));
     252         168 :         oslGenericFunction fn = modules.back().getFunctionSymbol(sym);
     253             : #else
     254             :         oslGenericFunction fn = 0;
     255             :         if (sym == "unoexceptionprotector")
     256             :             fn = (oslGenericFunction) unoexceptionprotector;
     257             :         else if (sym == "unobootstrapprotector")
     258             :             fn = (oslGenericFunction) unobootstrapprotector;
     259             :         else
     260             :         {
     261             :             std::cerr
     262             :                 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
     263             :                 << std::endl;
     264             :             std::exit(EXIT_FAILURE);
     265             :         }
     266             : #endif
     267             :         CppUnit::Protector *protector = fn == 0
     268             :             ? 0
     269         168 :             : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
     270         168 :         if (protector == 0) {
     271             :             std::cerr
     272           0 :                 << "Failure instantiating protector \"" << convertLazy(lib)
     273           0 :                 << "\", \"" << convertLazy(sym) << '"' << std::endl;
     274           0 :             std::exit(EXIT_FAILURE);
     275             :         }
     276         168 :         protectors.push_back(protector);
     277         168 :         index+=3;
     278         168 :     }
     279             : 
     280         284 :     ProtectedFixtureFunctor tests(testlib, args, protectors, result);
     281         142 :     bool ok = tests.run();
     282             : 
     283         284 :     return ok ? EXIT_SUCCESS : EXIT_FAILURE;
     284         426 : }
     285             : 
     286             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10