Back to index

salome-kernel  6.5.0
UtilsTest.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "UtilsTest.hxx"
00024 
00025 #include <iostream>
00026 #include <fstream>
00027 #include <string>
00028 #include <cstdlib>
00029 #include "Utils_SALOME_Exception.hxx"
00030 #include "utilities.h"
00031 
00032 
00033 #define TRACEFILE "/tmp/traceUnitTest.log"
00034 
00035 // ============================================================================
00041 // ============================================================================
00042 
00043 void 
00044 UtilsTest::setUp()
00045 {
00046   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
00047   CPPUNIT_ASSERT(bp1);
00048   bp1->deleteInstance(bp1);
00049 
00050   // --- trace on file
00051   const char *theFileName = TRACEFILE;
00052 
00053   std::string s = "file:";
00054   s += theFileName;
00055   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
00056 
00057   std::ofstream traceFile;
00058   traceFile.open(theFileName, std::ios::out | std::ios::app);
00059   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
00060   traceFile.close();
00061 
00062   bp1 = LocalTraceBufferPool::instance();
00063   CPPUNIT_ASSERT(bp1);
00064 }
00065 
00066 // ============================================================================
00070 // ============================================================================
00071 
00072 void 
00073 UtilsTest::tearDown()
00074 {
00075   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
00076   CPPUNIT_ASSERT(bp1);
00077   bp1->deleteInstance(bp1);
00078 }
00079 
00080 int genExcept()
00081 {
00082   throw SALOME_Exception("a message");
00083 };
00084 
00085 // ============================================================================
00089 // ============================================================================
00090 
00091 void
00092 UtilsTest::testSALOME_ExceptionThrow()
00093 {
00094   CPPUNIT_ASSERT_THROW(genExcept(), SALOME_Exception);
00095 }
00096 
00097 // ============================================================================
00101 // ============================================================================
00102 
00103 void
00104 UtilsTest::testSALOME_ExceptionMessage()
00105 {
00106 #define EXAMPLE_EXCEPTION_MESSAGE "something for the end user"
00107   try
00108     {
00109       throw SALOME_Exception(EXAMPLE_EXCEPTION_MESSAGE);
00110     }
00111   catch (const SALOME_Exception &ex)
00112     {
00113       std::string expectedMessage = EXAMPLE_EXCEPTION_MESSAGE;
00114       std::string actualMessage = ex.what();
00115       CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != std::string::npos);
00116     }
00117 }