1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_reference.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppu.hxx"
34 #include "sal/config.h"
36 #include "Interface1.hpp"
38 #include "cppunit/simpleheader.hxx"
39 #include "rtl/ustring.hxx"
40 #include "sal/types.h"
45 using ::com::sun::star::uno::Type
;
46 using ::com::sun::star::uno::Any
;
47 using ::com::sun::star::uno::Reference
;
48 using ::com::sun::star::uno::RuntimeException
;
49 using ::com::sun::star::uno::UNO_SET_THROW
;
51 class Foo
: public Interface1
59 virtual Any SAL_CALL
queryInterface(const Type
& _type
)
60 throw (RuntimeException
)
63 if (_type
== getCppuType
< Reference
< XInterface
> >())
65 Reference
< XInterface
> ref( static_cast< XInterface
* >( this ) );
66 aInterface
.setValue( &ref
, _type
);
68 else if (_type
== getCppuType
< Reference
< Interface1
> >())
70 Reference
< Interface1
> ref( this );
71 aInterface
.setValue( &ref
, _type
);
77 virtual void SAL_CALL
acquire() throw ()
79 osl_incrementInterlockedCount( &m_refCount
);
82 virtual void SAL_CALL
release() throw ()
84 if ( 0 == osl_decrementInterlockedCount( &m_refCount
) )
94 Foo(Foo
&); // not declared
95 Foo
& operator =(const Foo
&); // not declared
98 oslInterlockedCount m_refCount
;
101 class Test
: public ::CppUnit::TestFixture
105 void testUnoSetThrow();
107 CPPUNIT_TEST_SUITE(Test
);
108 CPPUNIT_TEST(testUnoSetThrow
);
109 CPPUNIT_TEST_SUITE_END();
112 void Test::testUnoSetThrow()
114 Reference
< Interface1
> xNull
;
115 Reference
< Interface1
> xFoo( new Foo
);
117 // ctor taking Reference< interface_type >
118 bool bCaughtException
= false;
119 try { Reference
< Interface1
> x( xNull
, UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
120 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
122 bCaughtException
= false;
123 try { Reference
< Interface1
> x( xFoo
, UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
124 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
126 // ctor taking interface_type*
127 bCaughtException
= false;
128 try { Reference
< Interface1
> x( xNull
.get(), UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
129 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
131 bCaughtException
= false;
132 try { Reference
< Interface1
> x( xFoo
.get(), UNO_SET_THROW
); (void)x
; } catch( const RuntimeException
& ) { bCaughtException
= true; }
133 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
135 Reference
< Interface1
> x
;
136 // "set" taking Reference< interface_type >
137 bCaughtException
= false;
138 try { x
.set( xNull
, UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
139 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
141 bCaughtException
= false;
142 try { x
.set( xFoo
, UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
143 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
145 // "set" taking interface_type*
146 bCaughtException
= false;
147 try { x
.set( xNull
.get(), UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
148 CPPUNIT_ASSERT_EQUAL( true, bCaughtException
);
150 bCaughtException
= false;
151 try { x
.set( xFoo
.get(), UNO_SET_THROW
); } catch( const RuntimeException
& ) { bCaughtException
= true; }
152 CPPUNIT_ASSERT_EQUAL( false, bCaughtException
);
155 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test
, "alltests");