xref: /aoo41x/main/cppuhelper/qa/weak/test_weak.cxx (revision 9d7e27ac)
1*9d7e27acSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9d7e27acSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9d7e27acSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9d7e27acSAndrew Rist  * distributed with this work for additional information
6*9d7e27acSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9d7e27acSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9d7e27acSAndrew Rist  * "License"); you may not use this file except in compliance
9*9d7e27acSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9d7e27acSAndrew Rist  *
11*9d7e27acSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9d7e27acSAndrew Rist  *
13*9d7e27acSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9d7e27acSAndrew Rist  * software distributed under the License is distributed on an
15*9d7e27acSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9d7e27acSAndrew Rist  * KIND, either express or implied.  See the License for the
17*9d7e27acSAndrew Rist  * specific language governing permissions and limitations
18*9d7e27acSAndrew Rist  * under the License.
19*9d7e27acSAndrew Rist  *
20*9d7e27acSAndrew Rist  *************************************************************/
21*9d7e27acSAndrew Rist 
22*9d7e27acSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sal/config.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "com/sun/star/lang/DisposedException.hpp"
30cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
31cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
32cdf0e10cSrcweir #include "com/sun/star/uno/XAdapter.hpp"
33cdf0e10cSrcweir #include "com/sun/star/uno/XReference.hpp"
34cdf0e10cSrcweir #include "com/sun/star/uno/XWeak.hpp"
35cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
36cdf0e10cSrcweir #include "cppuhelper/weak.hxx"
37cdf0e10cSrcweir #include "testshl/simpleheader.hxx"
38cdf0e10cSrcweir #include "rtl/ref.hxx"
39cdf0e10cSrcweir #include "sal/types.h"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace css = com::sun::star;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class Reference: public cppu::WeakImplHelper1< css::uno::XReference > {
46cdf0e10cSrcweir public:
Reference()47cdf0e10cSrcweir     Reference(): m_disposed(false) {}
48cdf0e10cSrcweir 
dispose()49cdf0e10cSrcweir     virtual void SAL_CALL dispose() throw (css::uno::RuntimeException) {
50cdf0e10cSrcweir         m_disposed = true;
51cdf0e10cSrcweir         handleDispose();
52cdf0e10cSrcweir     }
53cdf0e10cSrcweir 
isDisposed() const54cdf0e10cSrcweir     bool isDisposed() const { return m_disposed; }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir protected:
handleDispose()57cdf0e10cSrcweir     virtual void handleDispose() {};
58cdf0e10cSrcweir 
59cdf0e10cSrcweir private:
60cdf0e10cSrcweir     bool m_disposed;
61cdf0e10cSrcweir };
62cdf0e10cSrcweir 
63cdf0e10cSrcweir class RuntimeExceptionReference: public Reference {
64cdf0e10cSrcweir protected:
handleDispose()65cdf0e10cSrcweir     virtual void handleDispose() {
66cdf0e10cSrcweir         throw css::uno::RuntimeException();
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir };
69cdf0e10cSrcweir 
70cdf0e10cSrcweir class DisposedExceptionReference: public Reference {
71cdf0e10cSrcweir protected:
handleDispose()72cdf0e10cSrcweir     virtual void handleDispose() {
73cdf0e10cSrcweir         throw css::lang::DisposedException();
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir };
76cdf0e10cSrcweir 
77cdf0e10cSrcweir class Test: public ::CppUnit::TestFixture {
78cdf0e10cSrcweir public:
79cdf0e10cSrcweir     void testReferenceDispose();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test);
82cdf0e10cSrcweir     CPPUNIT_TEST(testReferenceDispose);
83cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
84cdf0e10cSrcweir };
85cdf0e10cSrcweir 
testReferenceDispose()86cdf0e10cSrcweir void Test::testReferenceDispose() {
87cdf0e10cSrcweir     css::uno::Reference< css::uno::XWeak > w(new ::cppu::OWeakObject);
88cdf0e10cSrcweir     css::uno::Reference< css::uno::XAdapter > a(w->queryAdapter());
89cdf0e10cSrcweir     ::rtl::Reference< Reference > r1(new RuntimeExceptionReference);
90cdf0e10cSrcweir     ::rtl::Reference< Reference > r2(new Reference);
91cdf0e10cSrcweir     ::rtl::Reference< Reference > r3(new DisposedExceptionReference);
92cdf0e10cSrcweir     a->addReference(r1.get());
93cdf0e10cSrcweir     a->addReference(r2.get());
94cdf0e10cSrcweir     a->addReference(r3.get());
95cdf0e10cSrcweir     w.clear();
96cdf0e10cSrcweir     CPPUNIT_ASSERT(r1->isDisposed());
97cdf0e10cSrcweir     CPPUNIT_ASSERT(r2->isDisposed());
98cdf0e10cSrcweir     CPPUNIT_ASSERT(r3->isDisposed());
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltests");
102cdf0e10cSrcweir 
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir NOADDITIONAL;
106