LibreOffice Module unotest (master) 1
unoexceptionprotector.cxx
Go to the documentation of this file.
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#include <cstdlib>
21#include <string>
22#include <string_view>
23
24#include <com/sun/star/uno/Any.hxx>
25#include <com/sun/star/uno/Exception.hpp>
27#include <cppunit/Message.h>
28#include <osl/thread.h>
29#include <rtl/string.hxx>
30#include <rtl/ustring.hxx>
31#include <sal/types.h>
32
33#include <cppunit/Protector.h>
34
35namespace {
36
37// Best effort conversion:
38std::string convert(std::u16string_view s16) {
39 OString s8(OUStringToOString(s16, osl_getThreadTextEncoding()));
40 static_assert(sizeof (sal_Int32) <= sizeof (std::string::size_type), "got to be at least equal");
41 // ensure following cast is legitimate
42 return std::string(s8);
43}
44
45class Prot : public CppUnit::Protector
46{
47public:
48 Prot() {}
49
50 Prot(const Prot&) = delete;
51 Prot& operator=(const Prot&) = delete;
52
53 virtual bool protect(
54 CppUnit::Functor const & functor,
55 CppUnit::ProtectorContext const & context) override;
56};
57
58bool Prot::protect(
59 CppUnit::Functor const & functor, CppUnit::ProtectorContext const & context)
60{
61 try {
62 return functor();
63 } catch (const css::uno::Exception &e) {
64 css::uno::Any a(cppu::getCaughtException());
65 reportError(
66 context,
67 CppUnit::Message(
68 "An uncaught exception of type " + convert(a.getValueTypeName()),
69 convert(e.Message)));
70 }
71 return false;
72}
73
74}
75
76extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector *
78 return std::getenv("CPPUNIT_PROPAGATE_EXCEPTIONS") == nullptr ? new Prot : nullptr;
79}
80
81/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno_Any a
Any SAL_CALL getCaughtException()
convert
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
SAL_DLLPUBLIC_EXPORT CppUnit::Protector * unoexceptionprotector()