Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / stoc / test / testsmgr_cpnt.cxx
blob638ad81c1ccece4fd58282dfa56cf5ad60b9cf10
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 #include <rtl/alloc.h>
21 #include <osl/security.h>
22 #include <osl/thread.h>
23 #include <osl/mutex.hxx>
24 #include <cppuhelper/queryinterface.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <cppuhelper/weak.hxx>
27 #include <uno/mapping.hxx>
30 #include <cppuhelper/factory.hxx>
31 #include <cppuhelper/servicefactory.hxx>
32 #include <cppuhelper/implbase.hxx>
33 #include <registry/registry.hxx>
35 #include <com/sun/star/registry/XSimpleRegistry.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/registry/XImplementationRegistration.hpp>
39 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
40 #include <com/sun/star/lang/XComponent.hpp>
41 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
42 #include <com/sun/star/container/XEnumerationAccess.hpp>
43 #include <com/sun/star/container/XEnumeration.hpp>
45 #include <stdio.h>
47 #if defined ( UNX )
48 #include <limits.h>
49 #define _MAX_PATH PATH_MAX
50 #endif
53 #define IMPLEMENTATION_NAME "com.sun.star.DummyService.V10"
54 #define SERVICE_NAME "com.sun.star.ts.TestManagerImpl"
57 using namespace css::uno;
58 using namespace css::registry;
59 using namespace css::lang;
60 using namespace css::container;
61 using namespace osl;
62 using namespace cppu;
65 Reference<XMultiServiceFactory> getProcessServiceManager()
67 Reference<XMultiServiceFactory > s_x;
68 if (! s_x.is())
70 MutexGuard aGuard( Mutex::getGlobalMutex() );
71 if (! s_x.is())
72 s_x = createRegistryServiceFactory( OUString("stoctest.rdb"), sal_False );
74 return s_x;
77 Reference< XMultiServiceFactory > createRegistryServiceManager( const OUString& registryName )
79 return createRegistryServiceFactory( registryName );
83 /**********************************
84 * The service, that is used to test the Service manager
88 *************************************/
89 static sal_uInt32 nInstanceCount = 0;
90 class Test_Manager_Impl : public WeakImplHelper< XServiceInfo >
92 public:
93 Test_Manager_Impl(){ nInstanceCount++;}
94 ~Test_Manager_Impl();
96 // XServiceInfo
97 OUString SAL_CALL getImplementationName() throw();
98 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
99 Sequence< OUString > SAL_CALL getSupportedServiceNames() throw();
100 static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static() throw();
102 private:
103 // static XIdlClassRef getStaticIdlClass();
106 Test_Manager_Impl::~Test_Manager_Impl()
108 nInstanceCount--;
112 // old, is no longer needed by the new Mimic
113 Reference< XInterface > SAL_CALL Test_Manager_Impl_CreateInstance_Impl()
115 return (OWeakObject *)new Test_Manager_Impl();
119 // Test_Manager_Impl_CreateInstance()
121 Reference < XInterface > SAL_CALL Test_Manager_Impl_CreateInstance(
122 const Reference< XMultiServiceFactory > & /*rSMgr*/ ) throw (Exception)
124 Reference < XInterface > xService = (XWeak *)(OWeakObject *)new Test_Manager_Impl( );
126 return xService;
130 // Test_Manager_Impl::getImplementationName
132 OUString Test_Manager_Impl::getImplementationName() throw()
134 return OUString(IMPLEMENTATION_NAME);
137 // Test_Manager_Impl::supportsService
138 sal_Bool Test_Manager_Impl::supportsService( const OUString& ServiceName ) throw()
140 return cppu::supportsService(this, ServiceName);
144 // Test_Manager_Impl::getSupportedServiceNames
146 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames() throw ()
148 return getSupportedServiceNames_Static();
152 // Test_Manager_Impl::getSupportedServiceNames_Static
154 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames_Static() throw ()
156 Sequence< OUString > aSNS( 2 );
157 aSNS.getArray()[0] = SERVICE_NAME;
158 aSNS.getArray()[1] = "com.sun.star.bridge.Bridge";
159 return aSNS;
163 /****
166 * This routine performs the test of the process service manager ( getProcessServiceManager is called )
170 ****/
172 extern "C" void SAL_CALL test_ServiceManager()
174 #if ! defined SAL_DLLPREFIX
175 #define SAL_DLLPREFIX ""
176 #endif
177 OUString atUModule2 = SAL_DLLPREFIX "testsmgr_component" SAL_DLLEXTENSION ;
179 // expand shared library name
180 OString atModule2( OUStringToOString(atUModule2, RTL_TEXTENCODING_ASCII_US) );
182 // get the process servicemanager
183 Reference <XMultiServiceFactory> xSMgr = getProcessServiceManager();
185 OSL_ENSURE( xSMgr.is() , "query on XServiceManager failed" );
187 Reference<XContentEnumerationAccess> xContEnum(xSMgr, UNO_QUERY);
188 OSL_ENSURE( xContEnum.is() , "query on XContentEnumerationAccess failed" );
189 Reference<XEnumeration > xEnum(xContEnum->createContentEnumeration(OUString("com.sun.star.registry.SimpleRegistry")));
190 OSL_ENSURE( xEnum.is() , "createContentEnumeration failed" );
191 sal_Int32 nLen = 0;
192 while( xEnum->hasMoreElements() )
194 nLen++;
195 xEnum->nextElement();
197 OSL_ENSURE( nLen == 1, "more than one implementation for SimpleRegistry" );
199 Reference<XEnumerationAccess> xImplEnum(xSMgr, UNO_QUERY);
200 OSL_ENSURE( xImplEnum.is() , "query on XEnumeration failed" );
201 xEnum.set(xImplEnum->createEnumeration());
202 OSL_ENSURE( xEnum.is() , "createEnumeration failed" );
203 nLen = 0;
204 while( xEnum->hasMoreElements() )
206 nLen++;
207 Reference< XServiceInfo > sf( xEnum->nextElement(), UNO_QUERY );
208 OString str( OUStringToOString( sf->getImplementationName(), RTL_TEXTENCODING_ASCII_US ) );
209 ::fprintf( stderr, "> implementation name: %s\n", str.getStr() );
211 OSL_ENSURE( nLen == 8, "more than 6 factories" );
213 // try to get an instance for a unknown service
214 OSL_VERIFY( !xSMgr->createInstance("bla.blup.Q").is() );
217 // First test : register service via the internal function of the component itself
220 Reference< XImplementationRegistration >
221 xInst( xSMgr->createInstance("com.sun.star.registry.ImplementationRegistration"), UNO_QUERY );
222 OSL_ENSURE( xInst.is(), "no ImplementationRegistration" );
224 try {
225 // register the services via writeComponentRegInfo (see at end of this file)
226 xInst->registerImplementation(OUString("com.sun.star.loader.SharedLibrary"), atUModule2, Reference< XSimpleRegistry >() );
228 catch(const CannotRegisterImplementationException &) {
229 OSL_ENSURE( 0, "register implementation failed" );
232 // getImplementations() check
233 Sequence<OUString> seqImpl = xInst->getImplementations(OUString("com.sun.star.loader.SharedLibrary"), atUModule2);
234 OSL_ENSURE( seqImpl.getLength() == 1, "count of implementations is wrong" );
235 OSL_ENSURE( seqImpl.getConstArray()[0] == "com.sun.star.DummyService.V10", "implementation name is not equal" );
238 // tests, if a service provider can be instantiated.
240 Reference< XInterface > xIFace(xSMgr->createInstance("com.sun.star.ts.TestManagerImpl"));
241 OSL_ENSURE( xIFace.is(), "loadable service not found" );
243 // remove the service
244 OSL_VERIFY( xInst->revokeImplementation(atUModule2, Reference< XSimpleRegistry > ()) );
247 Reference<XComponent> xComp(xSMgr, UNO_QUERY);
248 xComp->dispose();
250 xComp.clear();
251 xSMgr.clear();
255 extern "C"
258 sal_Bool SAL_CALL component_writeInfo(
259 void * /*pServiceManager*/, void * pRegistryKey )
261 if (pRegistryKey)
265 Reference< XRegistryKey > xNewKey(
266 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
267 OUString( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
269 const Sequence< OUString > & rSNL =
270 Test_Manager_Impl::getSupportedServiceNames_Static();
271 const OUString * pArray = rSNL.getConstArray();
272 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
273 xNewKey->createKey( pArray[nPos] );
275 return sal_True;
277 catch (InvalidRegistryException &)
279 OSL_FAIL( "### InvalidRegistryException!" );
282 return sal_False;
285 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
286 const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
288 void * pRet = 0;
290 if (rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
292 Reference< XSingleServiceFactory > xFactory( createSingleFactory(
293 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
294 OUString( IMPLEMENTATION_NAME ),
295 Test_Manager_Impl_CreateInstance,
296 Test_Manager_Impl::getSupportedServiceNames_Static() ) );
298 if (xFactory.is())
300 xFactory->acquire();
301 pRet = xFactory.get();
305 return pRet;
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */