27 #include <itkObject.h> 28 #include <itkObjectFactory.h> 31 #include <vtkDebugLeaks.h> 41 CPPUNIT_TEST_SUITE(mitkExceptionTestSuite);
43 MITK_TEST(TestExceptionConstructor_Success);
44 MITK_TEST(TestSpecializedExceptionConstructor_Success);
46 MITK_TEST(TestExceptionMessageStreamAddingString_Success);
47 MITK_TEST(TestExceptionMessageStreamAddingSingleChars_Success);
48 MITK_TEST(TestExceptionMessageStreamAddingObject_Success);
49 MITK_TEST(TestSpecializedExceptionMessageStreamAddingString);
50 MITK_TEST(TestExceptionMessageStreamThrowing_Success);
52 MITK_TEST(TestMitkThrowMacroThrowing_Success);
53 MITK_TEST(TestMitkThrowMacroMessage_Success);
54 MITK_TEST(TestMitkThrowMacroException_Success);
55 MITK_TEST(TestMitkThrowMacroSpezcializedException);
57 MITK_TEST(TestGetNumberOfRethrows_Success);
59 MITK_TEST(TestGetRethrowDataWithNegativNumber_Success);
60 MITK_TEST(TestGetRethrowDataWithNumberZero_Success);
61 MITK_TEST(TestGetRethrowDataWithNumberOne_Success);
65 MITK_TEST(TestFirstRethrowDataAreStoredProperly_Success);
66 MITK_TEST(TestSecondRethrowDataAreStoredProperly_Success);
70 CPPUNIT_TEST_SUITE_END();
73 bool m_ExceptionThrown;
75 std::string m_MessageText;
76 std::string m_Message;
86 itkFactorylessNewMacro(Self) itkCloneMacro(Self)
88 void throwExceptionManually()
95 void throwSpecializedExceptionManually()
99 throw SpecializedTestException(
"test.cpp", 155,
"",
"");
102 void throwExceptionManually(std::string message1, std::string message2)
106 throw mitk::Exception(
"testfile.cpp", 155, message1.c_str(),
"") << message2;
109 void throwExceptionWithThrowMacro() {
mitkThrow() <<
"TEST EXCEPION THROWING WITH mitkThrow()"; }
110 void throwExceptionWithThrowMacro(std::string message) {
mitkThrow() << message.c_str(); }
112 void throwSpecializedExceptionWithThrowMacro2(std::string message)
117 void reThrowExceptionWithReThrowMacro(std::string messageThrow, std::string messageReThrow)
121 throwExceptionWithThrowMacro(messageThrow);
129 void setUp()
override 131 m_ExceptionThrown =
false;
133 m_Message =
"invalid";
138 void tearDown()
override 140 m_ExceptionThrown =
false;
147 void TestExceptionConstructor_Success()
151 this->throwExceptionManually();
155 m_ExceptionThrown =
true;
157 CPPUNIT_ASSERT_MESSAGE(
"Testing constructor of mitkException", m_ExceptionThrown);
160 void TestSpecializedExceptionConstructor_Success()
164 this->throwSpecializedExceptionManually();
166 catch (
const SpecializedTestException &)
168 m_ExceptionThrown =
true;
170 CPPUNIT_ASSERT_MESSAGE(
"Testing constructor specialized exception (deriving from mitkException)",
177 void TestExceptionMessageStreamAddingString_Success()
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"));
184 void TestExceptionMessageStreamAddingSingleChars_Success()
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"));
192 void TestExceptionMessageStreamAddingObject_Success()
194 m_MyException.SetDescription(
"testmessage3");
195 m_MyException << m_MyException;
196 CPPUNIT_ASSERT_MESSAGE(
"Testing mitkException message stream (adding object)",
197 m_MyException.GetDescription() != std::string(
""));
200 void TestSpecializedExceptionMessageStreamAddingString()
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"));
209 void TestExceptionMessageStreamThrowing_Success()
211 std::string thrownMessage =
"";
214 this->throwExceptionManually(
"message1",
" and message2");
218 thrownMessage = e.GetDescription();
219 m_ExceptionThrown =
true;
221 CPPUNIT_ASSERT_MESSAGE(
"Testing throwing and streaming of mitk::Exception together.",
222 m_ExceptionThrown && (thrownMessage == std::string(
"message1 and message2")));
225 void TestMitkThrowMacroThrowing_Success()
230 this->throwExceptionWithThrowMacro();
234 m_ExceptionThrown =
true;
236 CPPUNIT_ASSERT_MESSAGE(
"Testing mitkThrow()", m_ExceptionThrown);
239 void TestMitkThrowMacroMessage_Success()
244 this->throwExceptionWithThrowMacro(
"test123");
248 m_ExceptionThrown =
true;
249 m_MessageText = e.GetDescription();
251 CPPUNIT_ASSERT_MESSAGE(
"Testing message test of mitkThrow()", (m_ExceptionThrown && (m_MessageText ==
"test123")));
254 void TestMitkThrowMacroException_Success()
259 this->throwSpecializedExceptionWithThrowMacro(
"test123");
263 m_ExceptionThrown =
true;
264 m_MessageText = e.GetDescription();
266 CPPUNIT_ASSERT_MESSAGE(
"Testing special exception with mitkThrow(mitk::Exception)",
267 m_ExceptionThrown && m_MessageText ==
"test123");
270 void TestMitkThrowMacroSpezcializedException()
275 this->throwSpecializedExceptionWithThrowMacro2(
"test123");
277 catch (
const SpecializedTestException &e)
279 m_ExceptionThrown =
true;
280 m_MessageText = e.GetDescription();
282 CPPUNIT_ASSERT_MESSAGE(
"Testing special exception with mitkThrow(mitk::SpecializedException)",
283 m_ExceptionThrown && m_MessageText ==
"test123");
288 void TestGetNumberOfRethrows_Success()
292 CPPUNIT_ASSERT_MESSAGE(
"Testing GetNumberOfRethrows() with empty rethrow information",
296 void TestGetRethrowDataWithNegativNumber_Success()
300 CPPUNIT_ASSERT_MESSAGE(
"Testing GetRethrowData() with invalid rethrow number (negative).",
301 ((m_File ==
"") && (m_Line == 0) && (m_Message ==
"")));
304 void TestGetRethrowDataWithNumberZero_Success()
308 CPPUNIT_ASSERT_MESSAGE(
"Testing GetRethrowData() with non-existing rethrow number (0).",
309 ((m_File ==
"") && (m_Line == 0) && (m_Message ==
"")));
312 void TestGetRethrowDataWithNumberOne_Success()
316 CPPUNIT_ASSERT_MESSAGE(
"Testing GetRethrowData() with non-existing rethrow number (1).",
317 ((m_File ==
"") && (m_Line == 0) && (m_Message ==
"")));
320 void TestAddRethrowData_Success()
326 CPPUNIT_ASSERT_MESSAGE(
"Testing adding of more rethrow data.", m_E.
GetNumberOfRethrows() == 2);
329 void TestFirstRethrowDataAreStoredProperly_Success()
334 CPPUNIT_ASSERT_MESSAGE(
"Testing stored information of first rethrow.",
335 ((m_File ==
"test2.cpp") && (m_Line == 10) && (m_Message ==
"Rethrow one")));
338 void TestSecondRethrowDataAreStoredProperly_Success()
343 CPPUNIT_ASSERT_MESSAGE(
"Testing stored information of second rethrow.",
344 ((m_File ==
"test3.cpp") && (m_Line == 15) && (m_Message ==
"Rethrow two")));
347 void TestRethrowMacro_Success()
352 this->reThrowExceptionWithReThrowMacro(
"Test original message.",
"Test rethrow message.");
356 m_Message = e.GetDescription();
357 m_ExceptionThrown =
true;
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.");
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)
int GetNumberOfRethrows()
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
#define mitkClassMacroItkParent(className, SuperClassName)
Test fixture for parameterized tests.
#define mitkExceptionClassMacro(ClassName, SuperClassName)
#define mitkThrowException(classname)