xref: /trunk/main/stoc/test/testsmgr_cpnt.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_stoc.hxx"
30 #include <rtl/alloc.h>
31 #include <osl/security.h>
32 #include <osl/thread.h>
33 #include <osl/mutex.hxx>
34 #include <cppuhelper/queryinterface.hxx>
35 #include <cppuhelper/weak.hxx>
36 #include <uno/mapping.hxx>
37 
38 
39 #include <cppuhelper/factory.hxx>
40 #include <cppuhelper/servicefactory.hxx>
41 #include <cppuhelper/implbase1.hxx>
42 #include <registry/registry.hxx>
43 
44 #include <com/sun/star/registry/XSimpleRegistry.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 #include <com/sun/star/registry/XImplementationRegistration.hpp>
48 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
49 #include <com/sun/star/lang/XComponent.hpp>
50 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
51 #include <com/sun/star/container/XEnumerationAccess.hpp>
52 #include <com/sun/star/container/XEnumeration.hpp>
53 
54 #if defined ( UNX )
55 #include <limits.h>
56 #define _MAX_PATH PATH_MAX
57 #endif
58 
59 
60 
61 #if OSL_DEBUG_LEVEL > 0
62 #define TEST_ENSHURE(c, m)   OSL_ENSURE(c, m)
63 #else
64 #define TEST_ENSHURE(c, m)   OSL_VERIFY(c)
65 #endif
66 
67 
68 #define IMPLEMENTATION_NAME "com.sun.star.DummyService.V10"
69 #define SERVICE_NAME "com.sun.star.ts.TestManagerImpl"
70 
71 
72 using namespace com::sun::star::uno;
73 using namespace com::sun::star::registry;
74 using namespace com::sun::star::lang;
75 using namespace com::sun::star::container;
76 using namespace osl;
77 using namespace rtl;
78 using namespace cppu;
79 
80 
81 Reference<XMultiServiceFactory> getProcessServiceManager()
82 {
83     Reference<XMultiServiceFactory > s_x;
84     if (! s_x.is())
85     {
86         MutexGuard aGuard( Mutex::getGlobalMutex() );
87         if (! s_x.is())
88             s_x = createRegistryServiceFactory( OUString::createFromAscii( "stoctest.rdb" ), sal_False );
89     }
90     return s_x;
91 }
92 
93 Reference< XMultiServiceFactory > createRegistryServiceManager( const OUString& registryName )
94 {
95     return createRegistryServiceFactory( registryName );
96 }
97 
98 
99 /**********************************
100 * The service, that is used to test the Service manager
101 *
102 *
103 *
104 *************************************/
105 static sal_uInt32 nInstanceCount = 0;
106 class Test_Manager_Impl : public WeakImplHelper1< XServiceInfo >
107 {
108 public:
109     Test_Manager_Impl(){ nInstanceCount++;}
110     ~Test_Manager_Impl();
111 
112     // XServiceInfo
113     OUString                    SAL_CALL getImplementationName() throw();
114     sal_Bool                    SAL_CALL supportsService(const OUString& ServiceName) throw();
115     Sequence< OUString >        SAL_CALL getSupportedServiceNames(void) throw();
116     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(void) throw();
117 
118 private:
119 //  static XIdlClassRef     getStaticIdlClass();
120 };
121 
122 Test_Manager_Impl::~Test_Manager_Impl()
123 {
124     nInstanceCount--;
125 }
126 
127 
128 // alt, wird von der neuen Mimic nicht mehr gebraucht
129 Reference< XInterface > SAL_CALL Test_Manager_Impl_CreateInstance_Impl()
130 {
131     return (OWeakObject *)new Test_Manager_Impl();
132 }
133 
134 
135 //*************************************************************************
136 // Test_Manager_Impl_CreateInstance()
137 //
138 Reference < XInterface > SAL_CALL Test_Manager_Impl_CreateInstance(
139     const Reference< XMultiServiceFactory > & /*rSMgr*/ ) throw (Exception)
140 {
141     Reference < XInterface >  xService = (XWeak *)(OWeakObject *)new Test_Manager_Impl( );
142 
143     return xService;
144 }
145 
146 
147 //*************************************************************************
148 // Test_Manager_Impl::getImplementationName
149 //
150 OUString Test_Manager_Impl::getImplementationName() throw()
151 {
152     return OUString::createFromAscii(IMPLEMENTATION_NAME);
153 }
154 
155 //*************************************************************************
156 // Test_Manager_Impl::supportsService
157 //
158 sal_Bool Test_Manager_Impl::supportsService( const OUString& ServiceName ) throw()
159 {
160     Sequence< OUString > aSNL = getSupportedServiceNames();
161     const OUString * pArray = aSNL.getConstArray();
162     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
163         if( pArray[i] == ServiceName )
164             return sal_True;
165     return sal_False;
166 }
167 
168 //*************************************************************************
169 // Test_Manager_Impl::getSupportedServiceNames
170 //
171 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames(void) throw ()
172 {
173     return getSupportedServiceNames_Static();
174 }
175 
176 //*************************************************************************
177 // Test_Manager_Impl::getSupportedServiceNames_Static
178 //
179 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames_Static(void) throw ()
180 {
181     Sequence< OUString > aSNS( 2 );
182     aSNS.getArray()[0] = OUString::createFromAscii(SERVICE_NAME);
183     aSNS.getArray()[1] = OUString::createFromAscii("com.sun.star.bridge.Bridge");
184     return aSNS;
185 }
186 
187 
188 
189 
190 /****
191 *
192 *
193 *  This routine performs the test of the process service manager ( getProcessServiceManager is called )
194 *
195 *
196 *
197 ****/
198 
199 #include <stdio.h>
200 
201 extern "C" void SAL_CALL test_ServiceManager()
202 {
203 #if ! defined SAL_DLLPREFIX
204 #define SAL_DLLPREFIX ""
205 #endif
206     OUString atUModule2 = OUString(
207         RTL_CONSTASCII_USTRINGPARAM(
208             SAL_DLLPREFIX "testsmgr_component" SAL_DLLEXTENSION ) );
209 
210     // expand shared library name
211     OString  atModule2( OUStringToOString(atUModule2, RTL_TEXTENCODING_ASCII_US) );
212 
213     // get the process servicemanager
214     Reference <XMultiServiceFactory>  xSMgr = getProcessServiceManager();
215 
216     TEST_ENSHURE( xSMgr.is() , "query on XServiceManager failed" );
217 
218     Reference<XContentEnumerationAccess> xContEnum(xSMgr, UNO_QUERY);
219     TEST_ENSHURE( xContEnum.is() , "query on XContentEnumerationAccess failed" );
220     Reference<XEnumeration > xEnum(xContEnum->createContentEnumeration(OUString::createFromAscii("com.sun.star.registry.SimpleRegistry")));
221     TEST_ENSHURE( xEnum.is() , "createContentEnumeration failed" );
222     sal_Int32 nLen = 0;
223     while( xEnum->hasMoreElements() )
224     {
225         nLen++;
226         xEnum->nextElement();
227     }
228     TEST_ENSHURE( nLen == 1, "more than one implementation for SimpleRegistry" );
229 
230     Reference<XEnumerationAccess> xImplEnum(xSMgr, UNO_QUERY);
231     TEST_ENSHURE( xImplEnum.is() , "query on XEnumeration failed" );
232     xEnum = Reference<XEnumeration >(xImplEnum->createEnumeration());
233     TEST_ENSHURE( xEnum.is() , "createEnumeration failed" );
234     nLen = 0;
235     while( xEnum->hasMoreElements() )
236     {
237         nLen++;
238         Reference< XServiceInfo > sf( xEnum->nextElement(), UNO_QUERY );
239         OString str( OUStringToOString( sf->getImplementationName(), RTL_TEXTENCODING_ASCII_US ) );
240         ::fprintf( stderr, "> implementation name: %s\n", str.getStr() );
241     }
242     TEST_ENSHURE( nLen == 8, "more than 6 factories" );
243 
244     // try to get an instance for a unknown service
245     TEST_ENSHURE( !xSMgr->createInstance(OUString::createFromAscii("bla.blup.Q")).is(), "unknown service provider found" );
246 
247     //
248     // First test : register service via the internal function of the component itself
249     //
250     {
251         Reference< XImplementationRegistration >
252             xInst( xSMgr->createInstance(OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration")), UNO_QUERY );
253         TEST_ENSHURE( xInst.is(), "no ImplementationRegistration" );
254 
255         try {
256             // register the services via writeComponentRegInfo (see at end of this file)
257             xInst->registerImplementation(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), atUModule2, Reference< XSimpleRegistry >() );
258         }
259         catch( CannotRegisterImplementationException e ) {
260             TEST_ENSHURE( 0, "register implementation failed" );
261         }
262 
263         // getImplementations() check
264         Sequence<OUString> seqImpl = xInst->getImplementations(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), atUModule2);
265         TEST_ENSHURE( seqImpl.getLength() == 1, "count of implementantions is wrong" );
266         TEST_ENSHURE( seqImpl.getConstArray()[0] == OUString::createFromAscii("com.sun.star.DummyService.V10"), "implementation name is not equal" );
267 
268 
269         // tests, if a service provider can be instantiated.
270 
271         Reference< XInterface > xIFace(xSMgr->createInstance(OUString::createFromAscii("com.sun.star.ts.TestManagerImpl")));
272         TEST_ENSHURE( xIFace.is(), "loadable service not found" );
273 
274         // remove the service
275         TEST_ENSHURE(   xInst->revokeImplementation(atUModule2, Reference< XSimpleRegistry > ()),
276                         "revoke implementation failed" );
277     }
278 
279 
280 //  Reference < XSimpleRegistry > xSReg(  xSMgr->createInstance(OUString::createFromAscii("com::sun::star.uno.repos.SimpleRegistry")), UNO_QUERY );
281 //  TEST_ENSHURE( xSReg.is() , "Simple registry couldn't be instantiated" );
282 
283 //  xSReg->open(OUString::createFromAscii("dummy.reg"), sal_False, sal_True);
284 //  xSReg->close();
285 
286     // laut dbo : C-API muss nicht mehr unterstuezt werden
287     //TEST_ENSHURE( registerExternService(atModule, "dummy.reg"), "install failed" );
288     //TEST_ENSHURE( deregisterExternService(atModule, "dummy.reg"), "deinstall failed" );
289 
290 
291 //  UNO_INTERFACE(XMultiServiceFactory) xUnoSMgr = {0,0};
292 //  smart2uno(xSMgr, xUnoSMgr);
293 
294 //  TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.SharedLibrary", atModule2, "dummy.reg"), "install failed" );
295 //  TEST_ENSHURE(revokeExternImplementation(xUnoSMgr, atModule2, "dummy.reg"), "deinstall failed" );
296 
297 //  TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.SharedLibrary", atModule2, "dummy2.reg"), "install failed" );
298 
299 //TODO : Java loader test
300 //  String testUrl(getTestJarUrl());
301 //  TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.Java", testUrl, "dummy.reg"), "install failed" );
302 //  TEST_ENSHURE(revokeExternImplementation(xUnoSMgr, testUrl, "dummy.reg"), "deinstall failed" );
303 
304 //  if (!UNO_isNull((UNO_Ifc*)&xUnoSMgr))
305 //      xUnoSMgr.m_pVmt->release(xUnoSMgr.m_pCtx);
306 
307 //  xSReg->open(OUString::createFromAscii("dummy.reg"), sal_True, sal_False);
308 //  TEST_ENSHURE(!xSReg->getRootKey()->openKey(OUString::createFromAscii("/SERVICES/com::sun::star/ts/TestManagerImpl/URL")).is(),
309 //              "deinstall failed" );
310 
311 //  xSReg->close();
312 
313 //  xSReg->open(OUString::createFromAscii("dummy.reg"), sal_False, sal_False);
314 //  xSReg->destroy();
315 //  xSReg->open(OUString::createFromAscii("dummy2.reg"), sal_False, sal_False);
316 //  xSReg->destroy();
317 
318 
319     // Test initialisieren
320 /*
321     XServiceProviderRef xSiSP1 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"), Test_Manager_Impl_getReflection );
322     XServiceProviderRef xSiSP11 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"), Test_Manager_Impl_getReflection );
323     XServiceProviderRef xSiSP2 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl2"), Test_Manager_Impl_getReflection );
324 */
325 /*
326     // second test
327     // create XServiceProvider via createSingleFactory and write them directly into the registry
328     // For this is needed a sequence of supported servicenames and a createComponent function pointer
329     {
330         Reference< XServiceProvider > xSiSP1(createSingleFactory(
331             xSMgr,
332             OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1),
333             Test_Manager_Impl_CreateInstance,
334             Test_Manager_Impl::getSupportedServiceNames_Static() ), UNO_QUERY);
335         Reference< XServiceProvider > xSiSP11(createSingleFactory(
336             xSMgr,
337             OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"),
338             Test_Manager_Impl_CreateInstance,
339             Test_Manager_Impl::getSupportedServiceNames_Static() ),UNO_QUERY);
340         Reference< XServiceProvider > xSiSP2(createSingleFactory(
341             xSMgr,
342             L"com::sun::star.usr.Test_Manager_Impl2",
343             Test_Manager_Impl_CreateInstance,
344             Test_Manager_Impl::getSupportedServiceNames_Static() ), UNO_QUERY);
345 
346         // put the service providers into the registry
347         xReg->registerServiceProvider( L"com::sun::star.test.TestManager1", xSiSP1 );
348         xReg->registerServiceProvider( L"com::sun::star.test.TestManager1", xSiSP11 );
349         xReg->registerServiceProvider( L"com::sun::star.test.TestManager2", xSiSP2 );
350 
351         // TestManager1
352         Reference< XServiceProvider > xProv = xSMgr->queryServiceProvider( L"com::sun::star.test.TestManager1");
353         Reference< XSingleServiceFactory > xFact( xProv, UNO_QUERY );
354         TEST_ENSHURE( xFact.is(), "Service com::sun::star.test.TestManager1 not found" );
355 
356         Reference< XInterface > xTest1 = xFact->createInstance();
357         TEST_ENSHURE( nInstanceCount == 1, "wrong service instanciated" );
358 
359         // TestManager2
360         xProv = xSMgr->queryServiceProvider( L"com::sun::star.test.TestManager2");
361         xFact = Reference < XSingleServiceFactory > ( xProv , UNO_QUERY );
362         TEST_ENSHURE( xFact.is(), "Service com::sun::star.test.TestManager2 not found" );
363 
364         Reference < XInterface > xTest2 = xFact->createInstance();
365         TEST_ENSHURE( nInstanceCount == 2, "wrong service instanciated" );
366 
367         xTest1 = Reference< XInterface >();
368         xTest2 = Reference< XInterface >();
369         TEST_ENSHURE( nInstanceCount == 0, "wrong service deleted" );
370 
371         Reference< XEnumeration > xEnum = xSMgr->createProviderEnumeration( L"com::sun::star.test.TestManager1");
372         TEST_ENSHURE( xEnum.is() , "no provider enumeration" );
373 
374         sal_Int32 nSPTestManagerImplLen2 = 0;
375 
376         while (xEnum.is() && xEnum->hasMoreElements())
377         {
378             nSPTestManagerImplLen2++;
379             xEnum->nextElement();
380 
381         }
382         TEST_ENSHURE( nSPTestManagerImplLen2 == 2, "queryServiceProviders() wrong length" );
383 
384         sal_Int32 nCount = 0;
385         xEnum = xSMgr->createProviderEnumeration( L"com::sun::star.test.TestManager1");
386         while (xEnum->hasMoreElements())
387         {
388             Reference< XServiceProvider > xProv;
389             xEnum->nextElement() >>= xProv;
390             if (xProv == xSiSP1 || xProv == xSiSP11)
391                 nCount++;
392         }
393 
394         TEST_ENSHURE( nCount == 2 , "not all com::sun::star.testimpl.TestManagerImpl registered" );
395 */
396 /*
397     {
398         Reference< XMultiServiceFactory > xTestManager(createRegistryServiceManager(L"testmanager.rdb"));
399         TEST_ENSHURE( xTestManager.is(), "create Test ServiceManager failed!" );
400     }
401 */
402     Reference<XComponent> xComp(xSMgr, UNO_QUERY);
403     xComp->dispose();
404 
405     xComp.clear();
406     xSMgr.clear();
407 }
408 
409 
410 
411 extern "C"
412 {
413 //==================================================================================================
414 void SAL_CALL component_getImplementationEnvironment(
415     const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
416 {
417     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
418 }
419 //==================================================================================================
420 sal_Bool SAL_CALL component_writeInfo(
421     void * /*pServiceManager*/, void * pRegistryKey )
422 {
423     if (pRegistryKey)
424     {
425         try
426         {
427             Reference< XRegistryKey > xNewKey(
428                 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
429                     OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLEMENTATION_NAME "/UNO/SERVICES") ) ) );
430 
431             const Sequence< OUString > & rSNL =
432                 Test_Manager_Impl::getSupportedServiceNames_Static();
433             const OUString * pArray = rSNL.getConstArray();
434             for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
435                 xNewKey->createKey( pArray[nPos] );
436 
437             return sal_True;
438         }
439         catch (InvalidRegistryException &)
440         {
441             OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
442         }
443     }
444     return sal_False;
445 }
446 //==================================================================================================
447 void * SAL_CALL component_getFactory(
448     const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
449 {
450     void * pRet = 0;
451 
452     if (rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
453     {
454         Reference< XSingleServiceFactory > xFactory( createSingleFactory(
455             reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
456             OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLEMENTATION_NAME) ),
457             Test_Manager_Impl_CreateInstance,
458             Test_Manager_Impl::getSupportedServiceNames_Static() ) );
459 
460         if (xFactory.is())
461         {
462             xFactory->acquire();
463             pRet = xFactory.get();
464         }
465     }
466 
467     return pRet;
468 }
469 }
470 
471 
472 
473