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