Bump for 4.0-15
[LibreOffice.git] / stoc / test / testsmgr_cpnt.cxx
blobfe4a701fb94679469b6c3ee1cbc7aa6cfb177fbe
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/weak.hxx>
26 #include <uno/mapping.hxx>
29 #include <cppuhelper/factory.hxx>
30 #include <cppuhelper/servicefactory.hxx>
31 #include <cppuhelper/implbase1.hxx>
32 #include <registry/registry.hxx>
34 #include <com/sun/star/registry/XSimpleRegistry.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/registry/XImplementationRegistration.hpp>
38 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
41 #include <com/sun/star/container/XEnumerationAccess.hpp>
42 #include <com/sun/star/container/XEnumeration.hpp>
44 #if defined ( UNX )
45 #include <limits.h>
46 #define _MAX_PATH PATH_MAX
47 #endif
50 #define IMPLEMENTATION_NAME "com.sun.star.DummyService.V10"
51 #define SERVICE_NAME "com.sun.star.ts.TestManagerImpl"
54 using namespace com::sun::star::uno;
55 using namespace com::sun::star::registry;
56 using namespace com::sun::star::lang;
57 using namespace com::sun::star::container;
58 using namespace osl;
59 using namespace cppu;
61 using ::rtl::OUString;
62 using ::rtl::OUStringToOString;
63 using ::rtl::OString;
66 Reference<XMultiServiceFactory> getProcessServiceManager()
68 Reference<XMultiServiceFactory > s_x;
69 if (! s_x.is())
71 MutexGuard aGuard( Mutex::getGlobalMutex() );
72 if (! s_x.is())
73 s_x = createRegistryServiceFactory( OUString("stoctest.rdb"), sal_False );
75 return s_x;
78 Reference< XMultiServiceFactory > createRegistryServiceManager( const OUString& registryName )
80 return createRegistryServiceFactory( registryName );
84 /**********************************
85 * The service, that is used to test the Service manager
89 *************************************/
90 static sal_uInt32 nInstanceCount = 0;
91 class Test_Manager_Impl : public WeakImplHelper1< XServiceInfo >
93 public:
94 Test_Manager_Impl(){ nInstanceCount++;}
95 ~Test_Manager_Impl();
97 // XServiceInfo
98 OUString SAL_CALL getImplementationName() throw();
99 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
100 Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
101 static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(void) throw();
103 private:
104 // static XIdlClassRef getStaticIdlClass();
107 Test_Manager_Impl::~Test_Manager_Impl()
109 nInstanceCount--;
113 // alt, wird von der neuen Mimic nicht mehr gebraucht
114 Reference< XInterface > SAL_CALL Test_Manager_Impl_CreateInstance_Impl()
116 return (OWeakObject *)new Test_Manager_Impl();
120 //*************************************************************************
121 // Test_Manager_Impl_CreateInstance()
123 Reference < XInterface > SAL_CALL Test_Manager_Impl_CreateInstance(
124 const Reference< XMultiServiceFactory > & /*rSMgr*/ ) throw (Exception)
126 Reference < XInterface > xService = (XWeak *)(OWeakObject *)new Test_Manager_Impl( );
128 return xService;
132 //*************************************************************************
133 // Test_Manager_Impl::getImplementationName
135 OUString Test_Manager_Impl::getImplementationName() throw()
137 return OUString(IMPLEMENTATION_NAME);
140 //*************************************************************************
141 // Test_Manager_Impl::supportsService
143 sal_Bool Test_Manager_Impl::supportsService( const OUString& ServiceName ) throw()
145 Sequence< OUString > aSNL = getSupportedServiceNames();
146 const OUString * pArray = aSNL.getConstArray();
147 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
148 if( pArray[i] == ServiceName )
149 return sal_True;
150 return sal_False;
153 //*************************************************************************
154 // Test_Manager_Impl::getSupportedServiceNames
156 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames(void) throw ()
158 return getSupportedServiceNames_Static();
161 //*************************************************************************
162 // Test_Manager_Impl::getSupportedServiceNames_Static
164 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames_Static(void) throw ()
166 Sequence< OUString > aSNS( 2 );
167 aSNS.getArray()[0] = OUString(SERVICE_NAME);
168 aSNS.getArray()[1] = OUString("com.sun.star.bridge.Bridge");
169 return aSNS;
175 /****
178 * This routine performs the test of the process service manager ( getProcessServiceManager is called )
182 ****/
184 #include <stdio.h>
186 extern "C" void SAL_CALL test_ServiceManager()
188 #if ! defined SAL_DLLPREFIX
189 #define SAL_DLLPREFIX ""
190 #endif
191 OUString atUModule2 = OUString( SAL_DLLPREFIX "testsmgr_component" SAL_DLLEXTENSION );
193 // expand shared library name
194 OString atModule2( OUStringToOString(atUModule2, RTL_TEXTENCODING_ASCII_US) );
196 // get the process servicemanager
197 Reference <XMultiServiceFactory> xSMgr = getProcessServiceManager();
199 OSL_ENSURE( xSMgr.is() , "query on XServiceManager failed" );
201 Reference<XContentEnumerationAccess> xContEnum(xSMgr, UNO_QUERY);
202 OSL_ENSURE( xContEnum.is() , "query on XContentEnumerationAccess failed" );
203 Reference<XEnumeration > xEnum(xContEnum->createContentEnumeration(OUString("com.sun.star.registry.SimpleRegistry")));
204 OSL_ENSURE( xEnum.is() , "createContentEnumeration failed" );
205 sal_Int32 nLen = 0;
206 while( xEnum->hasMoreElements() )
208 nLen++;
209 xEnum->nextElement();
211 OSL_ENSURE( nLen == 1, "more than one implementation for SimpleRegistry" );
213 Reference<XEnumerationAccess> xImplEnum(xSMgr, UNO_QUERY);
214 OSL_ENSURE( xImplEnum.is() , "query on XEnumeration failed" );
215 xEnum = Reference<XEnumeration >(xImplEnum->createEnumeration());
216 OSL_ENSURE( xEnum.is() , "createEnumeration failed" );
217 nLen = 0;
218 while( xEnum->hasMoreElements() )
220 nLen++;
221 Reference< XServiceInfo > sf( xEnum->nextElement(), UNO_QUERY );
222 OString str( OUStringToOString( sf->getImplementationName(), RTL_TEXTENCODING_ASCII_US ) );
223 ::fprintf( stderr, "> implementation name: %s\n", str.getStr() );
225 OSL_ENSURE( nLen == 8, "more than 6 factories" );
227 // try to get an instance for a unknown service
228 OSL_VERIFY( !xSMgr->createInstance(OUString("bla.blup.Q")).is() );
231 // First test : register service via the internal function of the component itself
234 Reference< XImplementationRegistration >
235 xInst( xSMgr->createInstance(OUString("com.sun.star.registry.ImplementationRegistration")), UNO_QUERY );
236 OSL_ENSURE( xInst.is(), "no ImplementationRegistration" );
238 try {
239 // register the services via writeComponentRegInfo (see at end of this file)
240 xInst->registerImplementation(OUString("com.sun.star.loader.SharedLibrary"), atUModule2, Reference< XSimpleRegistry >() );
242 catch(const CannotRegisterImplementationException &) {
243 OSL_ENSURE( 0, "register implementation failed" );
246 // getImplementations() check
247 Sequence<OUString> seqImpl = xInst->getImplementations(OUString("com.sun.star.loader.SharedLibrary"), atUModule2);
248 OSL_ENSURE( seqImpl.getLength() == 1, "count of implementantions is wrong" );
249 OSL_ENSURE( seqImpl.getConstArray()[0] == OUString("com.sun.star.DummyService.V10"), "implementation name is not equal" );
252 // tests, if a service provider can be instantiated.
254 Reference< XInterface > xIFace(xSMgr->createInstance(OUString("com.sun.star.ts.TestManagerImpl")));
255 OSL_ENSURE( xIFace.is(), "loadable service not found" );
257 // remove the service
258 OSL_VERIFY( xInst->revokeImplementation(atUModule2, Reference< XSimpleRegistry > ()) );
261 Reference<XComponent> xComp(xSMgr, UNO_QUERY);
262 xComp->dispose();
264 xComp.clear();
265 xSMgr.clear();
270 extern "C"
272 //==================================================================================================
273 sal_Bool SAL_CALL component_writeInfo(
274 void * /*pServiceManager*/, void * pRegistryKey )
276 if (pRegistryKey)
280 Reference< XRegistryKey > xNewKey(
281 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
282 OUString( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
284 const Sequence< OUString > & rSNL =
285 Test_Manager_Impl::getSupportedServiceNames_Static();
286 const OUString * pArray = rSNL.getConstArray();
287 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
288 xNewKey->createKey( pArray[nPos] );
290 return sal_True;
292 catch (InvalidRegistryException &)
294 OSL_FAIL( "### InvalidRegistryException!" );
297 return sal_False;
299 //==================================================================================================
300 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
301 const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
303 void * pRet = 0;
305 if (rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
307 Reference< XSingleServiceFactory > xFactory( createSingleFactory(
308 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
309 OUString( IMPLEMENTATION_NAME ),
310 Test_Manager_Impl_CreateInstance,
311 Test_Manager_Impl::getSupportedServiceNames_Static() ) );
313 if (xFactory.is())
315 xFactory->acquire();
316 pRet = xFactory.get();
320 return pRet;
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */