xref: /trunk/main/stoc/test/tdmanager/testtdmanager.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_stoc.hxx"
26 
27 #include "com/sun/star/container/XHierarchicalNameAccess.hpp"
28 #include "com/sun/star/container/XSet.hpp"
29 #include "com/sun/star/lang/XMain.hpp"
30 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
31 #include "com/sun/star/lang/IllegalArgumentException.hpp"
32 #include "com/sun/star/reflection/XIndirectTypeDescription.hpp"
33 #include "com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp"
34 #include "com/sun/star/reflection/XPublished.hpp"
35 #include "com/sun/star/reflection/XStructTypeDescription.hpp"
36 #include "com/sun/star/reflection/XTypeDescription.hpp"
37 #include "com/sun/star/registry/InvalidRegistryException.hpp"
38 #include "com/sun/star/registry/XRegistryKey.hpp"
39 #include "com/sun/star/registry/XSimpleRegistry.hpp"
40 #include "com/sun/star/uno/Exception.hpp"
41 #include "com/sun/star/uno/Reference.hxx"
42 #include "com/sun/star/uno/RuntimeException.hpp"
43 #include "com/sun/star/uno/Sequence.hxx"
44 #include "com/sun/star/uno/TypeClass.hpp"
45 #include "com/sun/star/uno/XComponentContext.hpp"
46 #include "com/sun/star/uno/XInterface.hpp"
47 #include "cppuhelper/factory.hxx"
48 #include "cppuhelper/implbase1.hxx"
49 #include "cppuhelper/weak.hxx"
50 #include "osl/file.h"
51 #include "osl/thread.h"
52 #include "rtl/textenc.h"
53 #include "rtl/ustring.h"
54 #include "rtl/ustring.hxx"
55 #include "sal/types.h"
56 #include "uno/environment.h"
57 #include "uno/lbnames.h"
58 
59 #include /*MSVC trouble: <cstdlib>*/ <stdlib.h>
60 #include <iostream>
61 #include <ostream>
62 
63 namespace css = com::sun::star;
64 
65 namespace {
66 
67 class Service: public cppu::WeakImplHelper1< css::lang::XMain > {
68 public:
69     virtual sal_Int32 SAL_CALL
70     run(css::uno::Sequence< rtl::OUString > const & arguments);
71 
72     static rtl::OUString getImplementationName();
73 
74     static css::uno::Sequence< rtl::OUString > getSupportedServiceNames();
75 
76     static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
77         css::uno::Reference< css::uno::XComponentContext > const & context);
78 
79 private:
Service(css::uno::Reference<css::uno::XComponentContext> const & context)80     explicit Service(
81         css::uno::Reference< css::uno::XComponentContext > const & context):
82         m_context(context)
83     {}
84 
85     css::uno::Reference< css::uno::XComponentContext > m_context;
86 };
87 
88 }
89 
90 namespace {
91 
operator <<(std::ostream & out,rtl::OUString const & value)92 std::ostream & operator <<(std::ostream & out, rtl::OUString const & value) {
93     return out << rtl::OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr();
94 }
95 
assertTrue(bool argument)96 void assertTrue(bool argument) {
97     if (!argument) {
98         std::cerr
99             << "assertTrue(" << argument << ") failed" << std::endl;
100         /*MSVC trouble: std::*/abort();
101     }
102 }
103 
assertFalse(bool argument)104 void assertFalse(bool argument) {
105     if (argument) {
106         std::cerr
107             << "assertFalse(" << argument << ") failed" << std::endl;
108         /*MSVC trouble: std::*/abort();
109     }
110 }
111 
assertEqual(T const & value,T const & argument)112 template< typename T > void assertEqual(T const & value, T const & argument) {
113     if (argument != value) {
114         std::cerr
115             << "assertEqual(" << value << ", " << argument << ") failed"
116             << std::endl;
117         /*MSVC trouble: std::*/abort();
118     }
119 }
120 
121 }
122 
run(css::uno::Sequence<rtl::OUString> const & arguments)123 sal_Int32 Service::run(css::uno::Sequence< rtl::OUString > const & arguments)
124 {
125     css::uno::Reference< css::lang::XMultiComponentFactory > factory(
126         m_context->getServiceManager());
127     assertTrue(factory.is());
128     css::uno::Reference< css::container::XHierarchicalNameAccess > manager(
129         m_context->getValueByName(
130             rtl::OUString(
131                 RTL_CONSTASCII_USTRINGPARAM(
132                     "/singletons/"
133                     "com.sun.star.reflection.theTypeDescriptionManager"))),
134         css::uno::UNO_QUERY_THROW);
135 
136     ////////////////////////////////////////
137     // test: add cmd line rdbs to manager
138     ////////////////////////////////////////
139 
140     OSL_ASSERT( arguments.getLength() > 0 );
141     css::uno::Reference<css::container::XSet> xSet(
142         manager, css::uno::UNO_QUERY_THROW );
143     for ( sal_Int32 argPos = 0; argPos < arguments.getLength(); ++argPos ) {
144         rtl::OUString url;
145         OSL_VERIFY( osl_File_E_None == osl_getFileURLFromSystemPath(
146                         arguments[argPos].pData, &url.pData ) );
147         bool supposedToBeCompatible = ! url.endsWithIgnoreAsciiCaseAsciiL(
148             RTL_CONSTASCII_STRINGPARAM("_incomp.rdb") );
149 
150         css::uno::Reference<css::registry::XSimpleRegistry> xReg(
151             m_context->getServiceManager()->createInstanceWithContext(
152                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
153                                    "com.sun.star.registry.SimpleRegistry") ),
154                 m_context ), css::uno::UNO_QUERY_THROW );
155         xReg->open( url, true /* read-only */, false /* ! create */ );
156         css::uno::Any arg( css::uno::makeAny(xReg) );
157         css::uno::Reference<css::container::XHierarchicalNameAccess> xTDprov(
158             m_context->getServiceManager()->
159             createInstanceWithArgumentsAndContext(
160                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
161                                    "com.sun.star.comp.stoc."
162                                    "RegistryTypeDescriptionProvider") ),
163                 css::uno::Sequence<css::uno::Any>( &arg, 1 ), m_context ),
164             css::uno::UNO_QUERY_THROW );
165         try {
166             xSet->insert( css::uno::makeAny(xTDprov) );
167             if (! supposedToBeCompatible)
168                 std::cerr << "current rdb file: " <<
169                     rtl::OUStringToOString(
170                         url, osl_getThreadTextEncoding()).getStr() << std::endl;
171             assertTrue(supposedToBeCompatible);
172         } catch (css::lang::IllegalArgumentException &) {
173             if (supposedToBeCompatible)
174                 throw;
175             assertFalse(supposedToBeCompatible);
176         }
177     }
178 
179     ///////
180 
181     css::uno::Reference< css::reflection::XIndirectTypeDescription > sequence(
182         manager->getByHierarchicalName(
183             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[][]boolean"))),
184         css::uno::UNO_QUERY_THROW);
185     assertEqual(css::uno::TypeClass_SEQUENCE, sequence->getTypeClass());
186     assertEqual(
187         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[][]boolean")),
188         sequence->getName());
189     assertEqual(
190         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[]boolean")),
191         sequence->getReferencedType()->getName());
192 
193     css::uno::Reference< css::reflection::XStructTypeDescription > structure(
194         manager->getByHierarchicalName(
195             rtl::OUString(
196                 RTL_CONSTASCII_USTRINGPARAM(
197                     "test.tdmanager.Struct<boolean,test.tdmanager.Struct<"
198                     "any,com.sun.star.uno.XInterface>>"))),
199         css::uno::UNO_QUERY_THROW);
200     assertEqual(css::uno::TypeClass_STRUCT, structure->getTypeClass());
201     assertEqual(
202         rtl::OUString(
203             RTL_CONSTASCII_USTRINGPARAM(
204                 "test.tdmanager.Struct<boolean,test.tdmanager.Struct<"
205                 "any,com.sun.star.uno.XInterface>>")),
206         structure->getName());
207     assertEqual< bool >(false, structure->getBaseType().is());
208     assertEqual< sal_Int32 >(1, structure->getMemberTypes().getLength());
209     assertEqual(
210         rtl::OUString(
211             RTL_CONSTASCII_USTRINGPARAM(
212                 "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>")),
213         structure->getMemberTypes()[0]->getName());
214     assertEqual< sal_Int32 >(1, structure->getMemberNames().getLength());
215     assertEqual(
216         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("s")),
217         structure->getMemberNames()[0]);
218     assertEqual< sal_Int32 >(0, structure->getTypeParameters().getLength());
219     assertEqual< sal_Int32 >(2, structure->getTypeArguments().getLength());
220     assertEqual(
221         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("boolean")),
222         structure->getTypeArguments()[0]->getName());
223     assertEqual(
224         rtl::OUString(
225             RTL_CONSTASCII_USTRINGPARAM(
226                 "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>")),
227         structure->getTypeArguments()[1]->getName());
228 
229     css::uno::Reference< css::reflection::XInterfaceMethodTypeDescription >
230         method(
231             manager->getByHierarchicalName(
232                 rtl::OUString(
233                     RTL_CONSTASCII_USTRINGPARAM(
234                         "com.sun.star.uno.XComponentContext::getValueByName"))),
235             css::uno::UNO_QUERY_THROW);
236     assertEqual(css::uno::TypeClass_INTERFACE_METHOD, method->getTypeClass());
237     assertEqual(
238         rtl::OUString(
239             RTL_CONSTASCII_USTRINGPARAM(
240                 "com.sun.star.uno.XComponentContext::getValueByName")),
241         method->getName());
242     assertEqual(
243         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("getValueByName")),
244         method->getMemberName());
245     assertEqual< sal_Int32 >(3, method->getPosition());
246     assertEqual(
247         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("any")),
248         method->getReturnType()->getName());
249     assertEqual< bool >(false, method->isOneway());
250     assertEqual< sal_Int32 >(1, method->getParameters().getLength());
251     assertEqual(
252         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Name")),
253         method->getParameters()[0]->getName());
254     assertEqual(
255         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("string")),
256         method->getParameters()[0]->getType()->getName());
257     assertEqual< bool >(true, method->getParameters()[0]->isIn());
258     assertEqual< bool >(false, method->getParameters()[0]->isOut());
259     assertEqual< sal_Int32 >(0, method->getParameters()[0]->getPosition());
260     assertEqual< sal_Int32 >(0, method->getExceptions().getLength());
261 
262     assertFalse(
263         css::uno::Reference< css::reflection::XPublished >(
264             css::uno::Reference< css::reflection::XTypeDescription >(
265                 manager->getByHierarchicalName(
266                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[]boolean"))),
267                 css::uno::UNO_QUERY_THROW),
268             css::uno::UNO_QUERY).is());
269     assertFalse(
270         css::uno::Reference< css::reflection::XPublished >(
271             css::uno::Reference< css::reflection::XTypeDescription >(
272                 manager->getByHierarchicalName(
273                     rtl::OUString(
274                         RTL_CONSTASCII_USTRINGPARAM(
275                             "com.sun.star.beans.XIntroTest::ObjectName"))),
276                 css::uno::UNO_QUERY_THROW),
277             css::uno::UNO_QUERY).is());
278     assertFalse(
279         css::uno::Reference< css::reflection::XPublished >(
280             css::uno::Reference< css::reflection::XTypeDescription >(
281                 manager->getByHierarchicalName(
282                     rtl::OUString(
283                         RTL_CONSTASCII_USTRINGPARAM(
284                             "com.sun.star.beans.XIntroTest::writeln"))),
285                 css::uno::UNO_QUERY_THROW),
286             css::uno::UNO_QUERY).is());
287     //TODO: check that the reflection of a property of an accumulation-based
288     // service does not support XPublished
289 
290     return 0;
291 }
292 
getImplementationName()293 rtl::OUString Service::getImplementationName() {
294     return rtl::OUString::createFromAscii("test.tdmanager.impl");
295 }
296 
getSupportedServiceNames()297 css::uno::Sequence< rtl::OUString > Service::getSupportedServiceNames() {
298     return css::uno::Sequence< rtl::OUString >();
299 }
300 
createInstance(css::uno::Reference<css::uno::XComponentContext> const & context)301 css::uno::Reference< css::uno::XInterface > Service::createInstance(
302     css::uno::Reference< css::uno::XComponentContext > const & context)
303 {
304     return static_cast< cppu::OWeakObject * >(new Service(context));
305 }
306 
component_getImplementationEnvironment(char const ** envTypeName,uno_Environment **)307 extern "C" void SAL_CALL component_getImplementationEnvironment(
308     char const ** envTypeName, uno_Environment **)
309 {
310     if (envTypeName != 0) {
311         *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
312     }
313 }
314 
component_getFactory(char const * implName,void * serviceManager,void *)315 extern "C" void * SAL_CALL component_getFactory(char const * implName,
316                                                 void * serviceManager, void *) {
317     void * p = 0;
318     if (serviceManager != 0) {
319         css::uno::Reference< css::lang::XSingleComponentFactory > f;
320         if (Service::getImplementationName().equalsAscii(implName)) {
321             f = cppu::createSingleComponentFactory(
322                 &Service::createInstance, Service::getImplementationName(),
323                 Service::getSupportedServiceNames());
324         }
325         if (f.is()) {
326             f->acquire();
327             p = f.get();
328         }
329     }
330     return p;
331 }
332 
333 namespace {
334 
writeInfo(void * registryKey,rtl::OUString const & implementationName,css::uno::Sequence<rtl::OUString> const & serviceNames)335 bool writeInfo(void * registryKey, rtl::OUString const & implementationName,
336                css::uno::Sequence< rtl::OUString > const & serviceNames) {
337     rtl::OUString keyName(rtl::OUString::createFromAscii("/"));
338     keyName += implementationName;
339     keyName += rtl::OUString::createFromAscii("/UNO/SERVICES");
340     css::uno::Reference< css::registry::XRegistryKey > key;
341     try {
342         key = static_cast< css::registry::XRegistryKey * >(registryKey)->
343             createKey(keyName);
344     } catch (css::registry::InvalidRegistryException &) {}
345     if (!key.is()) {
346         return false;
347     }
348     bool success = true;
349     for (sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
350         try {
351             key->createKey(serviceNames[i]);
352         } catch (css::registry::InvalidRegistryException &) {
353             success = false;
354             break;
355         }
356     }
357     return success;
358 }
359 
360 }
361 
component_writeInfo(void *,void * registryKey)362 extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * registryKey) {
363     return registryKey
364         && writeInfo(registryKey, Service::getImplementationName(),
365                      Service::getSupportedServiceNames());
366 }
367