update dev300-m58
[ooovba.git] / stoc / test / tdmanager / testtdmanager.cxx
blobb3724baeef99e64b49f6f4638e4d53e9fab6693f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testtdmanager.cxx,v $
10 * $Revision: 1.8 $
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_stoc.hxx"
34 #include "com/sun/star/container/XHierarchicalNameAccess.hpp"
35 #include "com/sun/star/container/XSet.hpp"
36 #include "com/sun/star/lang/XMain.hpp"
37 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
38 #include "com/sun/star/lang/IllegalArgumentException.hpp"
39 #include "com/sun/star/reflection/XIndirectTypeDescription.hpp"
40 #include "com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp"
41 #include "com/sun/star/reflection/XPublished.hpp"
42 #include "com/sun/star/reflection/XStructTypeDescription.hpp"
43 #include "com/sun/star/reflection/XTypeDescription.hpp"
44 #include "com/sun/star/registry/InvalidRegistryException.hpp"
45 #include "com/sun/star/registry/XRegistryKey.hpp"
46 #include "com/sun/star/registry/XSimpleRegistry.hpp"
47 #include "com/sun/star/uno/Exception.hpp"
48 #include "com/sun/star/uno/Reference.hxx"
49 #include "com/sun/star/uno/RuntimeException.hpp"
50 #include "com/sun/star/uno/Sequence.hxx"
51 #include "com/sun/star/uno/TypeClass.hpp"
52 #include "com/sun/star/uno/XComponentContext.hpp"
53 #include "com/sun/star/uno/XInterface.hpp"
54 #include "cppuhelper/factory.hxx"
55 #include "cppuhelper/implbase1.hxx"
56 #include "cppuhelper/weak.hxx"
57 #include "osl/file.h"
58 #include "osl/thread.h"
59 #include "rtl/textenc.h"
60 #include "rtl/ustring.h"
61 #include "rtl/ustring.hxx"
62 #include "sal/types.h"
63 #include "uno/environment.h"
64 #include "uno/lbnames.h"
66 #include /*MSVC trouble: <cstdlib>*/ <stdlib.h>
67 #include <iostream>
68 #include <ostream>
70 namespace css = com::sun::star;
72 namespace {
74 class Service: public cppu::WeakImplHelper1< css::lang::XMain > {
75 public:
76 virtual sal_Int32 SAL_CALL
77 run(css::uno::Sequence< rtl::OUString > const & arguments)
78 throw (css::uno::RuntimeException);
80 static rtl::OUString getImplementationName();
82 static css::uno::Sequence< rtl::OUString > getSupportedServiceNames();
84 static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
85 css::uno::Reference< css::uno::XComponentContext > const & context)
86 throw (css::uno::Exception);
88 private:
89 explicit Service(
90 css::uno::Reference< css::uno::XComponentContext > const & context):
91 m_context(context)
94 css::uno::Reference< css::uno::XComponentContext > m_context;
99 namespace {
101 std::ostream & operator <<(std::ostream & out, rtl::OUString const & value) {
102 return out << rtl::OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr();
105 void assertTrue(bool argument) {
106 if (!argument) {
107 std::cerr
108 << "assertTrue(" << argument << ") failed" << std::endl;
109 /*MSVC trouble: std::*/abort();
113 void assertFalse(bool argument) {
114 if (argument) {
115 std::cerr
116 << "assertFalse(" << argument << ") failed" << std::endl;
117 /*MSVC trouble: std::*/abort();
121 template< typename T > void assertEqual(T const & value, T const & argument) {
122 if (argument != value) {
123 std::cerr
124 << "assertEqual(" << value << ", " << argument << ") failed"
125 << std::endl;
126 /*MSVC trouble: std::*/abort();
132 sal_Int32 Service::run(css::uno::Sequence< rtl::OUString > const & arguments)
133 throw (css::uno::RuntimeException)
135 css::uno::Reference< css::lang::XMultiComponentFactory > factory(
136 m_context->getServiceManager());
137 assertTrue(factory.is());
138 css::uno::Reference< css::container::XHierarchicalNameAccess > manager(
139 m_context->getValueByName(
140 rtl::OUString(
141 RTL_CONSTASCII_USTRINGPARAM(
142 "/singletons/"
143 "com.sun.star.reflection.theTypeDescriptionManager"))),
144 css::uno::UNO_QUERY_THROW);
146 ////////////////////////////////////////
147 // test: add cmd line rdbs to manager
148 ////////////////////////////////////////
150 OSL_ASSERT( arguments.getLength() > 0 );
151 css::uno::Reference<css::container::XSet> xSet(
152 manager, css::uno::UNO_QUERY_THROW );
153 for ( sal_Int32 argPos = 0; argPos < arguments.getLength(); ++argPos ) {
154 rtl::OUString url;
155 OSL_VERIFY( osl_File_E_None == osl_getFileURLFromSystemPath(
156 arguments[argPos].pData, &url.pData ) );
157 bool supposedToBeCompatible = ! url.endsWithIgnoreAsciiCaseAsciiL(
158 RTL_CONSTASCII_STRINGPARAM("_incomp.rdb") );
160 css::uno::Reference<css::registry::XSimpleRegistry> xReg(
161 m_context->getServiceManager()->createInstanceWithContext(
162 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
163 "com.sun.star.registry.SimpleRegistry") ),
164 m_context ), css::uno::UNO_QUERY_THROW );
165 xReg->open( url, true /* read-only */, false /* ! create */ );
166 css::uno::Any arg( css::uno::makeAny(xReg) );
167 css::uno::Reference<css::container::XHierarchicalNameAccess> xTDprov(
168 m_context->getServiceManager()->
169 createInstanceWithArgumentsAndContext(
170 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
171 "com.sun.star.comp.stoc."
172 "RegistryTypeDescriptionProvider") ),
173 css::uno::Sequence<css::uno::Any>( &arg, 1 ), m_context ),
174 css::uno::UNO_QUERY_THROW );
175 try {
176 xSet->insert( css::uno::makeAny(xTDprov) );
177 if (! supposedToBeCompatible)
178 std::cerr << "current rdb file: " <<
179 rtl::OUStringToOString(
180 url, osl_getThreadTextEncoding()).getStr() << std::endl;
181 assertTrue(supposedToBeCompatible);
182 } catch (css::lang::IllegalArgumentException &) {
183 if (supposedToBeCompatible)
184 throw;
185 assertFalse(supposedToBeCompatible);
189 ///////
191 css::uno::Reference< css::reflection::XIndirectTypeDescription > sequence(
192 manager->getByHierarchicalName(
193 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[][]boolean"))),
194 css::uno::UNO_QUERY_THROW);
195 assertEqual(css::uno::TypeClass_SEQUENCE, sequence->getTypeClass());
196 assertEqual(
197 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[][]boolean")),
198 sequence->getName());
199 assertEqual(
200 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[]boolean")),
201 sequence->getReferencedType()->getName());
203 css::uno::Reference< css::reflection::XStructTypeDescription > structure(
204 manager->getByHierarchicalName(
205 rtl::OUString(
206 RTL_CONSTASCII_USTRINGPARAM(
207 "test.tdmanager.Struct<boolean,test.tdmanager.Struct<"
208 "any,com.sun.star.uno.XInterface>>"))),
209 css::uno::UNO_QUERY_THROW);
210 assertEqual(css::uno::TypeClass_STRUCT, structure->getTypeClass());
211 assertEqual(
212 rtl::OUString(
213 RTL_CONSTASCII_USTRINGPARAM(
214 "test.tdmanager.Struct<boolean,test.tdmanager.Struct<"
215 "any,com.sun.star.uno.XInterface>>")),
216 structure->getName());
217 assertEqual< bool >(false, structure->getBaseType().is());
218 assertEqual< sal_Int32 >(1, structure->getMemberTypes().getLength());
219 assertEqual(
220 rtl::OUString(
221 RTL_CONSTASCII_USTRINGPARAM(
222 "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>")),
223 structure->getMemberTypes()[0]->getName());
224 assertEqual< sal_Int32 >(1, structure->getMemberNames().getLength());
225 assertEqual(
226 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("s")),
227 structure->getMemberNames()[0]);
228 assertEqual< sal_Int32 >(0, structure->getTypeParameters().getLength());
229 assertEqual< sal_Int32 >(2, structure->getTypeArguments().getLength());
230 assertEqual(
231 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("boolean")),
232 structure->getTypeArguments()[0]->getName());
233 assertEqual(
234 rtl::OUString(
235 RTL_CONSTASCII_USTRINGPARAM(
236 "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>")),
237 structure->getTypeArguments()[1]->getName());
239 css::uno::Reference< css::reflection::XInterfaceMethodTypeDescription >
240 method(
241 manager->getByHierarchicalName(
242 rtl::OUString(
243 RTL_CONSTASCII_USTRINGPARAM(
244 "com.sun.star.uno.XComponentContext::getValueByName"))),
245 css::uno::UNO_QUERY_THROW);
246 assertEqual(css::uno::TypeClass_INTERFACE_METHOD, method->getTypeClass());
247 assertEqual(
248 rtl::OUString(
249 RTL_CONSTASCII_USTRINGPARAM(
250 "com.sun.star.uno.XComponentContext::getValueByName")),
251 method->getName());
252 assertEqual(
253 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("getValueByName")),
254 method->getMemberName());
255 assertEqual< sal_Int32 >(3, method->getPosition());
256 assertEqual(
257 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("any")),
258 method->getReturnType()->getName());
259 assertEqual< bool >(false, method->isOneway());
260 assertEqual< sal_Int32 >(1, method->getParameters().getLength());
261 assertEqual(
262 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Name")),
263 method->getParameters()[0]->getName());
264 assertEqual(
265 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("string")),
266 method->getParameters()[0]->getType()->getName());
267 assertEqual< bool >(true, method->getParameters()[0]->isIn());
268 assertEqual< bool >(false, method->getParameters()[0]->isOut());
269 assertEqual< sal_Int32 >(0, method->getParameters()[0]->getPosition());
270 assertEqual< sal_Int32 >(0, method->getExceptions().getLength());
272 assertFalse(
273 css::uno::Reference< css::reflection::XPublished >(
274 css::uno::Reference< css::reflection::XTypeDescription >(
275 manager->getByHierarchicalName(
276 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[]boolean"))),
277 css::uno::UNO_QUERY_THROW),
278 css::uno::UNO_QUERY).is());
279 assertFalse(
280 css::uno::Reference< css::reflection::XPublished >(
281 css::uno::Reference< css::reflection::XTypeDescription >(
282 manager->getByHierarchicalName(
283 rtl::OUString(
284 RTL_CONSTASCII_USTRINGPARAM(
285 "com.sun.star.beans.XIntroTest::ObjectName"))),
286 css::uno::UNO_QUERY_THROW),
287 css::uno::UNO_QUERY).is());
288 assertFalse(
289 css::uno::Reference< css::reflection::XPublished >(
290 css::uno::Reference< css::reflection::XTypeDescription >(
291 manager->getByHierarchicalName(
292 rtl::OUString(
293 RTL_CONSTASCII_USTRINGPARAM(
294 "com.sun.star.beans.XIntroTest::writeln"))),
295 css::uno::UNO_QUERY_THROW),
296 css::uno::UNO_QUERY).is());
297 //TODO: check that the reflection of a property of an accumulation-based
298 // service does not support XPublished
300 return 0;
303 rtl::OUString Service::getImplementationName() {
304 return rtl::OUString::createFromAscii("test.tdmanager.impl");
307 css::uno::Sequence< rtl::OUString > Service::getSupportedServiceNames() {
308 return css::uno::Sequence< rtl::OUString >();
311 css::uno::Reference< css::uno::XInterface > Service::createInstance(
312 css::uno::Reference< css::uno::XComponentContext > const & context)
313 throw (css::uno::Exception)
315 return static_cast< cppu::OWeakObject * >(new Service(context));
318 extern "C" void SAL_CALL component_getImplementationEnvironment(
319 char const ** envTypeName, uno_Environment **)
321 if (envTypeName != 0) {
322 *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
326 extern "C" void * SAL_CALL component_getFactory(char const * implName,
327 void * serviceManager, void *) {
328 void * p = 0;
329 if (serviceManager != 0) {
330 css::uno::Reference< css::lang::XSingleComponentFactory > f;
331 if (Service::getImplementationName().equalsAscii(implName)) {
332 f = cppu::createSingleComponentFactory(
333 &Service::createInstance, Service::getImplementationName(),
334 Service::getSupportedServiceNames());
336 if (f.is()) {
337 f->acquire();
338 p = f.get();
341 return p;
344 namespace {
346 bool writeInfo(void * registryKey, rtl::OUString const & implementationName,
347 css::uno::Sequence< rtl::OUString > const & serviceNames) {
348 rtl::OUString keyName(rtl::OUString::createFromAscii("/"));
349 keyName += implementationName;
350 keyName += rtl::OUString::createFromAscii("/UNO/SERVICES");
351 css::uno::Reference< css::registry::XRegistryKey > key;
352 try {
353 key = static_cast< css::registry::XRegistryKey * >(registryKey)->
354 createKey(keyName);
355 } catch (css::registry::InvalidRegistryException &) {}
356 if (!key.is()) {
357 return false;
359 bool success = true;
360 for (sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
361 try {
362 key->createKey(serviceNames[i]);
363 } catch (css::registry::InvalidRegistryException &) {
364 success = false;
365 break;
368 return success;
373 extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * registryKey) {
374 return registryKey
375 && writeInfo(registryKey, Service::getImplementationName(),
376 Service::getSupportedServiceNames());