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"
33 #define CPPUNIT_PLUGIN_EXPORTED_NAME cppunitTest_qa_cppu_reference
36 #include <cppunit/TestSuite.h>
37 #include <cppunit/TestFixture.h>
38 #include <cppunit/TestCase.h>
39 #include <cppunit/plugin/TestPlugIn.h>
40 #include <cppunit/extensions/HelperMacros.h>
42 #include "Interface1.hpp"
44 #include "rtl/ustring.hxx"
45 #include "sal/types.h"
50 using ::com::sun::star::uno::Type
;
51 using ::com::sun::star::uno::Any
;
52 using ::com::sun::star::uno::Reference
;
53 using ::com::sun::star::uno::RuntimeException
;
54 using ::com::sun::star::uno::UNO_SET_THROW
;
56 class Foo
: public Interface1
64 virtual Any SAL_CALL
queryInterface(const Type
& _type
)
65 throw (RuntimeException
)
68 if (_type
== getCppuType
< Reference
< XInterface
> >())
70 Reference
< XInterface
> ref( static_cast< XInterface
* >( this ) );
71 aInterface
.setValue( &ref
, _type
);
73 else if (_type
== getCppuType
< Reference
< Interface1
> >())
75 Reference
< Interface1
> ref( this );
76 aInterface
.setValue( &ref
, _type
);
82 virtual void SAL_CALL
acquire() throw ()
84 osl_incrementInterlockedCount( &m_refCount
);
87 virtual void SAL_CALL
release() throw ()
89 if ( 0 == osl_decrementInterlockedCount( &m_refCount
) )
99 Foo(Foo
&); // not declared
100 Foo
& operator =(const Foo
&); // not declared
103 oslInterlockedCount m_refCount
;
106 class Test
: public ::CppUnit::TestFixture
110 void testUnoSetThrow();
112 CPPUNIT_TEST_SUITE(Test
);
113 CPPUNIT_TEST(testUnoSetThrow
);
114 CPPUNIT_TEST_SUITE_END();
117 void Test::testUnoSetThrow()
119 Reference
< Interface1
> xNull
;
120 Reference
< Interface1
> xFoo( new Foo
);
122 // ctor taking Reference< interface_type >
123 bool bCaughtException
= false;
124 try { Reference
< Interface1
> x( xNull
, UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
125 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
127 bCaughtException
= false;
128 try { Reference
< Interface1
> x( xFoo
, UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
129 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
131 // ctor taking interface_type*
132 bCaughtException
= false;
133 try { Reference
< Interface1
> x( xNull
.get(), UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
134 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
136 bCaughtException
= false;
137 try { Reference
< Interface1
> x( xFoo
.get(), UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
138 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
140 Reference
< Interface1
> x
;
141 // "set" taking Reference< interface_type >
142 bCaughtException
= false;
143 try { x
.set( xNull
, UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
144 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
146 bCaughtException
= false;
147 try { x
.set( xFoo
, UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
148 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
150 // "set" taking interface_type*
151 bCaughtException
= false;
152 try { x
.set( xNull
.get(), UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
153 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
155 bCaughtException
= false;
156 try { x
.set( xFoo
.get(), UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
157 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
160 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
164 CPPUNIT_PLUGIN_IMPLEMENT();
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */