| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * ObserverTest.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Jan 19, 2010 | 
|---|
| 12 | *      Author: crueger | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 21 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 22 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 23 | #include <set> | 
|---|
| 24 |  | 
|---|
| 25 | #include "CodePatterns/Assert.hpp" | 
|---|
| 26 |  | 
|---|
| 27 | #include <iostream> | 
|---|
| 28 |  | 
|---|
| 29 | #include "stubs/ObserverStub.hpp" | 
|---|
| 30 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| 31 | #include "CodePatterns/Observer/ObservedContainer.hpp" | 
|---|
| 32 | #include "CodePatterns/Observer/ObservedContainer_impl.hpp" | 
|---|
| 33 | #include "CodePatterns/Observer/ObserverLog.hpp" | 
|---|
| 34 |  | 
|---|
| 35 | #include "ObserverUnitTest.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #ifdef HAVE_TESTRUNNER | 
|---|
| 38 | #include "UnitTestMain.hpp" | 
|---|
| 39 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 40 |  | 
|---|
| 41 | // Registers the fixture into the 'registry' | 
|---|
| 42 | CPPUNIT_TEST_SUITE_REGISTRATION( ObserverTest ); | 
|---|
| 43 |  | 
|---|
| 44 | /******************* Test stubs ************************/ | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | /******************* actuall tests ***************/ | 
|---|
| 48 |  | 
|---|
| 49 | void ObserverTest::setUp() { | 
|---|
| 50 | ASSERT_DO(Assert::Throw); | 
|---|
| 51 |  | 
|---|
| 52 | simpleObservable1 = new SimpleObservable(); | 
|---|
| 53 | simpleObservable2 = new SimpleObservable(); | 
|---|
| 54 | callObservable = new CallObservable(); | 
|---|
| 55 | superObservable = new SuperObservable(); | 
|---|
| 56 | blockObservable = new BlockObservable(); | 
|---|
| 57 | notificationObservable = new NotificationObservable(); | 
|---|
| 58 | obsset = new ObservableSet(5); | 
|---|
| 59 | obsmap = new ObservableMap(5); | 
|---|
| 60 |  | 
|---|
| 61 | observer1 = new UpdateCountObserver(); | 
|---|
| 62 | observer2 = new UpdateCountObserver(); | 
|---|
| 63 | observer3 = new UpdateCountObserver(); | 
|---|
| 64 | observer4 = new UpdateCountObserver(); | 
|---|
| 65 |  | 
|---|
| 66 | notificationObserver1 = new NotificationObserver( | 
|---|
| 67 | notificationObservable->getChannel(NotificationObservable::Operation1Notify)); | 
|---|
| 68 | notificationObserver2 = new NotificationObserver( | 
|---|
| 69 | notificationObservable->getChannel(NotificationObservable::Operation2Notify)); | 
|---|
| 70 |  | 
|---|
| 71 | RelayObservable = new RelayTest; | 
|---|
| 72 | RelayObserver = new RelayCountObserver(RelayObservable); | 
|---|
| 73 |  | 
|---|
| 74 | RelayNotifier = new RelayNotification; | 
|---|
| 75 | RelayNotified = new RelayNotificationObserver(RelayObservable); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void ObserverTest::tearDown() { | 
|---|
| 79 | delete simpleObservable1; | 
|---|
| 80 | delete simpleObservable2; | 
|---|
| 81 | delete callObservable; | 
|---|
| 82 | delete superObservable; | 
|---|
| 83 | delete blockObservable; | 
|---|
| 84 | delete notificationObservable; | 
|---|
| 85 | delete obsset; | 
|---|
| 86 | delete obsmap; | 
|---|
| 87 | delete RelayObservable; | 
|---|
| 88 | delete RelayObserver; | 
|---|
| 89 | delete RelayNotifier; | 
|---|
| 90 | delete RelayNotified; | 
|---|
| 91 |  | 
|---|
| 92 | delete observer1; | 
|---|
| 93 | delete observer2; | 
|---|
| 94 | delete observer3; | 
|---|
| 95 | delete observer4; | 
|---|
| 96 | delete notificationObserver1; | 
|---|
| 97 | delete notificationObserver2; | 
|---|
| 98 | #ifdef LOG_OBSERVER | 
|---|
| 99 | ObserverLog::purgeInstance(); | 
|---|
| 100 | #endif | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | void ObserverTest::doesUpdateTest() | 
|---|
| 104 | { | 
|---|
| 105 | simpleObservable1->signOn(observer1); | 
|---|
| 106 | simpleObservable1->signOn(observer2); | 
|---|
| 107 | simpleObservable1->signOn(observer3); | 
|---|
| 108 |  | 
|---|
| 109 | simpleObservable2->signOn(observer2); | 
|---|
| 110 | simpleObservable2->signOn(observer4); | 
|---|
| 111 |  | 
|---|
| 112 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
| 113 | CPPUNIT_ASSERT_EQUAL( 0, observer2->updates ); | 
|---|
| 114 | CPPUNIT_ASSERT_EQUAL( 0, observer3->updates ); | 
|---|
| 115 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates ); | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 | simpleObservable1->changeMethod(); | 
|---|
| 119 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
| 120 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates ); | 
|---|
| 121 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates ); | 
|---|
| 122 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates ); | 
|---|
| 123 |  | 
|---|
| 124 | simpleObservable1->signOff(observer3); | 
|---|
| 125 |  | 
|---|
| 126 | simpleObservable1->changeMethod(); | 
|---|
| 127 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); | 
|---|
| 128 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates ); | 
|---|
| 129 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates ); | 
|---|
| 130 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates ); | 
|---|
| 131 |  | 
|---|
| 132 | simpleObservable2->changeMethod(); | 
|---|
| 133 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); | 
|---|
| 134 | CPPUNIT_ASSERT_EQUAL( 3, observer2->updates ); | 
|---|
| 135 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates ); | 
|---|
| 136 | CPPUNIT_ASSERT_EQUAL( 1, observer4->updates ); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 |  | 
|---|
| 140 | void ObserverTest::doesBlockUpdateTest() { | 
|---|
| 141 | callObservable->signOn(observer1); | 
|---|
| 142 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
| 143 |  | 
|---|
| 144 | callObservable->changeMethod1(); | 
|---|
| 145 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
| 146 |  | 
|---|
| 147 | callObservable->changeMethod2(); | 
|---|
| 148 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void ObserverTest::doesSubObservableTest() { | 
|---|
| 152 | superObservable->signOn(observer1); | 
|---|
| 153 | superObservable->subObservable->signOn(observer2); | 
|---|
| 154 |  | 
|---|
| 155 | superObservable->subObservable->changeMethod(); | 
|---|
| 156 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
| 157 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates ); | 
|---|
| 158 |  | 
|---|
| 159 | superObservable->changeMethod(); | 
|---|
| 160 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); | 
|---|
| 161 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates ); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | void ObserverTest::outsideLockTest(){ | 
|---|
| 165 | callObservable->signOn(observer1); | 
|---|
| 166 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
| 167 |  | 
|---|
| 168 | { | 
|---|
| 169 | LOCK_OBSERVABLE(*callObservable); | 
|---|
| 170 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
| 171 | } | 
|---|
| 172 | // lock is gone now, observer should have notified | 
|---|
| 173 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | void ObserverTest::doesNotifyTest(){ | 
|---|
| 177 | notificationObservable->signOn(notificationObserver1, NotificationObservable::Operation1Notify); | 
|---|
| 178 | notificationObservable->signOn(notificationObserver2, NotificationObservable::Operation2Notify); | 
|---|
| 179 |  | 
|---|
| 180 | notificationObservable->operation1(); | 
|---|
| 181 | CPPUNIT_ASSERT(notificationObserver1->wasNotified); | 
|---|
| 182 | CPPUNIT_ASSERT(!notificationObserver2->wasNotified); | 
|---|
| 183 |  | 
|---|
| 184 | notificationObserver1->wasNotified=false; | 
|---|
| 185 |  | 
|---|
| 186 | notificationObservable->operation2(); | 
|---|
| 187 | CPPUNIT_ASSERT(!notificationObserver1->wasNotified); | 
|---|
| 188 | CPPUNIT_ASSERT(notificationObserver2->wasNotified); | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | void ObserverTest::doesReportTest(){ | 
|---|
| 192 | // Actual checks are in the Stub-methods for this | 
|---|
| 193 | blockObservable->changeMethod1(); | 
|---|
| 194 | blockObservable->changeMethod2(); | 
|---|
| 195 | blockObservable->noChangeMethod(); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | void ObserverTest::iteratorTest(){ | 
|---|
| 199 | int i = 0; | 
|---|
| 200 | // test the general iterator methods | 
|---|
| 201 | for(ObservableSet::iterator iter=obsset->begin(); iter!=obsset->end();++iter){ | 
|---|
| 202 | CPPUNIT_ASSERT(i< obsset->num); | 
|---|
| 203 | i++; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | i=0; | 
|---|
| 207 | for(ObservableSet::const_iterator iter=obsset->begin(); iter!=obsset->end();++iter){ | 
|---|
| 208 | CPPUNIT_ASSERT(i<obsset->num); | 
|---|
| 209 | i++; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | obsset->signOn(observer1); | 
|---|
| 213 | { | 
|---|
| 214 | // we construct this out of the loop, so the iterator dies at the end of | 
|---|
| 215 | // the scope and not the end of the loop (allows more testing) | 
|---|
| 216 | ObservableSet::iterator iter; | 
|---|
| 217 | for(iter=obsset->begin(); iter!=obsset->end(); ++iter){ | 
|---|
| 218 | (*iter)->changeMethod(); | 
|---|
| 219 | } | 
|---|
| 220 | // At this point no change should have been propagated | 
|---|
| 221 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates); | 
|---|
| 222 | } | 
|---|
| 223 | // After the Iterator has died the propagation should take place | 
|---|
| 224 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates); | 
|---|
| 225 |  | 
|---|
| 226 | // when using a const_iterator no changes should be propagated | 
|---|
| 227 | for(ObservableSet::const_iterator iter = obsset->begin(); iter!=obsset->end();++iter); | 
|---|
| 228 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates); | 
|---|
| 229 |  | 
|---|
| 230 | // we need to test the operator-> as well | 
|---|
| 231 | obsmap->signOn(observer2); | 
|---|
| 232 | { | 
|---|
| 233 | // we construct this out of the loop, so the iterator dies at the end of | 
|---|
| 234 | // the scope and not the end of the loop (allows more testing) | 
|---|
| 235 | ObservableMap::iterator iter; | 
|---|
| 236 | for(iter=obsmap->begin(); iter!=obsmap->end(); ++iter){ | 
|---|
| 237 | iter->second->changeMethod(); | 
|---|
| 238 | } | 
|---|
| 239 | // At this point no change should have been propagated | 
|---|
| 240 | CPPUNIT_ASSERT_EQUAL( 0, observer2->updates); | 
|---|
| 241 | } | 
|---|
| 242 | // After the Iterator has died the propagation should take place | 
|---|
| 243 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates); | 
|---|
| 244 |  | 
|---|
| 245 | obsset->signOff(observer1); | 
|---|
| 246 | obsmap->signOff(observer2); | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | void ObserverTest::relayTest() | 
|---|
| 250 | { | 
|---|
| 251 | // sign on some observables to the relay | 
|---|
| 252 | simpleObservable1->signOn(RelayObservable); | 
|---|
| 253 | simpleObservable2->signOn(RelayObservable); | 
|---|
| 254 |  | 
|---|
| 255 | // sign on an observer to the relay | 
|---|
| 256 | RelayObservable->signOn(RelayObserver); | 
|---|
| 257 | simpleObservable1->signOn(observer1); | 
|---|
| 258 |  | 
|---|
| 259 | // check that all is zero | 
|---|
| 260 | CPPUNIT_ASSERT_EQUAL( 0, RelayObserver->updates ); | 
|---|
| 261 | CPPUNIT_ASSERT_EQUAL( 0, observer1->updates ); | 
|---|
| 262 |  | 
|---|
| 263 | // signal update | 
|---|
| 264 | simpleObservable1->changeMethod(); | 
|---|
| 265 |  | 
|---|
| 266 | // check that both the change | 
|---|
| 267 | // (and RelayObserver checks being called correctly) | 
|---|
| 268 | CPPUNIT_ASSERT_EQUAL( 1, RelayObserver->updates ); | 
|---|
| 269 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
| 270 |  | 
|---|
| 271 | // signal update through relay only | 
|---|
| 272 | simpleObservable2->changeMethod(); | 
|---|
| 273 |  | 
|---|
| 274 | // check that only one got the change | 
|---|
| 275 | CPPUNIT_ASSERT_EQUAL( 2, RelayObserver->updates ); | 
|---|
| 276 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates ); | 
|---|
| 277 |  | 
|---|
| 278 | // check on signOff | 
|---|
| 279 | simpleObservable1->signOff(RelayObservable); | 
|---|
| 280 | simpleObservable1->signOff(observer1); | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | void ObserverTest::relayNotificationTest() | 
|---|
| 284 | { | 
|---|
| 285 | observerLog().enableLogging(); | 
|---|
| 286 |  | 
|---|
| 287 | // sign on some observables to the relay | 
|---|
| 288 | notificationObservable->signOn(RelayNotifier, NotificationObservable::Operation1Notify); | 
|---|
| 289 | notificationObservable->signOn(RelayNotifier, NotificationObservable::Operation2Notify); | 
|---|
| 290 | notificationObservable->signOn(notificationObserver1, NotificationObservable::Operation1Notify); | 
|---|
| 291 |  | 
|---|
| 292 | RelayNotifier->signOn(RelayNotified, NotificationObservable::Operation1Notify); | 
|---|
| 293 |  | 
|---|
| 294 | // operation1 | 
|---|
| 295 | notificationObservable->operation1(); | 
|---|
| 296 | CPPUNIT_ASSERT(RelayNotified->wasNotified); | 
|---|
| 297 | CPPUNIT_ASSERT(notificationObserver1->wasNotified); | 
|---|
| 298 |  | 
|---|
| 299 | RelayNotified->wasNotified=false; | 
|---|
| 300 | notificationObserver1->wasNotified=false; | 
|---|
| 301 |  | 
|---|
| 302 | // operation2 | 
|---|
| 303 | notificationObservable->operation2(); | 
|---|
| 304 | CPPUNIT_ASSERT(!RelayNotified->wasNotified); | 
|---|
| 305 | CPPUNIT_ASSERT(!notificationObserver1->wasNotified); | 
|---|
| 306 |  | 
|---|
| 307 | // signOff relay from 1 and operation1 | 
|---|
| 308 | notificationObserver1->wasNotified=false; | 
|---|
| 309 | notificationObservable->signOff(RelayNotifier, NotificationObservable::Operation1Notify); | 
|---|
| 310 |  | 
|---|
| 311 | notificationObservable->operation1(); | 
|---|
| 312 | CPPUNIT_ASSERT(!RelayNotified->wasNotified); | 
|---|
| 313 | CPPUNIT_ASSERT(notificationObserver1->wasNotified); | 
|---|
| 314 |  | 
|---|
| 315 | // test kill subject | 
|---|
| 316 | RelayNotifier->signOff(RelayNotified, NotificationObservable::Operation1Notify); | 
|---|
| 317 | delete RelayNotified; | 
|---|
| 318 | RelayNotified = NULL; // delete in tearDown is allowed for NULL | 
|---|
| 319 | notificationObservable->operation1(); | 
|---|
| 320 | notificationObservable->signOff(RelayNotifier, NotificationObservable::Operation2Notify); | 
|---|
| 321 | delete notificationObservable; | 
|---|
| 322 | notificationObservable = NULL; // delete in tearDown is allowed for NULL | 
|---|
| 323 |  | 
|---|
| 324 | observerLog().disableLogging(); | 
|---|
| 325 | } | 
|---|
| 326 |  | 
|---|
| 327 | void ObserverTest::CircleDetectionTest() { | 
|---|
| 328 | std::cout << std::endl << "Warning: the next test involved methods that can produce infinite loops." << std::endl; | 
|---|
| 329 | std::cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << std::endl; | 
|---|
| 330 | std::cout << "Instead tests are run on these methods to see if termination is assured" << std::endl << std::endl; | 
|---|
| 331 | std::cout << "If this test does not complete in a few seconds, kill the test-suite and fix the Error in the circle detection mechanism" << std::endl; | 
|---|
| 332 |  | 
|---|
| 333 | std::cout << std::endl << std::endl << "The following errors displayed by the observer framework can be ignored" << std::endl; | 
|---|
| 334 |  | 
|---|
| 335 | // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE | 
|---|
| 336 | simpleObservable1->signOn(simpleObservable1); | 
|---|
| 337 | #ifndef NDEBUG | 
|---|
| 338 | CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure); | 
|---|
| 339 | #else | 
|---|
| 340 | simpleObservable1->changeMethod(); | 
|---|
| 341 | #endif | 
|---|
| 342 |  | 
|---|
| 343 | // more complex test | 
|---|
| 344 | simpleObservable1->signOff(simpleObservable1); | 
|---|
| 345 | simpleObservable1->signOn(simpleObservable2); | 
|---|
| 346 | simpleObservable2->signOn(simpleObservable1); | 
|---|
| 347 | #ifndef NDEBUG | 
|---|
| 348 | CPPUNIT_ASSERT_THROW(simpleObservable1->changeMethod(),Assert::AssertionFailure); | 
|---|
| 349 | #else | 
|---|
| 350 | simpleObservable1->changeMethod(); | 
|---|
| 351 | #endif | 
|---|
| 352 |  | 
|---|
| 353 |  | 
|---|
| 354 | simpleObservable1->signOff(simpleObservable2); | 
|---|
| 355 | simpleObservable2->signOff(simpleObservable1); | 
|---|
| 356 | // when we reach this line, although we broke the DAG assumption the circle check works fine | 
|---|
| 357 | CPPUNIT_ASSERT(true); | 
|---|
| 358 | } | 
|---|