xref: /aoo42x/main/ure/source/uretest/cppmain.cc (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "sal/config.h"
29 
30 #include <cstddef>
31 #include <functional>
32 #include <memory>
33 #include <new>
34 #include <hash_map>
35 
36 #include "com/sun/star/lang/XMain.hpp"
37 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
38 #include "com/sun/star/uno/Exception.hpp"
39 #include "com/sun/star/uno/Reference.hxx"
40 #include "com/sun/star/uno/RuntimeException.hpp"
41 #include "com/sun/star/uno/Sequence.hxx"
42 #include "com/sun/star/uno/XComponentContext.hpp"
43 #include "com/sun/star/uno/XInterface.hpp"
44 #include "cppuhelper/factory.hxx"
45 #include "cppuhelper/implbase1.hxx"
46 #include "cppuhelper/implementationentry.hxx"
47 #include "cppuhelper/interfacecontainer.hxx"
48 #include "cppuhelper/unourl.hxx"
49 #include "cppuhelper/weak.hxx"
50 #include "osl/mutex.hxx"
51 #include "osl/thread.h"
52 #include "rtl/malformeduriexception.hxx"
53 #include "rtl/string.h"
54 #include "rtl/ustrbuf.hxx"
55 #include "rtl/ustring.h"
56 #include "rtl/ustring.hxx"
57 #include "sal/types.h"
58 #include "salhelper/simplereferenceobject.hxx"
59 #include "uno/current_context.hxx"
60 #include "uno/environment.h"
61 #include "uno/lbnames.h"
62 
63 #include "test/types/CppTest.hpp"
64 #include "test/types/JavaTest.hpp"
65 #include "test/types/TestException.hpp"
66 #include "test/types/XTest.hpp"
67 
68 namespace css = ::com::sun::star;
69 
70 namespace {
71 
72 class Service: public ::cppu::WeakImplHelper1< ::css::lang::XMain > {
73 public:
74     explicit Service(
75         ::css::uno::Reference< ::css::uno::XComponentContext > const & context):
76         context_(context) {}
77 
78     virtual ::sal_Int32 SAL_CALL run(
79         ::css::uno::Sequence< ::rtl::OUString > const &)
80         throw (::css::uno::RuntimeException);
81 
82 private:
83     Service(Service &); // not defined
84     void operator =(Service &); // not defined
85 
86     virtual ~Service() {}
87 
88     void test(
89         ::css::uno::Reference< test::types::XTest > const & test,
90         ::rtl::OUString const & name);
91 
92     ::css::uno::Reference< ::css::uno::XComponentContext > context_;
93 };
94 
95 ::sal_Int32 Service::run(::css::uno::Sequence< ::rtl::OUString > const &)
96     throw (::css::uno::RuntimeException)
97 {
98     osl_getThreadIdentifier(0); // check for sal
99     (new salhelper::SimpleReferenceObject)->release(); // check for salhelper
100     css::uno::getCurrentContext(); // check for cppu
101     try { // check for cppuhelper
102         std::auto_ptr< cppu::UnoUrl > dummy(new cppu::UnoUrl(rtl::OUString()));
103     } catch (rtl::MalformedUriException &) {}
104     { // check for stlport
105         osl::Mutex m;
106         std::auto_ptr< cppu::OMultiTypeInterfaceContainerHelperVar<
107             int, std::hash< int >, std::equal_to< int > > > dummy(
108                 new cppu::OMultiTypeInterfaceContainerHelperVar<
109                 int, std::hash< int >, std::equal_to< int > >(m));
110     }
111     static char const * const services[] = {
112         "com.sun.star.beans.Introspection",
113         "com.sun.star.bridge.Bridge",
114         "com.sun.star.bridge.BridgeFactory",
115         "com.sun.star.bridge.IiopBridge",
116         "com.sun.star.bridge.UnoUrlResolver",
117         "com.sun.star.bridge.UrpBridge",
118         "com.sun.star.connection.Acceptor",
119         "com.sun.star.connection.Connector",
120         "com.sun.star.io.DataInputStream",
121         "com.sun.star.io.DataOutputStream",
122         "com.sun.star.io.MarkableInputStream",
123         "com.sun.star.io.MarkableOutputStream",
124         "com.sun.star.io.ObjectInputStream",
125         "com.sun.star.io.ObjectOutputStream",
126         "com.sun.star.io.Pipe",
127         "com.sun.star.io.Pump",
128         "com.sun.star.io.TextInputStream",
129         "com.sun.star.io.TextOutputStream",
130         "com.sun.star.java.JavaVirtualMachine",
131         "com.sun.star.lang.MultiServiceFactory",
132         "com.sun.star.lang.RegistryServiceManager",
133         "com.sun.star.lang.ServiceManager",
134         "com.sun.star.loader.Java",
135         "com.sun.star.loader.Java2",
136         "com.sun.star.loader.SharedLibrary",
137         "com.sun.star.reflection.CoreReflection",
138         "com.sun.star.reflection.ProxyFactory",
139         "com.sun.star.reflection.TypeDescriptionManager",
140         "com.sun.star.reflection.TypeDescriptionProvider",
141         "com.sun.star.registry.ImplementationRegistration",
142         "com.sun.star.registry.NestedRegistry",
143         "com.sun.star.registry.SimpleRegistry",
144         "com.sun.star.script.Converter",
145         "com.sun.star.script.Invocation",
146         "com.sun.star.script.InvocationAdapterFactory",
147         "com.sun.star.security.AccessController",
148         "com.sun.star.security.Policy",
149         "com.sun.star.uno.NamingService",
150         "com.sun.star.uri.ExternalUriReferenceTranslator",
151         "com.sun.star.uri.UriReferenceFactory",
152         "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript",
153         "com.sun.star.uri.VndSunStarPkgUrlReferenceFactory"
154         // "com.sun.star.beans.PropertyBag",
155         // "com.sun.star.beans.PropertySet",
156         // "com.sun.star.bridge.OleApplicationRegistration",
157         // "com.sun.star.bridge.OleBridgeSupplier",
158         // "com.sun.star.bridge.OleBridgeSupplier2",
159         // "com.sun.star.bridge.OleBridgeSupplierVar1",
160         // "com.sun.star.bridge.OleObjectFactory",
161         // "com.sun.star.bridge.oleautomation.ApplicationRegistration",
162         // "com.sun.star.bridge.oleautomation.BridgeSupplier",
163         // "com.sun.star.bridge.oleautomation.Factory",
164         // "com.sun.star.loader.Dynamic",
165         // "com.sun.star.registry.DefaultRegistry",
166         // "com.sun.star.script.AllListenerAdapter",
167         // "com.sun.star.script.Engine",
168         // "com.sun.star.script.JavaScript",
169         // "com.sun.star.test.TestFactory",
170         // "com.sun.star.util.BootstrapMacroExpander",
171         // "com.sun.star.util.MacroExpander",
172         // "com.sun.star.util.logging.Logger",
173         // "com.sun.star.util.logging.LoggerRemote"
174     };
175     ::css::uno::Reference< ::css::lang::XMultiComponentFactory > manager(
176         context_->getServiceManager());
177     if (!manager.is()) {
178         throw ::css::uno::RuntimeException(
179             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("no service manager")),
180             static_cast< ::cppu::OWeakObject * >(this));
181     }
182     for (::std::size_t i = 0; i < sizeof services / sizeof services[0]; ++i) {
183         ::css::uno::Reference< ::css::uno::XInterface > instance;
184         try {
185             instance = manager->createInstanceWithContext(
186                 ::rtl::OUString::createFromAscii(services[i]), context_);
187         } catch (::css::uno::RuntimeException &) {
188             throw;
189         } catch (::css::uno::Exception &) {
190             throw ::css::uno::RuntimeException(
191                 ::rtl::OUString(
192                     RTL_CONSTASCII_USTRINGPARAM("error creating instance")),
193                 static_cast< ::cppu::OWeakObject * >(this));
194         }
195         if (!instance.is()) {
196             throw ::css::uno::RuntimeException(
197                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("no instance")),
198                 static_cast< ::cppu::OWeakObject * >(this));
199         }
200     }
201     static char const * const singletons[] = {
202         "com.sun.star.util.theMacroExpander" };
203     for (::std::size_t i = 0; i < sizeof singletons / sizeof singletons[0]; ++i)
204     {
205         ::rtl::OUStringBuffer b;
206         b.appendAscii(RTL_CONSTASCII_STRINGPARAM("/singletons/"));
207         b.appendAscii(singletons[i]);
208         ::css::uno::Reference< ::css::uno::XInterface > instance(
209             context_->getValueByName(b.makeStringAndClear()),
210             ::css::uno::UNO_QUERY_THROW);
211     }
212     test(
213         ::test::types::CppTest::create(context_),
214         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("test.types.CppTest")));
215     test(
216         ::test::types::JavaTest::create(context_),
217         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("test.types.JavaTest")));
218     return 0;
219 }
220 
221 void Service::test(
222     ::css::uno::Reference< test::types::XTest > const & test,
223     ::rtl::OUString const & name)
224 {
225     bool ok = false;
226     try {
227         test->throwException();
228     } catch (::test::types::TestException &) {
229         ok = true;
230     }
231     if (!ok) {
232         throw ::css::uno::RuntimeException(
233             (name
234              + ::rtl::OUString(
235                  RTL_CONSTASCII_USTRINGPARAM(".throwException failed"))),
236             static_cast< ::cppu::OWeakObject * >(this));
237     }
238 }
239 
240 namespace CppMain {
241 
242 ::css::uno::Reference< ::css::uno::XInterface > create(
243     ::css::uno::Reference< ::css::uno::XComponentContext > const & context)
244     SAL_THROW((::css::uno::Exception))
245 {
246     try {
247         return static_cast< ::cppu::OWeakObject * >(new Service(context));
248     } catch (::std::bad_alloc &) {
249         throw ::css::uno::RuntimeException(
250             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
251             ::css::uno::Reference< ::css::uno::XInterface >());
252     }
253 }
254 
255 ::rtl::OUString getImplementationName() {
256     return ::rtl::OUString(
257         RTL_CONSTASCII_USTRINGPARAM("test.cpp.cppmain.Component"));
258 }
259 
260 ::css::uno::Sequence< ::rtl::OUString > getSupportedServiceNames() {
261     return ::css::uno::Sequence< ::rtl::OUString >();
262 }
263 
264 }
265 
266 ::cppu::ImplementationEntry entries[] = {
267     { CppMain::create, CppMain::getImplementationName,
268       CppMain::getSupportedServiceNames, ::cppu::createSingleComponentFactory,
269       0, 0 },
270     { 0, 0, 0, 0, 0, 0 } };
271 
272 }
273 
274 extern "C" ::sal_Bool SAL_CALL component_writeInfo(
275     void * serviceManager, void * registryKey)
276 {
277     return ::cppu::component_writeInfoHelper(
278         serviceManager, registryKey, entries);
279 }
280 
281 extern "C" void * SAL_CALL component_getFactory(
282     char const * implName, void * serviceManager, void * registryKey)
283 {
284     return ::cppu::component_getFactoryHelper(
285         implName, serviceManager, registryKey, entries);
286 }
287 
288 extern "C" void SAL_CALL component_getImplementationEnvironment(
289     char const ** envTypeName, ::uno_Environment **)
290 {
291     *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
292 }
293