home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / testcase.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.0 KB  |  287 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "stdafx.h"
  20. #include "testcase.h"
  21. #include "resource.h"
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29.  
  30. // global variable to access Testcase
  31. extern CTestCase QA_TestCase;
  32.  
  33. // Assigns sTestID, sLogFile and sStartTime
  34. CTestCase::CTestCase(CTestCaseManager *tc)
  35. {
  36.     
  37.     CFile fFile;    
  38.     m_sTestID = tc->GetTestCaseName();
  39.     
  40.     m_TCManager = tc;
  41.     CString sDataPath = tc->m_sDataPath;
  42.  
  43.     
  44.     m_sBaseFile = (CString)(sDataPath + m_sTestID + ".bas");
  45.     m_sResultFile = (CString)(sDataPath + m_sTestID + ".ran");
  46.     m_sBeakerFile = "c:\\harness.log";  // DOS and Windows only path format
  47.     
  48.  
  49.     // if BaseLine File doesn't exist then log run to a .bas file instead of a .log file
  50.     if (!m_TCManager->FileExists(m_sBaseFile))
  51.     {
  52.         m_sLogFile = m_sBaseFile;
  53.         m_bCreateBaseLine = TRUE;
  54.     }
  55.     else
  56.     {
  57.         m_sLogFile = (CString)(sDataPath + m_sTestID + ".log");
  58.         // remove previous log file
  59.         if (m_TCManager->FileExists(m_sLogFile))
  60.         {
  61.             CFile fFile;
  62.             fFile.Remove(m_sLogFile);
  63.         }
  64.         m_bCreateBaseLine = FALSE;
  65.     }
  66.  
  67.         
  68.     // remove .ran file
  69.     if (m_TCManager->FileExists(m_sResultFile))
  70.         fFile.Remove(m_sResultFile);
  71.  
  72.     // create .ran file
  73.     fFile.Open(m_sResultFile,CFile::modeCreate | CFile::shareDenyNone);
  74.     fFile.Close();
  75.  
  76.     fFile.Open(m_sLogFile,CFile::modeCreate | CFile::shareDenyNone);
  77.     fFile.Close();
  78.  
  79.     // create c:\harness.log file 
  80.     fFile.Open(m_sBeakerFile,CFile::modeCreate | CFile::shareDenyNone);
  81.     fFile.Close();
  82.         
  83.  
  84.     // Log Clock
  85.     char tmpbuf[128];
  86.  
  87.      _strdate( tmpbuf );
  88.     m_sStartTimeString = (CString)((CString)"Start Date/Time:  " + tmpbuf + " ");
  89.     m_sStartDate = (CString)(tmpbuf);
  90.  
  91.     _strtime( tmpbuf );
  92.     m_sStartTimeString += (CString)(tmpbuf);
  93.     m_sStartTime = (CString)(tmpbuf);
  94.  
  95.     m_sExpectedResults = " ";
  96.     m_sActualResults    = "Some Error";
  97.  
  98.     // Log a header at the Top of the (.all) file
  99.     CString sHeader = "\n++Testcase : " + m_sTestID;
  100.     m_TCManager->PrintToFile(sHeader);
  101.  
  102.     PhaseTwoIsFalse();                    // added by par
  103.     m_bFileCompareResult = FALSE;        // added by par
  104. }
  105.  
  106.  
  107. CTestCase::~CTestCase()
  108. {
  109.     // release memory
  110.     m_sTestID.Empty();
  111.     m_sBaseFile.Empty();
  112.     m_sLogFile.Empty();
  113.     m_sResultFile.Empty();
  114.     m_sStartDate.Empty();
  115.     m_sStartTime.Empty();
  116.     m_sStartTimeString.Empty();
  117.     m_sEndTime.Empty();
  118.     m_sEndTimeString.Empty();
  119.     m_sManualResult.Empty();
  120. }
  121.  
  122.     
  123. // Compares two log files.  
  124. // returns result.
  125.  
  126. void CTestCase::LogResult()
  127. {
  128.     CStdioFile fResultFile;
  129.     CString sResult;
  130.  
  131.     // check if creating baseline or there was an actual compare
  132.     if (!m_bCreateBaseLine)
  133.         sResult = "File Compare Result: " + (m_bFileCompareResult ? "PASS" : "*** FAILED - Check "+ m_sLogFile);
  134.     else
  135.         sResult = "Created BaseLine File - " + m_sBaseFile;
  136.  
  137.     if (m_bPhaseTwoResult) // TRUE
  138.         sResult = sResult + "\n" + "Phase Two Result: PASS";
  139.     else
  140.         sResult = sResult + "\n" + "Phase Two Result: FAIL";
  141.  
  142.     if (!m_bCreateBaseLine)
  143.     {
  144.         if (m_bFileCompareResult && m_bPhaseTwoResult) {
  145.             sResult = sResult + "\n" + "Overall Test Result: PASS";
  146.             m_bOverallResult = TRUE;
  147.         } else {
  148.             sResult = sResult + "\n" + "Overall Test Result: FAIL";
  149.             m_bOverallResult = FALSE;
  150.         }
  151.     }
  152.     else
  153.         sResult = sResult + "\n" + "Overall Test Result: N/A";
  154.     
  155.     // Print Result to files (.ran)
  156.     PrintToFile(m_sResultFile,sResult);
  157.  
  158.     // (.all)
  159.     m_TCManager->PrintToFile(sResult);
  160.  
  161.         // Print Times to files (.ran)
  162.     CString sTimes = m_sStartTimeString + "\n" + m_sEndTimeString;
  163.     PrintToFile(m_sResultFile,sTimes);
  164.  
  165.     // (.all)
  166.     m_TCManager->PrintToFile(sTimes);
  167.  
  168.     
  169. }
  170.  
  171. // Compares 2 files one line at a time.
  172. BOOL CTestCase::FileCompare()
  173. {
  174.     BOOL bResult=FALSE;
  175.     CStdioFile fBase, fCompare;
  176.     CString sLine_Base, sLine_Comp;
  177.  
  178.     if (m_TCManager->FileExists(m_sLogFile))
  179.     {
  180.         if (fBase.Open((LPCTSTR)m_sBaseFile,CFile::modeRead) && \
  181.             fCompare.Open((LPCTSTR)m_sLogFile, CFile::modeRead))
  182.         {
  183.             // verify line at a time
  184.             while (fBase.ReadString(sLine_Base) && fCompare.ReadString(sLine_Comp) && bResult)
  185.                 bResult = (sLine_Base.Compare(sLine_Comp) == 0 ? 1 : 0);    
  186.  
  187.             fBase.Close();
  188.             fCompare.Close();
  189.             bResult = TRUE;
  190.         }
  191.     }
  192.  
  193.     return bResult;
  194. }
  195.  
  196. void CTestCase::QA_Trace(CString msg)
  197. {
  198.     // write to Log File (.bas if doesn't exist, otherwise .log)
  199.     PrintToFile(m_sLogFile,msg);
  200. }
  201.  
  202. void CTestCase::QA_LogError(CString msg)
  203. {
  204.     // write to Overall File (.all)
  205.     m_TCManager->PrintToFile(msg);
  206.  
  207.     // write to Testcase Result File (.ran)
  208.     PrintToFile(m_sResultFile, msg);
  209. }
  210.  
  211. void CTestCase::WrapUp()
  212. {
  213.     //Log End Time
  214.     char tmpbuf[128];
  215.  
  216.      _strdate( tmpbuf );
  217.     m_sEndTimeString = (CString)((CString)"End Date/Time:  " + tmpbuf + " ");
  218.     m_sEndDate = (CString)(tmpbuf);
  219.  
  220.     _strtime( tmpbuf );
  221.     m_sEndTimeString += (CString)(tmpbuf);
  222.     m_sEndTime = (CString)(tmpbuf);
  223.  
  224.  
  225.     if (!m_bCreateBaseLine)
  226.         m_bFileCompareResult = FileCompare();
  227.     
  228.     LogResult();
  229.     LogBeakerResult();  // new
  230. }
  231.  
  232. void CTestCase::PrintToFile(CString& sFile, CString& str)
  233. {
  234.     CStdioFile fFile;
  235.  
  236.     if (fFile.Open(sFile, CFile::modeWrite | CFile::shareDenyNone))
  237.     {  
  238.         fFile.Seek(0, CFile::end);
  239.         // write to Testcase Log File
  240.         fFile.WriteString(str + "\n");
  241.         fFile.Flush();
  242.         fFile.Close();
  243.     }
  244. }
  245.  
  246. void CTestCase::LogBeakerResult()
  247. {
  248.     // Offical Beaker format is
  249.     // (machine), -pass-, Test_case_Name/Beaker, Test_Case_Name, Date, Time Start, Time End, Expected Results, Actual Results
  250.  
  251.     CStdioFile fResultFile;
  252.     CString sResult;
  253.  
  254.     sResult = "(local), ";   // standard
  255.  
  256.     // check if creating baseline or there was an actual compare
  257.     if (!m_bOverallResult)
  258.         sResult = sResult + "*FAIL*, ";
  259.     else
  260.         sResult = sResult + "-pass-, "; 
  261.  
  262.     CString TestCaseName = m_TCManager->GetTestCaseName();
  263.     sResult = sResult + (LPCTSTR) TestCaseName + "\\Beaker, ";
  264.     sResult = sResult + (LPCTSTR) TestCaseName + ", ";
  265.  
  266.     // log the date
  267.     char tmpbuf[128];
  268.      _strdate( tmpbuf );
  269.     CString TodayDate = (CString)(tmpbuf);
  270.  
  271.     sResult = sResult + m_sStartDate + ", " + m_sStartTime + ", " + m_sEndTime + ", ";
  272.  
  273.     
  274.  
  275.     if (!m_bOverallResult) {
  276.         sResult = sResult + m_sExpectedResults + ", ";
  277.         sResult = sResult + m_sActualResults;
  278.     } else {
  279.         // do nothing as no need to post actual results
  280.         sResult = sResult + ", ";
  281.     }
  282.  
  283.     // Print Result to Beaker file (harness.log)
  284.     PrintToFile(m_sBeakerFile,sResult);
  285. }
  286.  
  287.