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: rtl_Bootstrap.cxx,v $
10 * $Revision: 1.10.20.2 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 // Documentation about bootstraping can be found at:
35 // http://udk.openoffice.org/common/man/concept/micro_deployment.html
40 #include <algorithm> // STL
42 #include "cppunit/stringhelper.hxx"
44 #include <cppunit/simpleheader.hxx>
45 //#include "stringhelper.hxx"
46 //#include "valueequal.hxx"
47 #include <rtl/bootstrap.hxx>
49 #include <rtl/ustrbuf.hxx>
50 #include <rtl/ustring.h>
51 #include <rtl/ustring.hxx>
52 #include <osl/file.hxx>
53 #include <osl/module.hxx>
54 #include <osl/process.h> /* osl_getExecutableFile() */
56 #include <osl/thread.hxx>
58 // using namespace osl;
61 #define TESTSHL2_INI "testshl2"
62 #define PSEUDO_INI "pseudo"
64 /** print a UNI_CODE String. And also print some comments of the string.
66 inline void printUString( const ::rtl::OUString
& str
, const sal_Char
* msg
= "" )
71 t_print("%s: ", msg
);
74 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
75 t_print("%s\n", (char *)aString
.getStr( ) );
80 bool t_fileExist(rtl::OUString
const& _sFilename
)
82 ::osl::FileBase::RC nError1
;
83 ::osl::File
aTestFile( _sFilename
);
84 nError1
= aTestFile
.open ( OpenFlag_Read
);
85 if ( ( ::osl::FileBase::E_NOENT
!= nError1
) && ( ::osl::FileBase::E_ACCES
!= nError1
) )
93 /** get the exectutable path ( here is bootstrap_process), on Linux, such as "sal/unxlngi4.pro/bin/"
95 inline ::rtl::OUString
getModulePath( void )
97 ::rtl::OUString suDirPath
;
98 ::osl::Module::getUrlFromAddress( ( oslGenericFunction
) &getModulePath
, suDirPath
);
100 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') );
101 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') + 1);
102 suDirPath
+= rtl::OUString::createFromAscii("bin");
106 #define TESTSHL2_INI "testshl2"
107 #define PSEUDO_INI "pseudo"
110 static rtl::OUString
getExecutableDirectory()
112 rtl::OUString fileName
;
113 osl_getExecutableFile(&fileName
.pData
);
115 sal_Int32 nDirEnd
= fileName
.lastIndexOf('/');
117 OSL_ENSURE(nDirEnd
>= 0, "Cannot locate executable directory");
119 rtl::OUString aDirURL
= fileName
.copy(0, nDirEnd
);
124 // get the URL of testshl2rc/rtlrc/pseudorc
125 inline rtl::OUString
t_getSourcePath(rtl::OString
const& _sFilename
)
128 rtl::OUString
aDirURL(getExecutableDirectory());
129 aDirURL
+= OUString::createFromAscii( "/");
130 aDirURL
+= OUString::createFromAscii( _sFilename
.getStr() );
131 #if defined(WNT) || defined(OS2)
132 aDirURL
+= rtl::OUString::createFromAscii(".ini");
134 aDirURL
+= rtl::OUString::createFromAscii("rc");
138 // LLA: does not right work on my personal laptop, SRC_ROOT does not show where the source is :-(.
140 sal_Char * pStr = getenv("SRC_ROOT");
141 rtl::OUString suPath;
144 suPath = rtl::OUString::createFromAscii(pStr) + rtl::OUString::createFromAscii( "/sal/qa/rtl/bootstrap/" )
145 + rtl::OUString::createFromAscii( filename );
149 suPath = rtl::OUString::createFromAscii(pStr) + rtl::OUString::createFromAscii( "/sal/qa/rtl/bootstrap" );
152 ::osl::FileBase::getFileURLFromSystemPath( suPath, suURL );
157 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec
)
160 Sleep(_nTenthSec
* 100 );
162 #if ( defined UNX ) || ( defined OS2 ) //Unix
164 nTV
.Seconds
= static_cast<sal_uInt32
>( _nTenthSec
/10 );
165 nTV
.Nanosec
= ( (_nTenthSec
%10 ) * 100000000 );
166 osl_waitThread(&nTV
);
170 // -----------------------------------------------------------------------------
172 namespace rtl_Bootstrap
174 class ctor
: public CppUnit::TestFixture
177 // initialise your test code values here.
186 // ctor with ini name
189 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
190 printUString( suIniname
);
191 Bootstrap
aBootstrap( suIniname
);
192 rtl::OUString suGetname
; // = rtl::OUString::createFromAscii("");
193 aBootstrap
.getIniName( suGetname
);
194 printUString( suGetname
);
196 // LLA: first: this seems to be a wrong test.
197 // second: there seems to be a design hole, if I give a absolute path ini file,
198 // but try to use ${file::KEYVALUE} than 'file' will only used out of the 'executable path'/file
199 // not from the path given from the absolute path.
201 // Due to the fact, we create at this position a file (createTestshl2rc() ), we check for existance
202 bool bFileExist
= t_fileExist( suGetname
);
203 CPPUNIT_ASSERT_MESSAGE("ctor error with initial file.", bFileExist
== true );
208 rtl::Bootstrap aBootstrap
;
209 rtl::OUString suGetname
;
210 aBootstrap
.getIniName( suGetname
);
211 printUString( suGetname
);
212 CPPUNIT_ASSERT_MESSAGE("ctor error without initial file.", suGetname
.getLength() != 0 );
215 CPPUNIT_TEST_SUITE(ctor
);
216 CPPUNIT_TEST(ctor_001
);
217 CPPUNIT_TEST(ctor_002
);
218 CPPUNIT_TEST_SUITE_END();
221 class getFrom
: public CppUnit::TestFixture
224 // initialise your test code values here.
232 // get the value of env variable
235 Bootstrap aBootstrap
;
236 rtl::OUString suValue
;
237 rtl::OUString suValuename
= rtl::OUString::createFromAscii( "SOLAR_JAVA" );
238 //aBootstrap.getFrom( suValuename, suValue );
239 aBootstrap
.getFrom( suValuename
, suValue
);
240 sal_Char
* pStr
= getenv("SOLAR_JAVA");
241 // printUString( suGetname );
242 CPPUNIT_ASSERT_MESSAGE("get the value of environment variable.", suValue
.compareToAscii( pStr
) == 0 );
247 Bootstrap aBootstrap;
248 rtl::OUString suValue;
249 rtl::OUString suValuename = rtl::OUString::createFromAscii( "SRC_ROOT" );
250 //aBootstrap.getFrom( suValuename, suValue );
251 aBootstrap.getFrom( suValuename, suValue );
252 sal_Char * pStr = getenv("SRC_ROOT");
253 // printUString( suGetname );
254 CPPUNIT_ASSERT_MESSAGE("get the value of environment variable.", suValue.compareToAscii( pStr ) == 0 );
256 The result on Windows:
257 # # the SRC_ROOT is e:\Qadev\cvs\m19
258 # # suValue is e:Qadevcvsm19
260 The problem is that the internally getenv()ed variable SRC_ROOT is macro expanded,
261 thus every \ will introduce an escape.
264 // get the value of a variable in ini file
267 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
268 Bootstrap
aBootstrap( suIniname
);
269 rtl::OUString suGetname
;
270 rtl::OUString suValuename
= rtl::OUString::createFromAscii( "INHERITED_VALUE" );
271 aBootstrap
.getFrom( suValuename
, suGetname
);
272 printUString( suGetname
);
273 CPPUNIT_ASSERT_MESSAGE("get the value of a variable in ini file.", suGetname
.getLength() != 0 );
279 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
280 Bootstrap
aBootstrap( suIniname
);
281 rtl::OUString suGetname
;
282 rtl::OUString suValuename
= rtl::OUString::createFromAscii( "MY_VALUE" );
283 rtl::OUString myDefault
= rtl::OUString::createFromAscii( "2" );
284 aBootstrap
.getFrom( suValuename
, suGetname
, myDefault
);
285 //printUString( suGetname );
286 CPPUNIT_ASSERT_MESSAGE("getFrom use default.", suGetname
.compareTo( myDefault
) == 0 );
292 // initialise Bootstrap with an own ini file
293 // PSEUDO_INI is pseudo(rc|.ini) created be create_pseudorc()
294 rtl::OUString suIniname
= t_getSourcePath(PSEUDO_INI
);
295 Bootstrap
aBootstrap( suIniname
);
297 rtl::OUString suGetIniName
;
298 aBootstrap
.getIniName( suGetIniName
);
300 printUString(suGetIniName
, "Current bootstrap file");
301 sal_Int32 nIndex
= suGetIniName
.indexOf(rtl::OUString::createFromAscii( "pseudo" ));
302 CPPUNIT_ASSERT_MESSAGE("ini name must have 'pseudo' in name.", nIndex
> 0);
304 // rtlBootstrapHandle bsHandle = aBootstrap.getHandle();
305 // CPPUNIT_ASSERT_MESSAGE("getHandle return NULL!", bsHandle != 0);
307 rtl::OUString suValue
;
308 rtl::OUString suKeyName
= rtl::OUString::createFromAscii( "FILE" );
309 aBootstrap
.getFrom( suKeyName
, suValue
);
310 printUString( suValue
);
311 sal_Int32 nCompare
= suValue
.compareTo( rtl::OUString::createFromAscii("pseudo file") );
313 CPPUNIT_ASSERT_MESSAGE("<Bootstrap('pseudo')>.getFrom('FILE', ...) result is unexpected.", nCompare
== 0);
317 // get the same key out of the default context
318 rtl::OUString suKeyName
= rtl::OUString::createFromAscii( "FILE" );
319 rtl::OUString suGetValue
;
320 Bootstrap::get( suKeyName
, suGetValue
);
321 printUString( suGetValue
);
323 CPPUNIT_ASSERT_MESSAGE("Bootstrap::get('FILE', ...)", suGetValue
.compareTo( rtl::OUString::createFromAscii("testshl2 file") ) == 0 );
326 /** helper function: return the child process's ret value( typedef sal_uInt32 oslProcessExitCode;)
327 * param1 is the process's name(only file name, not include path)
329 oslProcessExitCode
ini_execProcess( const sal_Char
* process_name
, const sal_Char
* flag
)
331 rtl::OUString suCWD
= getModulePath();
332 oslProcess hProcess
= NULL
;
333 rtl::OUString suFileURL
= suCWD
;
334 suFileURL
+= rtl::OUString::createFromAscii("/") + rtl::OUString::createFromAscii(process_name
) ;
335 #if defined(WNT) || defined(OS2)
336 suFileURL
+= rtl::OUString::createFromAscii(".exe");
338 const int nParameterCount
= 3;
339 rtl_uString
* pParameters
[ nParameterCount
];
340 OUString
suFlag( OUString::createFromAscii(flag
) );
341 OUString
suEnv1( OUString::createFromAscii("-env:UNO_SERVICES=service.rdb"));
342 OUString suIniname
= t_getSourcePath("rtl");
343 printUString( suIniname
, "rtl path:");
344 //OUString suEnv2( OUString::createFromAscii("-env:MYENV=bootstrap_process"));
346 pParameters
[0] = suFlag
.pData
;
347 pParameters
[1] = suEnv1
.pData
;
348 // the custom ini/rc file's URL
349 pParameters
[2] = suIniname
.pData
;
351 oslProcessError osl_error
= osl_executeProcess(
362 CPPUNIT_ASSERT_MESSAGE
364 "osl_createProcess failed",
365 osl_error
== osl_Process_E_None
367 osl_joinProcess(hProcess
);
368 oslProcessInfo
* pInfo
= new oslProcessInfo
;
369 pInfo
->Size
= sizeof( oslProcessInfo
);
370 osl_error
= osl_getProcessInfo( hProcess
, osl_Process_EXITCODE
, pInfo
);
371 CPPUNIT_ASSERT_MESSAGE
373 "osl_getProcessInfo returned with failure",
374 osl_Process_E_None
== osl_error
377 t_print("the exit code is %d.\n", pInfo
->Code
);
378 oslProcessExitCode nCode
= pInfo
->Code
;
385 oslProcessExitCode nExitCode
= ini_execProcess( "bootstrap_process", "1" );
386 CPPUNIT_ASSERT_MESSAGE("Parameters passed by command line can not be gotten!",
391 oslProcessExitCode nExitCode
= ini_execProcess( "bootstrap_process", "2" );
392 CPPUNIT_ASSERT_MESSAGE("Parameters passed by .ini/rc file can not be gotten!",
397 #if (defined WNT) || (defined SOLARIS)
398 putenv(const_cast< char * >("QADEV_BOOTSTRAP=sun&ms"));
400 setenv("QADEV_BOOTSTRAP", "sun&ms", 0);
402 oslProcessExitCode nExitCode
= ini_execProcess( "bootstrap_process", "3" );
403 CPPUNIT_ASSERT_MESSAGE("Parameters passed by environment variables can not be gotten!",
408 oslProcessExitCode nExitCode
= ini_execProcess( "bootstrap_process", "4" );
409 CPPUNIT_ASSERT_MESSAGE("Parameters passed by customed .ini/rc file can not be gotten!",
414 oslProcessExitCode nExitCode
= ini_execProcess( "bootstrap_process", "5" );
415 CPPUNIT_ASSERT_MESSAGE("Parameters passed by inheritance can not be gotten!",
420 oslProcessExitCode nExitCode
= ini_execProcess( "bootstrap_process", "6" );
421 CPPUNIT_ASSERT_MESSAGE("Parameters passed by default can not be gotten!",
425 CPPUNIT_TEST_SUITE(getFrom
);
426 CPPUNIT_TEST(getFrom_001
);
427 CPPUNIT_TEST(getFrom_002
);
428 CPPUNIT_TEST(getFrom_003
);
429 CPPUNIT_TEST(getFrom_004
);
430 CPPUNIT_TEST(getFrom_004_1
);
431 CPPUNIT_TEST(getFrom_005_1
);
432 CPPUNIT_TEST(getFrom_005_2
);
433 CPPUNIT_TEST(getFrom_005_3
);
434 CPPUNIT_TEST(getFrom_005_4
);
435 CPPUNIT_TEST(getFrom_005_5
);
436 CPPUNIT_TEST(getFrom_005_6
);
437 CPPUNIT_TEST_SUITE_END();
440 class setIniFilename
: public CppUnit::TestFixture
443 // initialise your test code values here.
452 void setIniFilename_001()
454 Bootstrap aBootstrap
;
456 rtl::OUString suGetIniname
;
457 aBootstrap
.getIniName( suGetIniname
);
458 //which should be .....testshl2rc
459 //printUString( suGetIniname );
461 rtl::OUString suIniname
= t_getSourcePath(PSEUDO_INI
);
462 Bootstrap::setIniFilename( suIniname
);
464 rtl::OUString suGetname
;
465 aBootstrap
.getIniName( suGetname
);
467 printUString( suGetname
);
468 CPPUNIT_ASSERT_MESSAGE("setIniFilename then get it.", suGetname
.compareTo( suIniname
) == 0
469 && suGetname
.compareTo( suGetIniname
) != 0 );
472 void setIniFilename_002()
474 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
475 // CPPUNIT_ASSERT_MESSAGE("test failed, Bootstrap ini does not exist.", t_fileExist(suIniname ) == true);
477 Bootstrap::setIniFilename( suIniname
);
478 //rtl_bootstrap_args_open( suIniname.pData );
479 rtl::OUString suGetname
;
480 rtl::OUString suValuename
= rtl::OUString::createFromAscii( "INHERITED_VALUE" );
481 //aBootstrap.getFrom( suValuename, suGetname );
482 Bootstrap::get( suValuename
, suGetname
);
483 printUString( suGetname
);
484 CPPUNIT_ASSERT_MESSAGE("setIniFilename and get value of the argument.", suGetname
.getLength() != 0 );
487 CPPUNIT_TEST_SUITE(setIniFilename
);
488 CPPUNIT_TEST(setIniFilename_001
);
489 CPPUNIT_TEST(setIniFilename_002
);
490 CPPUNIT_TEST_SUITE_END();
491 }; // class setIniFilename
493 class getHandle
: public CppUnit::TestFixture
496 // initialise your test code values here.
507 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
508 Bootstrap aBootstrap
;
509 rtlBootstrapHandle bsHandle
= aBootstrap
.getHandle();
510 CPPUNIT_ASSERT_MESSAGE("getHandle should return 0 if the bootstrap has no ini file!", bsHandle
== 0 );
514 rtl::OUString suIniname
= t_getSourcePath(PSEUDO_INI
);
515 Bootstrap
aBootstrap( suIniname
);
517 rtlBootstrapHandle bsHandle
= aBootstrap
.getHandle();
518 CPPUNIT_ASSERT_MESSAGE("getHandle return NULL!", bsHandle
!= 0);
520 //rtl::OUString iniName;
521 //rtl_bootstrap_get_iniName_from_handle( bsHandle, &iniName.pData );
523 rtl::OUString suValue
;
524 rtl::OUString suKeyName
= rtl::OUString::createFromAscii( "PSEUDOFILE" );
525 rtl_bootstrap_get_from_handle(bsHandle
, suKeyName
.pData
, &suValue
.pData
, NULL
);
526 printUString( suValue
);
528 CPPUNIT_ASSERT_MESSAGE("Can not use the handle which is returned by getHandle!", suValue
.equals( rtl::OUString::createFromAscii("be pseudo") ) == sal_True
);
530 // CPPUNIT_ASSERT_MESSAGE("Can not use the handle which is returned by getHandle!",
531 // suGetname.equalsIgnoreAsciiCase( iniName ) == sal_True );
534 CPPUNIT_TEST_SUITE(getHandle
);
535 CPPUNIT_TEST(getHandle_001
);
536 CPPUNIT_TEST(getHandle_002
);
537 CPPUNIT_TEST_SUITE_END();
538 }; // class getHandle
540 class set
: public CppUnit::TestFixture
543 // initialise your test code values here.
554 //in ini fle, INHERITED_VALUE=inherited_value
555 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
556 Bootstrap
aBootstrap( suIniname
);
557 rtl::OUString suName
= rtl::OUString::createFromAscii( "INHERITED_VALUE" );
558 rtl::OUString suValue
= rtl::OUString::createFromAscii( "ok" );
559 // set to another value
560 Bootstrap::set( suName
, suValue
);
561 rtl::OUString suGetValue
;
562 Bootstrap::get( suName
, suGetValue
);
563 CPPUNIT_ASSERT_MESSAGE("set and get argument failed.", suGetValue
.compareTo(suValue
) == 0 );
567 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
568 Bootstrap
myBootstrap( suIniname
);
569 rtl::OUString suName
= rtl::OUString::createFromAscii( "INHERITED_VALUE" );
570 rtl::OUString suGetOrientValue
;
571 Bootstrap::get( suName
, suGetOrientValue
);
572 // ?? INHERITED_VALUE = ok now, which is set in set_001
573 printUString( suGetOrientValue
);
575 rtl::OUString suValue
= rtl::OUString::createFromAscii( TESTSHL2_INI
);
576 // set to another value
577 Bootstrap::set( suName
, suValue
);
578 rtl::OUString suGetValue
;
579 Bootstrap::get( suName
, suGetValue
);
580 CPPUNIT_ASSERT_MESSAGE("set and get argument failed.", suGetValue
.compareTo(suValue
) == 0 );
583 CPPUNIT_TEST_SUITE(set
);
584 CPPUNIT_TEST(set_001
);
585 CPPUNIT_TEST(set_002
);
586 CPPUNIT_TEST_SUITE_END();
589 class expandMacrosFrom
: public CppUnit::TestFixture
599 void expandMacrosFrom_001()
601 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
602 Bootstrap
aBootstrap( suIniname
);
603 rtl::OUString suMacro
= rtl::OUString::createFromAscii( "$MYVAR/expand1" );
604 //printUString( suMacro );
606 aBootstrap
.expandMacrosFrom( suMacro
);
607 rtl::OUString suExpectedMacro
= rtl::OUString::createFromAscii( "src680_test/expand1" );
608 //printUString( suMacro );
609 CPPUNIT_ASSERT_MESSAGE("expandMacrosFrom failed.", suMacro
.compareTo(suExpectedMacro
) == 0 );
612 /** here a special macro should expand
613 * if rtlrc is under sal/qa/rtl/bootstrap/, "${rtlrc:Bootstrap:RTLVALUE}" could be expanded
614 * else rtlrc is under solver/680/unxlngi4.pro/bin/, "${file:/// ....solver/680/unxlngi4.pro/bin/rtlrc:Bootstrap:RTLVALUE}"
615 * could not be expanded
617 void expandMacrosFrom_002()
619 // Build a string with '${rtl.ini:RTLVALUE}' and try to expand it.
620 // In function 'create_rtlrc() is the content of the rtl.ini file.
622 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
623 t_print("inifile is:");
624 printUString( suIniname
);
625 Bootstrap
aBootstrap( suIniname
) ;
626 rtl::OUString suMacro
= rtl::OUString::createFromAscii( "${" ); //rtlrc:Bootstrap:RTLVALUE}");
628 rtl::OUString aDirURL
= OUString::createFromAscii( "$ORIGIN");
629 aDirURL
+= OUString::createFromAscii( "/");
630 aDirURL
+= OUString::createFromAscii( "rtl" );
631 #if defined(WNT) || defined(OS2)
632 aDirURL
+= rtl::OUString::createFromAscii(".ini");
634 aDirURL
+= rtl::OUString::createFromAscii("rc");
637 suMacro
+= aDirURL
;//t_getSourcePath("rtl");
638 suMacro
+= rtl::OUString::createFromAscii( "::RTLVALUE}");
640 t_print("created macro is: ");
641 printUString( suMacro
);
643 aBootstrap
.expandMacrosFrom( suMacro
);
644 t_print("expanded macro is:");
645 printUString( suMacro
);
646 rtl::OUString suExpectedMacro
= rtl::OUString::createFromAscii( "qadev17" );
647 CPPUNIT_ASSERT_MESSAGE("failed, can't expand '${file:///.../" SAL_CONFIGFILE("rtl") "::RTLVALUE}' to 'qadev17'", suMacro
.compareTo(suExpectedMacro
) == 0 );
649 void expandMacrosFrom_002_1()
651 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
652 t_print("inifile is:");
653 printUString( suIniname
);
654 Bootstrap
aBootstrap( suIniname
);
656 rtl::OUString suMacro
;
657 // just a simple test, if this really work.
658 aBootstrap
.getFrom(rtl::OUString::createFromAscii( "SOFROMVALUE2" ), suMacro
);
659 t_print("SOFROMVALUE2:");
660 printUString( suMacro
);
661 CPPUNIT_ASSERT_MESSAGE("'SOFROMVALUE2' seems to do not exist.", suMacro
.getLength() > 0 );
663 aBootstrap
.getFrom(rtl::OUString::createFromAscii( "SOFROMVALUE" ), suMacro
);
665 t_print("SOFROMVALUE:");
666 printUString( suMacro
);
669 // seems to be, that getFrom() already expand the string
670 // t_print("expanded macro is:");
671 // aBootstrap.expandMacrosFrom( suMacro );
672 // printUString( suMacro );
673 rtl::OUString suExpectedMacro
= rtl::OUString::createFromAscii( "src680_qadev" );
674 CPPUNIT_ASSERT_MESSAGE("failed, can't expand '${" SAL_CONFIGFILE("rtl") "::SOVALUE}' to 'src680_qadev'", suMacro
.compareTo(suExpectedMacro
) == 0 );
676 void expandMacrosFrom_002_2()
678 // test, to read and expand SOFROMVALUE3
679 // SOFROMVALUE3 is 'rtl(.ini|rc)::TESTSHL_SOVALUE' which should expand to 'rtlfile' if all is ok.
681 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
682 t_print("inifile is:");
683 printUString( suIniname
);
684 Bootstrap
aBootstrap( suIniname
);
686 rtl::OUString suMacro
;
687 aBootstrap
.getFrom(rtl::OUString::createFromAscii( "SOFROMVALUE3" ), suMacro
);
689 t_print("SOFROMVALUE3:");
690 printUString( suMacro
);
692 if (suMacro
.equals(rtl::OUString::createFromAscii("testshl2_file") ) == sal_True
)
694 CPPUNIT_ASSERT_MESSAGE("Value 'SOFROMVALUE3' is read from the wrong ini file.", 0 );
698 CPPUNIT_ASSERT_MESSAGE("SOFROMVALUE3 should contain 'rtlfile'.", suMacro
.equals(rtl::OUString::createFromAscii("rtlfile") ) == sal_True
);
701 //? I don't know if this is a right test.
702 // void expandMacrosFrom_002_3()
704 // // test, to read and expand SOFROMVALUE4
705 // // SOFROMVALUE4 is 'rtl(.ini|rc):Other_Section:TESTSHL_SOVALUE' which should expand to '' if all is ok.
707 // rtl::OUString suIniname = t_getSourcePath(TESTSHL2_INI);
708 // t_print("inifile is:");
709 // printUString( suIniname );
710 // Bootstrap aBootstrap( suIniname);
712 // rtl::OUString suMacro;
713 // aBootstrap.getFrom(rtl::OUString::createFromAscii( "SOFROMVALUE4" ), suMacro );
715 // t_print("SOFROMVALUE4:");
716 // printUString( suMacro );
718 // if (suMacro.equals(rtl::OUString::createFromAscii("testshl2_file") ) == sal_True)
720 // CPPUNIT_ASSERT_MESSAGE("Value 'SOFROMVALUE4' is read from the wrong section out of the wrong ini file.", 0 );
722 // else if (suMacro.equals(rtl::OUString::createFromAscii("testshl2_file_other") ) == sal_True)
724 // CPPUNIT_ASSERT_MESSAGE("Value 'SOFROMVALUE4' is read from the wrong ini file.", 0 );
728 // CPPUNIT_ASSERT_MESSAGE("Value 'SOFROMVALUE4' should contain 'rtlfile_other'.", suMacro.equals(rtl::OUString::createFromAscii("rtlfile_other") ) == sal_True );
731 void expandMacrosFrom_003()
733 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
734 Bootstrap
aBootstrap( suIniname
);
735 rtl::OUString suMacro
[4];
736 suMacro
[0] = rtl::OUString::createFromAscii( "$SYSUSERCONFIG" );
737 suMacro
[1] = rtl::OUString::createFromAscii( "$SYSUSERHOME" );
738 suMacro
[2] = rtl::OUString::createFromAscii( "$SYSBINDIR" );
739 suMacro
[3] = rtl::OUString::createFromAscii( "$ORIGIN" );
741 for ( int i
= 0; i
< 4; i
++ )
743 aBootstrap
.expandMacrosFrom( suMacro
[i
] );
744 printUString(suMacro
[i
]);
746 // printUString( t_getSourcePath("") );
747 // CPPUNIT_ASSERT_MESSAGE("some integral variables.", suMacro[3].equalsIgnoreAsciiCase(t_getSourcePath("")) == sal_True );
748 CPPUNIT_ASSERT_MESSAGE("some integral variables.", suMacro
[0].getLength() > 0 &&
749 suMacro
[1].getLength() > 0 &&
750 suMacro
[2].getLength() > 0 &&
751 suMacro
[3].getLength() > 0);
754 void testRecursion() {
755 rtl::OUString
t(RTL_CONSTASCII_USTRINGPARAM("$RECURSIVE"));
756 Bootstrap(t_getSourcePath(TESTSHL2_INI
)).expandMacrosFrom(t
);
757 CPPUNIT_ASSERT_MESSAGE(
758 "recursion detection",
760 RTL_CONSTASCII_STRINGPARAM("***RECURSION DETECTED***")));
764 rtl::OUString
t(RTL_CONSTASCII_USTRINGPARAM("$LINKED"));
765 Bootstrap(t_getSourcePath(TESTSHL2_INI
)).expandMacrosFrom(t
);
766 CPPUNIT_ASSERT_MESSAGE(
768 t
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("qadev17")));
771 void testOverride() {
773 RTL_CONSTASCII_USTRINGPARAM(
774 "${.override:$ORIGIN/" SAL_CONFIGFILE("rtl") ":ORIGIN}"));
775 Bootstrap(t_getSourcePath("rtl")).expandMacrosFrom(t1
);
776 CPPUNIT_ASSERT_MESSAGE(
778 t1
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("direct")));
780 RTL_CONSTASCII_USTRINGPARAM(
781 "${.override:$ORIGIN/" SAL_CONFIGFILE("none") ":MYVAR}"));
782 Bootstrap::expandMacros(t2
);
783 CPPUNIT_ASSERT_MESSAGE(
785 t2
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("src680_test")));
788 void testNonexisting() {
790 RTL_CONSTASCII_USTRINGPARAM(
791 "${$ORIGIN/" SAL_CONFIGFILE("none") ":MYVAR}"));
792 Bootstrap::expandMacros(t
);
793 CPPUNIT_ASSERT_MESSAGE(
795 t
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("src680_test")));
799 rtl::OUStringBuffer b
;
800 b
.appendAscii(RTL_CONSTASCII_STRINGPARAM("${"));
801 rtl::OUString
p(t_getSourcePath(TESTSHL2_INI
));
802 for (sal_Int32 i
= 0; i
< p
.getLength(); ++i
) {
804 b
.append(static_cast< sal_Unicode
>('\\'));
808 b
.appendAscii(RTL_CONSTASCII_STRINGPARAM(":Other_Section:EXPAND}"));
809 rtl::OUString
t(b
.makeStringAndClear());
810 Bootstrap(t_getSourcePath(TESTSHL2_INI
)).expandMacrosFrom(t
);
811 CPPUNIT_ASSERT_MESSAGE(
814 RTL_CONSTASCII_STRINGPARAM("$FILE")));
815 // the correct answer would be "testshl2 file" instead, but
816 // expansion including a section currently erroneously does not
817 // recursively expand macros in the resulting replacement text
820 CPPUNIT_TEST_SUITE(expandMacrosFrom
);
821 CPPUNIT_TEST(expandMacrosFrom_001
);
822 CPPUNIT_TEST(expandMacrosFrom_002
);
823 CPPUNIT_TEST(expandMacrosFrom_002_1
);
824 CPPUNIT_TEST(expandMacrosFrom_002_2
);
825 //? CPPUNIT_TEST(expandMacrosFrom_002_3);
826 CPPUNIT_TEST(expandMacrosFrom_003
);
827 CPPUNIT_TEST(testRecursion
);
828 CPPUNIT_TEST(testLink
);
829 CPPUNIT_TEST(testOverride
);
830 CPPUNIT_TEST(testNonexisting
);
831 CPPUNIT_TEST(testSection
);
832 CPPUNIT_TEST_SUITE_END();
833 }; // class expandMacrosFrom
835 class expandMacros
: public CppUnit::TestFixture
838 // initialise your test code values here.
847 void expandMacros_001()
849 rtl::OUString suIniname
= t_getSourcePath(TESTSHL2_INI
);
850 Bootstrap
aBootstrap( suIniname
) ;
851 rtl::OUString suMacro
= rtl::OUString::createFromAscii( "$INHERITED_VALUE/well" );
852 Bootstrap::expandMacros( suMacro
);
854 rtl::OUString suName
= rtl::OUString::createFromAscii( "INHERITED_VALUE" );
856 Bootstrap::get( suName
, suGetValue
);
857 suGetValue
+= OUString::createFromAscii( "/well" );
858 CPPUNIT_ASSERT_MESSAGE("expandMacros failed.", suGetValue
.compareTo(suMacro
) == 0 );
861 CPPUNIT_TEST_SUITE(expandMacros
);
862 CPPUNIT_TEST(expandMacros_001
);
863 // CPPUNIT_TEST(expandMacros_002);
864 CPPUNIT_TEST_SUITE_END();
865 }; // class expandMacros
867 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Bootstrap::ctor
, "rtl_Bootstrap");
868 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Bootstrap::getFrom
, "rtl_Bootstrap");
869 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Bootstrap::setIniFilename
, "rtl_Bootstrap");
870 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Bootstrap::getHandle
, "rtl_Bootstrap");
871 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Bootstrap::set
, "rtl_Bootstrap");
872 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Bootstrap::expandMacrosFrom
, "rtl_Bootstrap");
873 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Bootstrap::expandMacros
, "rtl_Bootstrap");
875 } // namespace rtl_Bootstrap
877 // -----------------------------------------------------------------------------
879 // this macro creates an empty function, which will called by the RegisterAllFunctions()
880 // to let the user the possibility to also register some functions by hand.
884 // Here are some helpers, which create a new file 'rtlrc' at the executable path position
885 // and fill the file with some information.
886 // static rtl::OUString getExecutableDirectory()
888 // rtl::OUString fileName;
889 // osl_getExecutableFile(&fileName.pData);
891 // sal_Int32 nDirEnd = fileName.lastIndexOf('/');
893 // OSL_ENSURE(nDirEnd >= 0, "Cannot locate executable directory");
895 // rtl::OUString aDirURL = fileName.copy(0, nDirEnd);
899 static void removeAndCreateFile(rtl::OUString
const& _suFileURL
, rtl::OString
const& _sContent
)
901 osl::File::remove(_suFileURL
);
903 ::std::auto_ptr
<osl::File
> pFile( new osl::File( _suFileURL
) );
904 ::osl::FileBase::RC nError
= pFile
->open( OpenFlag_Write
| OpenFlag_Create
);
905 if ( ::osl::FileBase::E_None
== nError
|| ::osl::FileBase::E_EXIST
== nError
)
907 t_print(T_VERBOSE
, "%s\n" , OString(_suFileURL
, _suFileURL
.getLength(), RTL_TEXTENCODING_ASCII_US
).getStr());
908 sal_uInt64 nWritenBytes
;
909 pFile
->write(_sContent
.getStr(), _sContent
.getLength(), nWritenBytes
);
910 // t_print("nBytes: %ld\n", nBytes);
912 rtl::OString sError
= "can't write enough bytes to file";
913 sError
+= OString(_suFileURL
, _suFileURL
.getLength(), RTL_TEXTENCODING_ASCII_US
);
914 OSL_ENSURE(nWritenBytes
== _sContent
.getLength(), sError
.getStr());
920 rtl::OString sError
= "can't create file URL: '";
922 sFile
<<= _suFileURL
;
924 sError
+= "' maybe no write access. If it is true with no write access, please create a local environment and start these tests again. rtl::Bootstrap test must quit.";
925 t_print("%s\n", sError
.getStr() );
928 OSL_ASSERT(t_fileExist(_suFileURL
) == true);
931 // -----------------------------------------------------------------------------
932 static void create_rtlrc()
934 rtl::OUString
aFileURL(getExecutableDirectory());
935 #if defined(WNT) || defined(OS2)
936 aFileURL
+= rtl::OUString::createFromAscii("/rtl.ini");
938 aFileURL
+= rtl::OUString::createFromAscii("/rtlrc");
942 sLines
+= "[Bootstrap]\n";
943 sLines
+= "SOVALUE=src680_qadev\n";
944 sLines
+= "RTLVALUE=qadev17\n";
945 sLines
+= "TESTSHL_SOVALUE=rtlfile\n";
946 sLines
+= "RECURSIVE=${$ORIGIN/" SAL_CONFIGFILE("testshl2") ":RECURSIVE}\n";
947 sLines
+= "ORIGIN=direct\n";
948 sLines
+= "[Other_Section]\n";
949 sLines
+= "TESTSHL_SOVALUE=rtlfile_other\n";
951 removeAndCreateFile(aFileURL
, sLines
);
954 // -----------------------------------------------------------------------------
955 static void create_testshl2rc()
957 rtl::OUString
aFileURL(getExecutableDirectory());
958 #if defined(WNT) || defined(OS2)
959 aFileURL
+= rtl::OUString::createFromAscii("/testshl2.ini");
961 aFileURL
+= rtl::OUString::createFromAscii("/testshl2rc");
964 sLines
+= "[Bootstrap]\n";
965 sLines
+= "FILE=testshl2 file\n";
966 sLines
+= "MYBOOTSTRAPTESTVALUE=file\n";
967 sLines
+= "INHERITED_VALUE=inherited_value\n";
968 sLines
+= "INHERITED_OVERWRITTEN_VALUE=not_overwritten\n";
969 sLines
+= "MYVAR=src680_test\n";
970 sLines
+= "SOFROMVALUE=${$ORIGIN/" SAL_CONFIGFILE("rtl") "::SOVALUE}\n";
971 sLines
+= "SOFROMVALUE2=test\n";
972 sLines
+= "SOFROMVALUE3=${$ORIGIN/" SAL_CONFIGFILE("rtl") "::TESTSHL_SOVALUE}\n";
973 sLines
+= "TESTSHL_SOVALUE=testshl2_file\n";
974 //? sLines += "SOFROMVALUE4=${" SAL_CONFIGFILE("rtl") ":Other_Section:TESTSHL_SOVALUE}\n";
975 sLines
+= "ILLEGAL VALUE=test\n";
976 sLines
+= "ILLEGAL.DOT=test\n";
977 sLines
+= "ILLEGAL;SEMICOLON=test\n";
978 sLines
+= "ILLEGAL:COLON=test\n";
979 sLines
+= " KEY_FOR_TRIM_TEST = value for trim test \n";
980 sLines
+= "RECURSIVE=${$ORIGIN/" SAL_CONFIGFILE("rtl") ":RECURSIVE}\n";
981 sLines
+= "LINKED=${${.link:$ORIGIN/testshl2-link}:RTLVALUE}\n";
982 sLines
+= "[Other_Section]\n";
983 sLines
+= "FILE=testshl2 file other\n";
984 sLines
+= "EXPAND=$FILE\n";
985 //? sLines += "TESTSHL_SOVALUE=testshl2_file_other\n";
987 removeAndCreateFile(aFileURL
, sLines
);
990 (getExecutableDirectory() +
991 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/testshl2-link"))),
992 SAL_CONFIGFILE("rtl"));
995 // -----------------------------------------------------------------------------
997 static void create_pseudorc()
999 rtl::OUString
aFileURL(getExecutableDirectory());
1000 #if defined(WNT) || defined(OS2)
1001 aFileURL
+= rtl::OUString::createFromAscii("/pseudo.ini");
1003 aFileURL
+= rtl::OUString::createFromAscii("/pseudorc");
1005 rtl::OString sLines
;
1006 sLines
+= "[Bootstrap]\n";
1007 sLines
+= "FILE=pseudo file\n";
1008 sLines
+= "PSEUDOFILE=be pseudo\n";
1010 removeAndCreateFile(aFileURL
, sLines
);
1013 // -----------------------------------------------------------------------------
1014 void create_bootstrap_processrc()
1016 rtl::OUString
aDirURL(getModulePath());
1017 #if defined(WNT) || defined(OS2)
1018 aDirURL
+= rtl::OUString::createFromAscii("/bootstrap_process.ini");
1020 aDirURL
+= rtl::OUString::createFromAscii("/bootstrap_processrc");
1022 rtl::OString sLines
;
1023 sLines
+= "[Bootstrap]\n";
1024 sLines
+= "EXECUTABLE_RC=true\n";
1025 sLines
+= "IF_CUSTOM_RC=false\n";
1027 removeAndCreateFile(aDirURL
, sLines
);
1029 // -----------------------------------------------------------------------------
1031 void RegisterAdditionalFunctions(FktRegFuncPtr _pFunc
)
1035 t_print(T_VERBOSE
, "Initializing ...\n" );
1037 create_testshl2rc();
1039 create_bootstrap_processrc();
1041 t_print(T_VERBOSE
, "Initialization Done.\n" );