1 /* 2 $Id: AddMessageTest2.cpp 383 2002-07-22 09:28:00Z tylerdauwalder $ 3 4 This file implements the second test for the Haiku BMessageQueue code. 5 It tests the Add Message 2 use case. It does so by doing the following: 6 - creates 4 BMessages 7 - it adds them to the queue and a list 8 - it checks the queue against the list 9 - it adds the second one to the queue and the list again 10 - it checks the queue against the list again (expecting an error) 11 12 */ 13 14 15 #include "ThreadedTestCaller.h" 16 #include "AddMessageTest2.h" 17 #include <Message.h> 18 #include <MessageQueue.h> 19 #include "MessageQueue.h" 20 21 22 /* 23 * Method: AddMessageTest2::AddMessageTest2() 24 * Descr: This is the constructor for this class. 25 */ 26 27 AddMessageTest2(std::string name)28 AddMessageTest2::AddMessageTest2(std::string name) : 29 MessageQueueTestCase(name) 30 { 31 } 32 33 34 /* 35 * Method: AddMessageTest2::~AddMessageTest2() 36 * Descr: This is the destructor for this class. 37 */ 38 39 ~AddMessageTest2()40 AddMessageTest2::~AddMessageTest2() 41 { 42 } 43 44 45 /* 46 * Method: AddMessageTest2::PerformTest() 47 * Descr: This member function performs this test. It adds 48 * 4 messages to the message queue and confirms that 49 * the queue contains the right messages. Then it re-adds 50 * the second message again. It checks the queue against 51 * the list and expects to find a difference. Then, it 52 * checks each element on the queue one by one. 53 */ 54 55 PerformTest(void)56 void AddMessageTest2::PerformTest(void) 57 { 58 NextSubTest(); 59 BMessage *firstMessage = new BMessage(1); 60 BMessage *secondMessage = new BMessage(2); 61 BMessage *thirdMessage = new BMessage(3); 62 BMessage *fourthMessage = new BMessage(4); 63 64 AddMessage(firstMessage); 65 AddMessage(secondMessage); 66 AddMessage(thirdMessage); 67 AddMessage(fourthMessage); 68 69 NextSubTest(); 70 CheckQueueAgainstList(); 71 72 AddMessage(secondMessage); 73 74 // Re-adding a message to the message queue will result in the list 75 // not matching the message queue. Therefore, we expect an exception 76 // to be raised. An error occurs if it doesn't get raised. 77 bool exceptionRaised = false; 78 try { 79 CheckQueueAgainstList(); 80 } 81 catch (CppUnit::Exception e) { 82 exceptionRaised = true; 83 } 84 CPPUNIT_ASSERT(exceptionRaised); 85 86 NextSubTest(); 87 CPPUNIT_ASSERT(theMessageQueue->FindMessage((int32)0) == firstMessage); 88 CPPUNIT_ASSERT(theMessageQueue->FindMessage((int32)1) == secondMessage); 89 CPPUNIT_ASSERT(theMessageQueue->FindMessage((int32)2) == NULL); 90 CPPUNIT_ASSERT(theMessageQueue->FindMessage((int32)3) == NULL); 91 CPPUNIT_ASSERT(theMessageQueue->FindMessage((int32)4) == NULL); 92 } 93 94 95 /* 96 * Method: AddMessageTest2::suite() 97 * Descr: This static member function returns a test caller for performing 98 * all combinations of "AddMessageTest2". The test 99 * is created as a ThreadedTestCase (typedef'd as 100 * AddMessageTest2Caller) with only one thread. 101 */ 102 suite(void)103 Test *AddMessageTest2::suite(void) 104 { 105 typedef BThreadedTestCaller<AddMessageTest2> 106 AddMessageTest2Caller; 107 AddMessageTest2 *theTest = new AddMessageTest2(""); 108 AddMessageTest2Caller *testCaller = new AddMessageTest2Caller("BMessageQueue::Add Message Test #2", theTest); 109 testCaller->addThread("A", &AddMessageTest2::PerformTest); 110 111 return(testCaller); 112 } 113 114 115