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