Line data Source code
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 <sal/types.h>
21 :
22 : #include <cppunit/TestSuite.h>
23 : #include <cppunit/TestFixture.h>
24 : #include <cppunit/TestCase.h>
25 : #include <cppunit/plugin/TestPlugIn.h>
26 : #include <cppunit/extensions/HelperMacros.h>
27 :
28 : #include "Interface1.hpp"
29 :
30 : #include "rtl/ustring.hxx"
31 : #include "sal/types.h"
32 :
33 : namespace
34 : {
35 :
36 : using ::com::sun::star::uno::Type;
37 : using ::com::sun::star::uno::Any;
38 : using ::com::sun::star::uno::Reference;
39 : using ::com::sun::star::uno::RuntimeException;
40 : using ::com::sun::star::uno::UNO_SET_THROW;
41 :
42 : class Foo: public Interface1
43 : {
44 : public:
45 1 : Foo()
46 1 : :m_refCount(0)
47 : {
48 1 : }
49 :
50 0 : virtual Any SAL_CALL queryInterface(const Type & _type)
51 : throw (RuntimeException)
52 : {
53 0 : Any aInterface;
54 0 : if (_type == getCppuType< Reference< XInterface > >())
55 : {
56 0 : Reference< XInterface > ref( static_cast< XInterface * >( this ) );
57 0 : aInterface.setValue( &ref, _type );
58 : }
59 0 : else if (_type == getCppuType< Reference< Interface1 > >())
60 : {
61 0 : Reference< Interface1 > ref( this );
62 0 : aInterface.setValue( &ref, _type );
63 : }
64 :
65 0 : return Any();
66 : }
67 :
68 5 : virtual void SAL_CALL acquire() throw ()
69 : {
70 5 : osl_atomic_increment( &m_refCount );
71 5 : }
72 :
73 5 : virtual void SAL_CALL release() throw ()
74 : {
75 5 : if ( 0 == osl_atomic_decrement( &m_refCount ) )
76 1 : delete this;
77 5 : }
78 :
79 : protected:
80 2 : virtual ~Foo()
81 1 : {
82 2 : }
83 :
84 : private:
85 : Foo(Foo &); // not declared
86 : Foo& operator =(const Foo&); // not declared
87 :
88 : private:
89 : oslInterlockedCount m_refCount;
90 : };
91 :
92 3 : class Test: public ::CppUnit::TestFixture
93 : {
94 :
95 : public:
96 : void testUnoSetThrow();
97 :
98 2 : CPPUNIT_TEST_SUITE(Test);
99 1 : CPPUNIT_TEST(testUnoSetThrow);
100 2 : CPPUNIT_TEST_SUITE_END();
101 : };
102 :
103 1 : void Test::testUnoSetThrow()
104 : {
105 1 : Reference< Interface1 > xNull;
106 1 : Reference< Interface1 > xFoo( new Foo );
107 :
108 : // ctor taking Reference< interface_type >
109 1 : bool bCaughtException = false;
110 2 : try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
111 1 : CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
112 :
113 1 : bCaughtException = false;
114 1 : try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
115 1 : CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
116 :
117 : // ctor taking interface_type*
118 1 : bCaughtException = false;
119 2 : try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
120 1 : CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
121 :
122 1 : bCaughtException = false;
123 1 : try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
124 1 : CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
125 :
126 1 : Reference< Interface1 > x;
127 : // "set" taking Reference< interface_type >
128 1 : bCaughtException = false;
129 2 : try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
130 1 : CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
131 :
132 1 : bCaughtException = false;
133 1 : try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
134 1 : CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
135 :
136 : // "set" taking interface_type*
137 1 : bCaughtException = false;
138 2 : try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
139 1 : CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
140 :
141 1 : bCaughtException = false;
142 1 : try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
143 1 : CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
144 1 : }
145 :
146 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
147 :
148 : } // namespace
149 :
150 4 : CPPUNIT_PLUGIN_IMPLEMENT();
151 :
152 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|