[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 |
|
---|
| 20 | // Registers the fixture into the 'registry'
|
---|
| 21 | CPPUNIT_TEST_SUITE_REGISTRATION( ObserverTest );
|
---|
| 22 |
|
---|
| 23 | /******************* Test stubs ************************/
|
---|
| 24 |
|
---|
| 25 | class UpdateCountObserver : public Observer {
|
---|
| 26 | public:
|
---|
| 27 | UpdateCountObserver() :
|
---|
| 28 | updates(0)
|
---|
| 29 | {};
|
---|
| 30 | void update(Observable *publisher){
|
---|
| 31 | updates++;
|
---|
| 32 | }
|
---|
| 33 | void subjectKilled(Observable *publisher) {
|
---|
| 34 | }
|
---|
| 35 | int updates;
|
---|
| 36 | };
|
---|
| 37 |
|
---|
| 38 | class SimpleObservable : public Observable {
|
---|
| 39 | public:
|
---|
| 40 | void changeMethod() {
|
---|
| 41 | START_OBSERVER;
|
---|
| 42 | int i;
|
---|
| 43 | i++;
|
---|
| 44 | FINISH_OBSERVER;
|
---|
| 45 | }
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | class CallObservable : public Observable {
|
---|
| 49 | public:
|
---|
| 50 | void changeMethod1() {
|
---|
| 51 | START_OBSERVER;
|
---|
| 52 | int i;
|
---|
| 53 | i++;
|
---|
| 54 | FINISH_OBSERVER;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | void changeMethod2() {
|
---|
| 58 | START_OBSERVER;
|
---|
| 59 | int i;
|
---|
| 60 | i++;
|
---|
| 61 | changeMethod1();
|
---|
| 62 | FINISH_OBSERVER;
|
---|
| 63 | }
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | class SuperObservable : public Observable {
|
---|
| 67 | public:
|
---|
| 68 | SuperObservable(){
|
---|
| 69 | subObservable = new SimpleObservable();
|
---|
| 70 | subObservable->signOn(this);
|
---|
| 71 | }
|
---|
| 72 | ~SuperObservable(){
|
---|
| 73 | delete subObservable;
|
---|
| 74 | }
|
---|
| 75 | void changeMethod() {
|
---|
| 76 | START_OBSERVER;
|
---|
| 77 | int i;
|
---|
| 78 | i++;
|
---|
| 79 | subObservable->changeMethod();
|
---|
| 80 | FINISH_OBSERVER;
|
---|
| 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);
|
---|
| 116 | simpleObservable2->signOn(observer2);
|
---|
| 117 | simpleObservable2->signOn(observer4);
|
---|
[63c1f6] | 118 |
|
---|
[d5f216] | 119 | simpleObservable1->changeMethod();
|
---|
[63c1f6] | 120 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
| 121 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
| 122 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
[d5f216] | 123 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
|
---|
[63c1f6] | 124 |
|
---|
[d5f216] | 125 | simpleObservable1->signOff(observer3);
|
---|
[63c1f6] | 126 |
|
---|
[d5f216] | 127 | simpleObservable1->changeMethod();
|
---|
[63c1f6] | 128 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 129 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
| 130 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
[d5f216] | 131 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
|
---|
| 132 |
|
---|
| 133 | simpleObservable2->changeMethod();
|
---|
| 134 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
| 135 | CPPUNIT_ASSERT_EQUAL( 3, observer2->updates );
|
---|
| 136 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
| 137 | CPPUNIT_ASSERT_EQUAL( 1, observer4->updates );
|
---|
[63c1f6] | 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | void ObserverTest::doesBlockUpdateTest() {
|
---|
| 142 | callObservable->signOn(observer1);
|
---|
| 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 |
|
---|
| 165 | void ObserverTest::CircleDetectionTest() {
|
---|
| 166 | cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
|
---|
| 167 | cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
|
---|
| 168 | cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
|
---|
| 169 | 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;
|
---|
| 170 |
|
---|
[d5f216] | 171 | cout << endl << endl << "The following errors displayed by the observer framework can be ignored" << endl;
|
---|
[63c1f6] | 172 |
|
---|
| 173 | // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
|
---|
[d5f216] | 174 | simpleObservable1->signOn(simpleObservable1);
|
---|
| 175 | simpleObservable1->changeMethod();
|
---|
| 176 |
|
---|
| 177 | // more complex test
|
---|
| 178 | simpleObservable1->signOff(simpleObservable1);
|
---|
| 179 | simpleObservable1->signOn(simpleObservable2);
|
---|
| 180 | simpleObservable2->signOn(simpleObservable1);
|
---|
| 181 | simpleObservable1->changeMethod();
|
---|
| 182 | simpleObservable1->signOff(simpleObservable2);
|
---|
| 183 | simpleObservable2->signOff(simpleObservable1);
|
---|
[63c1f6] | 184 | // when we reach this line, although we broke the DAG assumption the circle check works fine
|
---|
| 185 | CPPUNIT_ASSERT(true);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /********************************************** Main routine **************************************/
|
---|
| 189 |
|
---|
| 190 | int main(int argc, char **argv)
|
---|
| 191 | {
|
---|
| 192 | // Get the top level suite from the registry
|
---|
| 193 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
| 194 |
|
---|
| 195 | // Adds the test to the list of test to run
|
---|
| 196 | CppUnit::TextUi::TestRunner runner;
|
---|
| 197 | runner.addTest( suite );
|
---|
| 198 |
|
---|
| 199 | // Change the default outputter to a compiler error format outputter
|
---|
| 200 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
| 201 | std::cerr ) );
|
---|
| 202 | // Run the tests.
|
---|
| 203 | bool wasSucessful = runner.run();
|
---|
| 204 |
|
---|
| 205 | // Return error code 1 if the one of test failed.
|
---|
| 206 | return wasSucessful ? 0 : 1;
|
---|
| 207 | };
|
---|