xref: /aoo41x/main/ure/source/uretest/cppmain.cc (revision 5f94a14c)
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 #include "sal/config.h"
25 
26 #include <cstddef>
27 #include <functional>
28 #include <memory>
29 #include <new>
30 #include <hash_map>
31 
32 #include "com/sun/star/lang/XMain.hpp"
33 #include "com/sun/star/lang/XMultiComponentFactory.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/XComponentContext.hpp"
39 #include "com/sun/star/uno/XInterface.hpp"
40 #include "cppuhelper/factory.hxx"
41 #include "cppuhelper/implbase1.hxx"
42 #include "cppuhelper/implementationentry.hxx"
43 #include "cppuhelper/interfacecontainer.hxx"
44 #include "cppuhelper/unourl.hxx"
45 #include "cppuhelper/weak.hxx"
46 #include "osl/mutex.hxx"
47 #include "osl/thread.h"
48 #include "rtl/malformeduriexception.hxx"
49 #include "rtl/string.h"
50 #include "rtl/ustrbuf.hxx"
51 #include "rtl/ustring.h"
52 #include "rtl/ustring.hxx"
53 #include "sal/types.h"
54 #include "salhelper/simplereferenceobject.hxx"
55 #include "uno/current_context.hxx"
56 #include "uno/environment.h"
57 #include "uno/lbnames.h"
58 
59 #include "test/types/CppTest.hpp"
60 #include "test/types/JavaTest.hpp"
61 #include "test/types/TestException.hpp"
62 #include "test/types/XTest.hpp"
63 
64 namespace css = ::com::sun::star;
65 
66 namespace {
67 
68 class Service: public ::cppu::WeakImplHelper1< ::css::lang::XMain > {
69 public:
Service(::css::uno::Reference<::css::uno::XComponentContext> const & context)70     explicit Service(
71         ::css::uno::Reference< ::css::uno::XComponentContext > const & context):
72         context_(context) {}
73 
74     virtual ::sal_Int32 SAL_CALL run(
75         ::css::uno::Sequence< ::rtl::OUString > const &)
76         throw (::css::uno::RuntimeException);
77 
78 private:
79     Service(Service &); // not defined
80     void operator =(Service &); // not defined
81 
~Service()82     virtual ~Service() {}
83 
84     void test(
85         ::css::uno::Reference< test::types::XTest > const & test,
86         ::rtl::OUString const & name);
87 
88     ::css::uno::Reference< ::css::uno::XComponentContext > context_;
89 };
90 
run(::css::uno::Sequence<::rtl::OUString> const &)91 ::sal_Int32 Service::run(::css::uno::Sequence< ::rtl::OUString > const &)
92     throw (::css::uno::RuntimeException)
93 {
94     osl_getThreadIdentifier(0); // check for sal
95     (new salhelper::SimpleReferenceObject)->release(); // check for salhelper
96     css::uno::getCurrentContext(); // check for cppu
97     try { // check for cppuhelper
98         std::auto_ptr< cppu::UnoUrl > dummy(new cppu::UnoUrl(rtl::OUString()));
99     } catch (rtl::MalformedUriException &) {}
100     { // check for stlport
101         osl::Mutex m;
102         std::auto_ptr< cppu::OMultiTypeInterfaceContainerHelperVar<
103             int, std::hash< int >, std::equal_to< int > > > dummy(
104                 new cppu::OMultiTypeInterfaceContainerHelperVar<
105                 int, std::hash< int >, std::equal_to< int > >(m));
106     }
107     static char const * const services[] = {
108         "com.sun.star.beans.Introspection",
109         "com.sun.star.bridge.Bridge",
110         "com.sun.star.bridge.BridgeFactory",
111         "com.sun.star.bridge.IiopBridge",
112         "com.sun.star.bridge.UnoUrlResolver",
113         "com.sun.star.bridge.UrpBridge",
114         "com.sun.star.connection.Acceptor",
115         "com.sun.star.connection.Connector",
116         "com.sun.star.io.DataInputStream",
117         "com.sun.star.io.DataOutputStream",
118         "com.sun.star.io.MarkableInputStream",
119         "com.sun.star.io.MarkableOutputStream",
120         "com.sun.star.io.ObjectInputStream",
121         "com.sun.star.io.ObjectOutputStream",
122         "com.sun.star.io.Pipe",
123         "com.sun.star.io.Pump",
124         "com.sun.star.io.TextInputStream",
125         "com.sun.star.io.TextOutputStream",
126         "com.sun.star.java.JavaVirtualMachine",
127         "com.sun.star.lang.MultiServiceFactory",
128         "com.sun.star.lang.RegistryServiceManager",
129         "com.sun.star.lang.ServiceManager",
130         "com.sun.star.loader.Java",
131         "com.sun.star.loader.Java2",
132         "com.sun.star.loader.SharedLibrary",
133         "com.sun.star.reflection.CoreReflection",
134         "com.sun.star.reflection.ProxyFactory",
135         "com.sun.star.reflection.TypeDescriptionManager",
136         "com.sun.star.reflection.TypeDescriptionProvider",
137         "com.sun.star.registry.ImplementationRegistration",
138         "com.sun.star.registry.NestedRegistry",
139         "com.sun.star.registry.SimpleRegistry",
140         "com.sun.star.script.Converter",
141         "com.sun.star.script.Invocation",
142         "com.sun.star.script.InvocationAdapterFactory",
143         "com.sun.star.security.AccessController",
144         "com.sun.star.security.Policy",
145         "com.sun.star.uno.NamingService",
146         "com.sun.star.uri.ExternalUriReferenceTranslator",
147         "com.sun.star.uri.UriReferenceFactory",
148         "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript",
149         "com.sun.star.uri.VndSunStarPkgUrlReferenceFactory"
150         // "com.sun.star.beans.PropertyBag",
151         // "com.sun.star.beans.PropertySet",
152         // "com.sun.star.bridge.OleApplicationRegistration",
153         // "com.sun.star.bridge.OleBridgeSupplier",
154         // "com.sun.star.bridge.OleBridgeSupplier2",
155         // "com.sun.star.bridge.OleBridgeSupplierVar1",
156         // "com.sun.star.bridge.OleObjectFactory",
157         // "com.sun.star.bridge.oleautomation.ApplicationRegistration",
158         // "com.sun.star.bridge.oleautomation.BridgeSupplier",
159         // "com.sun.star.bridge.oleautomation.Factory",
160         // "com.sun.star.loader.Dynamic",
161         // "com.sun.star.registry.DefaultRegistry",
162         // "com.sun.star.script.AllListenerAdapter",
163         // "com.sun.star.script.Engine",
164         // "com.sun.star.script.JavaScript",
165         // "com.sun.star.test.TestFactory",
166         // "com.sun.star.util.BootstrapMacroExpander",
167         // "com.sun.star.util.MacroExpander",
168         // "com.sun.star.util.logging.Logger",
169         // "com.sun.star.util.logging.LoggerRemote"
170     };
171     ::css::uno::Reference< ::css::lang::XMultiComponentFactory > manager(
172         context_->getServiceManager());
173     if (!manager.is()) {
174         throw ::css::uno::RuntimeException(
175             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("no service manager")),
176             static_cast< ::cppu::OWeakObject * >(this));
177     }
178     for (::std::size_t i = 0; i < sizeof services / sizeof services[0]; ++i) {
179         ::css::uno::Reference< ::css::uno::XInterface > instance;
180         try {
181             instance = manager->createInstanceWithContext(
182                 ::rtl::OUString::createFromAscii(services[i]), context_);
183         } catch (::css::uno::RuntimeException &) {
184             throw;
185         } catch (::css::uno::Exception &) {
186             throw ::css::uno::RuntimeException(
187                 ::rtl::OUString(
188                     RTL_CONSTASCII_USTRINGPARAM("error creating instance")),
189                 static_cast< ::cppu::OWeakObject * >(this));
190         }
191         if (!instance.is()) {
192             throw ::css::uno::RuntimeException(
193                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("no instance")),
194                 static_cast< ::cppu::OWeakObject * >(this));
195         }
196     }
197     static char const * const singletons[] = {
198         "com.sun.star.util.theMacroExpander" };
199     for (::std::size_t i = 0; i < sizeof singletons / sizeof singletons[0]; ++i)
200     {
201         ::rtl::OUStringBuffer b;
202         b.appendAscii(RTL_CONSTASCII_STRINGPARAM("/singletons/"));
203         b.appendAscii(singletons[i]);
204         ::css::uno::Reference< ::css::uno::XInterface > instance(
205             context_->getValueByName(b.makeStringAndClear()),
206             ::css::uno::UNO_QUERY_THROW);
207     }
208     test(
209         ::test::types::CppTest::create(context_),
210         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("test.types.CppTest")));
211     test(
212         ::test::types::JavaTest::create(context_),
213         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("test.types.JavaTest")));
214     return 0;
215 }
216 
test(::css::uno::Reference<test::types::XTest> const & test,::rtl::OUString const & name)217 void Service::test(
218     ::css::uno::Reference< test::types::XTest > const & test,
219     ::rtl::OUString const & name)
220 {
221     bool ok = false;
222     try {
223         test->throwException();
224     } catch (::test::types::TestException &) {
225         ok = true;
226     }
227     if (!ok) {
228         throw ::css::uno::RuntimeException(
229             (name
230              + ::rtl::OUString(
231                  RTL_CONSTASCII_USTRINGPARAM(".throwException failed"))),
232             static_cast< ::cppu::OWeakObject * >(this));
233     }
234 }
235 
236 namespace CppMain {
237 
create(::css::uno::Reference<::css::uno::XComponentContext> const & context)238 ::css::uno::Reference< ::css::uno::XInterface > create(
239     ::css::uno::Reference< ::css::uno::XComponentContext > const & context)
240     SAL_THROW((::css::uno::Exception))
241 {
242     try {
243         return static_cast< ::cppu::OWeakObject * >(new Service(context));
244     } catch (::std::bad_alloc &) {
245         throw ::css::uno::RuntimeException(
246             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
247             ::css::uno::Reference< ::css::uno::XInterface >());
248     }
249 }
250 
getImplementationName()251 ::rtl::OUString getImplementationName() {
252     return ::rtl::OUString(
253         RTL_CONSTASCII_USTRINGPARAM("test.cpp.cppmain.Component"));
254 }
255 
getSupportedServiceNames()256 ::css::uno::Sequence< ::rtl::OUString > getSupportedServiceNames() {
257     return ::css::uno::Sequence< ::rtl::OUString >();
258 }
259 
260 }
261 
262 ::cppu::ImplementationEntry entries[] = {
263     { CppMain::create, CppMain::getImplementationName,
264       CppMain::getSupportedServiceNames, ::cppu::createSingleComponentFactory,
265       0, 0 },
266     { 0, 0, 0, 0, 0, 0 } };
267 
268 }
269 
component_writeInfo(void * serviceManager,void * registryKey)270 extern "C" ::sal_Bool SAL_CALL component_writeInfo(
271     void * serviceManager, void * registryKey)
272 {
273     return ::cppu::component_writeInfoHelper(
274         serviceManager, registryKey, entries);
275 }
276 
component_getFactory(char const * implName,void * serviceManager,void * registryKey)277 extern "C" void * SAL_CALL component_getFactory(
278     char const * implName, void * serviceManager, void * registryKey)
279 {
280     return ::cppu::component_getFactoryHelper(
281         implName, serviceManager, registryKey, entries);
282 }
283 
component_getImplementationEnvironment(char const ** envTypeName,::uno_Environment **)284 extern "C" void SAL_CALL component_getImplementationEnvironment(
285     char const ** envTypeName, ::uno_Environment **)
286 {
287     *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
288 }
289