Branch libreoffice-5-0-4
[LibreOffice.git] / cppuhelper / test / cfg_test.cxx
blob27e223936e3c1118e8bfc1aa36aaea09d5714b30
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 .
21 // starting the executable:
22 // -env:UNO_CFG_URL=local;<absolute_path>..\\..\\test\\cfg_data;<absolute_path>\\cfg_update
23 // -env:UNO_TYPES=cpputest.rdb
25 #include <sal/main.h>
27 #include <stdio.h>
29 #include <rtl/strbuf.hxx>
31 #include <cppuhelper/implementationentry.hxx>
32 #include <cppuhelper/bootstrap.hxx>
33 #include <cppuhelper/implbase2.hxx>
34 #include <cppuhelper/supportsservice.hxx>
36 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
37 #include <com/sun/star/lang/XInitialization.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
41 #include <com/sun/star/registry/XImplementationRegistration.hpp>
44 using namespace ::cppu;
45 using namespace ::osl;
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
49 namespace cfg_test
53 static Sequence< OUString > impl0_getSupportedServiceNames()
55 OUString str("com.sun.star.bootstrap.TestComponent0");
56 return Sequence< OUString >( &str, 1 );
59 static OUString impl0_getImplementationName()
61 return OUString("com.sun.star.comp.bootstrap.TestComponent0");
64 static Sequence< OUString > impl1_getSupportedServiceNames()
66 OUString str("com.sun.star.bootstrap.TestComponent1");
67 return Sequence< OUString >( &str, 1 );
70 static OUString impl1_getImplementationName()
72 return OUString("com.sun.star.comp.bootstrap.TestComponent1");
76 class ServiceImpl0
77 : public WeakImplHelper2< lang::XServiceInfo, lang::XInitialization >
79 Reference< XComponentContext > m_xContext;
81 public:
82 ServiceImpl0( Reference< XComponentContext > const & xContext );
84 // XInitialization
85 virtual void SAL_CALL initialize( const Sequence< Any >& rArgs ) throw (Exception, RuntimeException);
87 // XServiceInfo
88 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
89 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
90 virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw (RuntimeException);
93 ServiceImpl0::ServiceImpl0( Reference< XComponentContext > const & xContext )
94 : m_xContext( xContext )
96 sal_Int32 n;
97 OUString val;
99 // service properties
100 OSL_VERIFY( m_xContext->getValueByName(
101 "/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop0" ) >>= n );
102 OSL_VERIFY( n == 13 );
103 OSL_VERIFY( m_xContext->getValueByName(
104 "/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop1" ) >>= val );
105 OSL_VERIFY( val == "value of serviceprop1" );
106 // impl properties
107 OSL_VERIFY( m_xContext->getValueByName(
108 "/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop0" ) >>= n );
109 OSL_VERIFY( n == 15 );
110 OSL_VERIFY( m_xContext->getValueByName(
111 "/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop1" ) >>= val );
112 OSL_VERIFY( val == "value of implprop1" );
114 // XInitialization
116 void ServiceImpl0::initialize( const Sequence< Any >& rArgs )
117 throw (Exception, RuntimeException)
119 // check args
120 OUString val;
121 OSL_VERIFY( rArgs.getLength() == 3 );
122 OSL_VERIFY( rArgs[ 0 ] >>= val );
123 OSL_VERIFY( val == "first argument" );
124 OSL_VERIFY( rArgs[ 1 ] >>= val );
125 OSL_VERIFY( val == "second argument" );
126 OSL_VERIFY( rArgs[ 2 ] >>= val );
127 OSL_VERIFY( val == "third argument" );
129 // XServiceInfo
131 OUString ServiceImpl0::getImplementationName()
132 throw(::com::sun::star::uno::RuntimeException)
134 return impl0_getImplementationName();
137 Sequence< OUString > ServiceImpl0::getSupportedServiceNames()
138 throw(::com::sun::star::uno::RuntimeException)
140 return impl0_getSupportedServiceNames();
143 sal_Bool ServiceImpl0::supportsService( const OUString & rServiceName )
144 throw(::com::sun::star::uno::RuntimeException)
146 return cppu::supportsService(this, rServiceName);
150 class ServiceImpl1 : public ServiceImpl0
152 public:
153 inline ServiceImpl1( Reference< XComponentContext > const & xContext )
154 : ServiceImpl0( xContext )
157 // XServiceInfo
158 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
159 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
162 OUString ServiceImpl1::getImplementationName()
163 throw(::com::sun::star::uno::RuntimeException)
165 return impl1_getImplementationName();
168 Sequence< OUString > ServiceImpl1::getSupportedServiceNames()
169 throw(::com::sun::star::uno::RuntimeException)
171 return impl1_getSupportedServiceNames();
175 static Reference< XInterface > SAL_CALL ServiceImpl0_create(
176 Reference< XComponentContext > const & xContext )
178 return (OWeakObject *)new ServiceImpl0( xContext );
181 static Reference< XInterface > SAL_CALL ServiceImpl1_create(
182 Reference< XComponentContext > const & xContext )
184 return (OWeakObject *)new ServiceImpl1( xContext );
187 } // namespace cfg_test
189 static const struct ImplementationEntry g_entries[] =
192 ::cfg_test::ServiceImpl0_create, ::cfg_test::impl0_getImplementationName,
193 ::cfg_test::impl0_getSupportedServiceNames, createSingleComponentFactory,
194 0, 0
197 ::cfg_test::ServiceImpl1_create, ::cfg_test::impl1_getImplementationName,
198 ::cfg_test::impl1_getSupportedServiceNames, createSingleComponentFactory,
199 0, 0
201 { 0, 0, 0, 0, 0, 0 }
204 // component exports
205 extern "C"
208 sal_Bool SAL_CALL component_writeInfo(
209 void * pServiceManager, void * pRegistryKey )
211 return component_writeInfoHelper(
212 pServiceManager, pRegistryKey, g_entries );
215 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
216 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
218 return component_getFactoryHelper(
219 pImplName, pServiceManager, pRegistryKey , g_entries );
228 SAL_IMPLEMENT_MAIN()
232 Reference< XComponentContext > xContext( defaultBootstrap_InitialComponentContext() );
233 Reference< lang::XMultiComponentFactory > xMgr( xContext->getServiceManager() );
235 // show what is in context
236 xContext->getValueByName( "dump_maps" );
238 sal_Int32 n(0);
239 OSL_VERIFY( xContext->getValueByName( "/global-context-properties/TestValue" ) >>= n );
240 ::fprintf( stderr, "> n=%d\n", n );
242 Reference< XInterface > x;
243 OSL_VERIFY( !(xContext->getValueByName( "/singletons/my_converter" ) >>= x) );
244 OSL_VERIFY( xContext->getValueByName( "/singletons/com.sun.star.script.theConverter" ) >>= x );
245 OSL_VERIFY( xContext->getValueByName( "/singletons/com.sun.star.bootstrap.theTestComponent0" ) >>= x );
247 ::fprintf( stderr, "> registering service...\n");
248 #if defined(SAL_W32)
249 OUString libName( "cfg_test.dll" );
250 #elif defined(SAL_UNX)
251 OUString libName( "libcfg_test.so" );
252 #endif
253 Reference< registry::XImplementationRegistration > xImplReg( xMgr->createInstanceWithContext(
254 "com.sun.star.registry.ImplementationRegistration", xContext ), UNO_QUERY );
255 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
256 xImplReg->registerImplementation(
257 "com.sun.star.loader.SharedLibrary", libName,
258 Reference< registry::XSimpleRegistry >() );
260 OSL_VERIFY( (x = xMgr->createInstanceWithContext( "com.sun.star.bootstrap.TestComponent0", xContext )).is() );
261 OSL_VERIFY( (x = xMgr->createInstanceWithContext( "com.sun.star.bootstrap.TestComponent1", xContext )).is() );
263 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
264 if (xComp.is())
266 xComp->dispose();
268 return 0;
270 catch (Exception & exc)
272 OString str( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
273 ::fprintf( stderr, "# caught exception: %s\n", str.getStr() );
274 return 1;
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */