[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>
|
---|
| 13 |
|
---|
| 14 | #include "Patterns/Observer.hpp"
|
---|
| 15 |
|
---|
| 16 | #include <iostream>
|
---|
| 17 |
|
---|
| 18 | using namespace std;
|
---|
| 19 |
|
---|
[9b6b2f] | 20 | #ifdef HAVE_TESTRUNNER
|
---|
| 21 | #include "UnitTestMain.hpp"
|
---|
| 22 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 23 |
|
---|
[63c1f6] | 24 | // Registers the fixture into the 'registry'
|
---|
| 25 | CPPUNIT_TEST_SUITE_REGISTRATION( ObserverTest );
|
---|
| 26 |
|
---|
| 27 | /******************* Test stubs ************************/
|
---|
| 28 |
|
---|
| 29 | class UpdateCountObserver : public Observer {
|
---|
| 30 | public:
|
---|
| 31 | UpdateCountObserver() :
|
---|
| 32 | updates(0)
|
---|
| 33 | {};
|
---|
| 34 | void update(Observable *publisher){
|
---|
| 35 | updates++;
|
---|
| 36 | }
|
---|
| 37 | void subjectKilled(Observable *publisher) {
|
---|
| 38 | }
|
---|
| 39 | int updates;
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | class SimpleObservable : public Observable {
|
---|
| 43 | public:
|
---|
| 44 | void changeMethod() {
|
---|
[317df8] | 45 | OBSERVE;
|
---|
[5e5283] | 46 | int i = 0;
|
---|
[63c1f6] | 47 | i++;
|
---|
| 48 | }
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | class CallObservable : public Observable {
|
---|
| 52 | public:
|
---|
| 53 | void changeMethod1() {
|
---|
[317df8] | 54 | OBSERVE;
|
---|
[5e5283] | 55 | int i = 0;
|
---|
[63c1f6] | 56 | i++;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | void changeMethod2() {
|
---|
[317df8] | 60 | OBSERVE;
|
---|
[5e5283] | 61 | int i = 0;
|
---|
[63c1f6] | 62 | i++;
|
---|
| 63 | changeMethod1();
|
---|
| 64 | }
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | class SuperObservable : public Observable {
|
---|
| 68 | public:
|
---|
| 69 | SuperObservable(){
|
---|
| 70 | subObservable = new SimpleObservable();
|
---|
| 71 | subObservable->signOn(this);
|
---|
| 72 | }
|
---|
| 73 | ~SuperObservable(){
|
---|
| 74 | delete subObservable;
|
---|
| 75 | }
|
---|
| 76 | void changeMethod() {
|
---|
[317df8] | 77 | OBSERVE;
|
---|
[5e5283] | 78 | int i = 0;
|
---|
[63c1f6] | 79 | i++;
|
---|
| 80 | subObservable->changeMethod();
|
---|
| 81 | }
|
---|
| 82 | SimpleObservable *subObservable;
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | /******************* actuall tests ***************/
|
---|
| 86 |
|
---|
| 87 | void ObserverTest::setUp() {
|
---|
[d5f216] | 88 | simpleObservable1 = new SimpleObservable();
|
---|
| 89 | simpleObservable2 = new SimpleObservable();
|
---|
[63c1f6] | 90 | callObservable = new CallObservable();
|
---|
| 91 | superObservable = new SuperObservable();
|
---|
| 92 |
|
---|
| 93 | observer1 = new UpdateCountObserver();
|
---|
| 94 | observer2 = new UpdateCountObserver();
|
---|
| 95 | observer3 = new UpdateCountObserver();
|
---|
[d5f216] | 96 | observer4 = new UpdateCountObserver();
|
---|
[63c1f6] | 97 | }
|
---|
| 98 |
|
---|
| 99 | void ObserverTest::tearDown() {
|
---|
[d5f216] | 100 | delete simpleObservable1;
|
---|
| 101 | delete simpleObservable2;
|
---|
[63c1f6] | 102 | delete callObservable;
|
---|
| 103 | delete superObservable;
|
---|
| 104 |
|
---|
| 105 | delete observer1;
|
---|
| 106 | delete observer2;
|
---|
| 107 | delete observer3;
|
---|
[d5f216] | 108 | delete observer4;
|
---|
[63c1f6] | 109 | }
|
---|
| 110 |
|
---|
| 111 | void ObserverTest::doesUpdateTest()
|
---|
| 112 | {
|
---|
[d5f216] | 113 | simpleObservable1->signOn(observer1);
|
---|
| 114 | simpleObservable1->signOn(observer2);
|
---|
| 115 | simpleObservable1->signOn(observer3);
|
---|
[9b6b2f] | 116 |
|
---|
[d5f216] | 117 | simpleObservable2->signOn(observer2);
|
---|
| 118 | simpleObservable2->signOn(observer4);
|
---|
[63c1f6] | 119 |
|
---|
[d5f216] | 120 | simpleObservable1->changeMethod();
|
---|
[63c1f6] | 121 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
| 122 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
| 123 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
[d5f216] | 124 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
|
---|
[63c1f6] | 125 |
|
---|
[d5f216] | 126 | simpleObservable1->signOff(observer3);
|
---|
[63c1f6] | 127 |
|
---|
[d5f216] | 128 | simpleObservable1->changeMethod();
|
---|
[63c1f6] | 129 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 130 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
[d5f216] | 132 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
|
---|
| 133 |
|
---|
| 134 | simpleObservable2->changeMethod();
|
---|
| 135 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 136 | CPPUNIT_ASSERT_EQUAL( 3, observer2->updates );
|
---|
| 137 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
| 138 | CPPUNIT_ASSERT_EQUAL( 1, observer4->updates );
|
---|
[63c1f6] | 139 | }
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 | void ObserverTest::doesBlockUpdateTest() {
|
---|
| 143 | callObservable->signOn(observer1);
|
---|
| 144 |
|
---|
| 145 | callObservable->changeMethod1();
|
---|
| 146 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
| 147 |
|
---|
| 148 | callObservable->changeMethod2();
|
---|
| 149 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | void ObserverTest::doesSubObservableTest() {
|
---|
| 153 | superObservable->signOn(observer1);
|
---|
| 154 | superObservable->subObservable->signOn(observer2);
|
---|
| 155 |
|
---|
| 156 | superObservable->subObservable->changeMethod();
|
---|
| 157 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
| 158 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
| 159 |
|
---|
| 160 | superObservable->changeMethod();
|
---|
| 161 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 162 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 | void ObserverTest::CircleDetectionTest() {
|
---|
| 167 | cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
|
---|
| 168 | cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
|
---|
| 169 | cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
|
---|
| 170 | 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;
|
---|
| 171 |
|
---|
[d5f216] | 172 | cout << endl << endl << "The following errors displayed by the observer framework can be ignored" << endl;
|
---|
[63c1f6] | 173 |
|
---|
| 174 | // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
|
---|
[d5f216] | 175 | simpleObservable1->signOn(simpleObservable1);
|
---|
| 176 | simpleObservable1->changeMethod();
|
---|
| 177 |
|
---|
| 178 | // more complex test
|
---|
| 179 | simpleObservable1->signOff(simpleObservable1);
|
---|
| 180 | simpleObservable1->signOn(simpleObservable2);
|
---|
| 181 | simpleObservable2->signOn(simpleObservable1);
|
---|
| 182 | simpleObservable1->changeMethod();
|
---|
| 183 | simpleObservable1->signOff(simpleObservable2);
|
---|
| 184 | simpleObservable2->signOff(simpleObservable1);
|
---|
[63c1f6] | 185 | // when we reach this line, although we broke the DAG assumption the circle check works fine
|
---|
| 186 | CPPUNIT_ASSERT(true);
|
---|
| 187 | }
|
---|