Salome HOME
CCAR: import_hook.py was too strict in ensure_list (ImportError raised)
[modules/kernel.git] / src / Utils / Test / UtilsTest.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, 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.
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 "utilities.h"
31
32
33 #define TRACEFILE "/tmp/traceUnitTest.log"
34
35 // ============================================================================
36 /*!
37  * Set Trace mecanism
38  * - delete preexisting trace classes if any
39  * - set trace on file
40  */
41 // ============================================================================
42
43 void 
44 UtilsTest::setUp()
45 {
46   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
47   CPPUNIT_ASSERT(bp1);
48   bp1->deleteInstance(bp1);
49
50   // --- trace on file
51   const char *theFileName = TRACEFILE;
52
53   std::string s = "file:";
54   s += theFileName;
55   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
56
57   std::ofstream traceFile;
58   traceFile.open(theFileName, std::ios::out | std::ios::app);
59   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
60   traceFile.close();
61
62   bp1 = LocalTraceBufferPool::instance();
63   CPPUNIT_ASSERT(bp1);
64 }
65
66 // ============================================================================
67 /*!
68  *  - delete trace classes
69  */
70 // ============================================================================
71
72 void 
73 UtilsTest::tearDown()
74 {
75   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
76   CPPUNIT_ASSERT(bp1);
77   bp1->deleteInstance(bp1);
78 }
79
80 int genExcept()
81 {
82   throw SALOME_Exception("a message");
83 };
84
85 // ============================================================================
86 /*!
87  * Check basic SALOME_exception mecanism
88  */
89 // ============================================================================
90
91 void
92 UtilsTest::testSALOME_ExceptionThrow()
93 {
94   CPPUNIT_ASSERT_THROW(genExcept(), SALOME_Exception);
95 }
96
97 // ============================================================================
98 /*!
99  * Check message on catch
100  */
101 // ============================================================================
102
103 void
104 UtilsTest::testSALOME_ExceptionMessage()
105 {
106 #define EXAMPLE_EXCEPTION_MESSAGE "something for the end user"
107   try
108     {
109       throw SALOME_Exception(EXAMPLE_EXCEPTION_MESSAGE);
110     }
111   catch (const SALOME_Exception &ex)
112     {
113       std::string expectedMessage = EXAMPLE_EXCEPTION_MESSAGE;
114       std::string actualMessage = ex.what();
115       CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != std::string::npos);
116     }
117 }