searchengine/util/tsrc/itk/src/itkobservers.cpp
changeset 0 671dee74050a
child 8 6547bf8ca13a
equal deleted inserted replaced
-1:000000000000 0:671dee74050a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 #include <fcntl.h>
       
    18 #include <stdio.h>
       
    19 #include <sys/stat.h>
       
    20 #include <unistd.h>
       
    21 
       
    22 
       
    23 #include <exception>
       
    24 #include <fstream>
       
    25 #include <iostream>
       
    26 #include <memory>
       
    27 
       
    28 #include "cpixfstools.h"
       
    29 
       
    30 #include "itktesters.h"
       
    31 #include "itkobservers.h"
       
    32 
       
    33 namespace Itk
       
    34 {
       
    35     /**
       
    36      * ITestRunObserver
       
    37      */
       
    38     ITestRunObserver::~ITestRunObserver()
       
    39     {
       
    40         ;
       
    41     }
       
    42 
       
    43 
       
    44     /*****************************************************************
       
    45      * TestRunConsole
       
    46      */
       
    47     void TestRunConsole::beginRun(TestMgr    * ,
       
    48                                   size_t       /* testCount */,
       
    49                                   const char * /* baseDirPath */)
       
    50     {
       
    51         indent_ = 0;
       
    52         os_ << "TEST RUN STARTING." << std::endl;
       
    53     }
       
    54 
       
    55     
       
    56     void TestRunConsole::endRun(TestMgr * ) throw ()
       
    57     {
       
    58         using namespace std;
       
    59         os_ << endl << "TEST RUN COMPLETED." << endl;
       
    60     }
       
    61 
       
    62 
       
    63     void TestRunConsole::beginTestCase(TestMgr     * ,
       
    64                                        TesterBase  * testerBase)
       
    65     {
       
    66         ++indent_;
       
    67         
       
    68         using namespace std;
       
    69 
       
    70         os_ << endl;
       
    71         printIndent();
       
    72         os_ << testerBase->name() << ' ';
       
    73     }
       
    74 
       
    75 
       
    76     void TestRunConsole::endTestCase(TestMgr     * testMgr,
       
    77                                      TesterBase  * testerBase)
       
    78     {
       
    79         --indent_;
       
    80     }
       
    81 
       
    82         
       
    83     void TestRunConsole::expecting(TestMgr     * testMgr,
       
    84                                    bool          succeeded,
       
    85                                    const char  * expr,
       
    86                                    const char  * file,
       
    87                                    size_t        line,
       
    88                                    const char  * msg)
       
    89     {
       
    90         if (!succeeded)
       
    91             {
       
    92                 os_ << 'E';
       
    93             }
       
    94     }
       
    95 
       
    96 
       
    97     void TestRunConsole::asserting(TestMgr     * testMgr,
       
    98                                    bool          succeeded,
       
    99                                    const char  * expr,
       
   100                                    const char  * file,
       
   101                                    size_t        line,
       
   102                                    const char  * msg)
       
   103     {
       
   104         if (!succeeded)
       
   105             {
       
   106                 os_ << 'A';
       
   107             }
       
   108     }
       
   109 
       
   110 
       
   111     void TestRunConsole::unknownFailure(TestMgr          * testMgr,
       
   112                                         const char       * contextName)
       
   113     {
       
   114         os_ << "UNKNOWN ERROR IN CONTEXT: " << contextName;
       
   115     }
       
   116 
       
   117 
       
   118     void TestRunConsole::msg(TestMgr     * testMgr,
       
   119                              const char  * file,
       
   120                              size_t        line,
       
   121                              const char  * msg)
       
   122     {
       
   123         using namespace std;
       
   124 
       
   125         os_ << endl << msg << endl;
       
   126     }
       
   127 
       
   128 
       
   129     void TestRunConsole::panic(const char * file,
       
   130                                size_t       line,
       
   131                                const char * msg)
       
   132     {
       
   133         using namespace std;
       
   134 
       
   135         os_ 
       
   136             << "PANIC at "
       
   137             << file
       
   138             << '/'
       
   139             << line
       
   140             << ": "
       
   141             << msg
       
   142             << endl;
       
   143     }
       
   144     
       
   145 
       
   146     void TestRunConsole::ioCaptureDefined(const char * file,
       
   147                                           const char * msg)
       
   148     {
       
   149         using namespace std;
       
   150 
       
   151         os_ << "Please check manually and version "
       
   152             << file
       
   153             << endl;
       
   154     }
       
   155 
       
   156     
       
   157     void TestRunConsole::ioCaptureError(const char * file,
       
   158                                         const char * msg)
       
   159     {
       
   160         using namespace std;
       
   161 
       
   162         os_ << "Please check problem with file "
       
   163             << file
       
   164             << endl;
       
   165     }
       
   166 
       
   167 
       
   168     void TestRunConsole::report(const char * name,
       
   169                                 const char * value)
       
   170     {
       
   171         ;
       
   172     }
       
   173 
       
   174     
       
   175     TestRunConsole::TestRunConsole(std::ostream & os)
       
   176         : os_(os),
       
   177           indent_(0)
       
   178     {
       
   179         ;
       
   180     }
       
   181 
       
   182 
       
   183     TestRunConsole::~TestRunConsole()
       
   184     {
       
   185         ;
       
   186     }
       
   187 
       
   188 
       
   189     void TestRunConsole::printIndent()
       
   190     {
       
   191         for (int i = indent_; i >= 0; --i)
       
   192             {
       
   193                 os_ << "  ";
       
   194             }
       
   195     }
       
   196     
       
   197 
       
   198 
       
   199     /*****************************************************************
       
   200      * CompositeTestRunObserver
       
   201      */
       
   202     void CompositeTestRunObserver::beginRun(TestMgr    * testMgr,
       
   203                                             size_t       testCount,
       
   204                                             const char * baseDirPath)
       
   205     {
       
   206         std::list<ITestRunObserver*>::iterator
       
   207             i = observers_.begin(),
       
   208             end = observers_.end();
       
   209 
       
   210         for (; i != end; ++i)
       
   211             {
       
   212                 (*i)->beginRun(testMgr,
       
   213                                testCount,
       
   214                                baseDirPath);
       
   215             }
       
   216     }
       
   217 
       
   218 
       
   219     void CompositeTestRunObserver::endRun(TestMgr * testMgr) throw ()
       
   220     {
       
   221         std::list<ITestRunObserver*>::iterator
       
   222             i = observers_.begin(),
       
   223             end = observers_.end();
       
   224 
       
   225         for (; i != end; ++i)
       
   226             {
       
   227                 (*i)->endRun(testMgr);
       
   228             }
       
   229     }
       
   230 
       
   231 
       
   232     void CompositeTestRunObserver::beginTestCase(TestMgr     * testMgr,
       
   233                                                  TesterBase  * testerBase)
       
   234     {
       
   235         std::list<ITestRunObserver*>::iterator
       
   236             i = observers_.begin(),
       
   237             end = observers_.end();
       
   238 
       
   239         for (; i != end; ++i)
       
   240             {
       
   241                 (*i)->beginTestCase(testMgr,
       
   242                                     testerBase);
       
   243             }
       
   244     }
       
   245 
       
   246 
       
   247     void CompositeTestRunObserver::endTestCase(TestMgr     * testMgr,
       
   248                                                TesterBase  * testerBase)
       
   249     {
       
   250         std::list<ITestRunObserver*>::iterator
       
   251             i = observers_.begin(),
       
   252             end = observers_.end();
       
   253 
       
   254         for (; i != end; ++i)
       
   255             {
       
   256                 (*i)->endTestCase(testMgr,
       
   257                                   testerBase);
       
   258             }
       
   259     }
       
   260 
       
   261 
       
   262     void CompositeTestRunObserver::expecting(TestMgr     * testMgr,
       
   263                                              bool          succeeded,
       
   264                                              const char  * expr,
       
   265                                              const char  * file,
       
   266                                              size_t        line,
       
   267                                              const char  * msg)
       
   268     {
       
   269         std::list<ITestRunObserver*>::iterator
       
   270             i = observers_.begin(),
       
   271             end = observers_.end();
       
   272 
       
   273         for (; i != end; ++i)
       
   274             {
       
   275                 (*i)->expecting(testMgr,
       
   276                                 succeeded,
       
   277                                 expr,
       
   278                                 file,
       
   279                                 line,
       
   280                                 msg);
       
   281             }
       
   282     }
       
   283 
       
   284 
       
   285     void CompositeTestRunObserver::asserting(TestMgr     * testMgr,
       
   286                                              bool          succeeded,
       
   287                                              const char  * expr,
       
   288                                              const char  * file,
       
   289                                              size_t        line,
       
   290                                              const char  * msg)
       
   291     {
       
   292         std::list<ITestRunObserver*>::iterator
       
   293             i = observers_.begin(),
       
   294             end = observers_.end();
       
   295 
       
   296         for (; i != end; ++i)
       
   297             {
       
   298                 (*i)->asserting(testMgr,
       
   299                                 succeeded,
       
   300                                 expr,
       
   301                                 file,
       
   302                                 line,
       
   303                                 msg);
       
   304             }
       
   305     }
       
   306 
       
   307 
       
   308     void CompositeTestRunObserver::unknownFailure(TestMgr          * testMgr,
       
   309                                                   const char       * contextName)
       
   310     {
       
   311         std::list<ITestRunObserver*>::iterator
       
   312             i = observers_.begin(),
       
   313             end = observers_.end();
       
   314 
       
   315         for (; i != end; ++i)
       
   316             {
       
   317                 (*i)->unknownFailure(testMgr,
       
   318                                      contextName);
       
   319             }
       
   320     }
       
   321 
       
   322 
       
   323     void CompositeTestRunObserver::msg(TestMgr     * testMgr,
       
   324                                        const char  * file,
       
   325                                        size_t        line,
       
   326                                        const char  * msg)
       
   327     {
       
   328         std::list<ITestRunObserver*>::iterator
       
   329             i = observers_.begin(),
       
   330             end = observers_.end();
       
   331 
       
   332         for (; i != end; ++i)
       
   333             {
       
   334                 (*i)->msg(testMgr,
       
   335                           file,
       
   336                           line,
       
   337                           msg);
       
   338             }
       
   339     }
       
   340 
       
   341 
       
   342     void CompositeTestRunObserver::panic(const char * file,
       
   343                                          size_t       line,
       
   344                                          const char * msg)
       
   345     {
       
   346         std::list<ITestRunObserver*>::iterator
       
   347             i = observers_.begin(),
       
   348             end = observers_.end();
       
   349 
       
   350         for (; i != end; ++i)
       
   351             {
       
   352                 (*i)->panic(file,
       
   353                             line,
       
   354                             msg);
       
   355             }
       
   356     }
       
   357 
       
   358 
       
   359     void CompositeTestRunObserver::ioCaptureDefined(const char * file,
       
   360                                                     const char * msg)
       
   361     {
       
   362         std::list<ITestRunObserver*>::iterator
       
   363             i = observers_.begin(),
       
   364             end = observers_.end();
       
   365 
       
   366         for (; i != end; ++i)
       
   367             {
       
   368                 (*i)->ioCaptureDefined(file,
       
   369                                        msg);
       
   370             }
       
   371     }
       
   372 
       
   373 
       
   374     void CompositeTestRunObserver::ioCaptureError(const char * file,
       
   375                                                   const char * msg)
       
   376     {
       
   377         std::list<ITestRunObserver*>::iterator
       
   378             i = observers_.begin(),
       
   379             end = observers_.end();
       
   380 
       
   381         for (; i != end; ++i)
       
   382             {
       
   383                 (*i)->ioCaptureError(file,
       
   384                                      msg);
       
   385             }
       
   386     }
       
   387 
       
   388 
       
   389     void CompositeTestRunObserver::report(const char * name,
       
   390                                           const char * value)
       
   391     {
       
   392         std::list<ITestRunObserver*>::iterator
       
   393             i = observers_.begin(),
       
   394             end = observers_.end();
       
   395 
       
   396         for (; i != end; ++i)
       
   397             {
       
   398                 (*i)->report(name,
       
   399                              value);
       
   400             }
       
   401     }
       
   402 
       
   403     
       
   404     CompositeTestRunObserver::CompositeTestRunObserver()
       
   405     {
       
   406         ;
       
   407     }
       
   408 
       
   409 
       
   410     CompositeTestRunObserver::~CompositeTestRunObserver()
       
   411     {
       
   412         std::list<ITestRunObserver*>::iterator
       
   413             i = observers_.begin(),
       
   414             end = observers_.end();
       
   415 
       
   416         for (; i != end; ++i)
       
   417             {
       
   418                 delete *i;
       
   419             }
       
   420     }
       
   421 
       
   422 
       
   423     void CompositeTestRunObserver::add(ITestRunObserver * testRunObserver)
       
   424     {
       
   425         std::auto_ptr<ITestRunObserver>
       
   426             safeGuard(testRunObserver);
       
   427 
       
   428         observers_.push_back(testRunObserver);
       
   429         safeGuard.release();
       
   430     }
       
   431 
       
   432 
       
   433 
       
   434 
       
   435     /*****************************************************************
       
   436      * ProgressDumper
       
   437      */
       
   438     void ProgressDumper::beginRun(TestMgr    * testMgr,
       
   439                                   size_t       testCount,
       
   440                                   const char * baseDirPath)
       
   441     {
       
   442         ofs_ << "TEST RUN STARTING." << std::endl;
       
   443         ofs_.flush();
       
   444     }
       
   445 
       
   446 
       
   447     void ProgressDumper::endRun(TestMgr * testMgr) throw ()
       
   448     {
       
   449         ofs_ << "TEST RUN COMPLETED." << std::endl;
       
   450         ofs_.flush();
       
   451     }
       
   452 
       
   453 
       
   454     void ProgressDumper::beginTestCase(TestMgr     * testMgr,
       
   455                                        TesterBase  * testerBase)
       
   456     {
       
   457         ofs_ << "TESTCASE BEGIN: " << testerBase->name() << std::endl;
       
   458         ofs_.flush();
       
   459     }
       
   460 
       
   461 
       
   462     void ProgressDumper::endTestCase(TestMgr     * testMgr,
       
   463                                      TesterBase  * testerBase)
       
   464     {
       
   465         ofs_ << "TESTCASE END." << std::endl;
       
   466         ofs_.flush();
       
   467     }
       
   468 
       
   469 
       
   470     void ProgressDumper::expecting(TestMgr     * testMgr,
       
   471                                    bool          succeeded,
       
   472                                    const char  * expr,
       
   473                                    const char  * file,
       
   474                                    size_t        line,
       
   475                                    const char  * msg)
       
   476     {
       
   477         if (!succeeded)
       
   478             {
       
   479                 ofs_ << "EXPECTATION "
       
   480                      << expr
       
   481                      << " at ("
       
   482                      << file
       
   483                      << ":"
       
   484                      << line
       
   485                      << ") failed"
       
   486                      << std::endl;
       
   487                 ofs_.flush();
       
   488             }
       
   489     }
       
   490 
       
   491 
       
   492     void ProgressDumper::asserting(TestMgr     * testMgr,
       
   493                                    bool          succeeded,
       
   494                                    const char  * expr,
       
   495                                    const char  * file,
       
   496                                    size_t        line,
       
   497                                    const char  * msg)
       
   498     {
       
   499         if (!succeeded)
       
   500             {
       
   501                 ofs_ << "ASSERTION "
       
   502                      << expr
       
   503                      << " at ("
       
   504                      << file
       
   505                      << ":"
       
   506                      << line
       
   507                      << ") failed"
       
   508                      << std::endl;
       
   509                 ofs_.flush();
       
   510             }
       
   511     }
       
   512 
       
   513 
       
   514     void ProgressDumper::unknownFailure(TestMgr          * testMgr,
       
   515                                         const char       * contextName)
       
   516     {
       
   517         ofs_ << "UNKNOWN FAILURE IN CONTEXT " << contextName << std::endl;
       
   518         ofs_.flush();
       
   519     }
       
   520 
       
   521 
       
   522     void ProgressDumper::msg(TestMgr     * testMgr,
       
   523                              const char  * file,
       
   524                              size_t        line,
       
   525                              const char  * msg)
       
   526     {
       
   527         ofs_ << "MSG "
       
   528              << msg
       
   529              << " at ("
       
   530              << file
       
   531              << ":"
       
   532              << line
       
   533              << std::endl;
       
   534         ofs_.flush();
       
   535     }
       
   536 
       
   537 
       
   538     void ProgressDumper::panic(const char * file,
       
   539                                size_t       line,
       
   540                                const char * msg)
       
   541     {
       
   542         ofs_ << "PANIC "
       
   543              << msg
       
   544              << " at ("
       
   545              << file
       
   546              << ":"
       
   547              << line
       
   548              << std::endl;
       
   549         ofs_.flush();
       
   550     }
       
   551 
       
   552 
       
   553     void ProgressDumper::ioCaptureDefined(const char * file,
       
   554                                           const char * msg)
       
   555     {
       
   556         ofs_ << "IO CAPTURE DEFINED at ("
       
   557              << file
       
   558              << "): "
       
   559              << msg
       
   560              << std::endl;
       
   561         ofs_.flush();
       
   562     }
       
   563 
       
   564 
       
   565     void ProgressDumper::ioCaptureError(const char * file,
       
   566                                         const char * msg)
       
   567     {
       
   568         ofs_ << "IO CAPTURE ERROR at ("
       
   569              << file
       
   570              << "): "
       
   571              << msg
       
   572              << std::endl;
       
   573         ofs_.flush();
       
   574     }
       
   575 
       
   576 
       
   577     void ProgressDumper::report(const char * name,
       
   578                                 const char * value)
       
   579     {
       
   580         ofs_ << "REPORT "
       
   581              << name
       
   582              << ": "
       
   583              << value
       
   584              << std::endl;
       
   585         ofs_.flush();
       
   586     }
       
   587 
       
   588     
       
   589     ProgressDumper::ProgressDumper(const char * path)
       
   590         : ofs_(path)
       
   591     {
       
   592         if (!ofs_.is_open())
       
   593             {
       
   594                 throw std::exception();
       
   595             }
       
   596     }
       
   597 
       
   598 
       
   599     ProgressDumper::~ProgressDumper()
       
   600     {
       
   601         ;
       
   602     }
       
   603 
       
   604 
       
   605     /*****************************************************************
       
   606      * ProgressFsDisplayer
       
   607      */
       
   608 
       
   609     const size_t  BLINKENLICHT_CHARNUM = 4;
       
   610     const char    BLINKENLICHT_CHARS[] = "W3ME";
       
   611 
       
   612 
       
   613     void ProgressFsDisplayer::beginRun(TestMgr    * testMgr,
       
   614                                        size_t       testCount,
       
   615                                        const char * baseDirPath)
       
   616     {
       
   617         baseFilePath_ = baseDirPath;
       
   618         if (baseFilePath_[baseFilePath_.length() - 1] != '\\'
       
   619             && baseFilePath_[baseFilePath_.length() - 1] != '/')
       
   620             {
       
   621                 // TODO FIX platform-dependent code here
       
   622                 baseFilePath_ += '\\';
       
   623             }
       
   624         baseFilePath_ += "itk_";
       
   625         
       
   626         curFilePath_ = "";
       
   627 
       
   628         blinkenLichtStatus_ = 0;
       
   629 
       
   630         blink();
       
   631     }
       
   632 
       
   633 
       
   634     void ProgressFsDisplayer::endRun(TestMgr * testMgr) throw ()
       
   635     {
       
   636         deleteCurFile();
       
   637     }
       
   638 
       
   639 
       
   640     void ProgressFsDisplayer::beginTestCase(TestMgr     * testMgr,
       
   641                                             TesterBase  * testerBase)
       
   642     {
       
   643         blink();
       
   644     }
       
   645 
       
   646 
       
   647     void ProgressFsDisplayer::endTestCase(TestMgr     * testMgr,
       
   648                                           TesterBase  * testerBase)
       
   649     {
       
   650 
       
   651         blink();
       
   652     }
       
   653 
       
   654 
       
   655     void ProgressFsDisplayer::expecting(TestMgr     * testMgr,
       
   656                                         bool          succeeded,
       
   657                                         const char  * expr,
       
   658                                         const char  * file,
       
   659                                         size_t        line,
       
   660                                         const char  * msg)
       
   661     {
       
   662         blink();
       
   663     }
       
   664 
       
   665 
       
   666     void ProgressFsDisplayer::asserting(TestMgr     * testMgr,
       
   667                                         bool          succeeded,
       
   668                                         const char  * expr,
       
   669                                         const char  * file,
       
   670                                         size_t        line,
       
   671                                         const char  * msg)
       
   672     {
       
   673         blink();
       
   674     }
       
   675 
       
   676 
       
   677     void ProgressFsDisplayer::unknownFailure(TestMgr          * testMgr,
       
   678                                              const char       * contextName)
       
   679     {
       
   680         blink();
       
   681     }
       
   682 
       
   683 
       
   684     void ProgressFsDisplayer::msg(TestMgr     * testMgr,
       
   685                                   const char  * file,
       
   686                                   size_t        line,
       
   687                                   const char  * msg)
       
   688     {
       
   689         blink();
       
   690     }
       
   691 
       
   692 
       
   693     void ProgressFsDisplayer::panic(const char * file,
       
   694                                     size_t       line,
       
   695                                     const char * msg)
       
   696     {
       
   697         blink();
       
   698     }
       
   699 
       
   700 
       
   701     void ProgressFsDisplayer::ioCaptureDefined(const char * file,
       
   702                                                const char * msg)
       
   703     {
       
   704         blink();
       
   705     }
       
   706 
       
   707 
       
   708     void ProgressFsDisplayer::ioCaptureError(const char * file,
       
   709                                              const char * msg)
       
   710     {
       
   711         blink();
       
   712     }
       
   713 
       
   714 
       
   715     void ProgressFsDisplayer::report(const char * name,
       
   716                                      const char * value)
       
   717     {
       
   718         blink();
       
   719     }
       
   720 
       
   721      
       
   722     ProgressFsDisplayer::ProgressFsDisplayer()
       
   723     {
       
   724         ;
       
   725     }
       
   726 
       
   727 
       
   728     ProgressFsDisplayer::~ProgressFsDisplayer()
       
   729     {
       
   730         deleteCurFile();
       
   731     }
       
   732 
       
   733 
       
   734     void ProgressFsDisplayer::blink()
       
   735     {
       
   736         deleteCurFile();
       
   737 
       
   738         // computing next file name to display status with
       
   739         ++blinkenLichtStatus_;
       
   740         if (blinkenLichtStatus_ == BLINKENLICHT_CHARNUM)
       
   741             {
       
   742                 blinkenLichtStatus_ = 0;
       
   743             }
       
   744 
       
   745         curFilePath_ = baseFilePath_;
       
   746         curFilePath_ += BLINKENLICHT_CHARS[blinkenLichtStatus_];
       
   747         curFilePath_ += ".txt";
       
   748 
       
   749         // "touch"
       
   750         std::ofstream
       
   751             ofs(curFilePath_.c_str());
       
   752     }
       
   753 
       
   754 
       
   755     void ProgressFsDisplayer::deleteCurFile()
       
   756     {
       
   757         Cpt_EINTR_RETRY_SP( remove(curFilePath_.c_str()) );
       
   758     }
       
   759 
       
   760 
       
   761 
       
   762 
       
   763 }