bump product version to 6.4.0.3
[LibreOffice.git] / stoc / test / testsmgr_cpnt.cxx
blob7faf7fea9686765a6372efc583fc8edf5a9ff1ee
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 static Reference<XMultiServiceFactory> s_x(
68 createRegistryServiceFactory(OUString("stoctest.rdb"), sal_False));
69 return s_x;
72 Reference< XMultiServiceFactory > createRegistryServiceManager( const OUString& registryName )
74 return createRegistryServiceFactory( registryName );
78 /**********************************
79 * The service, that is used to test the Service manager
83 *************************************/
84 static sal_uInt32 nInstanceCount = 0;
85 class Test_Manager_Impl : public WeakImplHelper< XServiceInfo >
87 public:
88 Test_Manager_Impl(){ nInstanceCount++;}
89 ~Test_Manager_Impl();
91 // XServiceInfo
92 OUString SAL_CALL getImplementationName() throw();
93 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
94 Sequence< OUString > SAL_CALL getSupportedServiceNames() throw();
95 static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static() throw();
97 private:
98 // static XIdlClassRef getStaticIdlClass();
101 Test_Manager_Impl::~Test_Manager_Impl()
103 nInstanceCount--;
107 // old, is no longer needed by the new Mimic
108 Reference< XInterface > SAL_CALL Test_Manager_Impl_CreateInstance_Impl()
110 return (OWeakObject *)new Test_Manager_Impl();
114 // Test_Manager_Impl_CreateInstance()
116 Reference < XInterface > SAL_CALL Test_Manager_Impl_CreateInstance(
117 const Reference< XMultiServiceFactory > & /*rSMgr*/ ) throw (Exception)
119 Reference < XInterface > xService = (XWeak *)(OWeakObject *)new Test_Manager_Impl( );
121 return xService;
125 // Test_Manager_Impl::getImplementationName
127 OUString Test_Manager_Impl::getImplementationName() throw()
129 return OUString(IMPLEMENTATION_NAME);
132 // Test_Manager_Impl::supportsService
133 sal_Bool Test_Manager_Impl::supportsService( const OUString& ServiceName ) throw()
135 return cppu::supportsService(this, ServiceName);
139 // Test_Manager_Impl::getSupportedServiceNames
141 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames() throw ()
143 return getSupportedServiceNames_Static();
147 // Test_Manager_Impl::getSupportedServiceNames_Static
149 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames_Static() throw ()
151 return { SERVICE_NAME, "com.sun.star.bridge.Bridge" };
155 /****
158 * This routine performs the test of the process service manager ( getProcessServiceManager is called )
162 ****/
164 extern "C" void SAL_CALL test_ServiceManager()
166 #if ! defined SAL_DLLPREFIX
167 #define SAL_DLLPREFIX ""
168 #endif
169 OUString atUModule2 = SAL_DLLPREFIX "testsmgr_component" SAL_DLLEXTENSION ;
171 // expand shared library name
172 OString atModule2( OUStringToOString(atUModule2, RTL_TEXTENCODING_ASCII_US) );
174 // get the process servicemanager
175 Reference <XMultiServiceFactory> xSMgr = getProcessServiceManager();
177 OSL_ENSURE( xSMgr.is() , "query on XServiceManager failed" );
179 Reference<XContentEnumerationAccess> xContEnum(xSMgr, UNO_QUERY);
180 OSL_ENSURE( xContEnum.is() , "query on XContentEnumerationAccess failed" );
181 Reference<XEnumeration > xEnum(xContEnum->createContentEnumeration(OUString("com.sun.star.registry.SimpleRegistry")));
182 OSL_ENSURE( xEnum.is() , "createContentEnumeration failed" );
183 sal_Int32 nLen = 0;
184 while( xEnum->hasMoreElements() )
186 nLen++;
187 xEnum->nextElement();
189 OSL_ENSURE( nLen == 1, "more than one implementation for SimpleRegistry" );
191 Reference<XEnumerationAccess> xImplEnum(xSMgr, UNO_QUERY);
192 OSL_ENSURE( xImplEnum.is() , "query on XEnumeration failed" );
193 xEnum.set(xImplEnum->createEnumeration());
194 OSL_ENSURE( xEnum.is() , "createEnumeration failed" );
195 nLen = 0;
196 while( xEnum->hasMoreElements() )
198 nLen++;
199 Reference< XServiceInfo > sf( xEnum->nextElement(), UNO_QUERY );
200 OString str( OUStringToOString( sf->getImplementationName(), RTL_TEXTENCODING_ASCII_US ) );
201 ::fprintf( stderr, "> implementation name: %s\n", str.getStr() );
203 OSL_ENSURE( nLen == 8, "more than 6 factories" );
205 // try to get an instance for an unknown service
206 OSL_VERIFY( !xSMgr->createInstance("bla.blup.Q").is() );
209 // First test : register service via the internal function of the component itself
212 Reference< XImplementationRegistration >
213 xInst( xSMgr->createInstance("com.sun.star.registry.ImplementationRegistration"), UNO_QUERY );
214 OSL_ENSURE( xInst.is(), "no ImplementationRegistration" );
216 try {
217 // register the services via writeComponentRegInfo (see at end of this file)
218 xInst->registerImplementation(OUString("com.sun.star.loader.SharedLibrary"), atUModule2, Reference< XSimpleRegistry >() );
220 catch(const CannotRegisterImplementationException &) {
221 OSL_ENSURE( 0, "register implementation failed" );
224 // getImplementations() check
225 Sequence<OUString> seqImpl = xInst->getImplementations(OUString("com.sun.star.loader.SharedLibrary"), atUModule2);
226 OSL_ENSURE( seqImpl.getLength() == 1, "count of implementations is wrong" );
227 OSL_ENSURE( seqImpl.getConstArray()[0] == "com.sun.star.DummyService.V10", "implementation name is not equal" );
230 // tests, if a service provider can be instantiated.
232 Reference< XInterface > xIFace(xSMgr->createInstance("com.sun.star.ts.TestManagerImpl"));
233 OSL_ENSURE( xIFace.is(), "loadable service not found" );
235 // remove the service
236 OSL_VERIFY( xInst->revokeImplementation(atUModule2, Reference< XSimpleRegistry > ()) );
239 Reference<XComponent> xComp(xSMgr, UNO_QUERY);
240 xComp->dispose();
242 xComp.clear();
243 xSMgr.clear();
247 extern "C"
250 sal_Bool SAL_CALL component_writeInfo(
251 void * /*pServiceManager*/, void * pRegistryKey )
253 if (pRegistryKey)
257 Reference< XRegistryKey > xNewKey(
258 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
259 OUString( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
261 const Sequence< OUString > & rSNL =
262 Test_Manager_Impl::getSupportedServiceNames_Static();
263 const OUString * pArray = rSNL.getConstArray();
264 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
265 xNewKey->createKey( pArray[nPos] );
267 return sal_True;
269 catch (InvalidRegistryException &)
271 OSL_FAIL( "### InvalidRegistryException!" );
274 return sal_False;
277 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
278 const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
280 void * pRet = 0;
282 if (rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
284 Reference< XSingleServiceFactory > xFactory( createSingleFactory(
285 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
286 OUString( IMPLEMENTATION_NAME ),
287 Test_Manager_Impl_CreateInstance,
288 Test_Manager_Impl::getSupportedServiceNames_Static() ) );
290 if (xFactory.is())
292 xFactory->acquire();
293 pRet = xFactory.get();
297 return pRet;
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */