d9f1297eab45d5e16cb9660d65293f14459cd766
[senf.git] / senf / Utils / Exception.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Exception unit tests */
25
26 //#include "Exception.test.hh"
27 //#include "Exception.test.ih"
28
29 // Custom includes
30 #include "Exception.hh"
31 #include <boost/format.hpp>
32 #include <errno.h>
33
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 SENF_AUTO_UNIT_TEST(wrapException)
41 {
42     bool bad_cast (false);
43
44     try {
45         try {
46             try {
47                 try {
48                     try {
49                         throw std::bad_cast();
50                     }
51                     SENF_WRAP_EXC(std::bad_cast)
52                 }
53                 SENF_WRAP_EXC(std::bad_cast)
54             }
55             catch (senf::ExceptionMixin & ex) {
56                 ex << "\nspecial exception";
57                 throw;
58             }
59         }
60         catch (std::exception const & ex) {
61 #ifdef SENF_DEBUG
62             BOOST_CHECK( std::string(ex.what()).find("-- \n") != std::string::npos );
63 #endif
64             BOOST_CHECK( std::string(ex.what()).find("special exception") != std::string::npos );
65             throw;
66         }
67     }
68     catch (std::bad_cast &) {
69         bad_cast = true;
70     }
71     BOOST_CHECK( bad_cast );
72 }
73
74 SENF_AUTO_UNIT_TEST(errnoException)
75 {
76     try {
77         try {
78             throw senf::SystemException("::open()", ENOENT) << "\nmore";
79         }
80         catch(senf::Exception & e) {
81             e << "\nx=" << 1 << boost::format("\ny=%d") % 2;
82             throw;
83         }
84     }
85     catch (senf::SystemException & e) {
86         BOOST_CHECK_EQUAL( e.errorNumber(), ENOENT );
87         BOOST_CHECK_EQUAL( e.errorString(), "No such file or directory" );
88         std::string what (e.what());
89         std::string::size_type pos (what.find("-- \n"));
90         if (pos != std::string::npos)
91             what = std::string(what, pos+4);
92         BOOST_CHECK_EQUAL( what, "[No such file or directory] ::open()\nmore\nx=1\ny=2" );
93     }
94 }
95
96 //-/////////////////////////////////////////////////////////////////////////////////////////////////
97 #undef prefix_
98
99 \f
100 // Local Variables:
101 // mode: c++
102 // fill-column: 100
103 // comment-column: 40
104 // c-file-style: "senf"
105 // indent-tabs-mode: nil
106 // ispell-local-dictionary: "american"
107 // compile-command: "scons -u test"
108 // End: