merge the formfield patch from ooo-build
[ooovba.git] / cppu / qa / test_reference.cxx
blob34e3e8666bbf28b3eff33d5ba57590d2407fbf88
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_reference.cxx,v $
10 * $Revision: 1.3 $
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"
42 namespace
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
53 public:
54 Foo()
55 :m_refCount(0)
59 virtual Any SAL_CALL queryInterface(const Type & _type)
60 throw (RuntimeException)
62 Any aInterface;
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 );
74 return Any();
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 ) )
85 delete this;
88 protected:
89 virtual ~Foo()
93 private:
94 Foo(Foo &); // not declared
95 Foo& operator =(const Foo&); // not declared
97 private:
98 oslInterlockedCount m_refCount;
101 class Test: public ::CppUnit::TestFixture
104 public:
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");
157 } // namespace
159 NOADDITIONAL;