Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / cppuhelper / qa / weak / test_weak.cxx
blobc0e35ca6eee552da62993a95afd983869c9d0d99
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
30 #include "sal/precppunit.hxx"
32 #include <cppunit/TestFixture.h>
33 #include <cppunit/extensions/HelperMacros.h>
34 #include <cppunit/plugin/TestPlugIn.h>
36 #include "com/sun/star/lang/DisposedException.hpp"
37 #include "com/sun/star/uno/Reference.hxx"
38 #include "com/sun/star/uno/RuntimeException.hpp"
39 #include "com/sun/star/uno/XAdapter.hpp"
40 #include "com/sun/star/uno/XReference.hpp"
41 #include "com/sun/star/uno/XWeak.hpp"
42 #include "cppuhelper/implbase1.hxx"
43 #include "cppuhelper/weak.hxx"
44 #include "rtl/ref.hxx"
45 #include "sal/types.h"
47 namespace {
49 namespace css = com::sun::star;
51 class Reference: public cppu::WeakImplHelper1< css::uno::XReference > {
52 public:
53 Reference(): m_disposed(false) {}
55 virtual void SAL_CALL dispose() throw (css::uno::RuntimeException) {
56 m_disposed = true;
57 handleDispose();
60 bool isDisposed() const { return m_disposed; }
62 protected:
63 virtual void handleDispose() {};
65 private:
66 bool m_disposed;
69 class RuntimeExceptionReference: public Reference {
70 protected:
71 virtual void handleDispose() {
72 throw css::uno::RuntimeException();
76 class DisposedExceptionReference: public Reference {
77 protected:
78 virtual void handleDispose() {
79 throw css::lang::DisposedException();
83 class Test: public ::CppUnit::TestFixture {
84 public:
85 void testReferenceDispose();
87 CPPUNIT_TEST_SUITE(Test);
88 CPPUNIT_TEST(testReferenceDispose);
89 CPPUNIT_TEST_SUITE_END();
92 void Test::testReferenceDispose() {
93 css::uno::Reference< css::uno::XWeak > w(new ::cppu::OWeakObject);
94 css::uno::Reference< css::uno::XAdapter > a(w->queryAdapter());
95 ::rtl::Reference< Reference > r1(new RuntimeExceptionReference);
96 ::rtl::Reference< Reference > r2(new Reference);
97 ::rtl::Reference< Reference > r3(new DisposedExceptionReference);
98 a->addReference(r1.get());
99 a->addReference(r2.get());
100 a->addReference(r3.get());
101 w.clear();
102 CPPUNIT_ASSERT(r1->isDisposed());
103 CPPUNIT_ASSERT(r2->isDisposed());
104 CPPUNIT_ASSERT(r3->isDisposed());
107 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
111 CPPUNIT_PLUGIN_IMPLEMENT();
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */