Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / cppuhelper / test / cfg_test.cxx
blobac976be5774032d514daf7cea0928b7ea9ffb6d8
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 ************************************************************************/
30 // starting the executable:
31 // -env:UNO_CFG_URL=local;<absolute_path>..\\..\\test\\cfg_data;<absolute_path>\\cfg_update
32 // -env:UNO_TYPES=cpputest.rdb
34 #include <sal/main.h>
36 #include <stdio.h>
38 #include <rtl/strbuf.hxx>
40 #include <cppuhelper/implementationentry.hxx>
41 #include <cppuhelper/bootstrap.hxx>
42 #include <cppuhelper/implbase2.hxx>
44 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
45 #include <com/sun/star/lang/XInitialization.hpp>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 #include <com/sun/star/lang/XComponent.hpp>
49 #include <com/sun/star/registry/XImplementationRegistration.hpp>
51 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
54 using namespace ::cppu;
55 using namespace ::rtl;
56 using namespace ::osl;
57 using namespace ::com::sun::star;
58 using namespace ::com::sun::star::uno;
60 namespace cfg_test
63 //--------------------------------------------------------------------------------------------------
64 static Sequence< OUString > impl0_getSupportedServiceNames()
66 OUString str( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bootstrap.TestComponent0") );
67 return Sequence< OUString >( &str, 1 );
69 //--------------------------------------------------------------------------------------------------
70 static OUString impl0_getImplementationName()
72 return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.bootstrap.TestComponent0") );
74 //--------------------------------------------------------------------------------------------------
75 static Sequence< OUString > impl1_getSupportedServiceNames()
77 OUString str( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bootstrap.TestComponent1") );
78 return Sequence< OUString >( &str, 1 );
80 //--------------------------------------------------------------------------------------------------
81 static OUString impl1_getImplementationName()
83 return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.bootstrap.TestComponent1") );
86 //==================================================================================================
87 class ServiceImpl0
88 : public WeakImplHelper2< lang::XServiceInfo, lang::XInitialization >
90 Reference< XComponentContext > m_xContext;
92 public:
93 ServiceImpl0( Reference< XComponentContext > const & xContext ) SAL_THROW(());
95 // XInitialization
96 virtual void SAL_CALL initialize( const Sequence< Any >& rArgs ) throw (Exception, RuntimeException);
98 // XServiceInfo
99 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
100 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
101 virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw (RuntimeException);
103 //__________________________________________________________________________________________________
104 ServiceImpl0::ServiceImpl0( Reference< XComponentContext > const & xContext ) SAL_THROW(())
105 : m_xContext( xContext )
107 sal_Int32 n;
108 OUString val;
110 // service properties
111 OSL_VERIFY( m_xContext->getValueByName(
112 OUSTR("/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop0") ) >>= n );
113 OSL_VERIFY( n == 13 );
114 OSL_VERIFY( m_xContext->getValueByName(
115 OUSTR("/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop1") ) >>= val );
116 OSL_VERIFY( val == "value of serviceprop1" );
117 // impl properties
118 OSL_VERIFY( m_xContext->getValueByName(
119 OUSTR("/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop0") ) >>= n );
120 OSL_VERIFY( n == 15 );
121 OSL_VERIFY( m_xContext->getValueByName(
122 OUSTR("/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop1") ) >>= val );
123 OSL_VERIFY( val == "value of implprop1" );
125 // XInitialization
126 //__________________________________________________________________________________________________
127 void ServiceImpl0::initialize( const Sequence< Any >& rArgs )
128 throw (Exception, RuntimeException)
130 // check args
131 OUString val;
132 OSL_VERIFY( rArgs.getLength() == 3 );
133 OSL_VERIFY( rArgs[ 0 ] >>= val );
134 OSL_VERIFY( val == "first argument" );
135 OSL_VERIFY( rArgs[ 1 ] >>= val );
136 OSL_VERIFY( val == "second argument" );
137 OSL_VERIFY( rArgs[ 2 ] >>= val );
138 OSL_VERIFY( val == "third argument" );
140 // XServiceInfo
141 //__________________________________________________________________________________________________
142 OUString ServiceImpl0::getImplementationName()
143 throw(::com::sun::star::uno::RuntimeException)
145 return impl0_getImplementationName();
147 //__________________________________________________________________________________________________
148 Sequence< OUString > ServiceImpl0::getSupportedServiceNames()
149 throw(::com::sun::star::uno::RuntimeException)
151 return impl0_getSupportedServiceNames();
153 //__________________________________________________________________________________________________
154 sal_Bool ServiceImpl0::supportsService( const OUString & rServiceName )
155 throw(::com::sun::star::uno::RuntimeException)
157 const Sequence< OUString > & rSNL = getSupportedServiceNames();
158 const OUString * pArray = rSNL.getConstArray();
159 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
161 if (pArray[nPos] == rServiceName)
162 return sal_True;
164 return sal_False;
167 //==================================================================================================
168 class ServiceImpl1 : public ServiceImpl0
170 public:
171 inline ServiceImpl1( Reference< XComponentContext > const & xContext ) SAL_THROW(())
172 : ServiceImpl0( xContext )
175 // XServiceInfo
176 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
177 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
179 //__________________________________________________________________________________________________
180 OUString ServiceImpl1::getImplementationName()
181 throw(::com::sun::star::uno::RuntimeException)
183 return impl1_getImplementationName();
185 //__________________________________________________________________________________________________
186 Sequence< OUString > ServiceImpl1::getSupportedServiceNames()
187 throw(::com::sun::star::uno::RuntimeException)
189 return impl1_getSupportedServiceNames();
192 //==================================================================================================
193 static Reference< XInterface > SAL_CALL ServiceImpl0_create(
194 Reference< XComponentContext > const & xContext )
195 SAL_THROW( (Exception) )
197 return (OWeakObject *)new ServiceImpl0( xContext );
199 //==================================================================================================
200 static Reference< XInterface > SAL_CALL ServiceImpl1_create(
201 Reference< XComponentContext > const & xContext )
202 SAL_THROW( (Exception) )
204 return (OWeakObject *)new ServiceImpl1( xContext );
207 } // namespace cfg_test
209 static struct ImplementationEntry g_entries[] =
212 ::cfg_test::ServiceImpl0_create, ::cfg_test::impl0_getImplementationName,
213 ::cfg_test::impl0_getSupportedServiceNames, createSingleComponentFactory,
214 0, 0
217 ::cfg_test::ServiceImpl1_create, ::cfg_test::impl1_getImplementationName,
218 ::cfg_test::impl1_getSupportedServiceNames, createSingleComponentFactory,
219 0, 0
221 { 0, 0, 0, 0, 0, 0 }
224 // component exports
225 extern "C"
227 //==================================================================================================
228 sal_Bool SAL_CALL component_writeInfo(
229 void * pServiceManager, void * pRegistryKey )
231 return component_writeInfoHelper(
232 pServiceManager, pRegistryKey, g_entries );
234 //==================================================================================================
235 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
236 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
238 return component_getFactoryHelper(
239 pImplName, pServiceManager, pRegistryKey , g_entries );
244 //##################################################################################################
245 //##################################################################################################
246 //##################################################################################################
248 SAL_IMPLEMENT_MAIN()
252 Reference< XComponentContext > xContext( defaultBootstrap_InitialComponentContext() );
253 Reference< lang::XMultiComponentFactory > xMgr( xContext->getServiceManager() );
255 // show what is in context
256 xContext->getValueByName( OUSTR("dump_maps") );
258 sal_Int32 n(0);
259 OSL_VERIFY( xContext->getValueByName( OUSTR("/global-context-properties/TestValue") ) >>= n );
260 ::fprintf( stderr, "> n=%d\n", n );
262 Reference< XInterface > x;
263 OSL_VERIFY( !(xContext->getValueByName( OUSTR("/singletons/my_converter") ) >>= x) );
264 OSL_VERIFY( xContext->getValueByName( OUSTR("/singletons/com.sun.star.script.theConverter") ) >>= x );
265 OSL_VERIFY( xContext->getValueByName( OUSTR("/singletons/com.sun.star.bootstrap.theTestComponent0") ) >>= x );
267 ::fprintf( stderr, "> registering service...\n");
268 #if defined(SAL_W32)
269 OUString libName( OUSTR("cfg_test.dll") );
270 #elif defined(SAL_UNX)
271 OUString libName( OUSTR("libcfg_test.so") );
272 #endif
273 Reference< registry::XImplementationRegistration > xImplReg( xMgr->createInstanceWithContext(
274 OUSTR("com.sun.star.registry.ImplementationRegistration"), xContext ), UNO_QUERY );
275 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
276 xImplReg->registerImplementation(
277 OUSTR("com.sun.star.loader.SharedLibrary"), libName,
278 Reference< registry::XSimpleRegistry >() );
280 OSL_VERIFY( (x = xMgr->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent0"), xContext )).is() );
281 OSL_VERIFY( (x = xMgr->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent1"), xContext )).is() );
283 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
284 if (xComp.is())
286 xComp->dispose();
288 return 0;
290 catch (Exception & exc)
292 OString str( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
293 ::fprintf( stderr, "# caught exception: %s\n", str.getStr() );
294 return 1;
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */