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