Branch libreoffice-5-0-4
[LibreOffice.git] / cppuhelper / test / testimplhelper.cxx
blob0f359e376e72d4472d505af98ecba6b33883e214
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #if !defined(OSL_DEBUG_LEVEL) || OSL_DEBUG_LEVEL == 0
21 # undef OSL_DEBUG_LEVEL
22 # define OSL_DEBUG_LEVEL 2
23 #endif
26 #include <stdio.h>
28 #include <cppuhelper/implbase1.hxx>
29 #include <cppuhelper/implbase2.hxx>
30 #include <cppuhelper/implbase3.hxx>
31 #include <cppuhelper/implbase4.hxx>
32 #include <cppuhelper/implbase5.hxx>
33 #include <cppuhelper/implbase6.hxx>
34 #include <cppuhelper/implbase7.hxx>
35 #include <cppuhelper/implbase8.hxx>
36 #include <cppuhelper/implbase9.hxx>
37 #include <cppuhelper/implbase10.hxx>
38 #include <cppuhelper/implbase11.hxx>
39 #include <cppuhelper/implbase12.hxx>
41 #include <cppuhelper/compbase1.hxx>
42 #include <cppuhelper/compbase2.hxx>
43 #include <cppuhelper/compbase3.hxx>
44 #include <cppuhelper/compbase4.hxx>
45 #include <cppuhelper/compbase5.hxx>
46 #include <cppuhelper/compbase6.hxx>
47 #include <cppuhelper/compbase7.hxx>
48 #include <cppuhelper/compbase8.hxx>
49 #include <cppuhelper/compbase9.hxx>
50 #include <cppuhelper/compbase10.hxx>
51 #include <cppuhelper/compbase11.hxx>
52 #include <cppuhelper/compbase12.hxx>
54 #include <osl/diagnose.h>
55 #include <cppuhelper/servicefactory.hxx>
56 #include <cppuhelper/exc_hlp.hxx>
58 #include <test/A.hpp>
59 #include <test/BA.hpp>
60 #include <test/CA.hpp>
61 #include <test/DBA.hpp>
62 #include <test/E.hpp>
63 #include <test/FE.hpp>
64 #include <test/G.hpp>
65 #include <test/H.hpp>
66 #include <test/I.hpp>
68 #include <com/sun/star/lang/IllegalAccessException.hpp>
70 using namespace test;
71 using namespace osl;
72 using namespace cppu;
73 using namespace com::sun::star;
74 using namespace com::sun::star::uno;
76 using ::rtl::OUString;
77 using ::rtl::OUStringToOString;
78 using ::rtl::OString;
81 struct TestImpl : public ImplHelper4< CA, DBA, FE, G >
83 sal_Int32 nRef;
85 virtual ~TestImpl()
86 { OSL_TRACE( "> TestImpl dtor called... <\n" ); }
88 virtual Any SAL_CALL queryInterface( const Type & rType ) throw(RuntimeException)
89 { return ImplHelper4< CA, DBA, FE, G >::queryInterface( rType ); }
90 virtual void SAL_CALL acquire( ) throw()
91 { ++nRef; }
92 virtual void SAL_CALL release( ) throw()
93 { if (! --nRef) delete this; }
95 // A
96 virtual OUString SAL_CALL a() throw(RuntimeException)
97 { return OUString("a"); }
98 // BA
99 virtual OUString SAL_CALL ba() throw(RuntimeException)
100 { return OUString("ba"); }
101 // CA
102 virtual OUString SAL_CALL ca() throw(RuntimeException)
103 { return OUString("ca"); }
104 // DBA
105 virtual OUString SAL_CALL dba() throw(RuntimeException)
106 { return OUString("dba"); }
107 // E
108 virtual OUString SAL_CALL e() throw(RuntimeException)
109 { return OUString("e"); }
110 // FE
111 virtual OUString SAL_CALL fe() throw(RuntimeException)
112 { return OUString("fe"); }
113 // G
114 virtual OUString SAL_CALL g() throw(RuntimeException)
115 { return OUString("g"); }
119 struct TestWeakAggImpl : public WeakAggImplHelper4< CA, DBA, FE, G >
121 virtual ~TestWeakAggImpl()
122 { OSL_TRACE( "> TestWeakAggImpl dtor called... <\n" ); }
124 // A
125 virtual OUString SAL_CALL a() throw(RuntimeException)
126 { return OUString("a"); }
127 // BA
128 virtual OUString SAL_CALL ba() throw(RuntimeException)
129 { return OUString("ba"); }
130 // CA
131 virtual OUString SAL_CALL ca() throw(RuntimeException)
132 { return OUString("ca"); }
133 // DBA
134 virtual OUString SAL_CALL dba() throw(RuntimeException)
135 { return OUString("dba"); }
136 // E
137 virtual OUString SAL_CALL e() throw(RuntimeException)
138 { return OUString("e"); }
139 // FE
140 virtual OUString SAL_CALL fe() throw(RuntimeException)
141 { return OUString("fe"); }
142 // G
143 virtual OUString SAL_CALL g() throw(RuntimeException)
144 { return OUString("g"); }
148 struct TestWeakImpl : public WeakImplHelper4< CA, DBA, FE, G >
150 TestWeakImpl() {}
152 virtual ~TestWeakImpl()
153 { OSL_TRACE( "> TestWeakImpl dtor called... <\n" ); }
155 // A
156 virtual OUString SAL_CALL a() throw(RuntimeException)
157 { return OUString("a"); }
158 // BA
159 virtual OUString SAL_CALL ba() throw(RuntimeException)
160 { return OUString("ba"); }
161 // CA
162 virtual OUString SAL_CALL ca() throw(RuntimeException)
163 { return OUString("ca"); }
164 // DBA
165 virtual OUString SAL_CALL dba() throw(RuntimeException)
166 { return OUString("dba"); }
167 // E
168 virtual OUString SAL_CALL e() throw(RuntimeException)
169 { return OUString("e"); }
170 // FE
171 virtual OUString SAL_CALL fe() throw(RuntimeException)
172 { return OUString("fe"); }
173 // G
174 virtual OUString SAL_CALL g() throw(RuntimeException)
175 { return OUString("g"); }
177 protected:
178 TestWeakImpl(int) {}
180 TestWeakImpl(int, int) {}
182 TestWeakImpl(int, int, int) {}
184 TestWeakImpl(int, int, int, int) {}
186 TestWeakImpl(int, int, int, int, int) {}
188 TestWeakImpl(int, int, int, int, int, int, int = 0) {}
192 struct TestWeakComponentImpl : public WeakComponentImplHelper4< CA, DBA, FE, G >
194 Mutex m;
195 TestWeakComponentImpl()
196 : WeakComponentImplHelper4< CA, DBA, FE, G >( m )
198 virtual ~TestWeakComponentImpl()
199 { OSL_TRACE( "> TestWeakComponentImpl dtor called... <\n" ); }
201 void SAL_CALL disposing()
202 { OSL_TRACE( "> TestWeakComponentImpl disposing called... <\n" ); }
204 // A
205 virtual OUString SAL_CALL a() throw(RuntimeException)
206 { return OUString("a"); }
207 // BA
208 virtual OUString SAL_CALL ba() throw(RuntimeException)
209 { return OUString("ba"); }
210 // CA
211 virtual OUString SAL_CALL ca() throw(RuntimeException)
212 { return OUString("ca"); }
213 // DBA
214 virtual OUString SAL_CALL dba() throw(RuntimeException)
215 { return OUString("dba"); }
216 // E
217 virtual OUString SAL_CALL e() throw(RuntimeException)
218 { return OUString("e"); }
219 // FE
220 virtual OUString SAL_CALL fe() throw(RuntimeException)
221 { return OUString("fe"); }
222 // G
223 virtual OUString SAL_CALL g() throw(RuntimeException)
224 { return OUString("g"); }
228 struct TestWeakAggComponentImpl : public WeakAggComponentImplHelper4< CA, DBA, FE, G >
230 Mutex m;
231 TestWeakAggComponentImpl()
232 : WeakAggComponentImplHelper4< CA, DBA, FE, G >( m )
234 virtual ~TestWeakAggComponentImpl()
235 { OSL_TRACE( "> TestWeakAggComponentImpl dtor called... <\n" ); }
237 void SAL_CALL disposing()
238 { OSL_TRACE( "> TestWeakAggComponentImpl disposing called... <\n" ); }
240 // A
241 virtual OUString SAL_CALL a() throw(RuntimeException)
242 { return OUString("a"); }
243 // BA
244 virtual OUString SAL_CALL ba() throw(RuntimeException)
245 { return OUString("ba"); }
246 // CA
247 virtual OUString SAL_CALL ca() throw(RuntimeException)
248 { return OUString("ca"); }
249 // DBA
250 virtual OUString SAL_CALL dba() throw(RuntimeException)
251 { return OUString("dba"); }
252 // E
253 virtual OUString SAL_CALL e() throw(RuntimeException)
254 { return OUString("e"); }
255 // FE
256 virtual OUString SAL_CALL fe() throw(RuntimeException)
257 { return OUString("fe"); }
258 // G
259 virtual OUString SAL_CALL g() throw(RuntimeException)
260 { return OUString("g"); }
264 struct TestImplInh : public ImplInheritanceHelper2< TestWeakImpl, H, I >
266 TestImplInh() {}
268 explicit TestImplInh(int):
269 ImplInheritanceHelper2< TestWeakImpl, H, I >(1) {}
271 TestImplInh(int, int):
272 ImplInheritanceHelper2< TestWeakImpl, H, I >(1, 2) {}
274 TestImplInh(int, int, int):
275 ImplInheritanceHelper2< TestWeakImpl, H, I >(1, 2, 3) {}
277 TestImplInh(int, int, int, int):
278 ImplInheritanceHelper2< TestWeakImpl, H, I >(1, 2, 3, 4) {}
280 TestImplInh(int, int, int, int, int):
281 ImplInheritanceHelper2< TestWeakImpl, H, I >(1, 2, 3, 4, 5) {}
283 TestImplInh(int, int, int, int, int, int):
284 ImplInheritanceHelper2< TestWeakImpl, H, I >(1, 2, 3, 4, 5, 6) {}
286 virtual ~TestImplInh()
287 { OSL_TRACE( "> TestWeakImplInh dtor called... <\n" ); }
289 // H
290 virtual OUString SAL_CALL h() throw(RuntimeException)
291 { return OUString("h"); }
292 // I
293 virtual OUString SAL_CALL i() throw(RuntimeException)
294 { return OUString("i"); }
298 struct TestAggImplInh : public AggImplInheritanceHelper2< TestWeakAggImpl, H, I >
300 virtual ~TestAggImplInh()
301 { OSL_TRACE( "> TestAggImplInh dtor called... <\n" ); }
303 // H
304 virtual OUString SAL_CALL h() throw(RuntimeException)
305 { return OUString("h2"); }
306 // I
307 virtual OUString SAL_CALL i() throw(RuntimeException)
308 { return OUString("i2"); }
312 static bool isIn( Sequence< Type > const & rTypes, char const * name )
314 OUString str( OUString::createFromAscii( name ) );
315 Type const * pTypes = rTypes.getConstArray();
316 for ( sal_Int32 nPos = rTypes.getLength(); nPos--; )
318 if (pTypes[ nPos ].getTypeName().equals( str ))
319 return true;
321 return false;
325 static void dotest( const Reference< XInterface > & xOriginal )
327 Reference< lang::XTypeProvider > xTP( xOriginal, UNO_QUERY );
328 Sequence< sal_Int8 > id( xTP->getImplementationId() );
329 Sequence< Type > types( xTP->getTypes() );
330 ::fprintf( stderr, "> supported types: " );
331 long n = 0;
332 for ( ; n < types.getLength(); ++n )
334 OString str( OUStringToOString( types[ n ].getTypeName(), RTL_TEXTENCODING_ASCII_US ) );
335 ::fprintf( stderr, (n < (types.getLength()-1)) ? "%s, " : "%s; type-id=\n", str.getStr() );
337 for ( n = 0; n < 16; ++n )
339 ::fprintf( stderr, n < 15 ? "%x " : "%x \n", id[ n ] );
342 Reference< A > xa( xOriginal, UNO_QUERY );
343 OSL_ENSURE( xa->a() == "a", "### A failed!" );
344 Reference< BA > xba( xa, UNO_QUERY );
345 OSL_ENSURE( xba->ba() == "ba", "### BA failed!" );
346 OSL_ENSURE( xba->a() == "a", "### BA failed!" );
347 Reference< CA > xca( xba, UNO_QUERY );
348 OSL_ENSURE( xca->ca() == "ca", "### CA failed!" );
349 OSL_ENSURE( xca->a() == "a", "### CA failed!" );
350 Reference< DBA > xdba( xca, UNO_QUERY );
351 OSL_ENSURE( xdba->dba() == "dba", "### DBA failed!" );
352 OSL_ENSURE( xdba->ba() == "ba", "### DBA failed!" );
353 OSL_ENSURE( xdba->a() == "a", "### DBA failed!" );
354 Reference< E > xe( xdba, UNO_QUERY );
355 OSL_ENSURE( xe->e() == "e", "### E failed!" );
356 Reference< FE > xfe( xe, UNO_QUERY );
357 OSL_ENSURE( xfe->fe() == "fe", "### FE failed!" );
358 Reference< G > xg( xfe, UNO_QUERY );
359 OSL_ENSURE( xg->g() == "g", "### G failed!" );
360 OSL_ENSURE( xg->a() == "a", "### G failed!" );
362 // type provider
363 Reference< lang::XTypeProvider > xProv( xg, UNO_QUERY );
364 Sequence< Type > aTypes( xProv->getTypes() );
366 // CA, DBA, FE, G, XTypeProvider
367 OSL_ASSERT( isIn( aTypes, "test.CA" ) );
368 OSL_ASSERT( isIn( aTypes, "test.DBA" ) );
369 OSL_ASSERT( isIn( aTypes, "test.FE") );
370 OSL_ASSERT( isIn( aTypes, "test.G") );
371 OSL_ASSERT( isIn( aTypes, "com.sun.star.lang.XTypeProvider") );
373 Reference< XWeak > xWeak( xg, UNO_QUERY );
374 if (xWeak.is())
376 OSL_ASSERT( isIn( aTypes, "com.sun.star.uno.XWeak") );
378 Reference< lang::XComponent > xComp( xg, UNO_QUERY );
379 if (xComp.is())
381 OSL_ASSERT( isIn( aTypes, "com.sun.star.lang.XComponent") );
384 Reference< XAggregation > xAgg( xg, UNO_QUERY );
385 if (xAgg.is())
387 OSL_ASSERT( isIn( aTypes, "com.sun.star.uno.XAggregation") );
389 Reference< H > xH( xg, UNO_QUERY );
390 if (xH.is())
392 OSL_ASSERT( isIn( aTypes, "test.H") );
394 Reference< I > xI( xg, UNO_QUERY );
395 if (xI.is())
397 OSL_ASSERT( isIn( aTypes, "test.I") );
400 OSL_ENSURE( xg == xOriginal, "### root!" );
403 void rethrow()
405 throw;
408 void throw_one(
409 lang::IllegalAccessException exc )
413 throw exc;
415 catch (...)
417 rethrow();
423 void test_ImplHelper( const Reference< lang::XMultiServiceFactory > & /*xSF*/ )
425 Reference< XInterface > xImpl( (lang::XTypeProvider *)new TestImpl() );
426 Reference< lang::XTypeProvider > xTP1( xImpl, UNO_QUERY );
427 Reference< XInterface > xWeakImpl( (OWeakObject *)new TestWeakImpl() );
428 Reference< lang::XTypeProvider > xTP2( xWeakImpl, UNO_QUERY );
429 Reference< XInterface > xWeakAggImpl( (OWeakObject *)new TestWeakAggImpl() );
430 Reference< lang::XTypeProvider > xTP3( xWeakAggImpl, UNO_QUERY );
431 Reference< XInterface > xWeakComponentImpl( (OWeakObject *)new TestWeakComponentImpl() );
432 Reference< lang::XTypeProvider > xTP4( xWeakComponentImpl, UNO_QUERY );
433 Reference< XInterface > xWeakAggComponentImpl( (OWeakObject *)new TestWeakAggComponentImpl() );
434 Reference< lang::XTypeProvider > xTP5( xWeakAggComponentImpl, UNO_QUERY );
436 OSL_ASSERT(
437 xTP1->getImplementationId() != xTP2->getImplementationId() &&
438 xTP1->getImplementationId() != xTP3->getImplementationId() &&
439 xTP1->getImplementationId() != xTP4->getImplementationId() &&
440 xTP1->getImplementationId() != xTP5->getImplementationId() &&
441 xTP2->getImplementationId() != xTP3->getImplementationId() &&
442 xTP2->getImplementationId() != xTP4->getImplementationId() &&
443 xTP2->getImplementationId() != xTP5->getImplementationId() &&
444 xTP3->getImplementationId() != xTP4->getImplementationId() &&
445 xTP3->getImplementationId() != xTP5->getImplementationId() &&
446 xTP4->getImplementationId() != xTP5->getImplementationId() );
449 dotest( xImpl );
450 dotest( xWeakImpl );
451 dotest( xWeakAggImpl );
452 dotest( xWeakComponentImpl );
453 dotest( xWeakAggComponentImpl );
455 xWeakImpl = (OWeakObject *)new TestImplInh();
456 Reference< lang::XTypeProvider > xTP6( xWeakImpl, UNO_QUERY );
457 dotest( xWeakImpl );
458 Reference< H > xH( xWeakImpl, UNO_QUERY );
459 Reference< I > xI( xH, UNO_QUERY );
460 OSL_ASSERT( xH->h() == "h" );
461 OSL_ASSERT( xI->i() == "i" );
463 xWeakAggImpl = (OWeakObject *)new TestAggImplInh();
464 Reference< lang::XTypeProvider > xTP7( xWeakAggImpl, UNO_QUERY );
465 dotest( xWeakAggImpl );
466 xH.set( xWeakAggImpl, UNO_QUERY );
467 xI.set( xH, UNO_QUERY );
468 OSL_ASSERT( xH->h() == "h2" );
469 OSL_ASSERT( xI->i() == "i2" );
472 OSL_ASSERT(
473 xTP6->getImplementationId() != xTP1->getImplementationId() &&
474 xTP6->getImplementationId() != xTP2->getImplementationId() &&
475 xTP6->getImplementationId() != xTP3->getImplementationId() &&
476 xTP6->getImplementationId() != xTP4->getImplementationId() &&
477 xTP6->getImplementationId() != xTP5->getImplementationId() &&
478 xTP6->getImplementationId() != xTP7->getImplementationId() &&
479 xTP7->getImplementationId() != xTP1->getImplementationId() &&
480 xTP7->getImplementationId() != xTP2->getImplementationId() &&
481 xTP7->getImplementationId() != xTP3->getImplementationId() &&
482 xTP7->getImplementationId() != xTP4->getImplementationId() &&
483 xTP7->getImplementationId() != xTP5->getImplementationId() );
486 bool exc_succ = false;
487 lang::IllegalAccessException exc(
488 OUString("testtest"),
489 xWeakAggImpl );
490 // exception helper tests
493 throw exc;
495 catch (Exception &)
497 Any a( getCaughtException() );
498 OSL_ASSERT( a == exc );
502 throwException( a );
504 catch (lang::IllegalAccessException & e)
506 OSL_ASSERT( exc.Message == e.Message && exc.Context == e.Context );
510 throw_one( exc );
512 catch (Exception &)
514 Any a2( getCaughtException() );
515 OSL_ASSERT( (a2 == a) && (a2 == exc) );
519 throw_one( exc );
521 catch (lang::IllegalAccessException &)
523 Any a3( getCaughtException() );
524 OSL_ASSERT( (a3 == a) && (a3 == a2) && (a3 == exc) );
525 exc_succ = true;
530 OSL_ASSERT( exc_succ );
534 throwException( makeAny( RuntimeException(
535 OUString("runtime exc"), xImpl ) ) );
537 catch (RuntimeException & rExc)
539 OSL_ENSURE( rExc.Message == "runtime exc" && rExc.Context == xImpl, "### unexpected exception content!" );
542 throwException( makeAny( Exception(
543 OUString("exc"), rExc.Context ) ) );
545 catch (lang::IllegalAccessException &)
547 OSL_FAIL( "### unexpected IllegalAccessException exception caught!" );
549 catch (Exception & rExc2)
551 OSL_ENSURE( rExc2.Message == "exc" && rExc2.Context == xImpl, "### unexpected exception content!" );
554 throwException( makeAny( lang::IllegalAccessException(
555 OUString("axxess exc"), rExc2.Context ) ) );
557 catch (lang::IllegalAccessException & rExc3)
559 OSL_ENSURE( rExc3.Message == "axxess exc" && rExc3.Context == xImpl, "### unexpected exception content!" );
560 return;
564 catch (...)
567 OSL_FAIL( "### exception test failed!" );
570 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */