Branch libreoffice-5-0-4
[LibreOffice.git] / cppuhelper / test / bootstrap / bootstrap.test.cxx
blobd4d766e54acb985199eef1a8553983155733ba0b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <sal/main.h>
22 #include <osl/file.hxx>
24 #include <typelib/typedescription.hxx>
26 #include <cppuhelper/bootstrap.hxx>
27 #include <cppuhelper/shlib.hxx>
29 #include <com/sun/star/lang/XComponent.hpp>
31 #include <uno/environment.hxx>
32 #include <uno/lbnames.h>
33 #include <cppu/EnvDcp.hxx>
34 #include <cppu/EnvGuards.hxx>
36 #include <iostream>
39 #ifndef SAL_DLLPREFIX
40 # define SAL_DLLPREFIX ""
41 #endif
44 using namespace com::sun::star;
47 static rtl::OUString s_comment;
49 static bool s_check_object_is_in(void * pObject)
51 uno::Environment currentEnv(uno::Environment::getCurrent());
53 rtl_uString * pOId = NULL;
54 currentEnv.get()->pExtEnv->getObjectIdentifier(currentEnv.get()->pExtEnv, &pOId, pObject);
57 uno::TypeDescription typeDescription(rtl::OUString("com.sun.star.uno.XInterface"));
59 void * pRegisteredObject = NULL;
60 currentEnv.get()->pExtEnv->getRegisteredInterface(currentEnv.get()->pExtEnv,
61 &pRegisteredObject,
62 pOId,
63 (typelib_InterfaceTypeDescription *)typeDescription.get());
66 if (pOId) rtl_uString_release(pOId);
68 bool result = pRegisteredObject != NULL;
70 if (result)
71 currentEnv.get()->pExtEnv->releaseInterface(currentEnv.get()->pExtEnv, pRegisteredObject);
73 return result;
76 static void s_test__loadSharedLibComponentFactory(rtl::OUString const & clientPurpose,
77 rtl::OUString const & servicePurpose)
79 cppu::EnvGuard envGuard(uno::Environment(UNO_LB_UNO + clientPurpose, NULL));
80 if (!clientPurpose.isEmpty() && !envGuard.is())
82 s_comment += "\t\tcouldn't get purpose env: \"" + clientPurpose + "\" - FAILURE\n";
83 return;
86 rtl::OString os_clientPurpose(rtl::OUStringToOString(clientPurpose, RTL_TEXTENCODING_ASCII_US));
88 uno::Reference<uno::XInterface> xItf(
89 cppu::loadSharedLibComponentFactory(
90 rtl::OUString(SAL_DLLPREFIX "TestComponent.uno" SAL_DLLEXTENSION),
91 #ifdef WIN32
92 "",
93 #else
94 "file://../lib/",
95 #endif
96 "impl.test.TestComponent" + servicePurpose,
97 uno::Reference<lang::XMultiServiceFactory>(),
98 uno::Reference<registry::XRegistryKey>()
102 if (!xItf.is())
104 s_comment += rtl::OUString("\t\tgot no object - FAILURE\n");
105 return;
108 if (!clientPurpose.equals(servicePurpose) && !s_check_object_is_in(xItf.get()))
110 s_comment += "\t\tcouldn't find object in current purpose \"" + clientPurpose + "\" - FAILURE\n";
113 if (!cppu::EnvDcp::getPurpose(uno::Environment::getCurrent().getTypeName()).equals(clientPurpose))
115 s_comment += "\t\tdid not enter client purpose \"" + clientPurpose + "\" - FAILURE\n";
119 static void s_test__loadSharedLibComponentFactory__free_free()
121 s_comment += "\ts_test__loadSharedLibComponentFactory__free_free\n";
123 s_test__loadSharedLibComponentFactory("", "");
126 static void s_test__loadSharedLibComponentFactory__free_purpose()
128 s_comment += "\ts_test__loadSharedLibComponentFactory__free_purpose\n";
130 s_test__loadSharedLibComponentFactory("", ":testenv");
133 static void s_test__loadSharedLibComponentFactory__purpose_free()
135 s_comment += "\ts_test__loadSharedLibComponentFactory__purpose_free\n";
137 s_test__loadSharedLibComponentFactory(":testenv", "");
140 static void s_test__loadSharedLibComponentFactory__purpose_purpose()
142 s_comment += "\ts_test__loadSharedLibComponentFactory__purpose_purpose\n";
144 s_test__loadSharedLibComponentFactory(":testenv", ":testenv");
147 static rtl::OUString s_getSDrive()
149 rtl::OUString path;//("file://");
151 // FIXME no more S drive
153 osl::FileBase::getFileURLFromSystemPath(path, path);
155 return path;
158 static void s_test__createSimpleRegistry(rtl::OUString const & clientPurpose)
160 cppu::EnvGuard envGuard(uno::Environment(rtl::OUString(UNO_LB_UNO)
161 + clientPurpose, NULL));
162 if (!clientPurpose.isEmpty() && !envGuard.is())
164 s_comment += "\t\tcouldn't get purpose env: \"" + clientPurpose + "\" - FAILURE\n";
165 return;
168 uno::Reference<registry::XSimpleRegistry> registry(cppu::createSimpleRegistry(
169 s_getSDrive()));
171 if (!registry.is())
173 s_comment += "\t\tgot no object - FAILURE\n";
174 return;
177 if (!clientPurpose.isEmpty() && !s_check_object_is_in(registry.get()))
179 s_comment += "\t\tcouldn't find object in current purpose \"" + clientPurpose + "\" - FAILURE\n";
183 static void s_test__createSimpleRegistry__free()
185 s_comment += "\ts_test__createSimpleRegistry__free\n";
187 s_test__createSimpleRegistry(rtl::OUString());
190 static void s_test__createSimpleRegistry__purpose()
192 s_comment += "\ts_test__createSimpleRegistry__purpose\n";
194 s_test__createSimpleRegistry(":testenv");
198 static void s_test__bootstrap_InitialComponentContext(rtl::OUString const & clientPurpose)
200 cppu::EnvGuard envGuard(uno::Environment(rtl::OUString(UNO_LB_UNO)
201 + clientPurpose, NULL));
202 if (!clientPurpose.isEmpty() && !envGuard.is())
204 s_comment += "\t\tcouldn't get purpose env: \"" + clientPurpose + "\" - FAILURE\n";
205 return;
208 uno::Reference<uno::XComponentContext> xContext(
209 cppu::bootstrap_InitialComponentContext(
210 uno::Reference<registry::XSimpleRegistry>(),
211 s_getSDrive())
214 if (!xContext.is())
216 s_comment += "\t\tgot no object - FAILURE\n";
217 return;
220 if (!clientPurpose.isEmpty() && !s_check_object_is_in(xContext.get()))
222 s_comment += "\t\tcouldn't find object in current purpose \"" + clientPurpose + "\" - FAILURE\n";
225 uno::Reference<lang::XComponent> xComponent(xContext, uno::UNO_QUERY_THROW);
226 xComponent->dispose();
229 static void s_test__bootstrap_InitialComponentContext__free()
231 s_comment += "\ts_test__bootstrap_InitialComponentContext__free\n";
233 s_test__bootstrap_InitialComponentContext(rtl::OUString());
236 static void s_test__bootstrap_InitialComponentContext__purpose()
238 s_comment += "\ts_test__bootstrap_InitialComponentContext__purpose\n";
240 s_test__bootstrap_InitialComponentContext(":testenv");
244 SAL_IMPLEMENT_MAIN_WITH_ARGS(/*argc*/, argv)
246 s_test__createSimpleRegistry__free();
247 s_test__createSimpleRegistry__purpose();
249 s_test__loadSharedLibComponentFactory__free_free();
250 s_test__loadSharedLibComponentFactory__free_purpose();
251 s_test__loadSharedLibComponentFactory__purpose_free();
252 s_test__loadSharedLibComponentFactory__purpose_purpose();
254 s_test__bootstrap_InitialComponentContext__free();
255 s_test__bootstrap_InitialComponentContext__purpose();
257 int ret;
258 if (s_comment.indexOf("FAILURE") == -1)
260 s_comment += "TESTS PASSED\n";
261 ret = 0;
263 else
265 s_comment += "TESTS _NOT_ PASSED\n";
266 ret = -1;
269 std::cerr
270 << argv[0]
271 << std::endl
272 << rtl::OUStringToOString(s_comment, RTL_TEXTENCODING_ASCII_US).getStr()
273 << std::endl;
275 return ret;
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */