| [63c1f6] | 1 | /*
 | 
|---|
 | 2 |  * ObserverTest.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jan 19, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #include "ObserverTest.hpp"
 | 
|---|
 | 9 | 
 | 
|---|
 | 10 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 12 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| [bd58fb] | 13 | #include <set>
 | 
|---|
| [63c1f6] | 14 | 
 | 
|---|
 | 15 | #include "Patterns/Observer.hpp"
 | 
|---|
| [bd58fb] | 16 | #include "Patterns/ObservedIterator.hpp"
 | 
|---|
| [4fb5a3] | 17 | #include "Helpers/Assert.hpp"
 | 
|---|
| [63c1f6] | 18 | 
 | 
|---|
 | 19 | #include <iostream>
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | using namespace std;
 | 
|---|
 | 22 | 
 | 
|---|
| [9b6b2f] | 23 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 24 | #include "UnitTestMain.hpp"
 | 
|---|
 | 25 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 26 | 
 | 
|---|
| [63c1f6] | 27 | // Registers the fixture into the 'registry'
 | 
|---|
 | 28 | CPPUNIT_TEST_SUITE_REGISTRATION( ObserverTest );
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | /******************* Test stubs ************************/
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | class UpdateCountObserver : public Observer {
 | 
|---|
 | 33 | public:
 | 
|---|
 | 34 |   UpdateCountObserver() :
 | 
|---|
| [cd5047] | 35 |     Observer("UpdateCountObserver"),
 | 
|---|
| [63c1f6] | 36 |     updates(0)
 | 
|---|
 | 37 |   {};
 | 
|---|
 | 38 |   void update(Observable *publisher){
 | 
|---|
 | 39 |     updates++;
 | 
|---|
 | 40 |   }
 | 
|---|
 | 41 |   void subjectKilled(Observable *publisher) {
 | 
|---|
 | 42 |   }
 | 
|---|
 | 43 |   int updates;
 | 
|---|
 | 44 | };
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | class SimpleObservable : public Observable {
 | 
|---|
 | 47 | public:
 | 
|---|
| [cd5047] | 48 |   SimpleObservable() :
 | 
|---|
 | 49 |     Observable("SimpleObservable")
 | 
|---|
 | 50 |   {}
 | 
|---|
 | 51 | 
 | 
|---|
| [63c1f6] | 52 |   void changeMethod() {
 | 
|---|
| [317df8] | 53 |     OBSERVE;
 | 
|---|
| [5e5283] | 54 |     int i = 0;
 | 
|---|
| [63c1f6] | 55 |     i++;
 | 
|---|
 | 56 |   }
 | 
|---|
 | 57 | };
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | class CallObservable : public Observable {
 | 
|---|
 | 60 | public:
 | 
|---|
| [cd5047] | 61 |   CallObservable() :
 | 
|---|
 | 62 |     Observable("CallObservable")
 | 
|---|
 | 63 |   {}
 | 
|---|
 | 64 | 
 | 
|---|
| [63c1f6] | 65 |   void changeMethod1() {
 | 
|---|
| [317df8] | 66 |     OBSERVE;
 | 
|---|
| [5e5283] | 67 |     int i = 0;
 | 
|---|
| [63c1f6] | 68 |     i++;
 | 
|---|
 | 69 |   }
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 |   void changeMethod2() {
 | 
|---|
| [317df8] | 72 |     OBSERVE;
 | 
|---|
| [5e5283] | 73 |     int i = 0;
 | 
|---|
| [63c1f6] | 74 |     i++;
 | 
|---|
 | 75 |     changeMethod1();
 | 
|---|
 | 76 |   }
 | 
|---|
 | 77 | };
 | 
|---|
 | 78 | 
 | 
|---|
| [4fb5a3] | 79 | class BlockObservable : public Observable {
 | 
|---|
 | 80 | public:
 | 
|---|
| [cd5047] | 81 |   BlockObservable() :
 | 
|---|
 | 82 |     Observable("BlockObservable")
 | 
|---|
 | 83 |   {}
 | 
|---|
 | 84 | 
 | 
|---|
| [4fb5a3] | 85 |   void changeMethod1(){
 | 
|---|
 | 86 |     OBSERVE;
 | 
|---|
 | 87 |     // test if we report correctly as blocked
 | 
|---|
 | 88 |     CPPUNIT_ASSERT(isBlocked());
 | 
|---|
 | 89 |   }
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |   void changeMethod2(){
 | 
|---|
 | 92 |     OBSERVE;
 | 
|---|
 | 93 |     internalMethod1();
 | 
|---|
 | 94 |     internalMethod2();
 | 
|---|
 | 95 |   }
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 |   void internalMethod1(){
 | 
|---|
 | 98 |     // we did not block, but our caller did...
 | 
|---|
 | 99 |     // see if this is found
 | 
|---|
 | 100 |     CPPUNIT_ASSERT(isBlocked());
 | 
|---|
 | 101 |   }
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 |   void internalMethod2(){
 | 
|---|
 | 104 |     OBSERVE;
 | 
|---|
 | 105 |     // Both this method and the caller do block
 | 
|---|
 | 106 |     // Does the reporting still work as expected?
 | 
|---|
 | 107 |     CPPUNIT_ASSERT(isBlocked());
 | 
|---|
 | 108 |   }
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 |   void noChangeMethod(){
 | 
|---|
 | 111 |     // No Block introduced here
 | 
|---|
 | 112 |     // reported correctely?
 | 
|---|
 | 113 |     CPPUNIT_ASSERT(!isBlocked());
 | 
|---|
 | 114 |   }
 | 
|---|
 | 115 | };
 | 
|---|
 | 116 | 
 | 
|---|
| [63c1f6] | 117 | class SuperObservable : public Observable {
 | 
|---|
 | 118 | public:
 | 
|---|
| [cd5047] | 119 |   SuperObservable():
 | 
|---|
 | 120 |     Observable("SuperObservable")
 | 
|---|
 | 121 |   {
 | 
|---|
| [63c1f6] | 122 |     subObservable = new SimpleObservable();
 | 
|---|
 | 123 |     subObservable->signOn(this);
 | 
|---|
 | 124 |   }
 | 
|---|
 | 125 |   ~SuperObservable(){
 | 
|---|
 | 126 |     delete subObservable;
 | 
|---|
 | 127 |   }
 | 
|---|
 | 128 |   void changeMethod() {
 | 
|---|
| [317df8] | 129 |     OBSERVE;
 | 
|---|
| [5e5283] | 130 |     int i = 0;
 | 
|---|
| [63c1f6] | 131 |     i++;
 | 
|---|
 | 132 |     subObservable->changeMethod();
 | 
|---|
 | 133 |   }
 | 
|---|
 | 134 |   SimpleObservable *subObservable;
 | 
|---|
 | 135 | };
 | 
|---|
 | 136 | 
 | 
|---|
| [ccacba] | 137 | class NotificationObservable : public Observable {
 | 
|---|
 | 138 | public:
 | 
|---|
 | 139 |   NotificationObservable() :
 | 
|---|
| [cd5047] | 140 |     Observable("NotificationObservable"),
 | 
|---|
 | 141 |     notification1(new Notification(this)),
 | 
|---|
 | 142 |     notification2(new Notification(this))
 | 
|---|
| [ccacba] | 143 |   {}
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 |   ~NotificationObservable(){
 | 
|---|
 | 146 |     delete notification1;
 | 
|---|
 | 147 |     delete notification2;
 | 
|---|
 | 148 |   }
 | 
|---|
 | 149 | 
 | 
|---|
 | 150 |   void operation1(){
 | 
|---|
 | 151 |     OBSERVE;
 | 
|---|
 | 152 |     NOTIFY(notification1);
 | 
|---|
 | 153 |   }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 |   void operation2(){
 | 
|---|
 | 156 |     OBSERVE;
 | 
|---|
 | 157 |     NOTIFY(notification2);
 | 
|---|
 | 158 |   }
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 |   Notification_ptr notification1;
 | 
|---|
 | 161 |   Notification_ptr notification2;
 | 
|---|
 | 162 | };
 | 
|---|
 | 163 | 
 | 
