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: FreeReference.test.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 ************************************************************************/
35 #include "uno/environment.hxx"
37 #include "cppu/FreeReference.hxx"
38 #include "cppu/EnvGuards.hxx"
40 #include "../ObjectFactory/ObjectFactory.hxx"
44 using namespace com::sun::star
;
46 static rtl::OUString s_comment
;
47 static uno::Environment s_env
;
50 static void s_callee_in(rtl_uString
* pMethod_name
)
52 rtl::OUString
method_name(pMethod_name
);
54 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\ts_callee_in method:\""));
55 s_comment
+= method_name
;
57 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" env: "));
58 s_comment
+= uno::Environment::getCurrent().getTypeName();
62 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: s_env not set"));
67 int valid
= s_env
.isValid(&reason
);
70 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
74 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: "));
76 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
80 static void s_callee_out(rtl_uString
* pMethod_name
)
82 rtl::OUString
method_name(pMethod_name
);
84 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\ts_callee_out method:\""));
85 s_comment
+= method_name
;
87 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" env: "));
88 s_comment
+= uno::Environment::getCurrent().getTypeName();
92 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: s_env not set"));
97 int valid
= s_env
.isValid(&reason
);
100 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
104 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: is in\n"));
109 static cppu::FreeReference
<uno::XInterface
> s_get_envObject(void)
111 cppu::EnvGuard
envGuard(s_env
);
113 uno::XInterface
* pObject
= reinterpret_cast<uno::XInterface
*>(
114 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
117 cppu::FreeReference
<uno::XInterface
> env_obj(pObject
, SAL_NO_ACQUIRE
);
122 static cppu::FreeReference
<uno::XInterface
> s_get_flatObject(void)
124 uno::XInterface
* pObject
= reinterpret_cast<uno::XInterface
*>(
125 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
128 return cppu::FreeReference
<uno::XInterface
>(pObject
, SAL_NO_ACQUIRE
);
132 static void s_test_operator_arrow(void)
134 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_arrow:\n"));
136 cppu::FreeReference
<uno::XInterface
> env_obj(s_get_envObject());
143 static void s_test_operator_assign_empty_reference(void)
145 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_assign_empty_reference\n"));
147 cppu::FreeReference
<uno::XInterface
> flat_obj(s_get_flatObject());
148 cppu::FreeReference
<uno::XInterface
> empty_ref
;
150 flat_obj
= empty_ref
;
153 static void s_test_operator_assign_inner_reference_inside(void)
155 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_assign_inner_reference_inside\n"));
157 cppu::FreeReference
<uno::XInterface
> env_obj(s_get_envObject());
158 cppu::FreeReference
<uno::XInterface
> flat_obj(s_get_flatObject());
161 cppu::EnvGuard
envGuard(s_env
);
166 static void s_test_method_clear_inner_reference_inside(void)
168 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_clear_inner_reference_inside\n"));
170 cppu::FreeReference
<uno::XInterface
> env_obj(s_get_envObject());
173 cppu::EnvGuard
envGuard(s_env
);
178 static void s_test_method_clear_inner_reference_outside(void)
180 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_clear_inner_reference_outside\n"));
182 cppu::FreeReference
<uno::XInterface
> env_obj(s_get_envObject());
187 static void s_test_method_clear_outer_reference_inside(void)
189 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_clear_outer_reference_inside\n"));
191 cppu::FreeReference
<uno::XInterface
> flat_obj(s_get_flatObject());
194 cppu::EnvGuard
envGuard(s_env
);
199 static void s_test_method_clear_outer_reference_outside(void)
201 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_clear_outer_reference_outside\n"));
203 cppu::FreeReference
<uno::XInterface
> flat_obj(s_get_flatObject());
208 static void s_test_method_set_inside_with_inner_reference(void)
210 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_inside_with_inner_reference\n"));
212 cppu::FreeReference
<uno::XInterface
> env_obj(s_get_envObject());
215 cppu::EnvGuard
envGuard(s_env
);
216 uno::XInterface
* pObject2
= reinterpret_cast<uno::XInterface
*>(
217 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
220 uno::Reference
<uno::XInterface
> ref(pObject2
, SAL_NO_ACQUIRE
);
227 static void s_test_method_set_inside_with_outer_reference(void)
229 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_inside_with_outer_reference\n"));
231 cppu::FreeReference
<uno::XInterface
> env_obj(s_get_envObject());
233 uno::XInterface
* pObject
= reinterpret_cast<uno::XInterface
*>(
234 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
237 uno::Reference
<uno::XInterface
> ref(pObject
, SAL_NO_ACQUIRE
);
243 static void s_test_method_set_with_empty_reference(void)
245 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_outside_with_empty_reference\n"));
247 cppu::FreeReference
<uno::XInterface
> flat_obj(s_get_flatObject());
250 uno::Reference
<uno::XInterface
> ref
;
256 static void s_test_method_set_outside_with_inner_reference(void)
258 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_outside_with_inner_reference\n"));
260 cppu::FreeReference
<uno::XInterface
> flat_obj(s_get_flatObject());
263 cppu::EnvGuard
envGuard(s_env
);
265 uno::XInterface
* pObject2
= reinterpret_cast<uno::XInterface
*>(
266 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
269 uno::Reference
<uno::XInterface
> ref(pObject2
, SAL_NO_ACQUIRE
);
276 static void s_test_method_set_outside_with_outer_reference(void)
278 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_method_set_outside_with_outer_reference\n"));
280 cppu::FreeReference
<uno::XInterface
> flat_obj(s_get_flatObject());
282 uno::XInterface
* pObject2
= reinterpret_cast<uno::XInterface
*>(
283 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
286 uno::Reference
<uno::XInterface
> ref(pObject2
, SAL_NO_ACQUIRE
);
292 static void s_test_operator_equal(void)
294 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_equal\n"));
297 cppu::FreeReference
<uno::XInterface
> env_obj
;
298 uno::XInterface
* pObject
= NULL
;
300 cppu::EnvGuard
envGuard(s_env
);
301 pObject
= reinterpret_cast<uno::XInterface
*>(
302 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
305 env_obj
= cppu::FreeReference
<uno::XInterface
>(pObject
, SAL_NO_ACQUIRE
);
309 uno::Mapping
mapping(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
) ":unsafe")),
310 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))));
311 uno::Reference
<uno::XInterface
> tmp
;
312 uno::XInterface
* pMappedObject
= reinterpret_cast<uno::XInterface
*>(mapping
.mapInterface(pObject
, ::getCppuType(&tmp
)));
315 cppu::FreeReference
<uno::XInterface
> flat_obj(pMappedObject
, SAL_NO_ACQUIRE
);
318 cppu::EnvGuard
envGuard(s_env
);
320 if (!(env_obj
== flat_obj
))
321 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\trefs are not equal inside - FAILURE\n"));
324 if (!(env_obj
== flat_obj
))
325 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\trefs are not equal outside - FAILURE\n"));
328 static void s_test_operator_unequal(void)
330 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test_operator_unequal\n"));
333 uno::XInterface
* pObject
= NULL
;
334 cppu::FreeReference
<uno::XInterface
> env_obj
;
336 cppu::EnvGuard
envGuard(s_env
);
338 pObject
= reinterpret_cast<uno::XInterface
*>(
339 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
342 env_obj
= cppu::FreeReference
<uno::XInterface
>(pObject
, SAL_NO_ACQUIRE
);
346 uno::Mapping
mapping(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
) ":unsafe")),
347 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))));
348 uno::Reference
<uno::XInterface
> tmp
;
349 uno::XInterface
* pMappedObject
= reinterpret_cast<uno::XInterface
*>(mapping
.mapInterface(pObject
, ::getCppuType(&tmp
)));
352 cppu::FreeReference
<uno::XInterface
> flat_obj(pMappedObject
, SAL_NO_ACQUIRE
);
355 cppu::EnvGuard
envGuard(s_env
);
357 if (env_obj
!= flat_obj
)
358 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\trefs are not equal inside - FAILURE\n"));
361 if (env_obj
!= flat_obj
)
362 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\trefs are not equal outside - FAILURE\n"));
366 SAL_IMPLEMENT_MAIN_WITH_ARGS(/*argc*/, argv
)
368 uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO
))).enter();
370 s_env
= uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("uno:unsafe")));
373 rtl::OUString result
;
374 s_test_operator_arrow();
375 s_test_operator_equal();
376 s_test_operator_unequal();
378 s_test_operator_assign_empty_reference();
379 s_test_operator_assign_inner_reference_inside();
381 s_test_method_set_with_empty_reference();
382 s_test_method_set_inside_with_inner_reference();
383 s_test_method_set_inside_with_outer_reference();
384 s_test_method_set_outside_with_inner_reference();
385 s_test_method_set_outside_with_outer_reference();
387 s_test_method_clear_inner_reference_inside();
388 s_test_method_clear_inner_reference_outside();
389 s_test_method_clear_outer_reference_inside();
390 s_test_method_clear_outer_reference_outside();
395 uno_Environment_enter(NULL
);
399 if (s_comment
.indexOf(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILURE"))) == -1)
401 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS PASSED\n"));
406 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS _NOT_ PASSED\n"));
413 << rtl::OUStringToOString(s_comment
, RTL_TEXTENCODING_ASCII_US
).getStr()