[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() :
|
---|
| 35 | updates(0)
|
---|
| 36 | {};
|
---|
| 37 | void update(Observable *publisher){
|
---|
| 38 | updates++;
|
---|
| 39 | }
|
---|
| 40 | void subjectKilled(Observable *publisher) {
|
---|
| 41 | }
|
---|
| 42 | int updates;
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | class SimpleObservable : public Observable {
|
---|
| 46 | public:
|
---|
| 47 | void changeMethod() {
|
---|
[317df8] | 48 | OBSERVE;
|
---|
[5e5283] | 49 | int i = 0;
|
---|
[63c1f6] | 50 | i++;
|
---|
| 51 | }
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | class CallObservable : public Observable {
|
---|
| 55 | public:
|
---|
| 56 | void changeMethod1() {
|
---|
[317df8] | 57 | OBSERVE;
|
---|
[5e5283] | 58 | int i = 0;
|
---|
[63c1f6] | 59 | i++;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void changeMethod2() {
|
---|
[317df8] | 63 | OBSERVE;
|
---|
[5e5283] | 64 | int i = 0;
|
---|
[63c1f6] | 65 | i++;
|
---|
| 66 | changeMethod1();
|
---|
| 67 | }
|
---|
| 68 | };
|
---|
| 69 |
|
---|
[4fb5a3] | 70 | class BlockObservable : public Observable {
|
---|
| 71 | public:
|
---|
| 72 | void changeMethod1(){
|
---|
| 73 | OBSERVE;
|
---|
| 74 | // test if we report correctly as blocked
|
---|
| 75 | CPPUNIT_ASSERT(isBlocked());
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void changeMethod2(){
|
---|
| 79 | OBSERVE;
|
---|
| 80 | internalMethod1();
|
---|
| 81 | internalMethod2();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void internalMethod1(){
|
---|
| 85 | // we did not block, but our caller did...
|
---|
| 86 | // see if this is found
|
---|
| 87 | CPPUNIT_ASSERT(isBlocked());
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void internalMethod2(){
|
---|
| 91 | OBSERVE;
|
---|
| 92 | // Both this method and the caller do block
|
---|
| 93 | // Does the reporting still work as expected?
|
---|
| 94 | CPPUNIT_ASSERT(isBlocked());
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void noChangeMethod(){
|
---|
| 98 | // No Block introduced here
|
---|
| 99 | // reported correctely?
|
---|
| 100 | CPPUNIT_ASSERT(!isBlocked());
|
---|
| 101 | }
|
---|
| 102 | };
|
---|
| 103 |
|
---|
[63c1f6] | 104 | class SuperObservable : public Observable {
|
---|
| 105 | public:
|
---|
| 106 | SuperObservable(){
|
---|
| 107 | subObservable = new SimpleObservable();
|
---|
| 108 | subObservable->signOn(this);
|
---|
| 109 | }
|
---|
| 110 | ~SuperObservable(){
|
---|
| 111 | delete subObservable;
|
---|
| 112 | }
|
---|
| 113 | void changeMethod() {
|
---|
[317df8] | 114 | OBSERVE;
|
---|
[5e5283] | 115 | int i = 0;
|
---|
[63c1f6] | 116 | i++;
|
---|
| 117 | subObservable->changeMethod();
|
---|
| 118 | }
|
---|
| 119 | SimpleObservable *subObservable;
|
---|
| 120 | };
|
---|
| 121 |
|
---|
[ccacba] | 122 | class NotificationObservable : public Observable {
|
---|
| 123 | public:
|
---|
| 124 | NotificationObservable() :
|
---|
| 125 | notification1(new Notification(this)),
|
---|
| 126 | notification2(new Notification(this))
|
---|
| 127 | {}
|
---|
| 128 |
|
---|
| 129 | ~NotificationObservable(){
|
---|
| 130 | delete notification1;
|
---|
| 131 | delete notification2;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void operation1(){
|
---|
| 135 | OBSERVE;
|
---|
| 136 | NOTIFY(notification1);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void operation2(){
|
---|
| 140 | OBSERVE;
|
---|
| 141 | NOTIFY(notification2);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | Notification_ptr notification1;
|
---|
| 145 | Notification_ptr notification2;
|
---|
| 146 | };
|
---|
| 147 |
|
---|
| 148 | class NotificationObserver : public Observer {
|
---|
| 149 | public:
|
---|
| 150 | NotificationObserver(Notification_ptr notification) :
|
---|
| 151 | requestedNotification(notification),
|
---|
| 152 | wasNotified(false)
|
---|
| 153 | {}
|
---|
| 154 |
|
---|
| 155 | void update(Observable*){}
|
---|
| 156 | void subjectKilled(Observable*){}
|
---|
| 157 | void recieveNotification(Observable *publisher, Notification_ptr notification){
|
---|
[033a05] | 158 | ASSERT(requestedNotification==notification,"Notification received that was not requested");
|
---|
[ccacba] | 159 | wasNotified = true;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | Notification_ptr requestedNotification;
|
---|
| 163 |
|
---|
| 164 | bool wasNotified;
|
---|
| 165 | };
|
---|
| 166 |
|
---|
[bd58fb] | 167 | class ObservableCollection : public Observable {
|
---|
| 168 | public:
|
---|
| 169 | typedef std::set<SimpleObservable*> set;
|
---|
| 170 | typedef ObservedIterator<set> iterator;
|
---|
[a7b761b] | 171 | typedef set::const_iterator const_iterator;
|
---|
[bd58fb] | 172 |
|
---|
[a7b761b] | 173 | ObservableCollection(int _num) :
|
---|
| 174 | num(_num)
|
---|
| 175 | {
|
---|
[bd58fb] | 176 | for(int i=0; i<num; ++i){
|
---|
| 177 | SimpleObservable *content = new SimpleObservable();
|
---|
| 178 | content->signOn(this);
|
---|
| 179 | theSet.insert(content);
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | ~ObservableCollection(){
|
---|
| 184 | set::iterator iter;
|
---|
| 185 | for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
|
---|
| 186 | delete (*iter);
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | iterator begin(){
|
---|
| 191 | return iterator(theSet.begin(),this);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | iterator end(){
|
---|
| 195 | return iterator(theSet.end(),this);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[a7b761b] | 198 | const int num;
|
---|
| 199 |
|
---|
[bd58fb] | 200 | private:
|
---|
| 201 | set theSet;
|
---|
| 202 | };
|
---|
| 203 |
|
---|
[a7b761b] | 204 |
|
---|
[63c1f6] | 205 | /******************* actuall tests ***************/
|
---|
| 206 |
|
---|
| 207 | void ObserverTest::setUp() {
|
---|
[4fb5a3] | 208 | ASSERT_DO(Assert::Throw);
|
---|
[d5f216] | 209 | simpleObservable1 = new SimpleObservable();
|
---|
| 210 | simpleObservable2 = new SimpleObservable();
|
---|
[63c1f6] | 211 | callObservable = new CallObservable();
|
---|
| 212 | superObservable = new SuperObservable();
|
---|
[4fb5a3] | 213 | blockObservable = new BlockObservable();
|
---|
[ccacba] | 214 | notificationObservable = new NotificationObservable();
|
---|
[a7b761b] | 215 | collection = new ObservableCollection(5);
|
---|
[63c1f6] | 216 |
|
---|
| 217 | observer1 = new UpdateCountObserver();
|
---|
| 218 | observer2 = new UpdateCountObserver();
|
---|
| 219 | observer3 = new UpdateCountObserver();
|
---|
[d5f216] | 220 | observer4 = new UpdateCountObserver();
|
---|
[ccacba] | 221 |
|
---|
| 222 | notificationObserver1 = new NotificationObserver(notificationObservable->notification1);
|
---|
| 223 | notificationObserver2 = new NotificationObserver(notificationObservable->notification2);
|
---|
[63c1f6] | 224 | }
|
---|
| 225 |
|
---|
| 226 | void ObserverTest::tearDown() {
|
---|
[d5f216] | 227 | delete simpleObservable1;
|
---|
| 228 | delete simpleObservable2;
|
---|
[63c1f6] | 229 | delete callObservable;
|
---|
| 230 | delete superObservable;
|
---|
[033a05] | 231 | delete blockObservable;
|
---|
[ccacba] | 232 | delete notificationObservable;
|
---|
[a7b761b] | 233 | delete collection;
|
---|
[63c1f6] | 234 |
|
---|
| 235 | delete observer1;
|
---|
| 236 | delete observer2;
|
---|
| 237 | delete observer3;
|
---|
[d5f216] | 238 | delete observer4;
|
---|
[ccacba] | 239 | delete notificationObserver1;
|
---|
| 240 | delete notificationObserver2;
|
---|
[63c1f6] | 241 | }
|
---|
| 242 |
|
---|
| 243 | void ObserverTest::doesUpdateTest()
|
---|
| 244 | {
|
---|
[d5f216] | 245 | simpleObservable1->signOn(observer1);
|
---|
| 246 | simpleObservable1->signOn(observer2);
|
---|
| 247 | simpleObservable1->signOn(observer3);
|
---|
[9b6b2f] | 248 |
|
---|
[d5f216] | 249 | simpleObservable2->signOn(observer2);
|
---|
| 250 | simpleObservable2->signOn(observer4);
|
---|
[63c1f6] | 251 |
|
---|
[d5f216] | 252 | simpleObservable1->changeMethod();
|
---|
[63c1f6] | 253 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
| 254 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
| 255 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
[d5f216] | 256 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
|
---|
[63c1f6] | 257 |
|
---|
[d5f216] | 258 | simpleObservable1->signOff(observer3);
|
---|
[63c1f6] | 259 |
|
---|
[d5f216] | 260 | simpleObservable1->changeMethod();
|
---|
[63c1f6] | 261 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 262 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
| 263 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
[d5f216] | 264 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
|
---|
| 265 |
|
---|
| 266 | simpleObservable2->changeMethod();
|
---|
| 267 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 268 | CPPUNIT_ASSERT_EQUAL( 3, observer2->updates );
|
---|
| 269 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
| 270 | CPPUNIT_ASSERT_EQUAL( 1, observer4->updates );
|
---|
[63c1f6] | 271 | }
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | void ObserverTest::doesBlockUpdateTest() {
|
---|
| 275 | callObservable->signOn(observer1);
|
---|
| 276 |
|
---|
| 277 | callObservable->changeMethod1();
|
---|
| 278 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
| 279 |
|
---|
| 280 | callObservable->changeMethod2();
|
---|
| 281 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | void ObserverTest::doesSubObservableTest() {
|
---|
| 285 | superObservable->signOn(observer1);
|
---|
| 286 | superObservable->subObservable->signOn(observer2);
|
---|
| 287 |
|
---|
| 288 | superObservable->subObservable->changeMethod();
|
---|
| 289 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
| 290 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
| 291 |
|
---|
| 292 | superObservable->changeMethod();
|
---|
| 293 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 294 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[ccacba] | 297 | void ObserverTest::doesNotifyTest(){
|
---|
| 298 | notificationObservable->signOn(notificationObserver1,
|
---|
| 299 | notificationObservable->notification1);
|
---|
| 300 | notificationObservable->signOn(notificationObserver2,
|
---|
| 301 | notificationObservable->notification2);
|
---|
| 302 |
|
---|
| 303 | notificationObservable->operation1();
|
---|
| 304 | CPPUNIT_ASSERT(notificationObserver1->wasNotified);
|
---|
| 305 | CPPUNIT_ASSERT(!notificationObserver2->wasNotified);
|
---|
| 306 |
|
---|
| 307 | notificationObserver1->wasNotified=false;
|
---|
| 308 |
|
---|
| 309 | notificationObservable->operation2();
|
---|
| 310 | CPPUNIT_ASSERT(!notificationObserver1->wasNotified);
|
---|
| 311 | CPPUNIT_ASSERT(notificationObserver2->wasNotified);
|
---|
| 312 |
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[4fb5a3] | 315 | void ObserverTest::doesReportTest(){
|
---|
| 316 | // Actual checks are in the Stub-methods for this
|
---|
| 317 | blockObservable->changeMethod1();
|
---|
| 318 | blockObservable->changeMethod2();
|
---|
| 319 | blockObservable->noChangeMethod();
|
---|
| 320 | }
|
---|
[63c1f6] | 321 |
|
---|
[bd58fb] | 322 | void ObserverTest::iteratorTest(){
|
---|
[a7b761b] | 323 | int i = 0;
|
---|
| 324 | // test the general iterator methods
|
---|
| 325 | for(ObservableCollection::iterator iter=collection->begin(); iter!=collection->end();++iter){
|
---|
| 326 | CPPUNIT_ASSERT(i< collection->num);
|
---|
| 327 | i++;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | i=0;
|
---|
| 331 | for(ObservableCollection::const_iterator iter=collection->begin(); iter!=collection->end();++iter){
|
---|
| 332 | CPPUNIT_ASSERT(i<collection->num);
|
---|
| 333 | i++;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[bd58fb] | 336 | collection->signOn(observer1);
|
---|
| 337 | {
|
---|
[a7b761b] | 338 | // we construct this out of the loop, so the iterator dies at the end of
|
---|
| 339 | // the scope and not the end of the loop (allows more testing)
|
---|
[bd58fb] | 340 | ObservableCollection::iterator iter;
|
---|
| 341 | for(iter=collection->begin(); iter!=collection->end(); ++iter){
|
---|
| 342 | (*iter)->changeMethod();
|
---|
| 343 | }
|
---|
| 344 | // At this point no change should have been propagated
|
---|
| 345 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates);
|
---|
| 346 | }
|
---|
| 347 | // After the Iterator has died the propagation should take place
|
---|
| 348 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates);
|
---|
[a7b761b] | 349 |
|
---|
| 350 | // when using a const_iterator no changes should be propagated
|
---|
| 351 | for(ObservableCollection::const_iterator iter = collection->begin(); iter!=collection->end();++iter);
|
---|
| 352 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates);
|
---|
[bd58fb] | 353 | collection->signOff(observer1);
|
---|
| 354 | }
|
---|
| 355 |
|
---|
[63c1f6] | 356 | void ObserverTest::CircleDetectionTest() {
|
---|
| 357 | cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
|
---|
| 358 | cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
|
---|
| 359 | cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
|
---|
| 360 | 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;
|
---|
| 361 |
|
---|
[d5f216] | 362 | cout << endl << endl << "The following errors displayed by the observer framework can be ignored" << endl;
|
---|
[63c1f6] | 363 |
|
---|
| 364 | // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
|
---|
[d5f216] | 365 | simpleObservable1->signOn(simpleObservable1);
|
---|
[4fb5a3] | 366 | CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
|
---|
[d5f216] | 367 |
|
---|
| 368 | // more complex test
|
---|
| 369 | simpleObservable1->signOff(simpleObservable1);
|
---|
| 370 | simpleObservable1->signOn(simpleObservable2);
|
---|
| 371 | simpleObservable2->signOn(simpleObservable1);
|
---|
[4fb5a3] | 372 | CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure);
|
---|
[d5f216] | 373 | simpleObservable1->signOff(simpleObservable2);
|
---|
| 374 | simpleObservable2->signOff(simpleObservable1);
|
---|
[63c1f6] | 375 | // when we reach this line, although we broke the DAG assumption the circle check works fine
|
---|
| 376 | CPPUNIT_ASSERT(true);
|
---|
| 377 | }
|
---|