|---|
 | 164 | class NotificationObserver : public Observer {
 | 
|---|
 | 165 | public:
 | 
|---|
 | 166 |   NotificationObserver(Notification_ptr notification) :
 | 
|---|
| [cd5047] | 167 |     Observer("NotificationObserver"),
 | 
|---|
| [ccacba] | 168 |     requestedNotification(notification),
 | 
|---|
 | 169 |     wasNotified(false)
 | 
|---|
 | 170 |   {}
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 |   void update(Observable*){}
 | 
|---|
 | 173 |   void subjectKilled(Observable*){}
 | 
|---|
 | 174 |   void recieveNotification(Observable *publisher, Notification_ptr notification){
 | 
|---|
| [033a05] | 175 |     ASSERT(requestedNotification==notification,"Notification received that was not requested");
 | 
|---|
| [ccacba] | 176 |     wasNotified = true;
 | 
|---|
 | 177 |   }
 | 
|---|
 | 178 | 
 | 
|---|
 | 179 |   Notification_ptr requestedNotification;
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |   bool wasNotified;
 | 
|---|
 | 182 | };
 | 
|---|
 | 183 | 
 | 
|---|
| [8774c5] | 184 | class ObservableSet : public Observable {
 | 
|---|
| [bd58fb] | 185 | public:
 | 
|---|
 | 186 |   typedef std::set<SimpleObservable*> set;
 | 
|---|
 | 187 |   typedef ObservedIterator<set> iterator;
 | 
|---|
| [a7b761b] | 188 |   typedef set::const_iterator const_iterator;
 | 
|---|
| [bd58fb] | 189 | 
 | 
|---|
| [8774c5] | 190 |   ObservableSet(int _num) :
 | 
|---|
| [cd5047] | 191 |     Observable("ObservableCollection"),
 | 
|---|
 | 192 |     num(_num)
 | 
|---|
| [a7b761b] | 193 |   {
 | 
|---|
| [bd58fb] | 194 |     for(int i=0; i<num; ++i){
 | 
|---|
 | 195 |       SimpleObservable *content = new SimpleObservable();
 | 
|---|
 | 196 |       content->signOn(this);
 | 
|---|
 | 197 |       theSet.insert(content);
 | 
|---|
 | 198 |     }
 | 
|---|
 | 199 |   }
 | 
|---|
 | 200 | 
 | 
|---|
| [8774c5] | 201 |   ~ObservableSet(){
 | 
|---|
| [bd58fb] | 202 |     set::iterator iter;
 | 
|---|
 | 203 |     for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
 | 
|---|
 | 204 |       delete (*iter);
 | 
|---|
 | 205 |     }
 | 
|---|
 | 206 |   }
 | 
|---|
 | 207 | 
 | 
|---|
 | 208 |   iterator begin(){
 | 
|---|
 | 209 |     return iterator(theSet.begin(),this);
 | 
|---|
 | 210 |   }
 | 
|---|
 | 211 | 
 | 
|---|
 | 212 |   iterator end(){
 | 
|---|
 | 213 |     return iterator(theSet.end(),this);
 | 
|---|
 | 214 |   }
 | 
|---|
 | 215 | 
 | 
|---|
| [a7b761b] | 216 |   const int num;
 | 
|---|
 | 217 | 
 | 
|---|
| [bd58fb] | 218 | private:
 | 
|---|
 | 219 |   set theSet;
 | 
|---|
 | 220 | };
 | 
|---|
 | 221 | 
 | 
|---|
| [8774c5] | 222 | class ObservableMap : public Observable {
 | 
|---|
 | 223 | public:
 | 
|---|
 | 224 |   typedef std::map<int,SimpleObservable*> set;
 | 
|---|
 | 225 |   typedef ObservedIterator<set> iterator;
 | 
|---|
 | 226 |   typedef set::const_iterator const_iterator;
 | 
|---|
 | 227 | 
 | 
|---|
 | 228 |   ObservableMap(int _num) :
 | 
|---|
 | 229 |     Observable("ObservableCollection"),
 | 
|---|
 | 230 |     num(_num)
 | 
|---|
 | 231 |   {
 | 
|---|
 | 232 |     for(int i=0; i<num; ++i){
 | 
|---|
 | 233 |       SimpleObservable *content = new SimpleObservable();
 | 
|---|
 | 234 |       content->signOn(this);
 | 
|---|
 | 235 |       theSet.insert(make_pair(i,content));
 | 
|---|
 | 236 |     }
 | 
|---|
 | 237 |   }
 | 
|---|
 | 238 | 
 | 
|---|
 | 239 |   ~ObservableMap(){
 | 
|---|
 | 240 |     set::iterator iter;
 | 
|---|
 | 241 |     for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
 | 
|---|
 | 242 |       delete iter->second;
 | 
|---|
 | 243 |     }
 | 
|---|
 | 244 |   }
 | 
|---|
 | 245 | 
 | 
|---|
 | 246 |   iterator begin(){
 | 
|---|
 | 247 |     return iterator(theSet.begin(),this);
 | 
|---|
 | 248 |   }
 | 
|---|
 | 249 | 
 | 
|---|
 | 250 |   iterator end(){
 | 
|---|
 | 251 |     return iterator(theSet.end(),this);
 | 
|---|
 | 252 |   }
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 |   const int num;
 | 
|---|
 | 255 | 
 | 
|---|
 | 256 | private:
 | 
|---|
 | 257 |   set theSet;
 | 
|---|
 | 258 | };
 | 
|---|
 | 259 | 
 | 
|---|
| [a7b761b] | 260 | 
 | 
|---|
| [63c1f6] | 261 | /******************* actuall tests ***************/
 | 
|---|
 | 262 | 
 | 
|---|
 | 263 | void ObserverTest::setUp() {
 | 
|---|
| [4fb5a3] | 264 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| [d5f216] | 265 |   simpleObservable1 = new SimpleObservable();
 | 
|---|
 | 266 |   simpleObservable2 = new SimpleObservable();
 | 
|---|
| [63c1f6] | 267 |   callObservable = new CallObservable();
 | 
|---|
 | 268 |   superObservable = new SuperObservable();
 | 
|---|
| [4fb5a3] | 269 |   blockObservable = new BlockObservable();
 | 
|---|
| [ccacba] | 270 |   notificationObservable = new NotificationObservable();
 | 
|---|
| [8774c5] | 271 |   obsset = new ObservableSet(5);
 | 
|---|
 | 272 |   obsmap = new ObservableMap(5);
 | 
|---|
| [63c1f6] | 273 | 
 | 
|---|
 | 274 |   observer1 = new UpdateCountObserver();
 | 
|---|
 | 275 |   observer2 = new UpdateCountObserver();
 | 
|---|
 | 276 |   observer3 = new UpdateCountObserver();
 | 
|---|
| [d5f216] | 277 |   observer4 = new UpdateCountObserver();
 | 
|---|
| [ccacba] | 278 | 
 | 
|---|
 | 279 |   notificationObserver1 = new NotificationObserver(notificationObservable->notification1);
 | 
|---|
 | 280 |   notificationObserver2 = new NotificationObserver(notificationObservable->notification2);
 | 
|---|
| [63c1f6] | 281 | }
 | 
|---|
 | 282 | 
 | 
|---|
 | 283 | void ObserverTest::tearDown() {
 | 
|---|
| [d5f216] | 284 |   delete simpleObservable1;
 | 
|---|
 | 285 |   delete simpleObservable2;
 | 
|---|
| [63c1f6] | 286 |   delete callObservable;
 | 
|---|
 | 287 |   delete superObservable;
 | 
|---|
| [033a05] | 288 |   delete blockObservable;
 | 
|---|
| [ccacba] | 289 |   delete notificationObservable;
 | 
|---|
| [8774c5] | 290 |   delete obsset;
 | 
|---|
 | 291 |   delete obsmap;
 | 
|---|
| [63c1f6] | 292 | 
 | 
|---|
 | 293 |   delete observer1;
 | 
|---|
 | 294 |   delete observer2;
 | 
|---|
 | 295 |   delete observer3;
 | 
|---|
| [d5f216] | 296 |   delete observer4;
 | 
|---|
| [ccacba] | 297 |   delete notificationObserver1;
 | 
|---|
 | 298 |   delete notificationObserver2;
 | 
|---|
| [63c1f6] | 299 | }
 | 
|---|
 | 300 | 
 | 
|---|
 | 301 | void ObserverTest::doesUpdateTest()
 | 
|---|
 | 302 | {
 | 
|---|
| [d5f216] | 303 |   simpleObservable1->signOn(observer1);
 | 
|---|
 | 304 |   simpleObservable1->signOn(observer2);
 | 
|---|
 | 305 |   simpleObservable1->signOn(observer3);
 | 
|---|
| [9b6b2f] | 306 | 
 | 
|---|
| [d5f216] | 307 |   simpleObservable2->signOn(observer2);
 | 
|---|
 | 308 |   simpleObservable2->signOn(observer4);
 | 
|---|
| [63c1f6] | 309 | 
 | 
|---|
| [61ea5b] | 310 |   CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
 | 
|---|
 | 311 |   CPPUNIT_ASSERT_EQUAL( 0, observer2->updates );
 | 
|---|
 | 312 |   CPPUNIT_ASSERT_EQUAL( 0, observer3->updates );
 | 
|---|
 | 313 |   CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
 | 
|---|
 | 314 | 
 | 
|---|
 | 315 | 
 | 
|---|
| [d5f216] | 316 |   simpleObservable1->changeMethod();
 | 
|---|
| [63c1f6] | 317 |   CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
 | 
|---|
 | 318 |   CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
 | 
|---|
 | 319 |   CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
 | 
|---|
| [d5f216] | 320 |   CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
 | 
|---|
| [63c1f6] | 321 | 
 | 
|---|
| [d5f216] | 322 |   simpleObservable1->signOff(observer3);
 | 
|---|
| [63c1f6] | 323 | 
 | 
|---|
| [d5f216] | 324 |   simpleObservable1->changeMethod();
 | 
|---|
| [63c1f6] | 325 |   CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
 | 
|---|
 | 326 |   CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
 | 
|---|
 | 327 |   CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
 | 
|---|
| [d5f216] | 328 |   CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
 | 
|---|
 | 329 | 
 | 
|---|
 | 330 |   simpleObservable2->changeMethod();
 | 
|---|
 | 331 |   CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
 | 
|---|
 | 332 |   CPPUNIT_ASSERT_EQUAL( 3, observer2->updates );
 | 
|---|
 | 333 |   CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
 | 
|---|
 | 334 |   CPPUNIT_ASSERT_EQUAL( 1, observer4->updates );
 | 
|---|
| [63c1f6] | 335 | }
 | 
|---|
 | 336 | 
 | 
|---|
 | 337 | 
 | 
|---|
 | 338 | void ObserverTest::doesBlockUpdateTest() {
 | 
|---|
 | 339 |   callObservable->signOn(observer1);
 | 
|---|
| [61ea5b] | 340 |   CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
 | 
|---|
| [63c1f6] | 341 | 
 | 
|---|
 | 342 |   callObservable->changeMethod1();
 | 
|---|
 | 343 |   CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
 | 
|---|
 | 344 | 
 | 
|---|
 | 345 |   callObservable->changeMethod2();
 | 
|---|
 | 346 |   CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
 | 
|---|
 | 347 | }
 | 
|---|
 | 348 | 
 | 
|---|
 | 349 | void ObserverTest::doesSubObservableTest() {
 | 
|---|
 | 350 |   superObservable->signOn(observer1);
 | 
|---|
 | 351 |   superObservable->subObservable->signOn(observer2);
 | 
|---|
 | 352 | 
 | 
|---|
 | 353 |   superObservable->subObservable->changeMethod();
 | 
|---|
 | 354 |   CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
 | 
|---|
 | 355 |   CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
 | 
|---|
 | 356 | 
 | 
|---|
 | 357 |   superObservable->changeMethod();
 | 
|---|
 | 358 |   CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
 | 
|---|
 | 359 |   CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
 | 
|---|
 | 360 | }
 | 
|---|
 | 361 | 
 | 
|---|
| [61ea5b] | 362 | void ObserverTest::outsideLockTest(){
 | 
|---|
 | 363 |   callObservable->signOn(observer1);
 | 
|---|
 | 364 |   CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
 | 
|---|
 | 365 | 
 | 
|---|
 | 366 |   {
 | 
|---|
 | 367 |     LOCK_OBSERVABLE(*callObservable);
 | 
|---|
 | 368 |     CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
 | 
|---|
 | 369 |   }
 | 
|---|
 | 370 |   // lock is gone now, observer should have notified
 | 
|---|
 | 371 |   CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
 | 
|---|
 | 372 | }
 | 
|---|
 | 373 | 
 | 
|---|
| [ccacba] | 374 | void ObserverTest::doesNotifyTest(){
 | 
|---|
 | 375 |   notificationObservable->signOn(notificationObserver1,
 | 
|---|
 | 376 |                                  notificationObservable->notification1);
 | 
|---|
 | 377 |   notificationObservable->signOn(notificationObserver2,
 | 
|---|
 | 378 |                                  notificationObservable->notification2);
 | 
|---|
 | 379 | 
 | 
|---|
 | 380 |   notificationObservable->operation1();
 | 
|---|
 | 381 |   CPPUNIT_ASSERT(notificationObserver1->wasNotified);
 | 
|---|
 | 382 |   CPPUNIT_ASSERT(!notificationObserver2->wasNotified);
 | 
|---|
 | 383 | 
 | 
|---|
 | 384 |   notificationObserver1->wasNotified=false;
 | 
|---|
 | 385 | 
 | 
|---|
 | 386 |   notificationObservable->operation2();
 | 
|---|
 | 387 |   CPPUNIT_ASSERT(!notificationObserver1->wasNotified);
 | 
|---|
 | 388 |   CPPUNIT_ASSERT(notificationObserver2->wasNotified);
 | 
|---|
 | 389 | 
 | 
|---|
 | 390 | }
 | 
|---|
 | 391 | 
 | 
|---|
| [4fb5a3] | 392 | void ObserverTest::doesReportTest(){
 | 
|---|
 | 393 |   // Actual checks are in the Stub-methods for this
 | 
|---|
 | 394 |   blockObservable->changeMethod1();
 | 
|---|
 | 395 |   blockObservable->changeMethod2();
 | 
|---|
 | 396 |   blockObservable->noChangeMethod();
 | 
|---|
 | 397 | }
 | 
|---|
| [63c1f6] | 398 | 
 | 
|---|
| [bd58fb] | 399 | void ObserverTest::iteratorTest(){
 | 
|---|
| [a7b761b] | 400 |   int i = 0;
 | 
|---|
 | 401 |   // test the general iterator methods
 | 
|---|
| [8774c5] | 402 |   for(ObservableSet::iterator iter=obsset->begin(); iter!=obsset->end();++iter){
 | 
|---|
 | 403 |     CPPUNIT_ASSERT(i< obsset->num);
 | 
|---|
| [a7b761b] | 404 |     i++;
 | 
|---|
 | 405 |   }
 | 
|---|
 | 406 | 
 | 
|---|
 | 407 |   i=0;
 | 
|---|
| [8774c5] | 408 |   for(ObservableSet::const_iterator iter=obsset->begin(); iter!=obsset->end();++iter){
 | 
|---|
 | 409 |     CPPUNIT_ASSERT(i<obsset->num);
 | 
|---|
| [a7b761b] | 410 |     i++;
 | 
|---|
 | 411 |   }
 | 
|---|
 | 412 | 
 | 
|---|
| [8774c5] | 413 |   obsset->signOn(observer1);
 | 
|---|
| [bd58fb] | 414 |   {
 | 
|---|
| [a7b761b] | 415 |     // we construct this out of the loop, so the iterator dies at the end of
 | 
|---|
 | 416 |     // the scope and not the end of the loop (allows more testing)
 | 
|---|
| [8774c5] | 417 |     ObservableSet::iterator iter;
 | 
|---|
 | 418 |     for(iter=obsset->begin(); iter!=obsset->end(); ++iter){
 | 
|---|
| [bd58fb] | 419 |       (*iter)->changeMethod();
 | 
|---|
 | 420 |     }
 | 
|---|
 | 421 |     // At this point no change should have been propagated
 | 
|---|
 | 422 |     CPPUNIT_ASSERT_EQUAL( 0, observer1->updates);
 | 
|---|
 | 423 |   }
 | 
|---|
 | 424 |   // After the Iterator has died the propagation should take place
 | 
|---|
 | 425 |   CPPUNIT_ASSERT_EQUAL( 1, observer1->updates);
 | 
|---|
| [a7b761b] | 426 | 
 | 
|---|
 | 427 |   // when using a const_iterator no changes should be propagated
 | 
|---|
| [8774c5] | 428 |   for(ObservableSet::const_iterator iter = obsset->begin(); iter!=obsset->end();++iter);
 | 
|---|
| [a7b761b] | 429 |   CPPUNIT_ASSERT_EQUAL( 1, observer1->updates);
 | 
|---|
| [8774c5] | 430 | 
 | 
|---|
 | 431 |   // we need to test the operator-> as well
 | 
|---|
 | 432 |   obsmap->signOn(observer2);
 | 
|---|
 | 433 |   {
 | 
|---|
 | 434 |     // we construct this out of the loop, so the iterator dies at the end of
 | 
|---|
 | 435 |     // the scope and not the end of the loop (allows more testing)
 | 
|---|
 | 436 |     ObservableMap::iterator iter;
 | 
|---|
 | 437 |     for(iter=obsmap->begin(); iter!=obsmap->end(); ++iter){
 | 
|---|
 | 438 |       iter->second->changeMethod();
 | 
|---|
 | 439 |     }
 | 
|---|
 | 440 |     // At this point no change should have been propagated
 | 
|---|
 | 441 |     CPPUNIT_ASSERT_EQUAL( 0, observer2->updates);
 | 
|---|
 | 442 |   }
 | 
|---|
 | 443 |   // After the Iterator has died the propagation should take place
 | 
|---|
 | 444 |   CPPUNIT_ASSERT_EQUAL( 1, observer2->updates);
 | 
|---|
 | 445 | 
 | 
|---|
 | 446 | 
 | 
|---|
 | 447 |   obsset->signOff(observer1);
 | 
|---|
 | 448 |   obsmap->signOff(observer2);
 | 
|---|
| [bd58fb] | 449 | }
 | 
|---|
 | 450 | 
 | 
|---|
| [63c1f6] | 451 | void ObserverTest::CircleDetectionTest() {
 | 
|---|
 | 452 |   cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
 | 
|---|
 | 453 |   cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
 | 
|---|
 | 454 |   cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
 | 
|---|
 | 455 |   cout << "If this test does not complete in a few seconds, kill the test-suite and fix the Error in the circle detection mechanism" << endl;
 | 
|---|
 | 456 | 
 | 
|---|
| [d5f216] | 457 |   cout << endl << endl << "The following errors displayed by the observer framework can be ignored" << endl;
 | 
|---|
| [63c1f6] | 458 | 
 | 
|---|
 | 459 |   // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
 | 
|---|
| [d5f216] | 460 |   simpleObservable1->signOn(simpleObservable1);
 | 
|---|
| [7a176b] | 461 | #ifndef NDEBUG
 | 
|---|
| [4fb5a3] | 462 |   CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
 | 
|---|
| [7a176b] | 463 | #else
 | 
|---|
 | 464 |   simpleObservable1->changeMethod();
 | 
|---|
 | 465 | #endif
 | 
|---|
| [d5f216] | 466 | 
 | 
|---|
 | 467 |   // more complex test
 | 
|---|
 | 468 |   simpleObservable1->signOff(simpleObservable1);
 | 
|---|
 | 469 |   simpleObservable1->signOn(simpleObservable2);
 | 
|---|
 | 470 |   simpleObservable2->signOn(simpleObservable1);
 | 
|---|
| [7a176b] | 471 | #ifndef NDEBUG
 | 
|---|
| [4fb5a3] | 472 |   CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
 | 
|---|
| [7a176b] | 473 | #else
 | 
|---|
 | 474 |   simpleObservable1->changeMethod();
 | 
|---|
 | 475 | #endif
 | 
|---|
 | 476 | 
 | 
|---|
 | 477 | 
 | 
|---|
| [d5f216] | 478 |   simpleObservable1->signOff(simpleObservable2);
 | 
|---|
 | 479 |   simpleObservable2->signOff(simpleObservable1);
 | 
|---|
| [63c1f6] | 480 |   // when we reach this line, although we broke the DAG assumption the circle check works fine
 | 
|---|
 | 481 |   CPPUNIT_ASSERT(true);
 | 
|---|
 | 482 | }
 | 
|---|