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: Shield.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/Shield.hxx"
38 #include "cppu/EnvGuards.hxx"
40 #include "../ObjectFactory/ObjectFactory.hxx"
43 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();
59 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\""));
63 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: s_env not set"));
68 int valid
= s_env
.isValid(&reason
);
71 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
75 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: "));
77 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
81 static void s_callee_out(rtl_uString
* pMethod_name
)
83 rtl::OUString
method_name(pMethod_name
);
85 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\t\ts_callee_out method:\""));
86 s_comment
+= method_name
;
88 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\" env: \""));
89 s_comment
+= uno::Environment::getCurrent().getTypeName();
90 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\""));
94 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: s_env not set"));
99 int valid
= s_env
.isValid(&reason
);
102 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
105 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" - FAILURE: is in\n"));
109 static uno::Reference
<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 return uno::Reference
<uno::XInterface
>(pObject
, SAL_NO_ACQUIRE
);
120 static uno::XInterface
* s_x_get_flatObject(void)
122 uno::XInterface
* pObject
= reinterpret_cast<uno::XInterface
*>(
123 createObject(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
))),
129 static uno::Reference
<uno::XInterface
> s_get_flatObject(void)
131 return uno::Reference
<uno::XInterface
>(s_x_get_flatObject(), SAL_NO_ACQUIRE
);
135 static void s_test__shield(void)
137 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__shield:\n"));
139 uno::Reference
<uno::XInterface
> obj
;
142 cppu::EnvGuard
envGuard(s_env
);
143 obj
.set(cppu::shield(s_get_envObject().get()), SAL_NO_ACQUIRE
);
150 static void s_test__unshield(void)
152 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__unshield:\n"));
154 uno::Reference
<uno::XInterface
> obj
;
157 cppu::EnvGuard
envGuard(s_env
);
158 uno::XInterface
* pObj
= s_x_get_flatObject();
159 obj
.set(cppu::unshield(pObj
), SAL_NO_ACQUIRE
);
166 cppu::EnvGuard
envGuard(s_env
);
171 static void s_test__shieldAny(void)
173 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__shieldAny:\n"));
178 cppu::EnvGuard
envGuard(s_env
);
180 any
<<= s_get_envObject();
181 cppu::shieldAny(any
, &out
);
184 uno::Reference
<uno::XInterface
> obj
;
191 static void s_test__unshieldAny(void)
193 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\ts_test__unshieldAny:\n"));
197 uno::Any
any(s_get_flatObject());
200 cppu::EnvGuard
envGuard(s_env
);
201 cppu::unshieldAny(any
, &out
);
206 cppu::EnvGuard
envGuard(s_env
);
208 uno::Reference
<uno::XInterface
> obj
;
216 SAL_IMPLEMENT_MAIN_WITH_ARGS(/*argc*/, argv
)
218 uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO
))).enter();
220 s_env
= uno::Environment(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("uno:unsafe")));
225 s_test__unshieldAny();
230 uno_Environment_enter(NULL
);
234 if (s_comment
.indexOf(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FAILURE"))) == -1)
236 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS PASSED\n"));
241 s_comment
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TESTS _NOT_ PASSED\n"));
248 << rtl::OUStringToOString(s_comment
, RTL_TEXTENCODING_ASCII_US
).getStr()