fix toolbar import
[ooovba.git] / testshl2 / source / testshl.cxx
blobb15b4ab5e1e91c3822821d8e7041480390dbcbe1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testshl.cxx,v $
10 * $Revision: 1.25 $
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"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
38 #ifdef SOLARIS
39 #include <sys/time.h>
40 #endif
42 #ifdef WNT
43 # include "testshl/winstuff.hxx"
44 #endif
46 #include <iostream>
47 #include <vector>
48 #include <memory> /* auto_ptr */
49 #include "sal/main.h"
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>
63 using namespace std;
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);
71 CmdLineBits st_nBits;
73 void initTPrint(GetOpt & _aOptions)
75 st_nBits = FileHelper::createFlags(_aOptions);
78 void my_sleep(int nCount)
80 #ifdef WNT
81 WinSleep(nCount * 2);
82 #else
83 usleep(nCount * 2000);
84 #endif
87 // -----------------------------------------------------------------------------
88 class ProcessHandler
90 std::string m_sProcessIDFilename;
91 void write(int);
92 int getPID();
93 public:
94 ProcessHandler();
95 void setName(std::string const& _sFilename);
96 ~ProcessHandler();
99 // ------------------------------- ProcessHelper -------------------------------
100 ProcessHandler::ProcessHandler() {}
102 void ProcessHandler::setName(std::string const& _sPIDFilename)
104 m_sProcessIDFilename = _sPIDFilename;
105 write(getPID());
108 int ProcessHandler::getPID()
110 int nPID = 0;
111 #ifdef WNT
112 nPID = WinGetCurrentProcessId();
113 #else
114 nPID = getpid();
115 #endif
117 return nPID;
120 void ProcessHandler::write(int _nPID)
122 // get own PID
124 if (m_sProcessIDFilename.size() > 0)
126 FILE* out;
127 out = fopen(m_sProcessIDFilename.c_str(), "w");
128 if (!out)
130 fprintf(stderr, "warning: (testshl.cxx) can't write own pid.\n");
131 return;
132 // exit(0);
134 fprintf(out, "%d", _nPID);
135 fclose(out);
136 my_sleep(2); // short wait, so testshl2_timeout can find this pid.
138 else
140 fprintf(stderr, "warning: (testshl.cxx) PID Filename empty, must set.\n");
144 ProcessHandler::~ProcessHandler()
146 if (m_sProcessIDFilename.size() > 0)
148 write(0);
149 my_sleep(2);
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));
165 else
167 pResult.reset(new CppUnit::testshlTestResult(_aOptions));
169 return pResult;
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());
187 exit(1);
189 pOutputter.reset(new Outputter(pLog));
191 else
193 pOutputter.reset(new Outputter(std::cout));
196 return pOutputter;
199 int starttest(GetOpt & opt, AutomaticRegisterHelper const& aHelper);
201 void makeslow()
203 sal_Int32 n = 0;
204 for (sal_Int32 i=0;i<1024 * 1024;i++)
206 ++n;
209 // void endless()
210 // {
211 // sal_Int64 n = 0;
212 // while(1)
213 // {
214 // n++;
215 // makeslow();
216 // my_sleep(1);
217 // }
218 // }
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",
253 "-help:s, see -h",
254 NULL
257 ProcessHandler aCurrentProcess;
259 GetOpt opt( argv, optionSet );
260 initTPrint(opt);
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
274 // endless();
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.
285 sal_Int64 n = 0;
286 while(1)
288 n++;
289 makeslow();
290 my_sleep(1);
294 int nExitCode = 0;
295 bool bLibrary = true;
297 // someone indicates that he needs help
298 if ( opt.hasOpt( "-h" ) || opt.hasOpt( "-help" ) )
300 opt.showUsage();
301 nExitCode = 0;
303 else
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");
312 opt.showUsage();
313 nExitCode = 2;
315 else
317 bLibrary = false;
320 else
322 setSignalFilename(opt);
324 rtl::OUString suLibraryName;
325 if (bLibrary)
327 suLibraryName = rtl::OStringToOUString(opt.getFirstParam(), osl_getThreadTextEncoding() /* RTL_TEXTENCODING_ASCII_US */ );
330 AutomaticRegisterHelper aHelper(suLibraryName, opt /*, &aJobs*/);
332 if (aHelper.isOkToStartTests())
334 // start the tests
335 nExitCode = starttest(opt, aHelper);
337 else
340 fprintf(stderr, "error: The library '%s' can't initialised, must quit.\n", opt.getFirstParam().getStr());
341 nExitCode = 1;
346 if (opt.hasOpt("-noerroronexit"))
348 nExitCode = 0;
350 return nExitCode;
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);
367 // give the output
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.");
374 fflush(stderr);
375 getchar();
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");
393 abort();
396 int nExit = pResult->getExitValue();
397 return nExit;