1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_test.hxx"
25 #include "sal/config.h"
26 
27 #include <limits>
28 #include <string>
29 
30 #include "boost/noncopyable.hpp"
31 #include "com/sun/star/uno/Any.hxx"
32 #include "com/sun/star/uno/Exception.hpp"
33 #include "cppuhelper/exc_hlp.hxx"
34 #include "osl/thread.h"
35 #include "rtl/string.hxx"
36 #include "rtl/ustring.h"
37 #include "rtl/ustring.hxx"
38 #include "sal/types.h"
39 
40 #include "preextstl.h"
41 #include "cppunit/Message.h"
42 #include "cppunit/Protector.h"
43 #include "postextstl.h"
44 
45 namespace {
46 
47 namespace css = com::sun::star;
48 
49 // Best effort conversion:
convert(rtl::OUString const & s16)50 std::string convert(rtl::OUString const & s16) {
51     rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
52     return std::string(
53         s8.getStr(),
54         ((static_cast< sal_uInt32 >(s8.getLength())
55           > std::numeric_limits< std::string::size_type >::max())
56          ? std::numeric_limits< std::string::size_type >::max()
57          : static_cast< std::string::size_type >(s8.getLength())));
58 }
59 
60 class Prot: public CppUnit::Protector, private boost::noncopyable {
61 public:
Prot()62     Prot() {}
63 
~Prot()64     virtual ~Prot() {}
65 
66     virtual bool protect(
67         CppUnit::Functor const & functor,
68         CppUnit::ProtectorContext const & context);
69 };
70 
protect(CppUnit::Functor const & functor,CppUnit::ProtectorContext const & context)71 bool Prot::protect(
72     CppUnit::Functor const & functor, CppUnit::ProtectorContext const & context)
73 {
74     try {
75         return functor();
76     } catch (css::uno::Exception & e) {
77         css::uno::Any a(cppu::getCaughtException());
78         reportError(
79             context,
80             CppUnit::Message(
81                 convert(
82                     rtl::OUString(
83                         RTL_CONSTASCII_USTRINGPARAM(
84                             "uncaught exception of type "))
85                     + a.getValueTypeName()),
86                 convert(e.Message)));
87     }
88     return false;
89 }
90 
91 }
92 
93 extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * SAL_CALL
unoexceptionprotector()94 unoexceptionprotector() {
95     return new Prot;
96 }
97