Salome HOME
updated copyright message
[modules/kernel.git] / src / Utils / Test / UtilsTest.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "UtilsTest.hxx"
24
25 #include <iostream>
26 #include <fstream>
27 #include <string>
28 #include <cstdlib>
29 #include "Utils_SALOME_Exception.hxx"
30 #include "Basics_Utils.hxx"
31 #include "Basics_DirUtils.hxx"
32 #include "utilities.h"
33 #include "KernelBasis.hxx"
34
35 #ifdef WIN32
36 #define setenv Kernel_Utils::setenv
37 #endif
38
39 // ============================================================================
40 /*!
41  * Set Trace mechanism
42  * - delete preexisting trace classes if any
43  * - set trace on file
44  */
45 // ============================================================================
46
47 std::string
48 UtilsTest::_getTraceFileName()
49 {
50   std::string dir = Kernel_Utils::GetTmpDir();
51   return dir + "traceUnitTest-UtilsTest.log";
52 }
53
54 void
55 UtilsTest::setUp()
56 {
57   setSSLMode(false);
58   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
59   CPPUNIT_ASSERT(bp1);
60   bp1->deleteInstance(bp1);
61
62   // --- trace on file
63   std::string theFileName = _getTraceFileName();
64
65   std::string s = "file:";
66   s += theFileName;
67   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
68
69   std::ofstream traceFile;
70   traceFile.open(theFileName.c_str(), std::ios::out | std::ios::app);
71   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
72   traceFile.close();
73
74   bp1 = LocalTraceBufferPool::instance();
75   CPPUNIT_ASSERT(bp1);
76 }
77
78 // ============================================================================
79 /*!
80  *  - delete trace classes
81  */
82 // ============================================================================
83
84 void
85 UtilsTest::tearDown()
86 {
87   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
88   CPPUNIT_ASSERT(bp1);
89   bp1->deleteInstance(bp1);
90 }
91
92 int genExcept()
93 {
94   throw SALOME_Exception("a message");
95 }
96
97 // ============================================================================
98 /*!
99  * Check basic SALOME_exception mechanism
100  */
101 // ============================================================================
102
103 void
104 UtilsTest::testSALOME_ExceptionThrow()
105 {
106   CPPUNIT_ASSERT_THROW(genExcept(), SALOME_Exception);
107 }
108
109 // ============================================================================
110 /*!
111  * Check message on catch
112  */
113 // ============================================================================
114
115 void
116 UtilsTest::testSALOME_ExceptionMessage()
117 {
118 #define EXAMPLE_EXCEPTION_MESSAGE "something for the end user"
119   try
120     {
121       throw SALOME_Exception(EXAMPLE_EXCEPTION_MESSAGE);
122     }
123   catch (const SALOME_Exception &ex)
124     {
125       std::string expectedMessage = EXAMPLE_EXCEPTION_MESSAGE;
126       std::string actualMessage = ex.what();
127       CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != std::string::npos);
128     }
129 }