xref: /AOO42X/main/test/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2000, 2011 Oracle and/or its affiliates.
5 *
6 * OpenOffice.org - a multi-platform office productivity suite
7 *
8 * This file is part of OpenOffice.org.
9 *
10 * OpenOffice.org is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License version 3
12 * only, as published by the Free Software Foundation.
13 *
14 * OpenOffice.org is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Lesser General Public License version 3 for more details
18 * (a copy is included in the LICENSE file that accompanied this code).
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with OpenOffice.org.  If not, see
22 * <http://www.openoffice.org/license.html>
23 * for a copy of the LGPLv3 License.
24 ************************************************************************/
25 
26 #include "precompiled_test.hxx"
27 #include "sal/config.h"
28 
29 #include <limits>
30 #include <string>
31 
32 #include "boost/noncopyable.hpp"
33 #include "com/sun/star/uno/Any.hxx"
34 #include "com/sun/star/uno/Exception.hpp"
35 #include "cppuhelper/exc_hlp.hxx"
36 #include "osl/thread.h"
37 #include "rtl/string.hxx"
38 #include "rtl/ustring.h"
39 #include "rtl/ustring.hxx"
40 #include "sal/types.h"
41 
42 #include "preextstl.h"
43 #include "cppunit/Message.h"
44 #include "cppunit/Protector.h"
45 #include "postextstl.h"
46 
47 namespace {
48 
49 namespace css = com::sun::star;
50 
51 // Best effort conversion:
52 std::string convert(rtl::OUString const & s16) {
53     rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
54     return std::string(
55         s8.getStr(),
56         ((static_cast< sal_uInt32 >(s8.getLength())
57           > std::numeric_limits< std::string::size_type >::max())
58          ? std::numeric_limits< std::string::size_type >::max()
59          : static_cast< std::string::size_type >(s8.getLength())));
60 }
61 
62 class Prot: public CppUnit::Protector, private boost::noncopyable {
63 public:
64     Prot() {}
65 
66     virtual ~Prot() {}
67 
68     virtual bool protect(
69         CppUnit::Functor const & functor,
70         CppUnit::ProtectorContext const & context);
71 };
72 
73 bool Prot::protect(
74     CppUnit::Functor const & functor, CppUnit::ProtectorContext const & context)
75 {
76     try {
77         return functor();
78     } catch (css::uno::Exception & e) {
79         css::uno::Any a(cppu::getCaughtException());
80         reportError(
81             context,
82             CppUnit::Message(
83                 convert(
84                     rtl::OUString(
85                         RTL_CONSTASCII_USTRINGPARAM(
86                             "uncaught exception of type "))
87                     + a.getValueTypeName()),
88                 convert(e.Message)));
89     }
90     return false;
91 }
92 
93 }
94 
95 extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * SAL_CALL
96 unoexceptionprotector() {
97     return new Prot;
98 }
99