xref: /aoo41x/main/cppu/qa/test_reference.cxx (revision 129fa3d1)
1*129fa3d1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*129fa3d1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*129fa3d1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*129fa3d1SAndrew Rist  * distributed with this work for additional information
6*129fa3d1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*129fa3d1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*129fa3d1SAndrew Rist  * "License"); you may not use this file except in compliance
9*129fa3d1SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*129fa3d1SAndrew Rist  *
11*129fa3d1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*129fa3d1SAndrew Rist  *
13*129fa3d1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*129fa3d1SAndrew Rist  * software distributed under the License is distributed on an
15*129fa3d1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*129fa3d1SAndrew Rist  * KIND, either express or implied.  See the License for the
17*129fa3d1SAndrew Rist  * specific language governing permissions and limitations
18*129fa3d1SAndrew Rist  * under the License.
19*129fa3d1SAndrew Rist  *
20*129fa3d1SAndrew Rist  *************************************************************/
21*129fa3d1SAndrew Rist 
22*129fa3d1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppu.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sal/config.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "Interface1.hpp"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "testshl/simpleheader.hxx"
32cdf0e10cSrcweir #include "rtl/ustring.hxx"
33cdf0e10cSrcweir #include "sal/types.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace
36cdf0e10cSrcweir {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using ::com::sun::star::uno::Type;
39cdf0e10cSrcweir using ::com::sun::star::uno::Any;
40cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
41cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException;
42cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir class Foo: public Interface1
45cdf0e10cSrcweir {
46cdf0e10cSrcweir public:
Foo()47cdf0e10cSrcweir     Foo()
48cdf0e10cSrcweir         :m_refCount(0)
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir     }
51cdf0e10cSrcweir 
queryInterface(const Type & _type)52cdf0e10cSrcweir     virtual Any SAL_CALL queryInterface(const Type & _type)
53cdf0e10cSrcweir         throw (RuntimeException)
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         Any aInterface;
56cdf0e10cSrcweir         if (_type == getCppuType< Reference< XInterface > >())
57cdf0e10cSrcweir         {
58cdf0e10cSrcweir             Reference< XInterface > ref( static_cast< XInterface * >( this ) );
59cdf0e10cSrcweir             aInterface.setValue( &ref, _type );
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir         else if (_type == getCppuType< Reference< Interface1 > >())
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir             Reference< Interface1 > ref( this );
64cdf0e10cSrcweir             aInterface.setValue( &ref, _type );
65cdf0e10cSrcweir         }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         return Any();
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
acquire()70cdf0e10cSrcweir     virtual void SAL_CALL acquire() throw ()
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir 
release()75cdf0e10cSrcweir     virtual void SAL_CALL release() throw ()
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
78cdf0e10cSrcweir             delete this;
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir protected:
~Foo()82cdf0e10cSrcweir     virtual ~Foo()
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir private:
87cdf0e10cSrcweir     Foo(Foo &); // not declared
88cdf0e10cSrcweir     Foo& operator =(const Foo&); // not declared
89cdf0e10cSrcweir 
90cdf0e10cSrcweir private:
91cdf0e10cSrcweir     oslInterlockedCount m_refCount;
92cdf0e10cSrcweir };
93cdf0e10cSrcweir 
94cdf0e10cSrcweir class Test: public ::CppUnit::TestFixture
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 
97cdf0e10cSrcweir public:
98cdf0e10cSrcweir     void testUnoSetThrow();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test);
101cdf0e10cSrcweir     CPPUNIT_TEST(testUnoSetThrow);
102cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
103cdf0e10cSrcweir };
104cdf0e10cSrcweir 
testUnoSetThrow()105cdf0e10cSrcweir void Test::testUnoSetThrow()
106cdf0e10cSrcweir {
107cdf0e10cSrcweir     Reference< Interface1 > xNull;
108cdf0e10cSrcweir     Reference< Interface1 > xFoo( new Foo );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     // ctor taking Reference< interface_type >
111cdf0e10cSrcweir     bool bCaughtException = false;
112cdf0e10cSrcweir     try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
113cdf0e10cSrcweir     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     bCaughtException = false;
116cdf0e10cSrcweir     try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
117cdf0e10cSrcweir     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     // ctor taking interface_type*
120cdf0e10cSrcweir     bCaughtException = false;
121cdf0e10cSrcweir     try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
122cdf0e10cSrcweir     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     bCaughtException = false;
125cdf0e10cSrcweir     try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
126cdf0e10cSrcweir     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     Reference< Interface1 > x;
129cdf0e10cSrcweir     // "set" taking Reference< interface_type >
130cdf0e10cSrcweir     bCaughtException = false;
131cdf0e10cSrcweir     try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
132cdf0e10cSrcweir     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     bCaughtException = false;
135cdf0e10cSrcweir     try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
136cdf0e10cSrcweir     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     // "set" taking interface_type*
139cdf0e10cSrcweir     bCaughtException = false;
140cdf0e10cSrcweir     try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
141cdf0e10cSrcweir     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     bCaughtException = false;
144cdf0e10cSrcweir     try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
145cdf0e10cSrcweir     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltests");
149cdf0e10cSrcweir 
150cdf0e10cSrcweir }   // namespace
151cdf0e10cSrcweir 
152cdf0e10cSrcweir NOADDITIONAL;
153