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