Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / stoc / test / tdmanager / testtdmanager.cxx
blob9e45fb3ff27c6a75d0f200949d112f42536404c6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/implbase1.hxx"
43 #include "cppuhelper/weak.hxx"
44 #include "osl/file.h"
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"
51 #include "uno/lbnames.h"
53 #include /*MSVC trouble: <cstdlib>*/ <stdlib.h>
54 #include <iostream>
55 #include <ostream>
57 namespace {
59 class Service: public cppu::WeakImplHelper1< css::lang::XMain > {
60 public:
61 virtual sal_Int32 SAL_CALL
62 run(css::uno::Sequence< rtl::OUString > const & arguments)
63 throw (css::uno::RuntimeException);
65 static rtl::OUString getImplementationName();
67 static css::uno::Sequence< rtl::OUString > getSupportedServiceNames();
69 static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
70 css::uno::Reference< css::uno::XComponentContext > const & context)
71 throw (css::uno::Exception);
73 private:
74 explicit Service(
75 css::uno::Reference< css::uno::XComponentContext > const & context):
76 m_context(context)
79 css::uno::Reference< css::uno::XComponentContext > m_context;
84 namespace {
86 std::ostream & operator <<(std::ostream & out, rtl::OUString const & value) {
87 return out << rtl::OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr();
90 void assertTrue(bool argument) {
91 if (!argument) {
92 std::cerr
93 << "assertTrue(" << argument << ") failed" << std::endl;
94 /*MSVC trouble: std::*/abort();
98 void assertFalse(bool argument) {
99 if (argument) {
100 std::cerr
101 << "assertFalse(" << argument << ") failed" << std::endl;
102 /*MSVC trouble: std::*/abort();
106 template< typename T > void assertEqual(T const & value, T const & argument) {
107 if (argument != value) {
108 std::cerr
109 << "assertEqual(" << value << ", " << argument << ") failed"
110 << std::endl;
111 /*MSVC trouble: std::*/abort();
117 sal_Int32 Service::run(css::uno::Sequence< rtl::OUString > const & arguments)
118 throw (css::uno::RuntimeException)
120 css::uno::Reference< css::lang::XMultiComponentFactory > factory(
121 m_context->getServiceManager());
122 assertTrue(factory.is());
123 css::uno::Reference< css::container::XHierarchicalNameAccess > manager(
124 m_context->getValueByName(
125 rtl::OUString(
126 "/singletons/"
127 "com.sun.star.reflection.theTypeDescriptionManager")),
128 css::uno::UNO_QUERY_THROW);
130 ////////////////////////////////////////
131 // test: add cmd line rdbs to manager
132 ////////////////////////////////////////
134 OSL_ASSERT( arguments.getLength() > 0 );
135 css::uno::Reference<css::container::XSet> xSet(
136 manager, css::uno::UNO_QUERY_THROW );
137 for ( sal_Int32 argPos = 0; argPos < arguments.getLength(); ++argPos ) {
138 rtl::OUString url;
139 OSL_VERIFY( osl_File_E_None == osl_getFileURLFromSystemPath(
140 arguments[argPos].pData, &url.pData ) );
141 bool supposedToBeCompatible = ! url.endsWithIgnoreAsciiCaseAsciiL(
142 RTL_CONSTASCII_STRINGPARAM("_incomp.rdb") );
144 css::uno::Reference<css::registry::XSimpleRegistry> xReg(
145 m_context->getServiceManager()->createInstanceWithContext(
146 rtl::OUString( "com.sun.star.registry.SimpleRegistry" ),
147 m_context ), css::uno::UNO_QUERY_THROW );
148 xReg->open( url, true /* read-only */, false /* ! create */ );
149 css::uno::Any arg( css::uno::makeAny(xReg) );
150 css::uno::Reference<css::container::XHierarchicalNameAccess> xTDprov(
151 m_context->getServiceManager()->
152 createInstanceWithArgumentsAndContext(
153 rtl::OUString( "com.sun.star.comp.stoc."
154 "RegistryTypeDescriptionProvider" ),
155 css::uno::Sequence<css::uno::Any>( &arg, 1 ), m_context ),
156 css::uno::UNO_QUERY_THROW );
157 try {
158 xSet->insert( css::uno::makeAny(xTDprov) );
159 if (! supposedToBeCompatible)
160 std::cerr << "current rdb file: " <<
161 rtl::OUStringToOString(
162 url, osl_getThreadTextEncoding()).getStr() << std::endl;
163 assertTrue(supposedToBeCompatible);
164 } catch (css::lang::IllegalArgumentException &) {
165 if (supposedToBeCompatible)
166 throw;
167 assertFalse(supposedToBeCompatible);
171 ///////
173 css::uno::Reference< css::reflection::XIndirectTypeDescription > sequence(
174 manager->getByHierarchicalName(
175 rtl::OUString("[][]boolean")),
176 css::uno::UNO_QUERY_THROW);
177 assertEqual(css::uno::TypeClass_SEQUENCE, sequence->getTypeClass());
178 assertEqual(
179 rtl::OUString("[][]boolean"),
180 sequence->getName());
181 assertEqual(
182 rtl::OUString("[]boolean"),
183 sequence->getReferencedType()->getName());
185 css::uno::Reference< css::reflection::XStructTypeDescription > structure(
186 manager->getByHierarchicalName(
187 rtl::OUString( "test.tdmanager.Struct<boolean,test.tdmanager.Struct<"
188 "any,com.sun.star.uno.XInterface>>")),
189 css::uno::UNO_QUERY_THROW);
190 assertEqual(css::uno::TypeClass_STRUCT, structure->getTypeClass());
191 assertEqual(
192 rtl::OUString( "test.tdmanager.Struct<boolean,test.tdmanager.Struct<"
193 "any,com.sun.star.uno.XInterface>>"),
194 structure->getName());
195 assertEqual< bool >(false, structure->getBaseType().is());
196 assertEqual< sal_Int32 >(1, structure->getMemberTypes().getLength());
197 assertEqual(
198 rtl::OUString( "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>"),
199 structure->getMemberTypes()[0]->getName());
200 assertEqual< sal_Int32 >(1, structure->getMemberNames().getLength());
201 assertEqual(
202 rtl::OUString("s"),
203 structure->getMemberNames()[0]);
204 assertEqual< sal_Int32 >(0, structure->getTypeParameters().getLength());
205 assertEqual< sal_Int32 >(2, structure->getTypeArguments().getLength());
206 assertEqual(
207 rtl::OUString("boolean"),
208 structure->getTypeArguments()[0]->getName());
209 assertEqual(
210 rtl::OUString( "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>"),
211 structure->getTypeArguments()[1]->getName());
213 css::uno::Reference< css::reflection::XInterfaceMethodTypeDescription >
214 method(
215 manager->getByHierarchicalName(
216 rtl::OUString( "com.sun.star.uno.XComponentContext::getValueByName")),
217 css::uno::UNO_QUERY_THROW);
218 assertEqual(css::uno::TypeClass_INTERFACE_METHOD, method->getTypeClass());
219 assertEqual(
220 rtl::OUString( "com.sun.star.uno.XComponentContext::getValueByName"),
221 method->getName());
222 assertEqual(
223 rtl::OUString("getValueByName"),
224 method->getMemberName());
225 assertEqual< sal_Int32 >(3, method->getPosition());
226 assertEqual(
227 rtl::OUString("any"),
228 method->getReturnType()->getName());
229 assertEqual< bool >(false, method->isOneway());
230 assertEqual< sal_Int32 >(1, method->getParameters().getLength());
231 assertEqual(
232 rtl::OUString("Name"),
233 method->getParameters()[0]->getName());
234 assertEqual(
235 rtl::OUString("string"),
236 method->getParameters()[0]->getType()->getName());
237 assertEqual< bool >(true, method->getParameters()[0]->isIn());
238 assertEqual< bool >(false, method->getParameters()[0]->isOut());
239 assertEqual< sal_Int32 >(0, method->getParameters()[0]->getPosition());
240 assertEqual< sal_Int32 >(0, method->getExceptions().getLength());
242 assertFalse(
243 css::uno::Reference< css::reflection::XPublished >(
244 css::uno::Reference< css::reflection::XTypeDescription >(
245 manager->getByHierarchicalName(
246 rtl::OUString("[]boolean")),
247 css::uno::UNO_QUERY_THROW),
248 css::uno::UNO_QUERY).is());
249 assertFalse(
250 css::uno::Reference< css::reflection::XPublished >(
251 css::uno::Reference< css::reflection::XTypeDescription >(
252 manager->getByHierarchicalName(
253 rtl::OUString( "com.sun.star.beans.XIntroTest::ObjectName")),
254 css::uno::UNO_QUERY_THROW),
255 css::uno::UNO_QUERY).is());
256 assertFalse(
257 css::uno::Reference< css::reflection::XPublished >(
258 css::uno::Reference< css::reflection::XTypeDescription >(
259 manager->getByHierarchicalName(
260 rtl::OUString( "com.sun.star.beans.XIntroTest::writeln")),
261 css::uno::UNO_QUERY_THROW),
262 css::uno::UNO_QUERY).is());
263 //TODO: check that the reflection of a property of an accumulation-based
264 // service does not support XPublished
266 return 0;
269 rtl::OUString Service::getImplementationName() {
270 return rtl::OUString("test.tdmanager.impl");
273 css::uno::Sequence< rtl::OUString > Service::getSupportedServiceNames() {
274 return css::uno::Sequence< rtl::OUString >();
277 css::uno::Reference< css::uno::XInterface > Service::createInstance(
278 css::uno::Reference< css::uno::XComponentContext > const & context)
279 throw (css::uno::Exception)
281 return static_cast< cppu::OWeakObject * >(new Service(context));
284 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(char const * implName,
285 void * serviceManager, void *) {
286 void * p = 0;
287 if (serviceManager != 0) {
288 css::uno::Reference< css::lang::XSingleComponentFactory > f;
289 if (Service::getImplementationName().equalsAscii(implName)) {
290 f = cppu::createSingleComponentFactory(
291 &Service::createInstance, Service::getImplementationName(),
292 Service::getSupportedServiceNames());
294 if (f.is()) {
295 f->acquire();
296 p = f.get();
299 return p;
302 namespace {
304 bool writeInfo(void * registryKey, rtl::OUString const & implementationName,
305 css::uno::Sequence< rtl::OUString > const & serviceNames) {
306 rtl::OUString keyName("/");
307 keyName += implementationName;
308 keyName += rtl::OUString("/UNO/SERVICES");
309 css::uno::Reference< css::registry::XRegistryKey > key;
310 try {
311 key = static_cast< css::registry::XRegistryKey * >(registryKey)->
312 createKey(keyName);
313 } catch (css::registry::InvalidRegistryException &) {}
314 if (!key.is()) {
315 return false;
317 bool success = true;
318 for (sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
319 try {
320 key->createKey(serviceNames[i]);
321 } catch (css::registry::InvalidRegistryException &) {
322 success = false;
323 break;
326 return success;
331 extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * registryKey) {
332 return registryKey
333 && writeInfo(registryKey, Service::getImplementationName(),
334 Service::getSupportedServiceNames());
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */