Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 // Testing
14 #include "mitkTestFixture.h"
15 #include "mitkTestingMacros.h"
16 
17 // std includes
18 #include <string>
19 
20 // MITK includes
21 #include "mitkException.h"
22 #include "mitkExceptionMacro.h"
23 #include "mitkTestingMacros.h"
24 #include <mitkCommon.h>
25 
26 // ITK includes
27 #include <itkObject.h>
28 #include <itkObjectFactory.h>
29 
30 // VTK includes
31 #include <vtkDebugLeaks.h>
32 
33 class SpecializedTestException : public mitk::Exception
34 {
35 public:
36  mitkExceptionClassMacro(SpecializedTestException, mitk::Exception);
37 };
38 
39 class mitkExceptionTestSuite : public itk::Object, public mitk::TestFixture
40 {
41  CPPUNIT_TEST_SUITE(mitkExceptionTestSuite);
42 
43  MITK_TEST(TestExceptionConstructor_Success);
44  MITK_TEST(TestSpecializedExceptionConstructor_Success);
45 
46  MITK_TEST(TestExceptionMessageStreamAddingString_Success);
47  MITK_TEST(TestExceptionMessageStreamAddingSingleChars_Success);
48  MITK_TEST(TestExceptionMessageStreamAddingObject_Success);
49  MITK_TEST(TestSpecializedExceptionMessageStreamAddingString);
50  MITK_TEST(TestExceptionMessageStreamThrowing_Success);
51 
52  MITK_TEST(TestMitkThrowMacroThrowing_Success);
53  MITK_TEST(TestMitkThrowMacroMessage_Success);
54  MITK_TEST(TestMitkThrowMacroException_Success);
55  MITK_TEST(TestMitkThrowMacroSpezcializedException);
56 
57  MITK_TEST(TestGetNumberOfRethrows_Success);
58 
59  MITK_TEST(TestGetRethrowDataWithNegativNumber_Success);
60  MITK_TEST(TestGetRethrowDataWithNumberZero_Success);
61  MITK_TEST(TestGetRethrowDataWithNumberOne_Success);
62 
63  MITK_TEST(TestAddRethrowData_Success);
64 
65  MITK_TEST(TestFirstRethrowDataAreStoredProperly_Success);
66  MITK_TEST(TestSecondRethrowDataAreStoredProperly_Success);
67 
68  MITK_TEST(TestRethrowMacro_Success);
69 
70  CPPUNIT_TEST_SUITE_END();
71 
72 private:
73  bool m_ExceptionThrown;
74 
75  std::string m_MessageText;
76  std::string m_Message;
77  std::string m_File;
78 
79  int m_Line;
80 
81  mitk::Exception m_E = mitk::Exception("test.cpp", 155, "", "");
82  mitk::Exception m_MyException = mitk::Exception("testfile.cpp", 111, "testmessage");
83 
84 public:
85  mitkClassMacroItkParent(mitkExceptionTestSuite, itk::Object);
86  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
87 
88  void throwExceptionManually()
89  // this method is ONLY to test the constructor and no code example
90  // normally exceptions should only be thrown by using the exception macro!
91  {
92  throw mitk::Exception("test.cpp", 155, "", "");
93  }
94 
95  void throwSpecializedExceptionManually()
96  // this method is ONLY to test the constructor and no code example
97  // normally exceptions should only be thrown by using the exception macro!
98  {
99  throw SpecializedTestException("test.cpp", 155, "", "");
100  }
101 
102  void throwExceptionManually(std::string message1, std::string message2)
103  // this method is ONLY to test methods of mitk::Exception and no code example
104  // normally exceptions should only be thrown by using the exception macro!
105  {
106  throw mitk::Exception("testfile.cpp", 155, message1.c_str(), "") << message2;
107  }
108 
109  void throwExceptionWithThrowMacro() { mitkThrow() << "TEST EXCEPION THROWING WITH mitkThrow()"; }
110  void throwExceptionWithThrowMacro(std::string message) { mitkThrow() << message.c_str(); }
111  void throwSpecializedExceptionWithThrowMacro(std::string message) { mitkThrowException(mitk::Exception) << message; }
112  void throwSpecializedExceptionWithThrowMacro2(std::string message)
113  {
114  mitkThrowException(SpecializedTestException) << message;
115  }
116 
117  void reThrowExceptionWithReThrowMacro(std::string messageThrow, std::string messageReThrow)
118  {
119  try
120  {
121  throwExceptionWithThrowMacro(messageThrow);
122  }
123  catch (mitk::Exception &e)
124  {
125  mitkReThrow(e) << messageReThrow;
126  }
127  }
128 
129  void setUp() override
130  {
131  m_ExceptionThrown = false;
132  m_MessageText = "";
133  m_Message = "invalid";
134  m_File = "invalid";
135  m_Line = -1;
136  }
137 
138  void tearDown() override
139  {
140  m_ExceptionThrown = false;
141  m_MessageText = "";
142  m_Message = "";
143  m_File = "";
144  m_Line = 0;
145  }
146 
147  void TestExceptionConstructor_Success()
148  {
149  try
150  {
151  this->throwExceptionManually();
152  }
153  catch (const mitk::Exception &)
154  {
155  m_ExceptionThrown = true;
156  }
157  CPPUNIT_ASSERT_MESSAGE("Testing constructor of mitkException", m_ExceptionThrown);
158  }
159 
160  void TestSpecializedExceptionConstructor_Success()
161  {
162  try
163  {
164  this->throwSpecializedExceptionManually();
165  }
166  catch (const SpecializedTestException &)
167  {
168  m_ExceptionThrown = true;
169  }
170  CPPUNIT_ASSERT_MESSAGE("Testing constructor specialized exception (deriving from mitkException)",
171  m_ExceptionThrown);
172  }
173 
174  //##### this methods are ONLY to test the streaming operators of the exceptions and
175  //##### NO code example. Please do not instantiate exceptions by yourself in normal code!
176  //##### Normally exceptions should only be thrown by using the exception macro!
177  void TestExceptionMessageStreamAddingString_Success()
178  {
179  m_MyException << " and additional stream";
180  CPPUNIT_ASSERT_MESSAGE("Testing mitkException message stream (adding std::string)",
181  m_MyException.GetDescription() == std::string("testmessage and additional stream"));
182  }
183 
184  void TestExceptionMessageStreamAddingSingleChars_Success()
185  {
186  m_MyException.SetDescription("testmessage2");
187  m_MyException << ' ' << 'a' << 'n' << 'd' << ' ' << 'c' << 'h' << 'a' << 'r' << 's';
188  CPPUNIT_ASSERT_MESSAGE("Testing mitkException message stream (adding single chars)",
189  m_MyException.GetDescription() == std::string("testmessage2 and chars"));
190  }
191 
192  void TestExceptionMessageStreamAddingObject_Success()
193  {
194  m_MyException.SetDescription("testmessage3");
195  m_MyException << m_MyException; // adding the object itself makes no sense but should work
196  CPPUNIT_ASSERT_MESSAGE("Testing mitkException message stream (adding object)",
197  m_MyException.GetDescription() != std::string(""));
198  }
199 
200  void TestSpecializedExceptionMessageStreamAddingString()
201  {
202  SpecializedTestException mySpecializedException =
203  SpecializedTestException("testfile.cpp", 111, "testmessage", "test");
204  mySpecializedException << " and additional stream";
205  CPPUNIT_ASSERT_MESSAGE("Testing specialized exception message stream (adding std::string)",
206  mySpecializedException.GetDescription() == std::string("testmessage and additional stream"));
207  }
208 
209  void TestExceptionMessageStreamThrowing_Success()
210  {
211  std::string thrownMessage = "";
212  try
213  {
214  this->throwExceptionManually("message1", " and message2");
215  }
216  catch (const mitk::Exception &e)
217  {
218  thrownMessage = e.GetDescription();
219  m_ExceptionThrown = true;
220  }
221  CPPUNIT_ASSERT_MESSAGE("Testing throwing and streaming of mitk::Exception together.",
222  m_ExceptionThrown && (thrownMessage == std::string("message1 and message2")));
223  }
224 
225  void TestMitkThrowMacroThrowing_Success()
226  {
227  // case 1: test throwing
228  try
229  {
230  this->throwExceptionWithThrowMacro();
231  }
232  catch (const mitk::Exception &)
233  {
234  m_ExceptionThrown = true;
235  }
236  CPPUNIT_ASSERT_MESSAGE("Testing mitkThrow()", m_ExceptionThrown);
237  }
238 
239  void TestMitkThrowMacroMessage_Success()
240  {
241  // case 2: test message text
242  try
243  {
244  this->throwExceptionWithThrowMacro("test123");
245  }
246  catch (const mitk::Exception &e)
247  {
248  m_ExceptionThrown = true;
249  m_MessageText = e.GetDescription();
250  }
251  CPPUNIT_ASSERT_MESSAGE("Testing message test of mitkThrow()", (m_ExceptionThrown && (m_MessageText == "test123")));
252  }
253 
254  void TestMitkThrowMacroException_Success()
255  {
256  // case 3: specialized exception / command mitkThrow(mitk::Exception)
257  try
258  {
259  this->throwSpecializedExceptionWithThrowMacro("test123");
260  }
261  catch (const mitk::Exception &e)
262  {
263  m_ExceptionThrown = true;
264  m_MessageText = e.GetDescription();
265  }
266  CPPUNIT_ASSERT_MESSAGE("Testing special exception with mitkThrow(mitk::Exception)",
267  m_ExceptionThrown && m_MessageText == "test123");
268  }
269 
270  void TestMitkThrowMacroSpezcializedException()
271  {
272  // case 4: specialized exception / command mitkThrow(mitk::SpecializedException)
273  try
274  {
275  this->throwSpecializedExceptionWithThrowMacro2("test123");
276  }
277  catch (const SpecializedTestException &e)
278  {
279  m_ExceptionThrown = true;
280  m_MessageText = e.GetDescription();
281  }
282  CPPUNIT_ASSERT_MESSAGE("Testing special exception with mitkThrow(mitk::SpecializedException)",
283  m_ExceptionThrown && m_MessageText == "test123");
284  }
285 
286  //##### this methods are ONLY to test methods of mitk::Exception and no code example
287  //##### normally exceptions should only be instantiated and thrown by using the exception macros!
288  void TestGetNumberOfRethrows_Success()
289  {
290  // first: testing rethrow information methods, when no information is stored
291  // case 1.1: method GetNumberOfRethrows()
292  CPPUNIT_ASSERT_MESSAGE("Testing GetNumberOfRethrows() with empty rethrow information",
293  m_E.GetNumberOfRethrows() == 0);
294  }
295 
296  void TestGetRethrowDataWithNegativNumber_Success()
297  {
298  // case 1.2: GetRethrowData() with negative number
299  m_E.GetRethrowData(-1, m_File, m_Line, m_Message);
300  CPPUNIT_ASSERT_MESSAGE("Testing GetRethrowData() with invalid rethrow number (negative).",
301  ((m_File == "") && (m_Line == 0) && (m_Message == "")));
302  }
303 
304  void TestGetRethrowDataWithNumberZero_Success()
305  {
306  // case 1.3: GetRethrowData() with number 0
307  m_E.GetRethrowData(0, m_File, m_Line, m_Message);
308  CPPUNIT_ASSERT_MESSAGE("Testing GetRethrowData() with non-existing rethrow number (0).",
309  ((m_File == "") && (m_Line == 0) && (m_Message == "")));
310  }
311 
312  void TestGetRethrowDataWithNumberOne_Success()
313  {
314  // case 1.4: GetRethrowData() with number 1
315  m_E.GetRethrowData(1, m_File, m_Line, m_Message);
316  CPPUNIT_ASSERT_MESSAGE("Testing GetRethrowData() with non-existing rethrow number (1).",
317  ((m_File == "") && (m_Line == 0) && (m_Message == "")));
318  }
319 
320  void TestAddRethrowData_Success()
321  {
322  // second: add rethrow data
323  m_E.AddRethrowData("test2.cpp", 10, "Rethrow one");
324  CPPUNIT_ASSERT_MESSAGE("Testing adding of rethrow data.", m_E.GetNumberOfRethrows() == 1);
325  m_E.AddRethrowData("test3.cpp", 15, "Rethrow two");
326  CPPUNIT_ASSERT_MESSAGE("Testing adding of more rethrow data.", m_E.GetNumberOfRethrows() == 2);
327  }
328 
329  void TestFirstRethrowDataAreStoredProperly_Success()
330  {
331  // third: test if this rethrow data was stored properly
332  m_E.AddRethrowData("test2.cpp", 10, "Rethrow one");
333  m_E.GetRethrowData(0, m_File, m_Line, m_Message);
334  CPPUNIT_ASSERT_MESSAGE("Testing stored information of first rethrow.",
335  ((m_File == "test2.cpp") && (m_Line == 10) && (m_Message == "Rethrow one")));
336  }
337 
338  void TestSecondRethrowDataAreStoredProperly_Success()
339  {
340  m_E.AddRethrowData("test2.cpp", 10, "Rethrow one");
341  m_E.AddRethrowData("test3.cpp", 15, "Rethrow two");
342  m_E.GetRethrowData(1, m_File, m_Line, m_Message);
343  CPPUNIT_ASSERT_MESSAGE("Testing stored information of second rethrow.",
344  ((m_File == "test3.cpp") && (m_Line == 15) && (m_Message == "Rethrow two")));
345  }
346 
347  void TestRethrowMacro_Success()
348  {
349  // case 1: test throwing
350  try
351  {
352  this->reThrowExceptionWithReThrowMacro("Test original message.", "Test rethrow message.");
353  }
354  catch (const mitk::Exception &e)
355  {
356  m_Message = e.GetDescription();
357  m_ExceptionThrown = true;
358  }
359  CPPUNIT_ASSERT_MESSAGE("Testing mitkReThrow()", m_ExceptionThrown);
360  CPPUNIT_ASSERT_MESSAGE("Testing message/descriprion after rethrow.",
361  m_Message == "Test original message.Test rethrow message.");
362  }
363 };
364 
365 MITK_TEST_SUITE_REGISTRATION(mitkException)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
void AddRethrowData(const char *file, unsigned int lineNumber, const char *message)
Adds rethrow data to this exception.
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
#define mitkReThrow(mitkexception)
void GetRethrowData(int rethrowNumber, std::string &file, int &line, std::string &message)
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:49
#define mitkThrow()
Test fixture for parameterized tests.
#define mitkExceptionClassMacro(ClassName, SuperClassName)
#define mitkThrowException(classname)