xref: /AOO41X/main/sal/cppunittester/cppunittester.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir *
3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir *
5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir *
7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir *
9*cdf0e10cSrcweir * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir *
11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir *
15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir *
21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir *
26*cdf0e10cSrcweir ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "precompiled_sal.hxx"
29*cdf0e10cSrcweir #include "sal/config.h"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <cstdlib>
32*cdf0e10cSrcweir #include <iostream>
33*cdf0e10cSrcweir #include <limits>
34*cdf0e10cSrcweir #include <string>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "cppunittester/protectorfactory.hxx"
37*cdf0e10cSrcweir #include "osl/module.h"
38*cdf0e10cSrcweir #include "osl/module.hxx"
39*cdf0e10cSrcweir #include "osl/thread.h"
40*cdf0e10cSrcweir #include "rtl/process.h"
41*cdf0e10cSrcweir #include "rtl/string.h"
42*cdf0e10cSrcweir #include "rtl/string.hxx"
43*cdf0e10cSrcweir #include "rtl/textcvt.h"
44*cdf0e10cSrcweir #include "rtl/ustring.hxx"
45*cdf0e10cSrcweir #include "sal/main.h"
46*cdf0e10cSrcweir #include "sal/types.h"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include "preextstl.h"
49*cdf0e10cSrcweir #include "cppunit/CompilerOutputter.h"
50*cdf0e10cSrcweir #include "cppunit/TestResult.h"
51*cdf0e10cSrcweir #include "cppunit/TestResultCollector.h"
52*cdf0e10cSrcweir #include "cppunit/TestRunner.h"
53*cdf0e10cSrcweir #include "cppunit/extensions/TestFactoryRegistry.h"
54*cdf0e10cSrcweir #include "cppunit/plugin/PlugInManager.h"
55*cdf0e10cSrcweir #include "cppunit/portability/Stream.h"
56*cdf0e10cSrcweir #include "postextstl.h"
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace {
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir void usageFailure() {
61*cdf0e10cSrcweir     std::cerr
62*cdf0e10cSrcweir         << ("Usage: cppunittester (--protector <shared-library-path>"
63*cdf0e10cSrcweir             " <function-symbol>)* <shared-library-path>")
64*cdf0e10cSrcweir         << std::endl;
65*cdf0e10cSrcweir     std::exit(EXIT_FAILURE);
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir rtl::OUString getArgument(sal_Int32 index) {
69*cdf0e10cSrcweir     rtl::OUString arg;
70*cdf0e10cSrcweir     rtl_getAppCommandArg(index, &arg.pData);
71*cdf0e10cSrcweir     return arg;
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir std::string convertLazy(rtl::OUString const & s16) {
75*cdf0e10cSrcweir     rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
76*cdf0e10cSrcweir     return std::string(
77*cdf0e10cSrcweir         s8.getStr(),
78*cdf0e10cSrcweir         ((static_cast< sal_uInt32 >(s8.getLength())
79*cdf0e10cSrcweir           > std::numeric_limits< std::string::size_type >::max())
80*cdf0e10cSrcweir          ? std::numeric_limits< std::string::size_type >::max()
81*cdf0e10cSrcweir          : static_cast< std::string::size_type >(s8.getLength())));
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir std::string convertStrict(rtl::OUString const & s16) {
85*cdf0e10cSrcweir     rtl::OString s8;
86*cdf0e10cSrcweir     if (!s16.convertToString(
87*cdf0e10cSrcweir             &s8, osl_getThreadTextEncoding(),
88*cdf0e10cSrcweir             (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
89*cdf0e10cSrcweir              | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))
90*cdf0e10cSrcweir         || (static_cast< sal_uInt32 >(s8.getLength())
91*cdf0e10cSrcweir             > std::numeric_limits< std::string::size_type >::max()))
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir         std::cerr
94*cdf0e10cSrcweir             << "Failure converting argument from UTF-16 back to system encoding"
95*cdf0e10cSrcweir             << std::endl;
96*cdf0e10cSrcweir         std::exit(EXIT_FAILURE);
97*cdf0e10cSrcweir     }
98*cdf0e10cSrcweir     return std::string(
99*cdf0e10cSrcweir         s8.getStr(), static_cast< std::string::size_type >(s8.getLength()));
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir SAL_IMPLEMENT_MAIN() {
105*cdf0e10cSrcweir     CppUnit::TestResult result;
106*cdf0e10cSrcweir     sal_uInt32 index = 0;
107*cdf0e10cSrcweir     for (; index < rtl_getAppCommandArgCount(); index += 3) {
108*cdf0e10cSrcweir         if (!getArgument(index).equalsAsciiL(
109*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM("--protector")))
110*cdf0e10cSrcweir         {
111*cdf0e10cSrcweir             break;
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir         if (rtl_getAppCommandArgCount() - index < 3) {
114*cdf0e10cSrcweir             usageFailure();
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir         rtl::OUString lib(getArgument(index + 1));
117*cdf0e10cSrcweir         rtl::OUString sym(getArgument(index + 2));
118*cdf0e10cSrcweir         oslGenericFunction fn = (new osl::Module(lib, SAL_LOADMODULE_GLOBAL))
119*cdf0e10cSrcweir             ->getFunctionSymbol(sym);
120*cdf0e10cSrcweir         CppUnit::Protector * p = fn == 0
121*cdf0e10cSrcweir             ? 0
122*cdf0e10cSrcweir             : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
123*cdf0e10cSrcweir         if (p == 0) {
124*cdf0e10cSrcweir             std::cerr
125*cdf0e10cSrcweir                 << "Failure instantiating protector \"" << convertLazy(lib)
126*cdf0e10cSrcweir                 << "\", \"" << convertLazy(sym) << '"' << std::endl;
127*cdf0e10cSrcweir             std::exit(EXIT_FAILURE);
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir         result.pushProtector(p);
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir     if (rtl_getAppCommandArgCount() - index != 1) {
132*cdf0e10cSrcweir         usageFailure();
133*cdf0e10cSrcweir     }
134*cdf0e10cSrcweir     CppUnit::PlugInManager manager;
135*cdf0e10cSrcweir     manager.load(convertStrict(getArgument(index)));
136*cdf0e10cSrcweir     CppUnit::TestRunner runner;
137*cdf0e10cSrcweir     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
138*cdf0e10cSrcweir     CppUnit::TestResultCollector collector;
139*cdf0e10cSrcweir     result.addListener(&collector);
140*cdf0e10cSrcweir     runner.run(result);
141*cdf0e10cSrcweir     CppUnit::CompilerOutputter(&collector, CppUnit::stdCErr()).write();
142*cdf0e10cSrcweir     return collector.wasSuccessful() ? EXIT_SUCCESS : EXIT_FAILURE;
143*cdf0e10cSrcweir }
144