update dev300-m58
[ooovba.git] / stoc / test / testsmgr_cpnt.cxx
blobd4bb8ffbeec454624409130aae5637aa6ba87a2c
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: testsmgr_cpnt.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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_stoc.hxx"
33 #include <rtl/alloc.h>
34 #include <osl/security.h>
35 #include <osl/thread.h>
36 #include <osl/mutex.hxx>
37 #include <cppuhelper/queryinterface.hxx>
38 #include <cppuhelper/weak.hxx>
39 #include <uno/mapping.hxx>
42 #include <cppuhelper/factory.hxx>
43 #include <cppuhelper/servicefactory.hxx>
44 #include <cppuhelper/implbase1.hxx>
45 #include <registry/registry.hxx>
47 #include <com/sun/star/registry/XSimpleRegistry.hpp>
48 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
49 #include <com/sun/star/lang/XServiceInfo.hpp>
50 #include <com/sun/star/registry/XImplementationRegistration.hpp>
51 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
52 #include <com/sun/star/lang/XComponent.hpp>
53 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
54 #include <com/sun/star/container/XEnumerationAccess.hpp>
55 #include <com/sun/star/container/XEnumeration.hpp>
57 #if defined ( UNX )
58 #include <limits.h>
59 #define _MAX_PATH PATH_MAX
60 #endif
64 #if OSL_DEBUG_LEVEL > 0
65 #define TEST_ENSHURE(c, m) OSL_ENSURE(c, m)
66 #else
67 #define TEST_ENSHURE(c, m) OSL_VERIFY(c)
68 #endif
71 #define IMPLEMENTATION_NAME "com.sun.star.DummyService.V10"
72 #define SERVICE_NAME "com.sun.star.ts.TestManagerImpl"
75 using namespace com::sun::star::uno;
76 using namespace com::sun::star::registry;
77 using namespace com::sun::star::lang;
78 using namespace com::sun::star::container;
79 using namespace osl;
80 using namespace rtl;
81 using namespace cppu;
84 Reference<XMultiServiceFactory> getProcessServiceManager()
86 Reference<XMultiServiceFactory > s_x;
87 if (! s_x.is())
89 MutexGuard aGuard( Mutex::getGlobalMutex() );
90 if (! s_x.is())
91 s_x = createRegistryServiceFactory( OUString::createFromAscii( "stoctest.rdb" ), sal_False );
93 return s_x;
96 Reference< XMultiServiceFactory > createRegistryServiceManager( const OUString& registryName )
98 return createRegistryServiceFactory( registryName );
102 /**********************************
103 * The service, that is used to test the Service manager
107 *************************************/
108 static sal_uInt32 nInstanceCount = 0;
109 class Test_Manager_Impl : public WeakImplHelper1< XServiceInfo >
111 public:
112 Test_Manager_Impl(){ nInstanceCount++;}
113 ~Test_Manager_Impl();
115 // XServiceInfo
116 OUString SAL_CALL getImplementationName() throw();
117 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
118 Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
119 static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(void) throw();
121 private:
122 // static XIdlClassRef getStaticIdlClass();
125 Test_Manager_Impl::~Test_Manager_Impl()
127 nInstanceCount--;
131 // alt, wird von der neuen Mimic nicht mehr gebraucht
132 Reference< XInterface > SAL_CALL Test_Manager_Impl_CreateInstance_Impl()
134 return (OWeakObject *)new Test_Manager_Impl();
138 //*************************************************************************
139 // Test_Manager_Impl_CreateInstance()
141 Reference < XInterface > SAL_CALL Test_Manager_Impl_CreateInstance(
142 const Reference< XMultiServiceFactory > & /*rSMgr*/ ) throw (Exception)
144 Reference < XInterface > xService = (XWeak *)(OWeakObject *)new Test_Manager_Impl( );
146 return xService;
150 //*************************************************************************
151 // Test_Manager_Impl::getImplementationName
153 OUString Test_Manager_Impl::getImplementationName() throw()
155 return OUString::createFromAscii(IMPLEMENTATION_NAME);
158 //*************************************************************************
159 // Test_Manager_Impl::supportsService
161 sal_Bool Test_Manager_Impl::supportsService( const OUString& ServiceName ) throw()
163 Sequence< OUString > aSNL = getSupportedServiceNames();
164 const OUString * pArray = aSNL.getConstArray();
165 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
166 if( pArray[i] == ServiceName )
167 return sal_True;
168 return sal_False;
171 //*************************************************************************
172 // Test_Manager_Impl::getSupportedServiceNames
174 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames(void) throw ()
176 return getSupportedServiceNames_Static();
179 //*************************************************************************
180 // Test_Manager_Impl::getSupportedServiceNames_Static
182 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames_Static(void) throw ()
184 Sequence< OUString > aSNS( 2 );
185 aSNS.getArray()[0] = OUString::createFromAscii(SERVICE_NAME);
186 aSNS.getArray()[1] = OUString::createFromAscii("com.sun.star.bridge.Bridge");
187 return aSNS;
193 /****
196 * This routine performs the test of the process service manager ( getProcessServiceManager is called )
200 ****/
202 #include <stdio.h>
204 extern "C" void SAL_CALL test_ServiceManager()
206 #if ! defined SAL_DLLPREFIX
207 #define SAL_DLLPREFIX ""
208 #endif
209 OUString atUModule2 = OUString(
210 RTL_CONSTASCII_USTRINGPARAM(
211 SAL_DLLPREFIX "testsmgr_component" SAL_DLLEXTENSION ) );
213 // expand shared library name
214 OString atModule2( OUStringToOString(atUModule2, RTL_TEXTENCODING_ASCII_US) );
216 // get the process servicemanager
217 Reference <XMultiServiceFactory> xSMgr = getProcessServiceManager();
219 TEST_ENSHURE( xSMgr.is() , "query on XServiceManager failed" );
221 Reference<XContentEnumerationAccess> xContEnum(xSMgr, UNO_QUERY);
222 TEST_ENSHURE( xContEnum.is() , "query on XContentEnumerationAccess failed" );
223 Reference<XEnumeration > xEnum(xContEnum->createContentEnumeration(OUString::createFromAscii("com.sun.star.registry.SimpleRegistry")));
224 TEST_ENSHURE( xEnum.is() , "createContentEnumeration failed" );
225 sal_Int32 nLen = 0;
226 while( xEnum->hasMoreElements() )
228 nLen++;
229 xEnum->nextElement();
231 TEST_ENSHURE( nLen == 1, "more than one implementation for SimpleRegistry" );
233 Reference<XEnumerationAccess> xImplEnum(xSMgr, UNO_QUERY);
234 TEST_ENSHURE( xImplEnum.is() , "query on XEnumeration failed" );
235 xEnum = Reference<XEnumeration >(xImplEnum->createEnumeration());
236 TEST_ENSHURE( xEnum.is() , "createEnumeration failed" );
237 nLen = 0;
238 while( xEnum->hasMoreElements() )
240 nLen++;
241 Reference< XServiceInfo > sf( xEnum->nextElement(), UNO_QUERY );
242 OString str( OUStringToOString( sf->getImplementationName(), RTL_TEXTENCODING_ASCII_US ) );
243 ::fprintf( stderr, "> implementation name: %s\n", str.getStr() );
245 TEST_ENSHURE( nLen == 8, "more than 6 factories" );
247 // try to get an instance for a unknown service
248 TEST_ENSHURE( !xSMgr->createInstance(OUString::createFromAscii("bla.blup.Q")).is(), "unknown service provider found" );
251 // First test : register service via the internal function of the component itself
254 Reference< XImplementationRegistration >
255 xInst( xSMgr->createInstance(OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration")), UNO_QUERY );
256 TEST_ENSHURE( xInst.is(), "no ImplementationRegistration" );
258 try {
259 // register the services via writeComponentRegInfo (see at end of this file)
260 xInst->registerImplementation(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), atUModule2, Reference< XSimpleRegistry >() );
262 catch( CannotRegisterImplementationException e ) {
263 TEST_ENSHURE( 0, "register implementation failed" );
266 // getImplementations() check
267 Sequence<OUString> seqImpl = xInst->getImplementations(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), atUModule2);
268 TEST_ENSHURE( seqImpl.getLength() == 1, "count of implementantions is wrong" );
269 TEST_ENSHURE( seqImpl.getConstArray()[0] == OUString::createFromAscii("com.sun.star.DummyService.V10"), "implementation name is not equal" );
272 // tests, if a service provider can be instantiated.
274 Reference< XInterface > xIFace(xSMgr->createInstance(OUString::createFromAscii("com.sun.star.ts.TestManagerImpl")));
275 TEST_ENSHURE( xIFace.is(), "loadable service not found" );
277 // remove the service
278 TEST_ENSHURE( xInst->revokeImplementation(atUModule2, Reference< XSimpleRegistry > ()),
279 "revoke implementation failed" );
283 // Reference < XSimpleRegistry > xSReg( xSMgr->createInstance(OUString::createFromAscii("com::sun::star.uno.repos.SimpleRegistry")), UNO_QUERY );
284 // TEST_ENSHURE( xSReg.is() , "Simple registry couldn't be instantiated" );
286 // xSReg->open(OUString::createFromAscii("dummy.reg"), sal_False, sal_True);
287 // xSReg->close();
289 // laut dbo : C-API muss nicht mehr unterstuezt werden
290 //TEST_ENSHURE( registerExternService(atModule, "dummy.reg"), "install failed" );
291 //TEST_ENSHURE( deregisterExternService(atModule, "dummy.reg"), "deinstall failed" );
294 // UNO_INTERFACE(XMultiServiceFactory) xUnoSMgr = {0,0};
295 // smart2uno(xSMgr, xUnoSMgr);
297 // TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.SharedLibrary", atModule2, "dummy.reg"), "install failed" );
298 // TEST_ENSHURE(revokeExternImplementation(xUnoSMgr, atModule2, "dummy.reg"), "deinstall failed" );
300 // TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.SharedLibrary", atModule2, "dummy2.reg"), "install failed" );
302 //TODO : Java loader test
303 // String testUrl(getTestJarUrl());
304 // TEST_ENSHURE(registerExternImplementation(xUnoSMgr, "com::sun::star.loader.Java", testUrl, "dummy.reg"), "install failed" );
305 // TEST_ENSHURE(revokeExternImplementation(xUnoSMgr, testUrl, "dummy.reg"), "deinstall failed" );
307 // if (!UNO_isNull((UNO_Ifc*)&xUnoSMgr))
308 // xUnoSMgr.m_pVmt->release(xUnoSMgr.m_pCtx);
310 // xSReg->open(OUString::createFromAscii("dummy.reg"), sal_True, sal_False);
311 // TEST_ENSHURE(!xSReg->getRootKey()->openKey(OUString::createFromAscii("/SERVICES/com::sun::star/ts/TestManagerImpl/URL")).is(),
312 // "deinstall failed" );
314 // xSReg->close();
316 // xSReg->open(OUString::createFromAscii("dummy.reg"), sal_False, sal_False);
317 // xSReg->destroy();
318 // xSReg->open(OUString::createFromAscii("dummy2.reg"), sal_False, sal_False);
319 // xSReg->destroy();
322 // Test initialisieren
324 XServiceProviderRef xSiSP1 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"), Test_Manager_Impl_getReflection );
325 XServiceProviderRef xSiSP11 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"), Test_Manager_Impl_getReflection );
326 XServiceProviderRef xSiSP2 = createSimpleServiceProvider( OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl2"), Test_Manager_Impl_getReflection );
329 // second test
330 // create XServiceProvider via createSingleFactory and write them directly into the registry
331 // For this is needed a sequence of supported servicenames and a createComponent function pointer
333 Reference< XServiceProvider > xSiSP1(createSingleFactory(
334 xSMgr,
335 OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1),
336 Test_Manager_Impl_CreateInstance,
337 Test_Manager_Impl::getSupportedServiceNames_Static() ), UNO_QUERY);
338 Reference< XServiceProvider > xSiSP11(createSingleFactory(
339 xSMgr,
340 OUString::createFromAscii("com::sun::star.usr.Test_Manager_Impl1"),
341 Test_Manager_Impl_CreateInstance,
342 Test_Manager_Impl::getSupportedServiceNames_Static() ),UNO_QUERY);
343 Reference< XServiceProvider > xSiSP2(createSingleFactory(
344 xSMgr,
345 L"com::sun::star.usr.Test_Manager_Impl2",
346 Test_Manager_Impl_CreateInstance,
347 Test_Manager_Impl::getSupportedServiceNames_Static() ), UNO_QUERY);
349 // put the service providers into the registry
350 xReg->registerServiceProvider( L"com::sun::star.test.TestManager1", xSiSP1 );
351 xReg->registerServiceProvider( L"com::sun::star.test.TestManager1", xSiSP11 );
352 xReg->registerServiceProvider( L"com::sun::star.test.TestManager2", xSiSP2 );
354 // TestManager1
355 Reference< XServiceProvider > xProv = xSMgr->queryServiceProvider( L"com::sun::star.test.TestManager1");
356 Reference< XSingleServiceFactory > xFact( xProv, UNO_QUERY );
357 TEST_ENSHURE( xFact.is(), "Service com::sun::star.test.TestManager1 not found" );
359 Reference< XInterface > xTest1 = xFact->createInstance();
360 TEST_ENSHURE( nInstanceCount == 1, "wrong service instanciated" );
362 // TestManager2
363 xProv = xSMgr->queryServiceProvider( L"com::sun::star.test.TestManager2");
364 xFact = Reference < XSingleServiceFactory > ( xProv , UNO_QUERY );
365 TEST_ENSHURE( xFact.is(), "Service com::sun::star.test.TestManager2 not found" );
367 Reference < XInterface > xTest2 = xFact->createInstance();
368 TEST_ENSHURE( nInstanceCount == 2, "wrong service instanciated" );
370 xTest1 = Reference< XInterface >();
371 xTest2 = Reference< XInterface >();
372 TEST_ENSHURE( nInstanceCount == 0, "wrong service deleted" );
374 Reference< XEnumeration > xEnum = xSMgr->createProviderEnumeration( L"com::sun::star.test.TestManager1");
375 TEST_ENSHURE( xEnum.is() , "no provider enumeration" );
377 sal_Int32 nSPTestManagerImplLen2 = 0;
379 while (xEnum.is() && xEnum->hasMoreElements())
381 nSPTestManagerImplLen2++;
382 xEnum->nextElement();
385 TEST_ENSHURE( nSPTestManagerImplLen2 == 2, "queryServiceProviders() wrong length" );
387 sal_Int32 nCount = 0;
388 xEnum = xSMgr->createProviderEnumeration( L"com::sun::star.test.TestManager1");
389 while (xEnum->hasMoreElements())
391 Reference< XServiceProvider > xProv;
392 xEnum->nextElement() >>= xProv;
393 if (xProv == xSiSP1 || xProv == xSiSP11)
394 nCount++;
397 TEST_ENSHURE( nCount == 2 , "not all com::sun::star.testimpl.TestManagerImpl registered" );
401 Reference< XMultiServiceFactory > xTestManager(createRegistryServiceManager(L"testmanager.rdb"));
402 TEST_ENSHURE( xTestManager.is(), "create Test ServiceManager failed!" );
405 Reference<XComponent> xComp(xSMgr, UNO_QUERY);
406 xComp->dispose();
408 xComp.clear();
409 xSMgr.clear();
414 extern "C"
416 //==================================================================================================
417 void SAL_CALL component_getImplementationEnvironment(
418 const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
420 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
422 //==================================================================================================
423 sal_Bool SAL_CALL component_writeInfo(
424 void * /*pServiceManager*/, void * pRegistryKey )
426 if (pRegistryKey)
430 Reference< XRegistryKey > xNewKey(
431 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
432 OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLEMENTATION_NAME "/UNO/SERVICES") ) ) );
434 const Sequence< OUString > & rSNL =
435 Test_Manager_Impl::getSupportedServiceNames_Static();
436 const OUString * pArray = rSNL.getConstArray();
437 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
438 xNewKey->createKey( pArray[nPos] );
440 return sal_True;
442 catch (InvalidRegistryException &)
444 OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
447 return sal_False;
449 //==================================================================================================
450 void * SAL_CALL component_getFactory(
451 const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
453 void * pRet = 0;
455 if (rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
457 Reference< XSingleServiceFactory > xFactory( createSingleFactory(
458 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
459 OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLEMENTATION_NAME) ),
460 Test_Manager_Impl_CreateInstance,
461 Test_Manager_Impl::getSupportedServiceNames_Static() ) );
463 if (xFactory.is())
465 xFactory->acquire();
466 pRet = xFactory.get();
470 return pRet;