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