1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
22 #include <com/sun/star/container/XSet.hpp>
23 #include <com/sun/star/lang/XMain.hpp>
24 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <com/sun/star/reflection/XIndirectTypeDescription.hpp>
27 #include <com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp>
28 #include <com/sun/star/reflection/XPublished.hpp>
29 #include <com/sun/star/reflection/XStructTypeDescription.hpp>
30 #include <com/sun/star/reflection/XTypeDescription.hpp>
31 #include <com/sun/star/registry/InvalidRegistryException.hpp>
32 #include <com/sun/star/registry/XRegistryKey.hpp>
33 #include <com/sun/star/registry/XSimpleRegistry.hpp>
34 #include <com/sun/star/uno/Exception.hpp>
35 #include <com/sun/star/uno/Reference.hxx>
36 #include <com/sun/star/uno/RuntimeException.hpp>
37 #include <com/sun/star/uno/Sequence.hxx>
38 #include <com/sun/star/uno/TypeClass.hpp>
39 #include <com/sun/star/uno/XComponentContext.hpp>
40 #include <com/sun/star/uno/XInterface.hpp>
41 #include <cppuhelper/factory.hxx>
42 #include <cppuhelper/implbase.hxx>
43 #include <cppuhelper/weak.hxx>
45 #include <osl/thread.h>
46 #include <rtl/textenc.h>
47 #include <rtl/ustring.h>
48 #include <rtl/ustring.hxx>
49 #include <sal/types.h>
50 #include <uno/environment.h>
52 #include /*MSVC trouble: <cstdlib>*/ <stdlib.h>
58 class Service
: public cppu::WeakImplHelper
< css::lang::XMain
> {
60 virtual sal_Int32 SAL_CALL
61 run(css::uno::Sequence
< OUString
> const & arguments
)
62 throw (css::uno::RuntimeException
);
64 static OUString
getImplementationName();
66 static css::uno::Sequence
< OUString
> getSupportedServiceNames();
68 static css::uno::Reference
< css::uno::XInterface
> SAL_CALL
createInstance(
69 css::uno::Reference
< css::uno::XComponentContext
> const & context
)
70 throw (css::uno::Exception
);
74 css::uno::Reference
< css::uno::XComponentContext
> const & context
):
78 css::uno::Reference
< css::uno::XComponentContext
> m_context
;
85 std::ostream
& operator <<(std::ostream
& out
, OUString
const & value
) {
86 return out
<< OUStringToOString(value
, RTL_TEXTENCODING_UTF8
).getStr();
89 void assertTrue(bool argument
) {
92 << "assertTrue(" << argument
<< ") failed" << std::endl
;
93 /*MSVC trouble: std::*/abort();
97 void assertFalse(bool argument
) {
100 << "assertFalse(" << argument
<< ") failed" << std::endl
;
101 /*MSVC trouble: std::*/abort();
105 template< typename T
> void assertEqual(T
const & value
, T
const & argument
) {
106 if (argument
!= value
) {
108 << "assertEqual(" << value
<< ", " << argument
<< ") failed"
110 /*MSVC trouble: std::*/abort();
116 sal_Int32
Service::run(css::uno::Sequence
< OUString
> const & arguments
)
117 throw (css::uno::RuntimeException
)
119 css::uno::Reference
< css::lang::XMultiComponentFactory
> factory(
120 m_context
->getServiceManager());
121 assertTrue(factory
.is());
122 css::uno::Reference
< css::container::XHierarchicalNameAccess
> manager(
123 m_context
->getValueByName(
126 "com.sun.star.reflection.theTypeDescriptionManager")),
127 css::uno::UNO_QUERY_THROW
);
130 // test: add cmd line rdbs to manager
133 OSL_ASSERT( arguments
.getLength() > 0 );
134 css::uno::Reference
<css::container::XSet
> xSet(
135 manager
, css::uno::UNO_QUERY_THROW
);
136 for ( sal_Int32 argPos
= 0; argPos
< arguments
.getLength(); ++argPos
) {
138 OSL_VERIFY( osl_File_E_None
== osl_getFileURLFromSystemPath(
139 arguments
[argPos
].pData
, &url
.pData
) );
140 bool supposedToBeCompatible
= ! url
.endsWithIgnoreAsciiCase("_incomp.rdb");
142 css::uno::Reference
<css::registry::XSimpleRegistry
> xReg(
143 m_context
->getServiceManager()->createInstanceWithContext(
144 "com.sun.star.registry.SimpleRegistry",
145 m_context
), css::uno::UNO_QUERY_THROW
);
146 xReg
->open( url
, true /* read-only */, false /* ! create */ );
147 css::uno::Any
arg( css::uno::makeAny(xReg
) );
148 css::uno::Reference
<css::container::XHierarchicalNameAccess
> xTDprov(
149 m_context
->getServiceManager()->
150 createInstanceWithArgumentsAndContext(
151 "com.sun.star.comp.stoc.RegistryTypeDescriptionProvider",
152 css::uno::Sequence
<css::uno::Any
>( &arg
, 1 ), m_context
),
153 css::uno::UNO_QUERY_THROW
);
155 xSet
->insert( css::uno::makeAny(xTDprov
) );
156 if (! supposedToBeCompatible
)
157 std::cerr
<< "current rdb file: " <<
159 url
, osl_getThreadTextEncoding()).getStr() << std::endl
;
160 assertTrue(supposedToBeCompatible
);
161 } catch (css::lang::IllegalArgumentException
&) {
162 if (supposedToBeCompatible
)
164 assertFalse(supposedToBeCompatible
);
170 css::uno::Reference
< css::reflection::XIndirectTypeDescription
> sequence(
171 manager
->getByHierarchicalName(
172 OUString("[][]boolean")),
173 css::uno::UNO_QUERY_THROW
);
174 assertEqual(css::uno::TypeClass_SEQUENCE
, sequence
->getTypeClass());
176 OUString("[][]boolean"),
177 sequence
->getName());
179 OUString("[]boolean"),
180 sequence
->getReferencedType()->getName());
182 css::uno::Reference
< css::reflection::XStructTypeDescription
> structure(
183 manager
->getByHierarchicalName(
184 OUString( "test.tdmanager.Struct<boolean,test.tdmanager.Struct<"
185 "any,com.sun.star.uno.XInterface>>")),
186 css::uno::UNO_QUERY_THROW
);
187 assertEqual(css::uno::TypeClass_STRUCT
, structure
->getTypeClass());
189 OUString( "test.tdmanager.Struct<boolean,test.tdmanager.Struct<"
190 "any,com.sun.star.uno.XInterface>>"),
191 structure
->getName());
192 assertEqual
< bool >(false, structure
->getBaseType().is());
193 assertEqual
< sal_Int32
>(1, structure
->getMemberTypes().getLength());
195 OUString( "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>"),
196 structure
->getMemberTypes()[0]->getName());
197 assertEqual
< sal_Int32
>(1, structure
->getMemberNames().getLength());
200 structure
->getMemberNames()[0]);
201 assertEqual
< sal_Int32
>(0, structure
->getTypeParameters().getLength());
202 assertEqual
< sal_Int32
>(2, structure
->getTypeArguments().getLength());
205 structure
->getTypeArguments()[0]->getName());
207 OUString( "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>"),
208 structure
->getTypeArguments()[1]->getName());
210 css::uno::Reference
< css::reflection::XInterfaceMethodTypeDescription
>
212 manager
->getByHierarchicalName(
213 OUString( "com.sun.star.uno.XComponentContext::getValueByName")),
214 css::uno::UNO_QUERY_THROW
);
215 assertEqual(css::uno::TypeClass_INTERFACE_METHOD
, method
->getTypeClass());
217 OUString( "com.sun.star.uno.XComponentContext::getValueByName"),
220 OUString("getValueByName"),
221 method
->getMemberName());
222 assertEqual
< sal_Int32
>(3, method
->getPosition());
225 method
->getReturnType()->getName());
226 assertEqual
< bool >(false, method
->isOneway());
227 assertEqual
< sal_Int32
>(1, method
->getParameters().getLength());
230 method
->getParameters()[0]->getName());
233 method
->getParameters()[0]->getType()->getName());
234 assertEqual
< bool >(true, method
->getParameters()[0]->isIn());
235 assertEqual
< bool >(false, method
->getParameters()[0]->isOut());
236 assertEqual
< sal_Int32
>(0, method
->getParameters()[0]->getPosition());
237 assertEqual
< sal_Int32
>(0, method
->getExceptions().getLength());
240 css::uno::Reference
< css::reflection::XPublished
>(
241 css::uno::Reference
< css::reflection::XTypeDescription
>(
242 manager
->getByHierarchicalName(
243 OUString("[]boolean")),
244 css::uno::UNO_QUERY_THROW
),
245 css::uno::UNO_QUERY
).is());
247 css::uno::Reference
< css::reflection::XPublished
>(
248 css::uno::Reference
< css::reflection::XTypeDescription
>(
249 manager
->getByHierarchicalName(
250 OUString( "com.sun.star.beans.XIntroTest::ObjectName")),
251 css::uno::UNO_QUERY_THROW
),
252 css::uno::UNO_QUERY
).is());
254 css::uno::Reference
< css::reflection::XPublished
>(
255 css::uno::Reference
< css::reflection::XTypeDescription
>(
256 manager
->getByHierarchicalName(
257 OUString( "com.sun.star.beans.XIntroTest::writeln")),
258 css::uno::UNO_QUERY_THROW
),
259 css::uno::UNO_QUERY
).is());
260 //TODO: check that the reflection of a property of an accumulation-based
261 // service does not support XPublished
266 OUString
Service::getImplementationName() {
267 return OUString("test.tdmanager.impl");
270 css::uno::Sequence
< OUString
> Service::getSupportedServiceNames() {
271 return css::uno::Sequence
< OUString
>();
274 css::uno::Reference
< css::uno::XInterface
> Service::createInstance(
275 css::uno::Reference
< css::uno::XComponentContext
> const & context
)
276 throw (css::uno::Exception
)
278 return static_cast< cppu::OWeakObject
* >(new Service(context
));
281 extern "C" SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory(char const * implName
,
282 void * serviceManager
, void *) {
284 if (serviceManager
!= 0) {
285 css::uno::Reference
< css::lang::XSingleComponentFactory
> f
;
286 if (Service::getImplementationName().equalsAscii(implName
)) {
287 f
= cppu::createSingleComponentFactory(
288 &Service::createInstance
, Service::getImplementationName(),
289 Service::getSupportedServiceNames());
301 bool writeInfo(void * registryKey
, OUString
const & implementationName
,
302 css::uno::Sequence
< OUString
> const & serviceNames
) {
303 OUString keyName
= "/" + implementationName
+ "/UNO/SERVICES";
304 css::uno::Reference
< css::registry::XRegistryKey
> key
;
306 key
= static_cast< css::registry::XRegistryKey
* >(registryKey
)->
308 } catch (css::registry::InvalidRegistryException
&) {}
313 for (sal_Int32 i
= 0; i
< serviceNames
.getLength(); ++i
) {
315 key
->createKey(serviceNames
[i
]);
316 } catch (css::registry::InvalidRegistryException
&) {
326 extern "C" sal_Bool SAL_CALL
component_writeInfo(void *, void * registryKey
) {
328 && writeInfo(registryKey
, Service::getImplementationName(),
329 Service::getSupportedServiceNames());
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */