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: testshl.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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_testshl2.hxx"
43 # include "testshl/winstuff.hxx"
48 #include <memory> /* auto_ptr */
50 #include <rtl/string.hxx>
52 // #include <rtl/tres.h>
53 #include "testshl/autoregisterhelper.hxx"
54 // #include "cmdlinebits.hxx"
55 #include "testshl/log.hxx"
56 #include "testshl/filehelper.hxx"
57 #include "testshl/getopt.hxx"
58 #include "cppunit/result/outputter.hxx"
59 #include "cppunit/joblist.hxx"
61 // #include <osl/time.h>
65 #include "cppunit/result/testshlTestResult.h"
66 #include "cppunit/result/emacsTestResult.hxx"
68 // Prototype for signal handling
69 void setSignalFilename(GetOpt
& opt
);
70 void removeSignalFile(GetOpt
& opt
);
73 void initTPrint(GetOpt
& _aOptions
)
75 st_nBits
= FileHelper::createFlags(_aOptions
);
78 void my_sleep(int nCount
)
83 usleep(nCount
* 2000);
87 // -----------------------------------------------------------------------------
90 std::string m_sProcessIDFilename
;
95 void setName(std::string
const& _sFilename
);
99 // ------------------------------- ProcessHelper -------------------------------
100 ProcessHandler::ProcessHandler() {}
102 void ProcessHandler::setName(std::string
const& _sPIDFilename
)
104 m_sProcessIDFilename
= _sPIDFilename
;
108 int ProcessHandler::getPID()
112 nPID
= WinGetCurrentProcessId();
120 void ProcessHandler::write(int _nPID
)
124 if (m_sProcessIDFilename
.size() > 0)
127 out
= fopen(m_sProcessIDFilename
.c_str(), "w");
130 fprintf(stderr
, "warning: (testshl.cxx) can't write own pid.\n");
134 fprintf(out
, "%d", _nPID
);
136 my_sleep(2); // short wait, so testshl2_timeout can find this pid.
140 fprintf(stderr
, "warning: (testshl.cxx) PID Filename empty, must set.\n");
144 ProcessHandler::~ProcessHandler()
146 if (m_sProcessIDFilename
.size() > 0)
150 fprintf(stderr
, "hint: it is save to remove PID file.\n");
154 // -----------------------------------------------------------------------------
155 std::auto_ptr
<CppUnit::TestResult
> initResult(GetOpt
& _aOptions
)
157 std::auto_ptr
<CppUnit::TestResult
> pResult
;
158 if (_aOptions
.hasOpt("-mode"))
160 if (_aOptions
.getOpt("-mode").equals("emacs") == sal_True
)
162 pResult
.reset(new CppUnit::emacsTestResult(_aOptions
));
167 pResult
.reset(new CppUnit::testshlTestResult(_aOptions
));
172 // -----------------------------------------------------------------------------
174 std::auto_ptr
<Outputter
> initOutputter(GetOpt
& _aOptions
)
176 std::auto_ptr
<Outputter
> pOutputter
;
178 if (_aOptions
.hasOpt("-log"))
180 rtl::OString sLogFile
= _aOptions
.getOpt("-log");
182 Log
* pLog
= new Log( FileHelper::convertPath( sLogFile
) );
183 if ( pLog
->open() != osl::FileBase::E_None
)
185 // cerr << "could not open LogFile: " << pLog->getName().getStr() << endl;
186 fprintf(stderr
, "could not open LogFile: %s\n", pLog
->getName().getStr());
189 pOutputter
.reset(new Outputter(pLog
));
193 pOutputter
.reset(new Outputter(std::cout
));
199 int starttest(GetOpt
& opt
, AutomaticRegisterHelper
const& aHelper
);
204 for (sal_Int32 i
=0;i
<1024 * 1024;i
++)
220 // ----------------------------------- Main -----------------------------------
222 SAL_IMPLEMENT_MAIN_WITH_ARGS(, argv
)
224 static char const * optionSet
[] = {
225 "-boom, stop near error position, exception only",
226 "-mode=s, the output mode, emacs, xml, old. Default is -mode old",
227 "-log=s, destination file for logging",
228 "-noerrors, shows all tests, but not it's errors.",
229 "-onlyerrors, shows only the failed test functions",
230 /* "-tc=s@, name(s) of testcase(s) to generate", */
231 "-sf=s, absolute path and name to the signal file.",
232 //! new (helpful if at debug time)
233 "-dntsf, if given, do not touch signal file, also if one exist.",
234 "-dnrmsf, do not remove signalfile. ",
235 "-jobonly=s, job control file, these jobs should only executed.",
236 "-jobexclude=s, job control file, but contains only job, they not execute.",
237 "-jobfilter=s, use a filter for just some tests.",
238 "-onlyshowjobs, show only all jobs, do no tests.",
239 "-forward=s, this string will forwarded to the test libraries.",
240 "-projectid=s, this text is added to the date output line.",
241 "-buildid=s, this text is added to the date output line.",
242 "-waitforkey, wait until key pressed.",
243 "-verbose, be verbose.",
244 "-quiet, be quiet. No t_print(...) will create output.",
245 "-pid=s, write current process id to file",
246 "-endless, testshl runs endless, for test only!!!",
247 "-whereami, shows at runtime, which function is tested next.",
248 "-noerroronexit, if this is given the program exits with return code 0 even if runtime errors occurs.",
249 "-hardexit, Exit the tests without call any dtor.",
250 "-absolutepath, Use the given absolute path to local the test library.",
251 "-localpath, Make a chdir() to the test library path, then try to load the library without given path.",
252 "-h:s, display help or help on option",
257 ProcessHandler aCurrentProcess
;
259 GetOpt
opt( argv
, optionSet
);
262 if ( opt
.hasOpt("-pid") )
264 aCurrentProcess
.setName(opt
.getOpt("-pid").getStr());
267 if ( opt
.hasOpt("-verbose") )
269 fprintf(stderr
, "testshl2 $Revision: 1.25 $\n");
272 if ( opt
.hasOpt("-endless")) // this exists only for self test issues
276 // With parameter -endless a test is attachable by debuggers
277 // Attach to testshl2.exe set a breakpoint in this while (1) loop
279 // Then you have to set the process pointer (Set next Statement) to
280 // the next line outside the while loop.
281 // Now it is possible to debug the whole code.
282 // Never source this code to a function, because you will
283 // lost the variables like opt.
295 bool bLibrary
= true;
297 // someone indicates that he needs help
298 if ( opt
.hasOpt( "-h" ) || opt
.hasOpt( "-help" ) )
305 if (opt
.getParams().empty())
307 // no library is given, but if a jobonly list is given, we should generate UNKNOWN errors.
308 if (! opt
.hasOpt("-jobonly"))
310 // std::cerr << "error: At least a library or a job file should given." << std::endl;
311 fprintf(stderr
, "error: At least a library or a job file should given.\n");
322 setSignalFilename(opt
);
324 rtl::OUString suLibraryName
;
327 suLibraryName
= rtl::OStringToOUString(opt
.getFirstParam(), osl_getThreadTextEncoding() /* RTL_TEXTENCODING_ASCII_US */ );
330 AutomaticRegisterHelper
aHelper(suLibraryName
, opt
/*, &aJobs*/);
332 if (aHelper
.isOkToStartTests())
335 nExitCode
= starttest(opt
, aHelper
);
340 fprintf(stderr
, "error: The library '%s' can't initialised, must quit.\n", opt
.getFirstParam().getStr());
346 if (opt
.hasOpt("-noerroronexit"))
353 // -----------------------------------------------------------------------------
354 // this function is only inserted to give a better startpoint for breakpoints
356 int starttest(GetOpt
& _opt
, AutomaticRegisterHelper
const& aHelper
)
358 // create a TestResult
359 std::auto_ptr
<CppUnit::TestResult
> pResult
= initResult(_opt
);
361 // call all test functions
362 aHelper
.CallAll(pResult
.get());
364 // create and open log
365 std::auto_ptr
<Outputter
> pOutput
= initOutputter(_opt
);
368 pResult
->print(*pOutput
.get());
370 // this is a debug extension, so you can read the output and after a key is pressed the program will end.
371 if (_opt
.hasOpt("-waitforkey"))
373 fprintf(stderr
, "Press return key.");
378 if (_opt
.hasOpt("-verbose"))
380 fprintf(stderr
, "Remove signal handlers");
383 removeSignalFile(_opt
);
385 if (_opt
.hasOpt("-verbose"))
387 fprintf(stderr
, " [done]\n");
390 if (_opt
.hasOpt("-hardexit"))
392 fprintf(stderr
, "Hardexit forced.\n");
396 int nExit
= pResult
->getExitValue();