ExceptionTest.cpp 4.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

24
#include "common/exception/Exception.hpp"
25
#include "common/exception/Errnum.hpp"
26
27
28
29
30
#include <errno.h>

#include <gtest/gtest.h>
#include <gmock/gmock-cardinalities.h>

31
namespace unitTests {
32
33
34
35
36
37
38
39
40
  class Nested {
  public:
    void f1();
    void f2();
    Nested();
  };
  
  /* Prevent inlining: it makes this test fail! */
  void __attribute__((noinline)) Nested::f1() {
41
    throw cta::exception::Exception("Throwing in Nested's constructor");
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  }
  
  /* Prevent inlining: it makes this test fail!
   * Even with that, f2 does not show up in the trace */
  void __attribute__((noinline)) Nested::f2() {
    f1();
  }
  
  /* Prevent inlining: it makes this test fail! */
  __attribute__((noinline))  Nested::Nested() {
    f2();
  }

  TEST(castor_exceptions, stacktrace_with_demangling) {
    try {
      Nested x;
58
    } catch (cta::exception::Exception & e) {
59
60
      std::string bt = e.backtrace();
      ASSERT_NE(std::string::npos, bt.find("Nested::f1"));
61
      ASSERT_NE(std::string::npos, bt.find("cta::exception::Backtrace::Backtrace"));
62
      ASSERT_EQ("Throwing in Nested's constructor", std::string(e.getMessageValue()));
63
      std::string fullWhat(e.what());
64
      ASSERT_NE(std::string::npos, fullWhat.find("Nested::f1"));
65
66
67
68
69
70
71
72
    }
  }
  
    TEST(castor_exceptions, stacktrace_in_std_exception) {
    try {
      Nested x;
    } catch (std::exception & e) {
      std::string fullWhat(e.what());
73
74
      ASSERT_NE(std::string::npos, fullWhat.find("Nested::f1"));
      ASSERT_NE(std::string::npos, fullWhat.find("Throwing in Nested's constructor"));
75
76
77
    }
  }
  
78
  TEST(cta_exceptions, errnum_throwing) {
79
80
81
    /* Mickey Mouse test as we had trouble which throwing Errnum (with errno=ENOENT)*/
    errno = ENOENT;
    try {
82
      throw cta::exception::Errnum("Test ENOENT");
83
84
85
86
87
88
    } catch (std::exception & e) {
      std::string temp = e.what();
      temp += " ";
    }
  }

89
  TEST(cta_exceptions, Errnum_throwers) {
90
    /* throwOnReturnedErrno */
91
92
93
    ASSERT_NO_THROW(cta::exception::Errnum::throwOnReturnedErrno(0, "Context"));
    ASSERT_THROW(cta::exception::Errnum::throwOnReturnedErrno(ENOSPC, "Context"),
      cta::exception::Errnum);
94
95
96
    
    /* throwOnNonZero */
    errno = ENOENT;
97
98
99
    ASSERT_NO_THROW(cta::exception::Errnum::throwOnNonZero(0, "Context"));
    ASSERT_THROW(cta::exception::Errnum::throwOnNonZero(-1, "Context"),
      cta::exception::Errnum);
100
101
102
    
    /* throwOnMinusOne */
    errno = ENOENT;
103
104
105
    ASSERT_NO_THROW(cta::exception::Errnum::throwOnMinusOne(0, "Context"));
    ASSERT_THROW(cta::exception::Errnum::throwOnMinusOne(-1, "Context"),
      cta::exception::Errnum);
106
107
108
    
    /* throwOnNegative */
    errno = ENOENT;
109
110
111
    ASSERT_NO_THROW(cta::exception::Errnum::throwOnNegative(0, "Context"));
    ASSERT_THROW(cta::exception::Errnum::throwOnNegative(-1, "Context"),
      cta::exception::Errnum); 
112
113
114

    /* throwOnNull */
    errno = ENOENT;
115
116
117
    ASSERT_NO_THROW(cta::exception::Errnum::throwOnNull(this, "Context"));
    ASSERT_THROW(cta::exception::Errnum::throwOnNull(NULL, "Context"),
      cta::exception::Errnum);
118
119
120

    /* throwOnZero */
    errno = ENOENT;
121
122
123
    ASSERT_NO_THROW(cta::exception::Errnum::throwOnZero(1, "Context"));
    ASSERT_THROW(cta::exception::Errnum::throwOnZero(0, "Context"),
      cta::exception::Errnum); 
124
  }
125
}