| 1 | /*
 | 
|---|
| 2 |  * Project: CodePatterns
 | 
|---|
| 3 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 4 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 5 |  */
 | 
|---|
| 6 | 
 | 
|---|
| 7 | /*
 | 
|---|
| 8 |  * GraveyardUnitTest.cpp
 | 
|---|
| 9 |  *
 | 
|---|
| 10 |  *  Created on: Sep 05, 2013
 | 
|---|
| 11 |  *      Author: heber
 | 
|---|
| 12 |  */
 | 
|---|
| 13 | 
 | 
|---|
| 14 | // include config.h
 | 
|---|
| 15 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 16 | #include <config.h>
 | 
|---|
| 17 | #endif
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 20 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 21 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 22 | #include <set>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include <iostream>
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include "stubs/ObserverStub.hpp"
 | 
|---|
| 29 | #include "CodePatterns/Observer/Graveyard.hpp"
 | 
|---|
| 30 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| 31 | #include "CodePatterns/Observer/ObserverLog.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "GraveyardUnitTest.hpp"
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 36 | #include "UnitTestMain.hpp"
 | 
|---|
| 37 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 38 | 
 | 
|---|
| 39 | // Registers the fixture into the 'registry'
 | 
|---|
| 40 | CPPUNIT_TEST_SUITE_REGISTRATION( GraveyardUnitTest );
 | 
|---|
| 41 | 
 | 
|---|
| 42 | /******************* Test stubs ************************/
 | 
|---|
| 43 | 
 | 
|---|
| 44 | 
 | 
|---|
| 45 | /******************* actuall tests ***************/
 | 
|---|
| 46 | 
 | 
|---|
| 47 | void GraveyardUnitTest::setUp() {
 | 
|---|
| 48 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| 49 | #ifdef LOG_OBSERVER
 | 
|---|
| 50 |   ObserverLog::getInstance().enableLogging();
 | 
|---|
| 51 | #endif
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   graveyard = new Graveyard;
 | 
|---|
| 54 |   observer1 = new UpdateCountObserver();
 | 
|---|
| 55 |   observer2 = new UpdateCountObserver();
 | 
|---|
| 56 |   observer3 = new UpdateCountObserver();
 | 
|---|
| 57 |   observer4 = new UpdateCountObserver();
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | void GraveyardUnitTest::tearDown() {
 | 
|---|
| 61 |   delete observer1;
 | 
|---|
| 62 |   delete observer2;
 | 
|---|
| 63 |   delete observer3;
 | 
|---|
| 64 |   delete observer4;
 | 
|---|
| 65 | #ifdef LOG_OBSERVER
 | 
|---|
| 66 |   ObserverLog::getInstance().disableLogging();
 | 
|---|
| 67 | #endif
 | 
|---|
| 68 |   delete graveyard;
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | void GraveyardUnitTest::Zombie_soloTest()
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   // test with unobserved Observable
 | 
|---|
| 74 |   Observable *simpleObservable1 = new SimpleObservable();
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   graveyard->turnZombie( simpleObservable1);
 | 
|---|
| 77 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 0);
 | 
|---|
| 78 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | void GraveyardUnitTest::Zombie_oneTest()
 | 
|---|
| 81 | {
 | 
|---|
| 82 |   // test with one Observer
 | 
|---|
| 83 |   SimpleObservable *simpleObservable1 = new SimpleObservable();
 | 
|---|
| 84 |   Observable *_observable = simpleObservable1;
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   simpleObservable1->signOn(observer1);
 | 
|---|
| 87 |   graveyard->turnZombie(_observable);
 | 
|---|
| 88 |   simpleObservable1->signOff(observer1);
 | 
|---|
| 89 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 0);
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | void GraveyardUnitTest::Zombie_manyTest()
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   // test with some more Observers
 | 
|---|
| 95 |   SimpleObservable *simpleObservable1 = new SimpleObservable();
 | 
|---|
| 96 |   Observable *_observable = simpleObservable1;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   // create some observer noise
 | 
|---|
| 99 |   simpleObservable1->signOn(observer1);
 | 
|---|
| 100 |   simpleObservable1->signOn(observer2);
 | 
|---|
| 101 |   simpleObservable1->signOn(observer3);
 | 
|---|
| 102 |   simpleObservable1->signOn(observer4);
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   // turn zombie
 | 
|---|
| 105 |   graveyard->turnZombie(_observable);
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   // remove the noise again (in mixed order)
 | 
|---|
| 108 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 109 |   CPPUNIT_ASSERT_EQUAL( (size_t)4, simpleObservable1->getNumberOfObservers());
 | 
|---|
| 110 |   simpleObservable1->signOff(observer3);
 | 
|---|
| 111 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 112 |   CPPUNIT_ASSERT_EQUAL( (size_t)3, simpleObservable1->getNumberOfObservers());
 | 
|---|
| 113 |   simpleObservable1->signOff(observer2);
 | 
|---|
| 114 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 115 |   CPPUNIT_ASSERT_EQUAL( (size_t)2, simpleObservable1->getNumberOfObservers());
 | 
|---|
| 116 |   simpleObservable1->signOff(observer4);
 | 
|---|
| 117 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 118 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, simpleObservable1->getNumberOfObservers());
 | 
|---|
| 119 |   simpleObservable1->signOff(observer1);
 | 
|---|
| 120 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 0);
 | 
|---|
| 121 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, simpleObservable1->getNumberOfObservers());
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | void GraveyardUnitTest::Zombie_notificationsTest()
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   // test with an observable with notifications
 | 
|---|
| 127 |   NotificationObservable *notificationObservable = new NotificationObservable();
 | 
|---|
| 128 |   Observable *_observable = notificationObservable;
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   // create some observer noise
 | 
|---|
| 131 |   notificationObservable->signOn(observer1);
 | 
|---|
| 132 |   notificationObservable->signOn(observer2);
 | 
|---|
| 133 |   notificationObservable->signOn(observer2, NotificationObservable::Operation1Notify);
 | 
|---|
| 134 |   notificationObservable->signOn(observer3, NotificationObservable::Operation1Notify);
 | 
|---|
| 135 |   notificationObservable->signOn(observer4, NotificationObservable::Operation2Notify);
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   // turn zombie
 | 
|---|
| 138 |   graveyard->turnZombie(_observable);
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   // remove the noise again (in mixed order)
 | 
|---|
| 141 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 142 |   CPPUNIT_ASSERT_EQUAL( (size_t)5, notificationObservable->getNumberOfObservers());
 | 
|---|
| 143 |   notificationObservable->signOff(observer3, NotificationObservable::Operation1Notify);
 | 
|---|
| 144 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 145 |   CPPUNIT_ASSERT_EQUAL( (size_t)4, notificationObservable->getNumberOfObservers());
 | 
|---|
| 146 |   notificationObservable->signOff(observer2, NotificationObservable::Operation1Notify);
 | 
|---|
| 147 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 148 |   CPPUNIT_ASSERT_EQUAL( (size_t)3, notificationObservable->getNumberOfObservers());
 | 
|---|
| 149 |   notificationObservable->signOff(observer2);
 | 
|---|
| 150 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 151 |   CPPUNIT_ASSERT_EQUAL( (size_t)2, notificationObservable->getNumberOfObservers());
 | 
|---|
| 152 |   notificationObservable->signOff(observer4, NotificationObservable::Operation2Notify);
 | 
|---|
| 153 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 1);
 | 
|---|
| 154 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, notificationObservable->getNumberOfObservers());
 | 
|---|
| 155 |   notificationObservable->signOff(observer1);
 | 
|---|
| 156 |   CPPUNIT_ASSERT( graveyard->graveyard.size() == 0);
 | 
|---|
| 157 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, notificationObservable->getNumberOfObservers());
 | 
|---|
| 158 | }
 | 
|---|