merged tag LIBREOFFICE_3_2_99_3
[LibreOffice.git] / cppuhelper / test / cfg_test.cxx
blob2a0671867157b3b190660a988a31d3e05aa4d1c8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_cppuhelper.hxx"
32 // starting the executable:
33 // -env:UNO_CFG_URL=local;<absolute_path>..\\..\\test\\cfg_data;<absolute_path>\\cfg_update
34 // -env:UNO_TYPES=cpputest.rdb
36 #include <sal/main.h>
38 #include <stdio.h>
40 #include <rtl/strbuf.hxx>
42 #include <cppuhelper/implementationentry.hxx>
43 #include <cppuhelper/bootstrap.hxx>
44 #include <cppuhelper/implbase2.hxx>
46 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
47 #include <com/sun/star/lang/XInitialization.hpp>
48 #include <com/sun/star/lang/XServiceInfo.hpp>
49 #include <com/sun/star/lang/XComponent.hpp>
51 #include <com/sun/star/registry/XImplementationRegistration.hpp>
53 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
56 using namespace ::cppu;
57 using namespace ::rtl;
58 using namespace ::osl;
59 using namespace ::com::sun::star;
60 using namespace ::com::sun::star::uno;
62 namespace cfg_test
65 //--------------------------------------------------------------------------------------------------
66 static Sequence< OUString > impl0_getSupportedServiceNames()
68 OUString str( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bootstrap.TestComponent0") );
69 return Sequence< OUString >( &str, 1 );
71 //--------------------------------------------------------------------------------------------------
72 static OUString impl0_getImplementationName()
74 return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.bootstrap.TestComponent0") );
76 //--------------------------------------------------------------------------------------------------
77 static Sequence< OUString > impl1_getSupportedServiceNames()
79 OUString str( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bootstrap.TestComponent1") );
80 return Sequence< OUString >( &str, 1 );
82 //--------------------------------------------------------------------------------------------------
83 static OUString impl1_getImplementationName()
85 return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.bootstrap.TestComponent1") );
88 //==================================================================================================
89 class ServiceImpl0
90 : public WeakImplHelper2< lang::XServiceInfo, lang::XInitialization >
92 Reference< XComponentContext > m_xContext;
94 public:
95 ServiceImpl0( Reference< XComponentContext > const & xContext ) SAL_THROW( () );
97 // XInitialization
98 virtual void SAL_CALL initialize( const Sequence< Any >& rArgs ) throw (Exception, RuntimeException);
100 // XServiceInfo
101 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
102 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
103 virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw (RuntimeException);
105 //__________________________________________________________________________________________________
106 ServiceImpl0::ServiceImpl0( Reference< XComponentContext > const & xContext ) SAL_THROW( () )
107 : m_xContext( xContext )
109 sal_Int32 n;
110 OUString val;
112 // service properties
113 OSL_VERIFY( m_xContext->getValueByName(
114 OUSTR("/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop0") ) >>= n );
115 OSL_VERIFY( n == 13 );
116 OSL_VERIFY( m_xContext->getValueByName(
117 OUSTR("/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop1") ) >>= val );
118 OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("value of serviceprop1") ) );
119 // impl properties
120 OSL_VERIFY( m_xContext->getValueByName(
121 OUSTR("/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop0") ) >>= n );
122 OSL_VERIFY( n == 15 );
123 OSL_VERIFY( m_xContext->getValueByName(
124 OUSTR("/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop1") ) >>= val );
125 OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("value of implprop1") ) );
127 // XInitialization
128 //__________________________________________________________________________________________________
129 void ServiceImpl0::initialize( const Sequence< Any >& rArgs )
130 throw (Exception, RuntimeException)
132 // check args
133 OUString val;
134 OSL_VERIFY( rArgs.getLength() == 3 );
135 OSL_VERIFY( rArgs[ 0 ] >>= val );
136 OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("first argument") ) );
137 OSL_VERIFY( rArgs[ 1 ] >>= val );
138 OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("second argument") ) );
139 OSL_VERIFY( rArgs[ 2 ] >>= val );
140 OSL_VERIFY( val.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("third argument") ) );
142 // XServiceInfo
143 //__________________________________________________________________________________________________
144 OUString ServiceImpl0::getImplementationName()
145 throw(::com::sun::star::uno::RuntimeException)
147 return impl0_getImplementationName();
149 //__________________________________________________________________________________________________
150 Sequence< OUString > ServiceImpl0::getSupportedServiceNames()
151 throw(::com::sun::star::uno::RuntimeException)
153 return impl0_getSupportedServiceNames();
155 //__________________________________________________________________________________________________
156 sal_Bool ServiceImpl0::supportsService( const OUString & rServiceName )
157 throw(::com::sun::star::uno::RuntimeException)
159 const Sequence< OUString > & rSNL = getSupportedServiceNames();
160 const OUString * pArray = rSNL.getConstArray();
161 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
163 if (pArray[nPos] == rServiceName)
164 return sal_True;
166 return sal_False;
169 //==================================================================================================
170 class ServiceImpl1 : public ServiceImpl0
172 public:
173 inline ServiceImpl1( Reference< XComponentContext > const & xContext ) SAL_THROW( () )
174 : ServiceImpl0( xContext )
177 // XServiceInfo
178 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
179 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
181 //__________________________________________________________________________________________________
182 OUString ServiceImpl1::getImplementationName()
183 throw(::com::sun::star::uno::RuntimeException)
185 return impl1_getImplementationName();
187 //__________________________________________________________________________________________________
188 Sequence< OUString > ServiceImpl1::getSupportedServiceNames()
189 throw(::com::sun::star::uno::RuntimeException)
191 return impl1_getSupportedServiceNames();
194 //==================================================================================================
195 static Reference< XInterface > SAL_CALL ServiceImpl0_create(
196 Reference< XComponentContext > const & xContext )
197 SAL_THROW( (Exception) )
199 return (OWeakObject *)new ServiceImpl0( xContext );
201 //==================================================================================================
202 static Reference< XInterface > SAL_CALL ServiceImpl1_create(
203 Reference< XComponentContext > const & xContext )
204 SAL_THROW( (Exception) )
206 return (OWeakObject *)new ServiceImpl1( xContext );
209 } // namespace cfg_test
211 static struct ImplementationEntry g_entries[] =
214 ::cfg_test::ServiceImpl0_create, ::cfg_test::impl0_getImplementationName,
215 ::cfg_test::impl0_getSupportedServiceNames, createSingleComponentFactory,
216 0, 0
219 ::cfg_test::ServiceImpl1_create, ::cfg_test::impl1_getImplementationName,
220 ::cfg_test::impl1_getSupportedServiceNames, createSingleComponentFactory,
221 0, 0
223 { 0, 0, 0, 0, 0, 0 }
226 // component exports
227 extern "C"
229 //==================================================================================================
230 void SAL_CALL component_getImplementationEnvironment(
231 const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
233 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
235 //==================================================================================================
236 sal_Bool SAL_CALL component_writeInfo(
237 void * pServiceManager, void * pRegistryKey )
239 return component_writeInfoHelper(
240 pServiceManager, pRegistryKey, g_entries );
242 //==================================================================================================
243 void * SAL_CALL component_getFactory(
244 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
246 return component_getFactoryHelper(
247 pImplName, pServiceManager, pRegistryKey , g_entries );
252 //##################################################################################################
253 //##################################################################################################
254 //##################################################################################################
256 SAL_IMPLEMENT_MAIN()
260 Reference< XComponentContext > xContext( defaultBootstrap_InitialComponentContext() );
261 Reference< lang::XMultiComponentFactory > xMgr( xContext->getServiceManager() );
263 // show what is in context
264 xContext->getValueByName( OUSTR("dump_maps") );
266 sal_Int32 n;
267 OSL_VERIFY( xContext->getValueByName( OUSTR("/global-context-properties/TestValue") ) >>= n );
268 ::fprintf( stderr, "> n=%d\n", n );
270 Reference< XInterface > x;
271 OSL_VERIFY( !(xContext->getValueByName( OUSTR("/singletons/my_converter") ) >>= x) );
272 OSL_VERIFY( xContext->getValueByName( OUSTR("/singletons/com.sun.star.script.theConverter") ) >>= x );
273 OSL_VERIFY( xContext->getValueByName( OUSTR("/singletons/com.sun.star.bootstrap.theTestComponent0") ) >>= x );
275 ::fprintf( stderr, "> registering service...\n", n );
276 #if defined(SAL_W32) || defined(SAL_OS2)
277 OUString libName( OUSTR("cfg_test.dll") );
278 #elif defined(SAL_UNX)
279 OUString libName( OUSTR("libcfg_test.so") );
280 #endif
281 Reference< registry::XImplementationRegistration > xImplReg( xMgr->createInstanceWithContext(
282 OUSTR("com.sun.star.registry.ImplementationRegistration"), xContext ), UNO_QUERY );
283 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
284 xImplReg->registerImplementation(
285 OUSTR("com.sun.star.loader.SharedLibrary"), libName,
286 Reference< registry::XSimpleRegistry >() );
288 OSL_VERIFY( (x = xMgr->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent0"), xContext )).is() );
289 OSL_VERIFY( (x = xMgr->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent1"), xContext )).is() );
291 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
292 if (xComp.is())
294 xComp->dispose();
296 return 0;
298 catch (Exception & exc)
300 OString str( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
301 ::fprintf( stderr, "# caught exception: %s\n", str.getStr() );
302 return 1;
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */