update dev300-m58
[ooovba.git] / cppuhelper / test / bootstrap / bootstrap.test.cxx
blob732b0b7071e51899805615d14c064e32a09885b8
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: bootstrap.test.cxx,v $
10 * $Revision: 1.3 $
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 ************************************************************************/
32 #include "sal/main.h"
33 #include "osl/file.hxx"
35 #include "typelib/typedescription.hxx"
37 #include "cppuhelper/bootstrap.hxx"
38 #include "cppuhelper/shlib.hxx"
40 #include <com/sun/star/lang/XComponent.hpp>
42 #include "uno/environment.hxx"
43 #include "cppu/EnvDcp.hxx"
44 #include "cppu/EnvGuards.hxx"
46 #include <iostream>
49 #ifndef SAL_DLLPREFIX
50 # define SAL_DLLPREFIX ""
51 #endif
54 using namespace com::sun::star;
57 static rtl::OUString s_comment;
59 static bool s_check_object_is_in(void * pObject)
61 uno::Environment currentEnv(uno::Environment::getCurrent());
63 rtl_uString * pOId = NULL;
64 currentEnv.get()->pExtEnv->getObjectIdentifier(currentEnv.get()->pExtEnv, &pOId, pObject);
67 uno::TypeDescription typeDescription(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XInterface")));
69 void * pRegisteredObject = NULL;
70 currentEnv.get()->pExtEnv->getRegisteredInterface(currentEnv.get()->pExtEnv,
71 &pRegisteredObject,
72 pOId,
73 (typelib_InterfaceTypeDescription *)typeDescription.get());
76 if (pOId) rtl_uString_release(pOId);
78 bool result = pRegisteredObject != NULL;
80 if (result)
81 currentEnv.get()->pExtEnv->releaseInterface(currentEnv.get()->pExtEnv, pRegisteredObject);
83 return result;
86 static void s_test__loadSharedLibComponentFactory(rtl::OUString const & clientPurpose,
87 rtl::OUString const & servicePurpose)
89 cppu::EnvGuard envGuard(uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO))
90 + clientPurpose, NULL));
91 if (clientPurpose.getLength() && !envGuard.is())
93 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't get purpose env: \""));
94 s_comment += clientPurpose;
95 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
97 return;
100 rtl::OString os_clientPurpose(rtl::OUStringToOString(clientPurpose, RTL_TEXTENCODING_ASCII_US));
102 uno::Reference<uno::XInterface> xItf(
103 cppu::loadSharedLibComponentFactory(
104 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SAL_DLLPREFIX "TestComponent.uno" SAL_DLLEXTENSION)),
105 #ifdef WIN32
106 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("")),
107 #else
108 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file://../lib/")),
109 #endif
110 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent")) + servicePurpose,
111 uno::Reference<lang::XMultiServiceFactory>(),
112 uno::Reference<registry::XRegistryKey>()
116 if (!xItf.is())
118 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tgot no object - FAILURE\n"));
119 return;
122 if (!clientPurpose.equals(servicePurpose) && !s_check_object_is_in(xItf.get()))
124 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't find object in current purpose \""));
125 s_comment += clientPurpose;
126 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
129 if (!cppu::EnvDcp::getPurpose(uno::Environment::getCurrent().getTypeName()).equals(clientPurpose))
131 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tdid not enter client purpose \""));
132 s_comment += clientPurpose;
133 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
137 static void s_test__loadSharedLibComponentFactory__free_free()
139 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__loadSharedLibComponentFactory__free_free\n"));
141 s_test__loadSharedLibComponentFactory(rtl::OUString(), rtl::OUString());
144 static void s_test__loadSharedLibComponentFactory__free_purpose()
146 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__loadSharedLibComponentFactory__free_purpose\n"));
148 s_test__loadSharedLibComponentFactory(rtl::OUString(),
149 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")));
152 static void s_test__loadSharedLibComponentFactory__purpose_free()
154 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__loadSharedLibComponentFactory__purpose_free\n"));
156 s_test__loadSharedLibComponentFactory(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")),
157 rtl::OUString());
160 static void s_test__loadSharedLibComponentFactory__purpose_purpose()
162 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__loadSharedLibComponentFactory__purpose_purpose\n"));
164 s_test__loadSharedLibComponentFactory(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")),
165 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")));
168 static rtl::OUString s_getSDrive(void)
170 rtl::OUString path;//(RTL_CONSTASCII_USTRINGPARAM("file://"));
172 char const * tmp = getenv("SOLARVER");
173 path += rtl::OUString(tmp, rtl_str_getLength(tmp), RTL_TEXTENCODING_ASCII_US);
174 path += rtl::OUString(SAL_PATHDELIMITER);
176 tmp = getenv("INPATH");
177 path += rtl::OUString(tmp, rtl_str_getLength(tmp), RTL_TEXTENCODING_ASCII_US);
178 path += rtl::OUString(SAL_PATHDELIMITER);
179 #ifdef WIN32
180 path += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bin"));
182 #else
183 path += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("lib"));
184 #endif
186 tmp = getenv("UPDMINOREXT");
187 if (tmp)
188 path += rtl::OUString(tmp, rtl_str_getLength(tmp), RTL_TEXTENCODING_ASCII_US);
190 osl::FileBase::getFileURLFromSystemPath(path, path);
192 return path;
195 static void s_test__createSimpleRegistry(rtl::OUString const & clientPurpose)
197 cppu::EnvGuard envGuard(uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO))
198 + clientPurpose, NULL));
199 if (clientPurpose.getLength() && !envGuard.is())
201 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't get purpose env: \""));
202 s_comment += clientPurpose;
203 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
205 return;
208 uno::Reference<registry::XSimpleRegistry> registry(cppu::createSimpleRegistry(
209 s_getSDrive()));
211 if (!registry.is())
213 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tgot no object - FAILURE\n"));
214 return;
217 if (clientPurpose.getLength() != 0 && !s_check_object_is_in(registry.get()))
219 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't find object in current purpose \""));
220 s_comment += clientPurpose;
221 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
225 static void s_test__createSimpleRegistry__free(void)
227 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__createSimpleRegistry__free\n"));
229 s_test__createSimpleRegistry(rtl::OUString());
232 static void s_test__createSimpleRegistry__purpose(void)
234 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__createSimpleRegistry__purpose\n"));
236 s_test__createSimpleRegistry(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")));
240 static void s_test__bootstrap_InitialComponentContext(rtl::OUString const & clientPurpose)
242 cppu::EnvGuard envGuard(uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO))
243 + clientPurpose, NULL));
244 if (clientPurpose.getLength() && !envGuard.is())
246 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't get purpose env: \""));
247 s_comment += clientPurpose;
248 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
250 return;
253 uno::Reference<uno::XComponentContext> xContext(
254 cppu::bootstrap_InitialComponentContext(
255 uno::Reference<registry::XSimpleRegistry>(),
256 s_getSDrive())
259 if (!xContext.is())
261 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tgot no object - FAILURE\n"));
262 return;
265 if (clientPurpose.getLength() != 0 && !s_check_object_is_in(xContext.get()))
267 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\tcouldn't find object in current purpose \""));
268 s_comment += clientPurpose;
269 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" - FAILURE\n"));
272 uno::Reference<lang::XComponent> xComponent(xContext, uno::UNO_QUERY_THROW);
273 xComponent->dispose();
276 static void s_test__bootstrap_InitialComponentContext__free(void)
278 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__bootstrap_InitialComponentContext__free\n"));
280 s_test__bootstrap_InitialComponentContext(rtl::OUString());
283 static void s_test__bootstrap_InitialComponentContext__purpose(void)
285 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__bootstrap_InitialComponentContext__purpose\n"));
287 s_test__bootstrap_InitialComponentContext(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":testenv")));
291 SAL_IMPLEMENT_MAIN_WITH_ARGS(/*argc*/, argv)
293 s_test__createSimpleRegistry__free();
294 s_test__createSimpleRegistry__purpose();
296 s_test__loadSharedLibComponentFactory__free_free();
297 s_test__loadSharedLibComponentFactory__free_purpose();
298 s_test__loadSharedLibComponentFactory__purpose_free();
299 s_test__loadSharedLibComponentFactory__purpose_purpose();
301 s_test__bootstrap_InitialComponentContext__free();
302 s_test__bootstrap_InitialComponentContext__purpose();
304 int ret;
305 if (s_comment.indexOf(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILURE"))) == -1)
307 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS PASSED\n"));
308 ret = 0;
310 else
312 s_comment += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS _NOT_ PASSED\n"));
313 ret = -1;
316 std::cerr
317 << argv[0]
318 << std::endl
319 << rtl::OUStringToOString(s_comment, RTL_TEXTENCODING_ASCII_US).getStr()
320 << std::endl;
322 return ret;