1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cfg_test.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppuhelper.hxx"
34 // starting the executable:
35 // -env:UNO_CFG_URL=local;<absolute_path>..\\..\\test\\cfg_data;<absolute_path>\\cfg_update
36 // -env:UNO_TYPES=cpputest.rdb
42 #include <rtl/strbuf.hxx>
44 #include <cppuhelper/implementationentry.hxx>
45 #include <cppuhelper/bootstrap.hxx>
46 #include <cppuhelper/implbase2.hxx>
48 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
49 #include <com/sun/star/lang/XInitialization.hpp>
50 #include <com/sun/star/lang/XServiceInfo.hpp>
51 #include <com/sun/star/lang/XComponent.hpp>
53 #include <com/sun/star/registry/XImplementationRegistration.hpp>
55 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
58 using namespace ::cppu
;
59 using namespace ::rtl
;
60 using namespace ::osl
;
61 using namespace ::com::sun::star
;
62 using namespace ::com::sun::star::uno
;
67 //--------------------------------------------------------------------------------------------------
68 static Sequence
< OUString
> impl0_getSupportedServiceNames()
70 OUString
str( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bootstrap.TestComponent0") );
71 return Sequence
< OUString
>( &str
, 1 );
73 //--------------------------------------------------------------------------------------------------
74 static OUString
impl0_getImplementationName()
76 return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.bootstrap.TestComponent0") );
78 //--------------------------------------------------------------------------------------------------
79 static Sequence
< OUString
> impl1_getSupportedServiceNames()
81 OUString
str( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bootstrap.TestComponent1") );
82 return Sequence
< OUString
>( &str
, 1 );
84 //--------------------------------------------------------------------------------------------------
85 static OUString
impl1_getImplementationName()
87 return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.bootstrap.TestComponent1") );
90 //==================================================================================================
92 : public WeakImplHelper2
< lang::XServiceInfo
, lang::XInitialization
>
94 Reference
< XComponentContext
> m_xContext
;
97 ServiceImpl0( Reference
< XComponentContext
> const & xContext
) SAL_THROW( () );
100 virtual void SAL_CALL
initialize( const Sequence
< Any
>& rArgs
) throw (Exception
, RuntimeException
);
103 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw (RuntimeException
);
104 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
);
105 virtual sal_Bool SAL_CALL
supportsService( const OUString
& rServiceName
) throw (RuntimeException
);
107 //__________________________________________________________________________________________________
108 ServiceImpl0::ServiceImpl0( Reference
< XComponentContext
> const & xContext
) SAL_THROW( () )
109 : m_xContext( xContext
)
114 // service properties
115 OSL_VERIFY( m_xContext
->getValueByName(
116 OUSTR("/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop0") ) >>= n
);
117 OSL_VERIFY( n
== 13 );
118 OSL_VERIFY( m_xContext
->getValueByName(
119 OUSTR("/services/com.sun.star.bootstrap.TestComponent0/context-properties/serviceprop1") ) >>= val
);
120 OSL_VERIFY( val
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("value of serviceprop1") ) );
122 OSL_VERIFY( m_xContext
->getValueByName(
123 OUSTR("/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop0") ) >>= n
);
124 OSL_VERIFY( n
== 15 );
125 OSL_VERIFY( m_xContext
->getValueByName(
126 OUSTR("/implementations/com.sun.star.comp.bootstrap.TestComponent0/context-properties/implprop1") ) >>= val
);
127 OSL_VERIFY( val
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("value of implprop1") ) );
130 //__________________________________________________________________________________________________
131 void ServiceImpl0::initialize( const Sequence
< Any
>& rArgs
)
132 throw (Exception
, RuntimeException
)
136 OSL_VERIFY( rArgs
.getLength() == 3 );
137 OSL_VERIFY( rArgs
[ 0 ] >>= val
);
138 OSL_VERIFY( val
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("first argument") ) );
139 OSL_VERIFY( rArgs
[ 1 ] >>= val
);
140 OSL_VERIFY( val
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("second argument") ) );
141 OSL_VERIFY( rArgs
[ 2 ] >>= val
);
142 OSL_VERIFY( val
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("third argument") ) );
145 //__________________________________________________________________________________________________
146 OUString
ServiceImpl0::getImplementationName()
147 throw(::com::sun::star::uno::RuntimeException
)
149 return impl0_getImplementationName();
151 //__________________________________________________________________________________________________
152 Sequence
< OUString
> ServiceImpl0::getSupportedServiceNames()
153 throw(::com::sun::star::uno::RuntimeException
)
155 return impl0_getSupportedServiceNames();
157 //__________________________________________________________________________________________________
158 sal_Bool
ServiceImpl0::supportsService( const OUString
& rServiceName
)
159 throw(::com::sun::star::uno::RuntimeException
)
161 const Sequence
< OUString
> & rSNL
= getSupportedServiceNames();
162 const OUString
* pArray
= rSNL
.getConstArray();
163 for ( sal_Int32 nPos
= rSNL
.getLength(); nPos
--; )
165 if (pArray
[nPos
] == rServiceName
)
171 //==================================================================================================
172 class ServiceImpl1
: public ServiceImpl0
175 inline ServiceImpl1( Reference
< XComponentContext
> const & xContext
) SAL_THROW( () )
176 : ServiceImpl0( xContext
)
180 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw (RuntimeException
);
181 virtual OUString SAL_CALL
getImplementationName() throw (RuntimeException
);
183 //__________________________________________________________________________________________________
184 OUString
ServiceImpl1::getImplementationName()
185 throw(::com::sun::star::uno::RuntimeException
)
187 return impl1_getImplementationName();
189 //__________________________________________________________________________________________________
190 Sequence
< OUString
> ServiceImpl1::getSupportedServiceNames()
191 throw(::com::sun::star::uno::RuntimeException
)
193 return impl1_getSupportedServiceNames();
196 //==================================================================================================
197 static Reference
< XInterface
> SAL_CALL
ServiceImpl0_create(
198 Reference
< XComponentContext
> const & xContext
)
199 SAL_THROW( (Exception
) )
201 return (OWeakObject
*)new ServiceImpl0( xContext
);
203 //==================================================================================================
204 static Reference
< XInterface
> SAL_CALL
ServiceImpl1_create(
205 Reference
< XComponentContext
> const & xContext
)
206 SAL_THROW( (Exception
) )
208 return (OWeakObject
*)new ServiceImpl1( xContext
);
211 } // namespace cfg_test
213 static struct ImplementationEntry g_entries
[] =
216 ::cfg_test::ServiceImpl0_create
, ::cfg_test::impl0_getImplementationName
,
217 ::cfg_test::impl0_getSupportedServiceNames
, createSingleComponentFactory
,
221 ::cfg_test::ServiceImpl1_create
, ::cfg_test::impl1_getImplementationName
,
222 ::cfg_test::impl1_getSupportedServiceNames
, createSingleComponentFactory
,
231 //==================================================================================================
232 void SAL_CALL
component_getImplementationEnvironment(
233 const sal_Char
** ppEnvTypeName
, uno_Environment
** /*ppEnv*/ )
235 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
237 //==================================================================================================
238 sal_Bool SAL_CALL
component_writeInfo(
239 void * pServiceManager
, void * pRegistryKey
)
241 return component_writeInfoHelper(
242 pServiceManager
, pRegistryKey
, g_entries
);
244 //==================================================================================================
245 void * SAL_CALL
component_getFactory(
246 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
248 return component_getFactoryHelper(
249 pImplName
, pServiceManager
, pRegistryKey
, g_entries
);
254 //##################################################################################################
255 //##################################################################################################
256 //##################################################################################################
262 Reference
< XComponentContext
> xContext( defaultBootstrap_InitialComponentContext() );
263 Reference
< lang::XMultiComponentFactory
> xMgr( xContext
->getServiceManager() );
265 // show what is in context
266 xContext
->getValueByName( OUSTR("dump_maps") );
269 OSL_VERIFY( xContext
->getValueByName( OUSTR("/global-context-properties/TestValue") ) >>= n
);
270 ::fprintf( stderr
, "> n=%d\n", n
);
272 Reference
< XInterface
> x
;
273 OSL_VERIFY( !(xContext
->getValueByName( OUSTR("/singletons/my_converter") ) >>= x
) );
274 OSL_VERIFY( xContext
->getValueByName( OUSTR("/singletons/com.sun.star.script.theConverter") ) >>= x
);
275 OSL_VERIFY( xContext
->getValueByName( OUSTR("/singletons/com.sun.star.bootstrap.theTestComponent0") ) >>= x
);
277 ::fprintf( stderr
, "> registering service...\n", n
);
278 #if defined(SAL_W32) || defined(SAL_OS2)
279 OUString
libName( OUSTR("cfg_test.dll") );
280 #elif defined(SAL_UNX)
281 OUString
libName( OUSTR("libcfg_test.so") );
283 Reference
< registry::XImplementationRegistration
> xImplReg( xMgr
->createInstanceWithContext(
284 OUSTR("com.sun.star.registry.ImplementationRegistration"), xContext
), UNO_QUERY
);
285 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
286 xImplReg
->registerImplementation(
287 OUSTR("com.sun.star.loader.SharedLibrary"), libName
,
288 Reference
< registry::XSimpleRegistry
>() );
290 OSL_VERIFY( (x
= xMgr
->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent0"), xContext
)).is() );
291 OSL_VERIFY( (x
= xMgr
->createInstanceWithContext( OUSTR("com.sun.star.bootstrap.TestComponent1"), xContext
)).is() );
293 Reference
< lang::XComponent
> xComp( xContext
, UNO_QUERY
);
300 catch (Exception
& exc
)
302 OString
str( OUStringToOString( exc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
303 ::fprintf( stderr
, "# caught exception: %s\n", str
.getStr() );