update dev300-m57
[ooovba.git] / sal / test / unloading / unloadTest.cxx
blob2ce9b4df44f4c03f6c0d58e98a7c7740c4950953
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: unloadTest.cxx,v $
10 * $Revision: 1.7 $
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 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
35 #include<osl/module.hxx>
36 #include <osl/time.h>
37 #include <rtl/ustring.hxx>
38 #include <stdio.h>
39 #include <cppuhelper/factory.hxx>
40 #include <cppuhelper/servicefactory.hxx>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43 #include <com/sun/star/lang/XServiceInfo.hpp>
44 #include <com/sun/star/lang/XComponent.hpp>
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include <com/sun/star/uno/XComponentContext.hpp>
47 #include <com/sun/star/uno/XUnloadingPreference.hpp>
48 #include <com/sun/star/lang/XTypeProvider.hpp>
49 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
51 #include <stdio.h>
52 using namespace ::rtl;
53 using namespace ::osl;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::lang;
56 using namespace ::cppu;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::container;
60 #define IMPLNAME1 "com.sun.star.comp.sal.UnloadingTest1"
61 #define SERVICENAME1 "com.sun.star.UnloadingTest1"
62 #define IMPLNAME2 "com.sun.star.comp.sal.UnloadingTest2"
63 #define SERVICENAME2 "com.sun.star.UnloadingTest2"
64 #define IMPLNAME3 "com.sun.star.comp.sal.UnloadingTest3"
65 #define SERVICENAME3 "com.sun.star.UnloadingTest3"
66 #define IMPLNAME4 "com.sun.star.comp.sal.OneInstanceTest"
67 #define SERVICENAME4 "com.sun.star.OneInstanceTest"
69 #define IMPLNAME21 "com.sun.star.comp.sal.UnloadingTest21"
70 #define SERVICENAME21 "com.sun.star.UnloadingTest21"
71 #define IMPLNAME22 "com.sun.star.comp.sal.UnloadingTest22"
72 #define SERVICENAME22 "com.sun.star.UnloadingTest22"
73 #define IMPLNAME23 "com.sun.star.comp.sal.UnloadingTest23"
74 #define SERVICENAME23 "com.sun.star.UnloadingTest23"
76 #ifdef UNX
77 #define LIBRARY1 "libsamplelib1.so"
78 #define LIBRARY2 "libsamplelib2.so"
79 #elif defined WNT
80 #define LIBRARY1 "samplelib1"
81 #define LIBRARY2 "samplelib2"
82 #endif
84 Tested: rtl_registerModuleForUnloading, rtl_unregisterModuleForUnloading, rtl_unloadUnusedLibraries
85 1 component.
87 next: Test with multiple components
88 Listener mechanism.
91 sal_Bool test1();
92 sal_Bool test2();
93 sal_Bool test3();
94 sal_Bool test4();
95 sal_Bool test5();
96 sal_Bool test6();
97 sal_Bool test7();
98 sal_Bool test8();
99 sal_Bool test9();
100 void SAL_CALL listenerCallback( void* id);
102 int main(int argc, char* argv[])
104 // Test if the servicemanager can be created and if the sample libs
105 // can be loaded
106 // Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
107 // OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
108 // if( !serviceManager.is())
109 // {
110 // printf("\n ####################################################\n"
111 // "Error: could not create service manager. \n"
112 // "Is the executable in the office program directory?\n");
113 // return -1;
114 // }
116 // Reference<XInterface> xint1= serviceManager->createInstance( OUString(
117 // RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1)));
119 // if( !xint1.is())
120 // {
121 // printf("\n ###################################################\n"
122 // "Error: could not create service from samplelib1\n"
123 // "Is samplelib1 in the office program directory and is it "
124 // "registered?\n");
125 // return -1;
126 // }
127 // Reference<XInterface> xint2= serviceManager->createInstance( OUString(
128 // RTL_CONSTASCII_USTRINGPARAM(SERVICENAME21)));
129 // if( !xint2.is())
130 // {
131 // printf("\n ###################################################"
132 // "Error: could not create service from samplelib2\n"
133 // "Is samplelib2 in the office program directory and is it "
134 // "registered?\n");
135 // return -1;
136 // }
137 // //destroy servicemanager
138 // Reference<XPropertySet> xSet( serviceManager, UNO_QUERY);
139 // Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
140 // Reference<XComponentContext> xContext;
141 // any_prop >>= xContext;
142 // Reference<XComponent> xComponent( xContext, UNO_QUERY);
143 // xComponent->dispose();
145 // //unload samplelib1 and samplelib2. We need the handles, therefore load
146 // // the libs
147 // OUString libname1( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
148 // OUString libname2( RTL_CONSTASCII_USTRINGPARAM(LIBRARY2));
149 // oslModule m1= osl_loadModule(libname1.pData, 0);
150 // oslModule m2= osl_loadModule(libname2.pData, 0);
151 // osl_unloadModule( m1);
152 // osl_unloadModule( m1);
153 // osl_unloadModule( m2);
154 // osl_unloadModule( m2);
157 sal_Bool ret1= test1();
158 if( ret1) printf( "\n Test 1 successful \n");
159 else printf("\n !!!!!! Test 1 failed\n");
160 sal_Bool ret2= test2();
161 if( ret2) printf( "\n Test 2 successful \n");
162 else printf("\n !!!!!! Test 2 failed\n");
163 sal_Bool ret3= test3();
164 if( ret3) printf( "\n Test 3 successful \n");
165 else printf("\n !!!!!! Test 3 failed\n");
166 sal_Bool ret4= test4();
167 if( ret4) printf( "\n Test 4 successful \n");
168 else printf("\n !!!!!! Test 4 failed\n");
169 sal_Bool ret5= test5();
170 if( ret5) printf( "\n Test 5 successful \n");
171 else printf("\n !!!!!! Test 5 failed\n");
172 // takes some time (10s)
173 sal_Bool ret6= test6();
174 sal_Bool ret7= test7(); // prints message itself
175 sal_Bool ret8= test8();
176 if( ret8) printf( "\n Test 8 successful \n");
177 else printf("\n !!!!!! Test 8 failed\n");
178 sal_Bool ret9= test9();
179 if( ret9) printf( "\n Test 9 successful: service manager is unloading listener\n");
180 else printf("\n !!!!! Test 9 failed\n");
182 return 0;
185 /* Create an instance of SERVICENAME1, call a function and unload module.
186 This tests the loader and basic functionality.
187 The library will be loaded once manually and the handle will be stored.
188 Then the library will be unloaded. After rtl_unloadUnusedLibraries we try to
189 get a symbol of the unloaded lib. If this fails then the test is successful.
191 sal_Bool test1()
193 printf("Test 1 ####################################################\n");
194 oslModule handleMod=0;
195 OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
197 Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
198 OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
199 Reference<XInterface> xint= serviceManager->createInstance( OUString(
200 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1)));
202 // get the handle
203 handleMod= osl_loadModule( lib1Name.pData, 0);
204 osl_unloadModule( handleMod);
205 xint=0;
207 Reference<XPropertySet> xSet( serviceManager, UNO_QUERY);
208 Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
209 Reference<XComponentContext> xContext;
210 any_prop >>= xContext;
211 Reference<XComponent> xComponent( xContext, UNO_QUERY);
212 xComponent->dispose();
214 rtl_unloadUnusedModules( NULL);
216 // Try to get a symbol, must fail
217 OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory"));
218 void* pSymbol= osl_getSymbol( handleMod, sSymbol.pData);
220 if( !pSymbol)
221 return sal_True;
222 return sal_False;
225 /* Multipe loadModule + rtl_registerModuleForUnloading.
226 The module will be registered as often as it has been loaded.
228 sal_Bool test2()
230 printf("Test 2 ####################################################\n");
231 oslModule handleMod=0;
232 OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
234 Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
235 OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
237 Reference<XInterface> xint= serviceManager->createInstance( OUString(
238 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1)));
240 handleMod= osl_loadModule( lib1Name.pData, 0);
241 osl_unloadModule( handleMod);
242 //-----------------------------------------------------------
243 oslModule mod1= osl_loadModule( lib1Name.pData, 0);
244 oslModule mod2= osl_loadModule( lib1Name.pData, 0);
245 oslModule mod3= osl_loadModule( lib1Name.pData, 0);
247 rtl_registerModuleForUnloading(mod1);
248 rtl_registerModuleForUnloading(mod2);
249 rtl_registerModuleForUnloading(mod3);
250 // ----------------------------------------------------------
251 Reference<XPropertySet> xSet( serviceManager, UNO_QUERY);
252 Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
253 Reference<XComponentContext> xContext;
254 any_prop >>= xContext;
255 Reference<XComponent> xComponent( xContext, UNO_QUERY);
256 xComponent->dispose();
258 rtl_unloadUnusedModules( NULL);
260 // Try to get a symbol, must fail
261 OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory"));
262 void* pSymbol= osl_getSymbol( handleMod, sSymbol.pData);
264 if( !pSymbol)
265 return sal_True;
266 return sal_False;
269 /* Multipe loadModule + rtl_registerModuleForUnloading.
270 The module will be registered one time less as it has been loaded.
272 sal_Bool test3()
274 printf("Test 3 ####################################################\n");
275 oslModule handleMod=0;
276 sal_Bool retval=sal_False;
277 OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
279 Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
280 OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
282 Reference<XInterface> xint= serviceManager->createInstance( OUString(
283 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1)));
285 handleMod= osl_loadModule( lib1Name.pData, 0);
286 osl_unloadModule( handleMod);
287 //-----------------------------------------------------------
288 oslModule mod1= osl_loadModule( lib1Name.pData, 0);
289 oslModule mod2= osl_loadModule( lib1Name.pData, 0);
290 oslModule mod3= osl_loadModule( lib1Name.pData, 0);
292 rtl_registerModuleForUnloading(mod1);
293 rtl_registerModuleForUnloading(mod2);
294 // ----------------------------------------------------------
295 Reference<XPropertySet> xSet( serviceManager, UNO_QUERY);
296 Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
297 Reference<XComponentContext> xContext;
298 any_prop >>= xContext;
299 Reference<XComponent> xComponent( xContext, UNO_QUERY);
300 xComponent->dispose();
302 rtl_unloadUnusedModules( NULL);
304 // Try to get a symbol, must succeed
305 OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory"));
306 void* pSymbol= osl_getSymbol( handleMod, sSymbol.pData);
308 if( pSymbol)
310 retval= sal_True;
311 osl_unloadModule( handleMod);
312 pSymbol= osl_getSymbol( handleMod, sSymbol.pData);
314 return retval;
316 /* 2 Modules
319 sal_Bool test4()
321 printf("Test 4 ####################################################\n");
322 oslModule handleMod1=0;
323 oslModule handleMod2=0;
324 OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
325 OUString lib2Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY2));
327 Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
328 OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
330 Reference<XInterface> xint= serviceManager->createInstance( OUString(
331 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1)));
333 handleMod1= osl_loadModule( lib1Name.pData, 0);
334 osl_unloadModule( handleMod1);
335 Reference<XInterface> xint2= serviceManager->createInstance( OUString(
336 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME21)));
338 handleMod2= osl_loadModule( lib2Name.pData, 0);
339 osl_unloadModule( handleMod2);
341 //-----------------------------------------------------------
342 // ----------------------------------------------------------
343 Reference<XPropertySet> xSet( serviceManager, UNO_QUERY);
344 Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
345 Reference<XComponentContext> xContext;
346 any_prop >>= xContext;
347 Reference<XComponent> xComponent( xContext, UNO_QUERY);
348 xComponent->dispose();
350 rtl_unloadUnusedModules( NULL);
352 // Try to get a symbol, must fail
353 OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory"));
354 void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData);
356 void* pSymbol2= osl_getSymbol( handleMod2, sSymbol.pData);
357 if( ! pSymbol && !pSymbol2)
358 return sal_True;
359 return sal_False;
362 /* 2 Modules and 6 services
365 sal_Bool test5()
367 printf("test5 ####################################################\n");
368 oslModule handleMod1=0;
369 oslModule handleMod2=0;
370 sal_Bool btest1= sal_False;
371 OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
372 OUString lib2Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY2));
373 OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory"));
375 Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
376 OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
378 //-----------------------------------------------------------
379 Reference<XInterface> xint= serviceManager->createInstance( OUString(
380 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1)));
381 Reference<XInterface> xint2= serviceManager->createInstance( OUString(
382 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME2)));
383 Reference<XInterface> xint3= serviceManager->createInstance( OUString(
384 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME3)));
385 Reference<XInterface> xint4= serviceManager->createInstance( OUString(
386 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME21)));
387 Reference<XInterface> xint5= serviceManager->createInstance( OUString(
388 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME22)));
389 Reference<XInterface> xint6= serviceManager->createInstance( OUString(
390 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME23)));
392 // ----------------------------------------------------------
393 handleMod1= osl_loadModule( lib1Name.pData, 0);
394 osl_unloadModule( handleMod1);
395 handleMod2= osl_loadModule( lib2Name.pData, 0);
396 osl_unloadModule( handleMod2);
398 // get rid of the service manager
399 Reference<XPropertySet> xSet( serviceManager, UNO_QUERY);
400 Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
401 Reference<XComponentContext> xContext;
402 any_prop >>= xContext;
403 Reference<XComponent> xComponent( xContext, UNO_QUERY);
404 xComponent->dispose();
406 // try unloading, must fail
407 rtl_unloadUnusedModules( NULL);
408 void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData);
410 void* pSymbol2= osl_getSymbol( handleMod2, sSymbol.pData);
411 if( pSymbol && pSymbol2)
412 btest1= sal_True;
416 // Try to get a symbol, must succeed
417 rtl_unloadUnusedModules( NULL);
419 void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData);
420 void* pSymbol2= osl_getSymbol( handleMod2, sSymbol.pData);
421 if( ! pSymbol && !pSymbol2 && btest1)
422 return sal_True;
423 return sal_False;
427 TimeValue Test
428 rtl_unloadUnusedModules takes a TimeValue which determines a timespan
429 a module must have been constantly unused in order to be unloaded.
430 This is only a rough test. To make accurate tests, one should directly
431 write code in the unload.cxx file.
432 The function will not return (loop) when the test fails or the result value
433 is far off the 10 seconds value.
435 sal_Bool test6()
437 printf("test6 ####################################################\n");
438 oslModule handleMod1=0;
439 oslModule handleMod2=0;
440 OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
441 OUString lib2Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY2));
442 OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory"));
444 Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
445 OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
446 Reference<XInterface> xint= serviceManager->createInstance( OUString(
447 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1)));
449 // ----------------------------------------------------------
450 handleMod1= osl_loadModule( lib1Name.pData, 0);
451 osl_unloadModule( handleMod1);
453 // get rid of the service manager
454 Reference<XPropertySet> xSet( serviceManager, UNO_QUERY);
455 Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
456 Reference<XComponentContext> xContext;
457 any_prop >>= xContext;
458 Reference<XComponent> xComponent( xContext, UNO_QUERY);
459 xComponent->dispose();
462 // Enter a loop and try unloading. At least after 10 seconds
463 // this should be successful.
464 TimeValue time={10,0};
466 TimeValue beforeTime={0,0};
467 printf("\n unloading should take about 10 seconds\n");
468 osl_getSystemTime( &beforeTime);
469 for(;;)
472 rtl_unloadUnusedModules( &time);
473 void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData);
474 if( ! pSymbol)
475 break;
477 TimeValue afterTime={0,0};
478 osl_getSystemTime( &afterTime);
480 printf("\n it took about %i seconds \n Check the value!!!", afterTime.Seconds - beforeTime.Seconds);
481 printf(" hit return to continue\n");
482 getchar();
484 return sal_True;
489 sal_Bool test7()
491 printf("Test 7 ####################################################"
492 "\nThe callback function should be called 3 times\n");
493 sal_Int32 id1=1;
494 sal_Int32 id2=2;
495 sal_Int32 id3=3;
496 sal_Int32 cookie1= rtl_addUnloadingListener( listenerCallback, &id1);
497 sal_Int32 cookie2= rtl_addUnloadingListener( listenerCallback, &id2);
498 sal_Int32 cookie3= rtl_addUnloadingListener( listenerCallback, &id3);
500 printf("\nTest 7 \nThe listener should be called 3 times\n");
501 rtl_unloadUnusedModules( NULL);
503 rtl_removeUnloadingListener( cookie1);
504 rtl_removeUnloadingListener( cookie2);
505 rtl_removeUnloadingListener( cookie3);
507 sal_Int32 cookie4= rtl_addUnloadingListener( listenerCallback, &id1);
508 sal_Int32 cookie5= rtl_addUnloadingListener( listenerCallback, &id2);
509 sal_Int32 cookie6= rtl_addUnloadingListener( listenerCallback, &id3);
511 if( cookie1 == cookie4 &&
512 cookie2 == cookie5 )
514 printf("\n###cookie recycling works\n");
515 printf("hit return to continue\n");
516 getchar();
518 else
520 printf("\n###cookie recycling failed!!!\n");
521 printf("hit return to continue\n");
522 getchar();
525 rtl_removeUnloadingListener( cookie1);
526 rtl_removeUnloadingListener( cookie2);
527 rtl_removeUnloadingListener( cookie3);
528 return sal_True;
531 /* Test one-instance-service default factory (XUnloadingPreference)
532 cppuhelper/source/factory.cxx
534 sal_Bool test8()
536 printf("Test 8 ####################################################\n");
537 oslModule handleMod1=0;
538 OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
539 OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory"));
541 sal_Bool b_ifaceSupported=sal_False;
542 sal_Bool b_instances_identical= sal_False;
543 sal_Bool b_releaseBeforeLoading= sal_False;
544 sal_Bool b_releaseAfterLoading= sal_False;
545 sal_Bool b_unloaded= sal_False;
548 Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
549 OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
550 Reference<XContentEnumerationAccess> xContent( serviceManager, UNO_QUERY);
551 Reference<XEnumeration> xenum= xContent->createContentEnumeration(
552 OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME4)));
554 Any any_elem;
555 if( xenum->hasMoreElements())
556 any_elem= xenum->nextElement();
557 Reference<XInterface> xinterfaceFact;
558 any_elem>>=xinterfaceFact;
559 Reference<XTypeProvider> xprov( xinterfaceFact, UNO_QUERY);
561 Sequence<Type> seqTypes= xprov->getTypes();
563 // XTypeProvider test
564 for( sal_Int32 i=0; i<seqTypes.getLength(); i++)
566 OUString name= seqTypes[i].getTypeName();
567 if( name == OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XUnloadingPreference")))
568 b_ifaceSupported= sal_True;
571 // XUnloadingPreference::releaseOnNotification should return true now because we haven't
572 // created an instance yet
573 Reference<XUnloadingPreference> xreject( xinterfaceFact, UNO_QUERY);
574 b_releaseBeforeLoading= xreject->releaseOnNotification();
576 // Create instance. Afterwards releaseOnNotification should return false.
577 Reference<XInterface> xint= serviceManager->createInstance( OUString(
578 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME4)));
579 b_releaseAfterLoading= xreject->releaseOnNotification();
580 b_releaseAfterLoading= b_releaseAfterLoading? sal_False : sal_True;
582 // safe the handle of the module
583 handleMod1= osl_loadModule( lib1Name.pData, 0);
584 osl_unloadModule( handleMod1);
586 // ----------------------------------------------------------
587 // for debugging
588 Reference<XServiceInfo> info( xint, UNO_QUERY);
589 OUString s= info->getImplementationName();
591 // get another instance which must be the same
592 Reference<XInterface> xint2= serviceManager->createInstance( OUString(
593 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME4)));
595 b_instances_identical= xint == xint2;
597 // get rid of the service manager
598 Reference<XPropertySet> xSet( serviceManager, UNO_QUERY);
599 Any any_prop= xSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
600 Reference<XComponentContext> xContext;
601 any_prop >>= xContext;
602 Reference<XComponent> xComponent( xContext, UNO_QUERY);
603 xComponent->dispose();
606 rtl_unloadUnusedModules( NULL);
608 // The library must be unloaded now
609 void* pSymbol= osl_getSymbol( handleMod1, sSymbol.pData);
610 if( ! pSymbol )
611 b_unloaded= sal_True;
613 if( b_ifaceSupported && b_instances_identical && b_releaseBeforeLoading &&
614 b_releaseAfterLoading && b_unloaded)
615 return sal_True;
616 return sal_False;
619 void SAL_CALL listenerCallback( void* id)
621 printf(" listener called with id= %i\n", *(sal_Int32*)id);
627 sal_Bool test9()
629 printf("Test 9 ####################################################\n");
630 oslModule handleMod=0;
631 sal_Bool retval=sal_False;
632 OUString lib1Name( RTL_CONSTASCII_USTRINGPARAM(LIBRARY1));
634 Reference<XMultiServiceFactory> serviceManager= createRegistryServiceFactory(
635 OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")));
637 Reference<XInterface> xint= serviceManager->createInstance( OUString(
638 RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1)));
639 // Release the service. The library refcount should be 1
640 xint=0;
642 handleMod= osl_loadModule( lib1Name.pData, 0);
643 osl_unloadModule( handleMod);
644 //-----------------------------------------------------------
646 // the service manager is still alive
647 rtl_unloadUnusedModules( NULL);
648 // Try to get a symbol, must fail
649 OUString sSymbol( RTL_CONSTASCII_USTRINGPARAM("component_getFactory"));
650 void* pSymbol= osl_getSymbol( handleMod, sSymbol.pData);
652 if( pSymbol)
654 retval= sal_False;
656 else
657 retval= sal_True;
658 return retval;