bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / osl / module / osl_Module.cxx
blob56f781069f2891b0952f1f1898a47bed09542f2d
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 .
20 // include files
22 #include <osl_Module_Const.h>
24 using namespace osl;
26 using ::rtl::OUString;
27 using ::rtl::OUStringToOString;
28 using ::rtl::OString;
30 /** get dll file URL.
32 inline ::rtl::OUString getDllURL()
34 #if ( defined WNT ) // lib in Unix and lib in Windows are not same in file name.
35 ::rtl::OUString libPath( "test_Module_DLL.dll" );
36 #else
37 ::rtl::OUString libPath( "libtest_Module_DLL.so" );
38 #endif
40 ::rtl::OUString dirPath, dllPath;
41 osl::Module::getUrlFromAddress(
42 reinterpret_cast<oslGenericFunction>(&getDllURL), dirPath);
43 dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
44 osl::FileBase::getAbsoluteFileURL( dirPath, libPath, dllPath );
46 return dllPath;
49 namespace osl_Module
52 /** class and member function that is available for module test :
55 class testClass
57 public:
58 static void myFunc()
60 printf("#Sun Microsystem\n");
64 /** testing the methods:
65 Module();
66 Module( const ::rtl::OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT);
68 class ctors : public CppUnit::TestFixture
70 public:
71 bool bRes, bRes1;
73 void ctors_none( )
75 ::osl::Module aMod;
76 bRes = aMod.is();
78 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
79 !bRes );
82 void ctors_name_mode( )
84 OUString aFileURL;
85 bRes = osl::Module::getUrlFromAddress(
86 reinterpret_cast<oslGenericFunction>(
87 &osl_Module::testClass::myFunc),
88 aFileURL);
90 if ( !( bRes ) )
92 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
95 ::osl::Module aMod( aFileURL );
96 bRes = aMod.is( );
97 aMod.unload( );
99 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
100 bRes );
103 CPPUNIT_TEST_SUITE( ctors );
104 CPPUNIT_TEST( ctors_none );
105 CPPUNIT_TEST( ctors_name_mode );
106 CPPUNIT_TEST_SUITE_END( );
107 }; // class ctors
109 /** testing the methods:
110 static sal_Bool getUrlFromAddress(void * addr, ::rtl::OUString & libraryUrl)
112 class getUrlFromAddress : public CppUnit::TestFixture
114 public:
115 bool bRes, bRes1;
117 void getUrlFromAddress_001( )
119 OUString aFileURL;
120 bRes = osl::Module::getUrlFromAddress(
121 reinterpret_cast<oslGenericFunction>(
122 &osl_Module::testClass::myFunc),
123 aFileURL);
124 if ( !( bRes ) )
126 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
129 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
130 bRes && 0 < aFileURL.lastIndexOf('/') );
133 void getUrlFromAddress_002( )
135 #if !defined( MACOSX )
136 // TODO: Find out why this fails on Mac OS X
137 ::osl::Module aMod( getDllURL( ) );
138 FuncPtr pFunc = reinterpret_cast<FuncPtr>(aMod.getSymbol( rtl::OUString("firstfunc") ));
140 OUString aFileURL;
141 bRes = osl::Module::getUrlFromAddress(
142 reinterpret_cast<oslGenericFunction>(pFunc), aFileURL);
143 if ( !( bRes ) )
145 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
147 aMod.unload( );
149 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
150 bRes && 0 < aFileURL.lastIndexOf('/') && aFileURL.equalsIgnoreAsciiCase( getDllURL( ) ) );
151 #endif
154 /* tester comments: another case is getFunctionSymbol_001*/
156 CPPUNIT_TEST_SUITE( getUrlFromAddress );
157 CPPUNIT_TEST( getUrlFromAddress_001 );
158 CPPUNIT_TEST( getUrlFromAddress_002 );
159 CPPUNIT_TEST_SUITE_END( );
160 }; // class getUrlFromAddress
162 /** testing the method:
163 sal_Bool SAL_CALL load( const ::rtl::OUString& strModuleName,
164 sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT)
166 class load : public CppUnit::TestFixture
168 public:
169 bool bRes, bRes1;
171 void load_001( )
173 ::osl::Module aMod( getDllURL( ) );
174 ::osl::Module aMod1;
176 aMod1.load( getDllURL( ) );
177 bRes = oslModule(aMod) == oslModule(aMod1);
178 aMod.unload( );
179 aMod1.unload( );
181 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
182 bRes );
185 CPPUNIT_TEST_SUITE( load );
186 CPPUNIT_TEST( load_001 );
187 CPPUNIT_TEST_SUITE_END( );
188 }; // class load
190 /** testing the method:
191 void SAL_CALL unload()
193 class unload : public CppUnit::TestFixture
195 public:
196 bool bRes, bRes1;
198 void unload_001( )
200 ::osl::Module aMod( getDllURL( ) );
202 aMod.unload( );
203 bRes = oslModule(aMod) ==NULL;
205 CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
206 bRes );
209 CPPUNIT_TEST_SUITE( unload );
210 CPPUNIT_TEST( unload_001 );
211 CPPUNIT_TEST_SUITE_END( );
212 }; // class unload
214 /** testing the methods:
215 sal_Bool SAL_CALL is() const
217 class is : public CppUnit::TestFixture
219 public:
220 bool bRes, bRes1;
222 void is_001( )
224 OUString aFileURL;
225 bRes = osl::Module::getUrlFromAddress(
226 reinterpret_cast<oslGenericFunction>(
227 osl_Module::testClass::myFunc),
228 aFileURL);
229 if ( !( bRes ) )
231 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead", false );
234 ::osl::Module aMod;
235 bRes = aMod.is( );
236 aMod.load( aFileURL );
237 bRes1 = aMod.is( );
238 aMod.unload( );
240 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
241 !bRes && bRes1 );
243 CPPUNIT_TEST_SUITE( is );
244 CPPUNIT_TEST( is_001 );
245 CPPUNIT_TEST_SUITE_END( );
246 }; // class is
248 /** testing the methods:
249 void* SAL_CALL getSymbol( const ::rtl::OUString& strSymbolName)
251 class getSymbol : public CppUnit::TestFixture
253 public:
254 bool bRes;
256 void getSymbol_001( )
258 #if !defined( MACOSX )
259 // TODO: Find out why this fails on Mac OS X
260 ::osl::Module aMod( getDllURL( ) );
261 FuncPtr pFunc = reinterpret_cast<FuncPtr>(aMod.getSymbol( rtl::OUString("firstfunc") ));
262 bRes = false;
263 if ( pFunc )
264 bRes = pFunc( bRes );
265 aMod.unload();
267 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
268 bRes );
269 #endif
272 CPPUNIT_TEST_SUITE( getSymbol );
273 CPPUNIT_TEST( getSymbol_001 );
274 CPPUNIT_TEST_SUITE_END( );
275 }; // class getSymbol
277 /** testing the methods:
278 operator oslModule() const
280 class optr_oslModule : public CppUnit::TestFixture
282 public:
283 bool bRes, bRes1;
285 void optr_oslModule_001( )
287 #if !defined( MACOSX )
288 // TODO: Find out why this fails on Mac OS X
289 ::osl::Module aMod;
290 bRes = ( (oslModule)aMod == NULL );
292 aMod.load( getDllURL( ) );
293 bRes1 = (oslModule)aMod != NULL;
295 aMod.unload( );
297 CPPUNIT_ASSERT_MESSAGE( "#test comment#: the m_Module of a Module instance will be NULL when is not loaded, it will not be NULL after loaded.",
298 bRes && bRes1 );
299 #endif
302 void optr_oslModule_002( )
304 #if !defined( MACOSX )
305 // TODO: Find out why this fails on Mac OS X
306 ::osl::Module aMod( getDllURL( ) );
307 ::rtl::OUString funcName( "firstfunc" );
309 FuncPtr pFunc = reinterpret_cast<FuncPtr>(osl_getSymbol( (oslModule)aMod, funcName.pData ));
310 bRes = false;
311 if ( pFunc )
312 bRes = pFunc( bRes );
314 aMod.unload();
316 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
317 bRes );
318 #endif
321 CPPUNIT_TEST_SUITE( optr_oslModule );
322 CPPUNIT_TEST( optr_oslModule_001 );
323 CPPUNIT_TEST( optr_oslModule_002 );
324 CPPUNIT_TEST_SUITE_END( );
325 }; // class optr_oslModule
327 /** testing the methods:
328 oslGenericFunction SAL_CALL getFunctionSymbol( const ::rtl::OUString& ustrFunctionSymbolName )
330 class getFunctionSymbol : public CppUnit::TestFixture
332 public:
333 bool bRes, bRes1;
335 void getFunctionSymbol_001( )
337 #if !defined( MACOSX )
338 // TODO: Find out why this fails on Mac OS X
339 ::osl::Module aMod( getDllURL( ) );
340 oslGenericFunction oslFunc = aMod.getFunctionSymbol( rtl::OUString("firstfunc") );
341 ::rtl::OUString aLibraryURL;
342 bRes = ::osl::Module::getUrlFromAddress( oslFunc, aLibraryURL);
343 aMod.unload();
344 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
345 bRes && aLibraryURL.equalsIgnoreAsciiCase( getDllURL() ) );
346 #endif
349 CPPUNIT_TEST_SUITE( getFunctionSymbol );
350 CPPUNIT_TEST( getFunctionSymbol_001 );
351 CPPUNIT_TEST_SUITE_END( );
352 }; // class getFunctionSymbol
354 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::ctors);
355 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getUrlFromAddress);
356 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::load);
357 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::unload);
358 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::is);
359 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getSymbol);
360 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::optr_oslModule);
361 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getFunctionSymbol);
363 } // namespace osl_Module
365 // this macro creates an empty function, which will called by the RegisterAllFunctions()
366 // to let the user the possibility to also register some functions by hand.
367 CPPUNIT_PLUGIN_IMPLEMENT();
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */