xref: /aoo42x/main/cppu/qa/test_reference.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppu.hxx"
30 
31 #include "sal/config.h"
32 
33 #include "Interface1.hpp"
34 
35 #include "testshl/simpleheader.hxx"
36 #include "rtl/ustring.hxx"
37 #include "sal/types.h"
38 
39 namespace
40 {
41 
42 using ::com::sun::star::uno::Type;
43 using ::com::sun::star::uno::Any;
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::RuntimeException;
46 using ::com::sun::star::uno::UNO_SET_THROW;
47 
48 class Foo: public Interface1
49 {
50 public:
51     Foo()
52         :m_refCount(0)
53     {
54     }
55 
56     virtual Any SAL_CALL queryInterface(const Type & _type)
57         throw (RuntimeException)
58     {
59         Any aInterface;
60         if (_type == getCppuType< Reference< XInterface > >())
61         {
62             Reference< XInterface > ref( static_cast< XInterface * >( this ) );
63             aInterface.setValue( &ref, _type );
64         }
65         else if (_type == getCppuType< Reference< Interface1 > >())
66         {
67             Reference< Interface1 > ref( this );
68             aInterface.setValue( &ref, _type );
69         }
70 
71         return Any();
72     }
73 
74     virtual void SAL_CALL acquire() throw ()
75     {
76         osl_incrementInterlockedCount( &m_refCount );
77     }
78 
79     virtual void SAL_CALL release() throw ()
80     {
81         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
82             delete this;
83     }
84 
85 protected:
86     virtual ~Foo()
87     {
88     }
89 
90 private:
91     Foo(Foo &); // not declared
92     Foo& operator =(const Foo&); // not declared
93 
94 private:
95     oslInterlockedCount m_refCount;
96 };
97 
98 class Test: public ::CppUnit::TestFixture
99 {
100 
101 public:
102     void testUnoSetThrow();
103 
104     CPPUNIT_TEST_SUITE(Test);
105     CPPUNIT_TEST(testUnoSetThrow);
106     CPPUNIT_TEST_SUITE_END();
107 };
108 
109 void Test::testUnoSetThrow()
110 {
111     Reference< Interface1 > xNull;
112     Reference< Interface1 > xFoo( new Foo );
113 
114     // ctor taking Reference< interface_type >
115     bool bCaughtException = false;
116     try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
117     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
118 
119     bCaughtException = false;
120     try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
121     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
122 
123     // ctor taking interface_type*
124     bCaughtException = false;
125     try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
126     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
127 
128     bCaughtException = false;
129     try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); (void)x; } catch( const RuntimeException& ) { bCaughtException = true; }
130     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
131 
132     Reference< Interface1 > x;
133     // "set" taking Reference< interface_type >
134     bCaughtException = false;
135     try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
136     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
137 
138     bCaughtException = false;
139     try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
140     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
141 
142     // "set" taking interface_type*
143     bCaughtException = false;
144     try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
145     CPPUNIT_ASSERT_EQUAL( true, bCaughtException );
146 
147     bCaughtException = false;
148     try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; }
149     CPPUNIT_ASSERT_EQUAL( false, bCaughtException );
150 }
151 
152 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltests");
153 
154 }   // namespace
155 
156 NOADDITIONAL;
157