Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / ure / source / uretest / cppmain.cc
blobfb957d9f221ab4ceb9865ef4b97b532e1671b8ec
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "sal/config.h"
20 #include "sal/macros.h"
22 #include <cstddef>
23 #include <memory>
25 #include "com/sun/star/beans/Introspection.hpp"
26 #include "com/sun/star/beans/theIntrospection.hpp"
27 #include "com/sun/star/bridge/BridgeFactory.hpp"
28 #include "com/sun/star/bridge/UnoUrlResolver.hpp"
29 #include "com/sun/star/connection/Acceptor.hpp"
30 #include "com/sun/star/connection/Connector.hpp"
31 #include "com/sun/star/io/Pipe.hpp"
32 #include "com/sun/star/io/TextInputStream.hpp"
33 #include "com/sun/star/io/TextOutputStream.hpp"
34 #include "com/sun/star/java/JavaVirtualMachine.hpp"
35 #include "com/sun/star/lang/XMain.hpp"
36 #include "com/sun/star/loader/Java.hpp"
37 #include "com/sun/star/loader/SharedLibrary.hpp"
38 #include "com/sun/star/reflection/ProxyFactory.hpp"
39 #include "com/sun/star/registry/ImplementationRegistration.hpp"
40 #include "com/sun/star/registry/SimpleRegistry.hpp"
41 #include "com/sun/star/script/Converter.hpp"
42 #include "com/sun/star/script/Invocation.hpp"
43 #include "com/sun/star/security/AccessController.hpp"
44 #include "com/sun/star/security/Policy.hpp"
45 #include "com/sun/star/uno/Exception.hpp"
46 #include "com/sun/star/uno/NamingService.hpp"
47 #include "com/sun/star/uno/Reference.hxx"
48 #include "com/sun/star/uno/RuntimeException.hpp"
49 #include "com/sun/star/uno/Sequence.hxx"
50 #include "com/sun/star/uno/XComponentContext.hpp"
51 #include "com/sun/star/uno/XInterface.hpp"
52 #include "com/sun/star/uri/ExternalUriReferenceTranslator.hpp"
53 #include "com/sun/star/uri/UriReferenceFactory.hpp"
54 #include "com/sun/star/uri/VndSunStarPkgUrlReferenceFactory.hpp"
55 #include "com/sun/star/util/theMacroExpander.hpp"
56 #include "cppuhelper/factory.hxx"
57 #include "cppuhelper/implbase1.hxx"
58 #include "cppuhelper/implementationentry.hxx"
59 #include "cppuhelper/interfacecontainer.hxx"
60 #include "cppuhelper/unourl.hxx"
61 #include "cppuhelper/weak.hxx"
62 #include "osl/thread.h"
63 #include "rtl/malformeduriexception.hxx"
64 #include "rtl/string.h"
65 #include "rtl/ustring.h"
66 #include "rtl/ustring.hxx"
67 #include "sal/types.h"
68 #include "salhelper/simplereferenceobject.hxx"
69 #include "uno/current_context.hxx"
70 #include "uno/environment.h"
72 #include "test/types/CppTest.hpp"
73 #include "test/types/JavaTest.hpp"
74 #include "test/types/TestException.hpp"
75 #include "test/types/XTest.hpp"
77 namespace {
79 class Service: public ::cppu::WeakImplHelper1< css::lang::XMain > {
80 public:
81 explicit Service(
82 css::uno::Reference< css::uno::XComponentContext > const & context):
83 context_(context) {}
85 virtual ::sal_Int32 SAL_CALL run(
86 css::uno::Sequence< ::rtl::OUString > const &);
88 private:
89 Service(Service &); // not defined
90 void operator =(Service &); // not defined
92 virtual ~Service() {}
94 void test(
95 css::uno::Reference< test::types::XTest > const & test,
96 ::rtl::OUString const & name);
98 css::uno::Reference< css::uno::XComponentContext > context_;
101 ::sal_Int32 Service::run(css::uno::Sequence< ::rtl::OUString > const &) {
102 osl_getThreadIdentifier(0); // check for sal
103 (new salhelper::SimpleReferenceObject)->release(); // check for salhelper
104 css::uno::getCurrentContext(); // check for cppu
105 try { // check for cppuhelper
106 std::auto_ptr< cppu::UnoUrl > dummy(new cppu::UnoUrl(rtl::OUString()));
107 } catch (rtl::MalformedUriException &) {}
108 static char const * const services[] = {
109 "com.sun.star.io.DataInputStream",
110 "com.sun.star.io.DataOutputStream",
111 "com.sun.star.io.MarkableInputStream",
112 "com.sun.star.io.MarkableOutputStream",
113 "com.sun.star.io.ObjectInputStream",
114 "com.sun.star.io.ObjectOutputStream",
115 "com.sun.star.io.Pump",
116 "com.sun.star.lang.RegistryServiceManager",
117 "com.sun.star.lang.ServiceManager",
118 "com.sun.star.reflection.CoreReflection",
119 "com.sun.star.registry.NestedRegistry",
120 "com.sun.star.script.InvocationAdapterFactory",
121 "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript"
123 for (::std::size_t i = 0; i < SAL_N_ELEMENTS(services); ++i) {
124 ::rtl::OUString name(::rtl::OUString::createFromAscii(services[i]));
125 css::uno::Reference< css::uno::XInterface > instance;
126 try {
127 instance = context_->getServiceManager()->createInstanceWithContext(
128 name, context_);
129 } catch (css::uno::RuntimeException &) {
130 throw;
131 } catch (css::uno::Exception &) {
132 throw css::uno::RuntimeException(
133 ::rtl::OUString("error creating instance"),
134 static_cast< ::cppu::OWeakObject * >(this));
136 if (!instance.is()) {
137 throw css::uno::RuntimeException(
138 "no instance: " + name,
139 static_cast< ::cppu::OWeakObject * >(this));
142 css::beans::Introspection::create(context_);
143 css::beans::theIntrospection::get(context_);
144 css::bridge::BridgeFactory::create(context_);
145 css::bridge::UnoUrlResolver::create(context_);
146 css::connection::Acceptor::create(context_);
147 css::connection::Connector::create(context_);
148 css::io::Pipe::create(context_);
149 css::io::TextInputStream::create(context_);
150 css::io::TextOutputStream::create(context_);
151 css::java::JavaVirtualMachine::create(context_);
152 css::loader::Java::create(context_);
153 css::loader::SharedLibrary::create(context_);
154 css::reflection::ProxyFactory::create(context_);
155 css::registry::ImplementationRegistration::create(context_);
156 css::registry::SimpleRegistry::create(context_);
157 css::script::Converter::create(context_);
158 css::script::Invocation::create(context_);
159 css::security::AccessController::create(context_);
160 css::security::Policy::create(context_);
161 css::uno::NamingService::create(context_);
162 css::uri::ExternalUriReferenceTranslator::create(context_);
163 css::uri::UriReferenceFactory::create(context_);
164 css::uri::VndSunStarPkgUrlReferenceFactory::create(context_);
165 static char const * const singletons[] = {
166 "com.sun.star.reflection.theTypeDescriptionManager"
168 for (std::size_t i = 0; i != SAL_N_ELEMENTS(singletons); ++i) {
169 css::uno::Reference< css::uno::XInterface > instance(
170 context_->getValueByName(
171 "/singletons/" + rtl::OUString::createFromAscii(singletons[i])),
172 css::uno::UNO_QUERY_THROW);
174 css::util::theMacroExpander::get(context_);
175 test(
176 ::test::types::CppTest::create(context_),
177 ::rtl::OUString("test.types.CppTest"));
178 test(
179 ::test::types::JavaTest::create(context_),
180 ::rtl::OUString("test.types.JavaTest"));
181 return 0;
184 void Service::test(
185 css::uno::Reference< test::types::XTest > const & test,
186 ::rtl::OUString const & name)
188 bool ok = false;
189 try {
190 test->throwException();
191 } catch (::test::types::TestException &) {
192 ok = true;
194 if (!ok) {
195 throw css::uno::RuntimeException(
196 (name
197 + ::rtl::OUString(".throwException failed")),
198 static_cast< ::cppu::OWeakObject * >(this));
202 namespace CppMain {
204 css::uno::Reference< css::uno::XInterface > create(
205 css::uno::Reference< css::uno::XComponentContext > const & context)
207 return static_cast< ::cppu::OWeakObject * >(new Service(context));
210 rtl::OUString getImplementationName() {
211 return rtl::OUString("test.cpp.cppmain.Component");
214 css::uno::Sequence< ::rtl::OUString > getSupportedServiceNames() {
215 return css::uno::Sequence< ::rtl::OUString >();
220 ::cppu::ImplementationEntry entries[] = {
221 { CppMain::create, CppMain::getImplementationName,
222 CppMain::getSupportedServiceNames, ::cppu::createSingleComponentFactory,
223 0, 0 },
224 { 0, 0, 0, 0, 0, 0 } };
228 extern "C" SAL_DLLPUBLIC_EXPORT ::sal_Bool SAL_CALL component_writeInfo(
229 void * serviceManager, void * registryKey)
231 return ::cppu::component_writeInfoHelper(
232 serviceManager, registryKey, entries);
235 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
236 char const * implName, void * serviceManager, void * registryKey)
238 return ::cppu::component_getFactoryHelper(
239 implName, serviceManager, registryKey, entries);