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