Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkExceptionTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkExceptionMacro.h"
18 #include "mitkTestingMacros.h"
19 #include <itkObject.h>
20 #include <itkObjectFactory.h>
21 #include <mitkCommon.h>
22 
23 class SpecializedTestException : public mitk::Exception
24 {
25 public:
26  mitkExceptionClassMacro(SpecializedTestException, mitk::Exception);
27 };
28 
29 class ExceptionTestClass : public itk::Object
30 {
31 public:
32  mitkClassMacroItkParent(ExceptionTestClass, itk::Object);
33  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
34 
35  void throwExceptionManually()
36  // this method is ONLY to test the constructor and no code example
37  // normally exceptions should only be thrown by using the exception macro!
38  {
39  throw mitk::Exception("test.cpp", 155, "", "");
40  }
41 
42  void throwSpecializedExceptionManually()
43  // this method is ONLY to test the constructor and no code example
44  // normally exceptions should only be thrown by using the exception macro!
45  {
46  throw SpecializedTestException("test.cpp", 155, "", "");
47  }
48 
49  void throwExceptionManually(std::string message1, std::string message2)
50  // this method is ONLY to test methods of mitk::Exception and no code example
51  // normally exceptions should only be thrown by using the exception macro!
52  {
53  throw mitk::Exception("testfile.cpp", 155, message1.c_str(), "") << message2;
54  }
55 
56  void throwExceptionWithThrowMacro() { mitkThrow() << "TEST EXCEPION THROWING WITH mitkThrow()"; }
57  void throwExceptionWithThrowMacro(std::string message) { mitkThrow() << message.c_str(); }
58  void throwSpecializedExceptionWithThrowMacro(std::string message) { mitkThrowException(mitk::Exception) << message; }
59  void throwSpecializedExceptionWithThrowMacro2(std::string message)
60  {
61  mitkThrowException(SpecializedTestException) << message;
62  }
63 
64  void reThrowExceptionWithReThrowMacro(std::string messageThrow, std::string messageReThrow)
65  {
66  try
67  {
68  throwExceptionWithThrowMacro(messageThrow);
69  }
70  catch (mitk::Exception &e)
71  {
72  mitkReThrow(e) << messageReThrow;
73  }
74  }
75 
76  static void TestExceptionConstructor()
77  {
78  bool exceptionThrown = false;
79  ExceptionTestClass::Pointer myExceptionTestObject = ExceptionTestClass::New();
80  try
81  {
82  myExceptionTestObject->throwExceptionManually();
83  }
84  catch (mitk::Exception)
85  {
86  exceptionThrown = true;
87  }
88  MITK_TEST_CONDITION_REQUIRED(exceptionThrown, "Testing constructor of mitkException");
89 
90  exceptionThrown = false;
91  try
92  {
93  myExceptionTestObject->throwSpecializedExceptionManually();
94  }
95  catch (SpecializedTestException)
96  {
97  exceptionThrown = true;
98  }
99  MITK_TEST_CONDITION_REQUIRED(exceptionThrown,
100  "Testing constructor specialized exception (deriving from mitkException)");
101  }
102 
103  static void TestExceptionMessageStream()
104  {
105  //##### this method is ONLY to test the streaming operators of the exceptions and
106  //##### NO code example. Please do not instantiate exceptions by yourself in normal code!
107  //##### Normally exceptions should only be thrown by using the exception macro!
108  mitk::Exception myException = mitk::Exception("testfile.cpp", 111, "testmessage");
109  myException << " and additional stream";
110  MITK_TEST_CONDITION_REQUIRED(myException.GetDescription() == std::string("testmessage and additional stream"),
111  "Testing mitkException message stream (adding std::string)");
112 
113  myException.SetDescription("testmessage2");
114  myException << ' ' << 'a' << 'n' << 'd' << ' ' << 'c' << 'h' << 'a' << 'r' << 's';
115  MITK_TEST_CONDITION_REQUIRED(myException.GetDescription() == std::string("testmessage2 and chars"),
116  "Testing mitkException message stream (adding single chars)");
117 
118  myException.SetDescription("testmessage3");
119  myException << myException; // adding the object itself makes no sense but should work
120  MITK_TEST_CONDITION_REQUIRED(myException.GetDescription() != std::string(""),
121  "Testing mitkException message stream (adding object)");
122 
123  SpecializedTestException mySpecializedException =
124  SpecializedTestException("testfile.cpp", 111, "testmessage", "test");
125  mySpecializedException << " and additional stream";
127  mySpecializedException.GetDescription() == std::string("testmessage and additional stream"),
128  "Testing specialized exception message stream (adding std::string)");
129  }
130 
131  static void TestExceptionMessageStreamThrowing()
132  {
133  bool exceptionThrown = false;
134  ExceptionTestClass::Pointer myExceptionTestObject = ExceptionTestClass::New();
135  std::string thrownMessage = "";
136  try
137  {
138  myExceptionTestObject->throwExceptionManually("message1", " and message2");
139  }
140  catch (mitk::Exception &e)
141  {
142  thrownMessage = e.GetDescription();
143  exceptionThrown = true;
144  }
145  MITK_TEST_CONDITION_REQUIRED(exceptionThrown && (thrownMessage == std::string("message1 and message2")),
146  "Testing throwing and streaming of mitk::Exception together.")
147  }
148 
149  static void TestMitkThrowMacro()
150  {
151  bool exceptionThrown = false;
152  ExceptionTestClass::Pointer myExceptionTestObject = ExceptionTestClass::New();
153 
154  // case 1: test throwing
155 
156  try
157  {
158  myExceptionTestObject->throwExceptionWithThrowMacro();
159  }
160  catch (mitk::Exception)
161  {
162  exceptionThrown = true;
163  }
164  MITK_TEST_CONDITION_REQUIRED(exceptionThrown, "Testing mitkThrow()");
165 
166  // case 2: test message text
167 
168  exceptionThrown = false;
169  std::string messageText = "";
170 
171  try
172  {
173  myExceptionTestObject->throwExceptionWithThrowMacro("test123");
174  }
175  catch (mitk::Exception &e)
176  {
177  exceptionThrown = true;
178  messageText = e.GetDescription();
179  }
180  MITK_TEST_CONDITION_REQUIRED((exceptionThrown && (messageText == "test123")),
181  "Testing message test of mitkThrow()");
182 
183  // case 3: specialized exception / command mitkThrow(mitk::Exception)
184 
185  exceptionThrown = false;
186  messageText = "";
187 
188  try
189  {
190  myExceptionTestObject->throwSpecializedExceptionWithThrowMacro("test123");
191  }
192  catch (mitk::Exception &e)
193  {
194  exceptionThrown = true;
195  messageText = e.GetDescription();
196  }
197  MITK_TEST_CONDITION_REQUIRED(exceptionThrown && messageText == "test123",
198  "Testing special exception with mitkThrow(mitk::Exception)");
199 
200  // case 4: specialized exception / command mitkThrow(mitk::SpecializedException)
201 
202  exceptionThrown = false;
203  messageText = "";
204 
205  try
206  {
207  myExceptionTestObject->throwSpecializedExceptionWithThrowMacro2("test123");
208  }
209  catch (SpecializedTestException &e)
210  {
211  exceptionThrown = true;
212  messageText = e.GetDescription();
213  }
214  MITK_TEST_CONDITION_REQUIRED(exceptionThrown && messageText == "test123",
215  "Testing special exception with mitkThrow(mitk::SpecializedException)");
216  }
217 
218  static void TestRethrowInformation()
219  // this method is ONLY to test methods of mitk::Exception and no code example
220  // normally exceptions should only be instantiated and thrown by using the exception macros!
221  {
222  // first: testing rethrow information methods, when no information is stored
223 
224  // case 1.1: method GetNumberOfRethrows()
225  mitk::Exception e = mitk::Exception("test.cpp", 155, "", "");
227  "Testing GetNumberOfRethrows() with empty rethrow information");
228 
229  // case 1.2: GetRethrowData() with negative number
230  {
231  std::string file = "invalid";
232  int line = -1;
233  std::string message = "invalid";
234  e.GetRethrowData(-1, file, line, message);
235  MITK_TEST_CONDITION_REQUIRED(((file == "") && (line == 0) && (message == "")),
236  "Testing GetRethrowData() with invalid rethrow number (negative).");
237  }
238 
239  // case 1.3: GetRethrowData() with number 0
240  {
241  std::string file = "invalid";
242  int line = -1;
243  std::string message = "invalid";
244  e.GetRethrowData(0, file, line, message);
245  MITK_TEST_CONDITION_REQUIRED(((file == "") && (line == 0) && (message == "")),
246  "Testing GetRethrowData() with non-existing rethrow number (0).");
247  }
248 
249  // case 1.4: GetRethrowData() with number 1
250  {
251  std::string file = "invalid";
252  int line = -1;
253  std::string message = "invalid";
254  e.GetRethrowData(1, file, line, message);
255  MITK_TEST_CONDITION_REQUIRED(((file == "") && (line == 0) && (message == "")),
256  "Testing GetRethrowData() with non-existing rethrow number (1).");
257  }
258 
259  // second: add rethrow data
260  e.AddRethrowData("test2.cpp", 10, "Rethrow one");
261  MITK_TEST_CONDITION_REQUIRED(e.GetNumberOfRethrows() == 1, "Testing adding of rethrow data.");
262  e.AddRethrowData("test3.cpp", 15, "Rethrow two");
263  MITK_TEST_CONDITION_REQUIRED(e.GetNumberOfRethrows() == 2, "Testing adding of more rethrow data.");
264 
265  // third: test if this rethrow data was stored properly
266  {
267  std::string file = "invalid";
268  int line = -1;
269  std::string message = "invalid";
270  e.GetRethrowData(0, file, line, message);
271  MITK_TEST_CONDITION_REQUIRED(((file == "test2.cpp") && (line == 10) && (message == "Rethrow one")),
272  "Testing stored information of first rethrow.");
273  }
274 
275  {
276  std::string file = "invalid";
277  int line = -1;
278  std::string message = "invalid";
279  e.GetRethrowData(1, file, line, message);
280  MITK_TEST_CONDITION_REQUIRED(((file == "test3.cpp") && (line == 15) && (message == "Rethrow two")),
281  "Testing stored information of second rethrow.");
282  }
283  }
284 
285  static void TestRethrowMacro()
286  {
287  bool exceptionThrown = false;
288  std::string message = "";
289  ExceptionTestClass::Pointer myExceptionTestObject = ExceptionTestClass::New();
290 
291  // case 1: test throwing
292 
293  try
294  {
295  myExceptionTestObject->reThrowExceptionWithReThrowMacro("Test original message.", "Test rethrow message.");
296  }
297  catch (mitk::Exception &e)
298  {
299  message = e.GetDescription();
300  exceptionThrown = true;
301  }
302  MITK_TEST_CONDITION_REQUIRED(exceptionThrown, "Testing mitkReThrow()");
303  MITK_TEST_CONDITION_REQUIRED(message == "Test original message.Test rethrow message.",
304  "Testing message/descriprion after rethrow.")
305  }
306 };
307 
308 int mitkExceptionTest(int /*argc*/, char * /*argv*/ [])
309 {
310  MITK_TEST_BEGIN("MITKException");
311  ExceptionTestClass::TestExceptionConstructor();
312  ExceptionTestClass::TestExceptionMessageStream();
313  ExceptionTestClass::TestExceptionMessageStreamThrowing();
314  ExceptionTestClass::TestMitkThrowMacro();
315  ExceptionTestClass::TestRethrowInformation();
316  ExceptionTestClass::TestRethrowMacro();
317  MITK_TEST_END();
318 }
static char * line
Definition: svm.cpp:2884
itk::SmartPointer< Self > Pointer
void AddRethrowData(const char *file, unsigned int lineNumber, const char *message)
Adds rethrow data to this exception.
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define mitkReThrow(mitkexception)
void GetRethrowData(int rethrowNumber, std::string &file, int &line, std::string &message)
int mitkExceptionTest(int, char *[])
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:53
#define mitkThrow()
#define mitkExceptionClassMacro(ClassName, SuperClassName)
#define mitkThrowException(classname)
and MITK_TEST_END()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.