1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mapping.tester.cxx,v $
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 #define CPPU_TEST_MAPPING_TESTER_TESTS_IMPL
37 #include "osl/module.hxx"
38 #include "rtl/ustring.hxx"
39 #include "uno/environment.hxx"
40 #include "uno/mapping.hxx"
42 #include "../ObjectFactory/ObjectFactory.hxx"
43 #include "Mapping.tester.hxx"
45 using namespace ::com::sun::star
;
49 static uno::Mapping
s_getMapping(rtl::OUString
const & from_envDcp
,
50 rtl::OUString
const & to_envDcp
,
51 uno::Environment
* pSourceEnv
,
52 uno::Environment
* pTargetEnv
)
54 uno::Environment
sourceEnv(from_envDcp
);
55 uno::Environment
targetEnv(to_envDcp
);
57 uno::Mapping
mapping(sourceEnv
, targetEnv
);
60 *pSourceEnv
= sourceEnv
;
63 *pTargetEnv
= targetEnv
;
68 static void * s_mapObject(rtl::OUString
const & from_envDcp
,
69 rtl::OUString
const & to_envDcp
,
71 uno::Environment
* pSourceEnv
,
72 uno::Environment
* pTargetEnv
)
74 uno::Mapping
mapping(s_getMapping(from_envDcp
, to_envDcp
, pSourceEnv
, pTargetEnv
));
76 void * mapped_object
= NULL
;
78 typelib_TypeDescriptionReference
* type_XInterface
=
79 * typelib_static_type_getByTypeClass( typelib_TypeClass_INTERFACE
);
81 typelib_InterfaceTypeDescription
* pTXInterfaceDescr
= 0;
83 TYPELIB_DANGER_GET( (typelib_TypeDescription
**) &pTXInterfaceDescr
, type_XInterface
);
84 mapping
.mapInterface(&mapped_object
, object
, pTXInterfaceDescr
);
85 TYPELIB_DANGER_RELEASE( (typelib_TypeDescription
*) pTXInterfaceDescr
);
91 static rtl::OUString
s_test_regetMapping(void)
93 g_result
= rtl::OUString();
94 g_custom
= rtl::OUString();
96 rtl::OUString
result(RTL_CONSTASCII_USTRINGPARAM("\tmapping reget test --> "));
98 uno::Mapping
mapping1(g_from_envDcp
, g_to_envDcp
);
99 uno::Mapping
mapping2(g_from_envDcp
, g_to_envDcp
);
101 if (mapping2
.get() == mapping1
.get())
102 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OKAY\n"));
105 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED\n"));
112 static rtl::OUString
s_test_mapObject(Callee
* pCallee
)
114 g_result
= rtl::OUString();
115 g_custom
= rtl::OUString();
117 rtl::OUString
result(RTL_CONSTASCII_USTRINGPARAM("\tobject map test --> "));
119 void * pObject
= createObject(g_to_envDcp
, pCallee
);
121 uno::Environment sourceEnv
;
122 uno::Environment targetEnv
;
123 void * mapped_object
= s_mapObject(g_to_envDcp
, g_from_envDcp
, pObject
, &sourceEnv
, &targetEnv
);
124 sourceEnv
.get()->pExtEnv
->releaseInterface(sourceEnv
.get()->pExtEnv
, pObject
);
128 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OKAY\n"));
131 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED\n"));
133 targetEnv
.get()->pExtEnv
->releaseInterface(targetEnv
.get()->pExtEnv
, mapped_object
);
140 static rtl::OUString
s_test_remapObject(Callee
* pCallee
)
142 g_result
= rtl::OUString();
143 g_custom
= rtl::OUString();
145 rtl::OUString
result(RTL_CONSTASCII_USTRINGPARAM("\tobject remap test --> "));
147 void * pObject
= createObject(g_to_envDcp
, pCallee
);
149 uno::Environment sourceEnv
;
150 uno::Environment targetEnv
;
151 void * mapped_object1
= s_mapObject(g_to_envDcp
, g_from_envDcp
, pObject
, &sourceEnv
, &targetEnv
);
152 void * mapped_object2
= s_mapObject(g_to_envDcp
, g_from_envDcp
, pObject
, NULL
, NULL
);
153 sourceEnv
.get()->pExtEnv
->releaseInterface(sourceEnv
.get()->pExtEnv
, pObject
);
156 if (mapped_object2
== mapped_object1
)
157 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OKAY\n"));
160 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED\n"));
162 targetEnv
.get()->pExtEnv
->releaseInterface(targetEnv
.get()->pExtEnv
, mapped_object1
);
163 targetEnv
.get()->pExtEnv
->releaseInterface(targetEnv
.get()->pExtEnv
, mapped_object2
);
170 static rtl::OUString
s_test_mapBackObject(Callee
* pCallee
)
172 g_result
= rtl::OUString();
173 g_custom
= rtl::OUString();
175 rtl::OUString
result(RTL_CONSTASCII_USTRINGPARAM("\tobject map back test --> "));
177 void * pObject
= createObject(g_to_envDcp
, pCallee
);
179 uno::Environment to_sourceEnv
;
180 uno::Environment to_targetEnv
;
181 void * mapped_object
= s_mapObject(g_to_envDcp
,
187 uno::Environment back_sourceEnv
;
188 uno::Environment back_targetEnv
;
189 void * mapped_back_object
= s_mapObject(g_from_envDcp
,
195 if (back_targetEnv
.get() != to_sourceEnv
.get())
198 if (back_sourceEnv
.get() != to_targetEnv
.get())
201 if (pObject
== mapped_back_object
)
202 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OKAY\n"));
205 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED\n"));
207 to_sourceEnv
.get()->pExtEnv
->releaseInterface(to_sourceEnv
.get()->pExtEnv
, pObject
);
208 to_targetEnv
.get()->pExtEnv
->releaseInterface(to_targetEnv
.get()->pExtEnv
, mapped_object
);
209 back_targetEnv
.get()->pExtEnv
->releaseInterface(back_targetEnv
.get()->pExtEnv
, mapped_back_object
);
216 static rtl::OUString
s_test_objectRegistered(Callee
* pCallee
)
218 g_result
= rtl::OUString();
219 g_custom
= rtl::OUString();
221 rtl::OUString
result(RTL_CONSTASCII_USTRINGPARAM("\tobject registered test --> "));
223 void * pObject
= createObject(g_to_envDcp
, pCallee
);
225 uno::Environment sourceEnv
;
226 uno::Environment targetEnv
;
227 void * mapped_object
= s_mapObject(g_to_envDcp
,
233 if (sourceEnv
.get() == targetEnv
.get())
235 if (mapped_object
!= pObject
)
240 typelib_TypeDescriptionReference
* type_XInterface
=
241 * typelib_static_type_getByTypeClass( typelib_TypeClass_INTERFACE
);
243 typelib_InterfaceTypeDescription
* pTXInterfaceDescr
= 0;
244 TYPELIB_DANGER_GET( (typelib_TypeDescription
**) &pTXInterfaceDescr
, type_XInterface
);
246 rtl_uString
* pOId
= 0;
247 targetEnv
.get()->pExtEnv
->getObjectIdentifier(targetEnv
.get()->pExtEnv
, &pOId
, mapped_object
);
251 targetEnv
.get()->pExtEnv
->getRegisteredInterface(targetEnv
.get()->pExtEnv
,
256 TYPELIB_DANGER_RELEASE( (typelib_TypeDescription
*) pTXInterfaceDescr
);
258 if (mapped_object
== pOut
)
259 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OKAY\n"));
262 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILED\n"));
265 targetEnv
.get()->pExtEnv
->releaseInterface(targetEnv
.get()->pExtEnv
, pOut
);
268 sourceEnv
.get()->pExtEnv
->releaseInterface(sourceEnv
.get()->pExtEnv
, pObject
);
269 targetEnv
.get()->pExtEnv
->releaseInterface(targetEnv
.get()->pExtEnv
, mapped_object
);
277 // static void s_test_mappingNotEqual(char const * source1Name, char const * dest1Name,
278 // char const * source2Name, char const * dest2Name)
280 // rtl::OUString source1EnvName(source1Name, rtl_str_getLength(source1Name), RTL_TEXTENCODING_ASCII_US);
281 // rtl::OUString dest1EnvName (dest1Name, rtl_str_getLength(dest1Name), RTL_TEXTENCODING_ASCII_US);
283 // rtl::OUString source2EnvName(source2Name, rtl_str_getLength(source2Name), RTL_TEXTENCODING_ASCII_US);
284 // rtl::OUString dest2EnvName (dest2Name, rtl_str_getLength(dest2Name), RTL_TEXTENCODING_ASCII_US);
286 // uno::Mapping mapping1(s_getMapping(source1EnvName, dest1EnvName, NULL, NULL));
287 // uno::Mapping mapping2(s_getMapping(source2EnvName, dest2EnvName, NULL, NULL));
289 // if (mapping2.get() == mapping1.get())
291 // // fprintf(stderr, "***************** can not reget mapping\n");
296 static rtl::OUString
s_test_call(Callee
* pCallee
)
298 g_result
= rtl::OUString();
299 g_custom
= rtl::OUString();
301 rtl::OUString
result(RTL_CONSTASCII_USTRINGPARAM("\tobject call test --> "));
303 void * pObject
= createObject(g_to_envDcp
, pCallee
);
305 uno::Environment sourceEnv
;
306 uno::Environment targetEnv
;
307 void * mapped_object
= s_mapObject(g_to_envDcp
, g_from_envDcp
, pObject
, &sourceEnv
, &targetEnv
);
308 sourceEnv
.get()->pExtEnv
->releaseInterface(sourceEnv
.get()->pExtEnv
, pObject
);
312 callObject(g_from_envDcp
, mapped_object
);
315 targetEnv
.get()->pExtEnv
->releaseInterface(targetEnv
.get()->pExtEnv
, mapped_object
);
323 static rtl::OString
test_mapping_a(char const * pfrom_envDcp
,
324 char const * pto_envDcp
,
328 g_from_envDcp
= rtl::OUString(pfrom_envDcp
, rtl_str_getLength(pfrom_envDcp
), RTL_TEXTENCODING_ASCII_US
);
329 g_to_envDcp
= rtl::OUString(pto_envDcp
, rtl_str_getLength(pto_envDcp
), RTL_TEXTENCODING_ASCII_US
);
330 g_ref
= rtl::OUString(pRef
, rtl_str_getLength(pRef
), RTL_TEXTENCODING_ASCII_US
);
332 rtl::OUString result
;
336 result
+= s_test_regetMapping ();
337 result
+= s_test_objectRegistered(pCallee
);
338 result
+= s_test_remapObject (pCallee
);
339 result
+= s_test_mapBackObject (pCallee
);
340 result
+= s_test_mapObject (pCallee
);
341 result
+= s_test_call (pCallee
);
343 return rtl::OUStringToOString(result
, RTL_TEXTENCODING_ASCII_US
);
347 static rtl::OString
s_replaceCPP(rtl::OString
const & str
)
351 sal_Int32 index_old
= 0;
352 sal_Int32 index
= str
.indexOf("CPP", index_old
);
355 result
+= str
.copy(index_old
, index
- index_old
);
356 result
+= rtl::OString(CPPU_STRINGIFY(CPPU_ENV
));
358 index_old
= index
+ 3;
359 index
= str
.indexOf("CPP", index_old
);
361 result
+= str
.copy(index_old
);
367 extern "C" { static void s_dummy(rtl_uString
* pMethod_name
)
369 rtl::OUString
result(RTL_CONSTASCII_USTRINGPARAM("\t\tmethod: "));
370 result
+= rtl::OUString(pMethod_name
);
371 result
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" -dummy-\n"));
376 static Callee
* s_pCustomCallee
= s_dummy
;
378 extern "C" { static void s_callee(rtl_uString
* pMethod_name
)
380 if (rtl::OUString(pMethod_name
).equals(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XInterface::queryInterface"))))
384 if (!g_from_envDcp
.match(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO
))))
386 tmp
+= g_from_envDcp
;
387 tmp
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-->"));
392 if (!g_to_envDcp
.match(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO
))))
394 tmp
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-->"));
399 s_pCustomCallee(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XInterface::queryInterface")).pData
);
402 s_pCustomCallee(pMethod_name
);
406 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
409 rtl::OString message
;
411 if (argc
== 3 || argc
== 6)
413 rtl::OString
from_envDcp(argv
[1]);
414 from_envDcp
= s_replaceCPP(from_envDcp
);
416 rtl::OString
to_envDcp(argv
[2]);
417 to_envDcp
= s_replaceCPP(to_envDcp
);
422 if (argc
== 6 && rtl::OString(argv
[3]).equals(rtl::OString("-s")))
424 rtl::OUString
libName(argv
[4], rtl_str_getLength(argv
[4]), RTL_TEXTENCODING_ASCII_US
);
425 libName
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SAL_DLLEXTENSION
));
426 module
.load(libName
);
428 s_pCustomCallee
= (Callee
*)module
.getFunctionSymbol(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CALLEE")));
429 if (!s_pCustomCallee
)
432 ref
= s_replaceCPP(rtl::OString(argv
[5]));
435 message
+= rtl::OString("TESTING: ");
436 message
+= from_envDcp
;
437 message
+= rtl::OString("-->");
438 message
+= to_envDcp
;
439 message
+= rtl::OString("\n");
441 message
+= test_mapping_a(from_envDcp
.getStr(), to_envDcp
.getStr(), s_callee
, ref
);
443 if (message
.indexOf(rtl::OString("FAILED")) == -1)
444 message
+= rtl::OString("TESTS PASSED\n");
448 message
+= rtl::OString("TESTS _NOT_ PASSED\n");
453 message
= "Usage: <source env. dcp> <destination env. dcp> [-s testfun <ref. string>]\n";
456 std::cout
<< message
.getStr();