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 | OBSERVE;
|
---|
42 | int i;
|
---|
43 | i++;
|
---|
44 | }
|
---|
45 | };
|
---|
46 |
|
---|
47 | class CallObservable : public Observable {
|
---|
48 | public:
|
---|
49 | void changeMethod1() {
|
---|
50 | OBSERVE;
|
---|
51 | int i;
|
---|
52 | i++;
|
---|
53 | }
|
---|
54 |
|
---|
55 | void changeMethod2() {
|
---|
56 | OBSERVE;
|
---|
57 | int i;
|
---|
58 | i++;
|
---|
59 | changeMethod1();
|
---|
60 | }
|
---|
61 | };
|
---|
62 |
|
---|
63 | class SuperObservable : public Observable {
|
---|
64 | public:
|
---|
65 | SuperObservable(){
|
---|
66 | subObservable = new SimpleObservable();
|
---|
67 | subObservable->signOn(this);
|
---|
68 | }
|
---|
69 | ~SuperObservable(){
|
---|
70 | delete subObservable;
|
---|
71 | }
|
---|
72 | void changeMethod() {
|
---|
73 | OBSERVE;
|
---|
74 | int i;
|
---|
75 | i++;
|
---|
76 | subObservable->changeMethod();
|
---|
77 | }
|
---|
78 | SimpleObservable *subObservable;
|
---|
79 | };
|
---|
80 |
|
---|
81 | /******************* actuall tests ***************/
|
---|
82 |
|
---|
83 | void ObserverTest::setUp() {
|
---|
84 | simpleObservable1 = new SimpleObservable();
|
---|
85 | simpleObservable2 = new SimpleObservable();
|
---|
86 | callObservable = new CallObservable();
|
---|
87 | superObservable = new SuperObservable();
|
---|
88 |
|
---|
89 | observer1 = new UpdateCountObserver();
|
---|
90 | observer2 = new UpdateCountObserver();
|
---|
91 | observer3 = new UpdateCountObserver();
|
---|
92 | observer4 = new UpdateCountObserver();
|
---|
93 | }
|
---|
94 |
|
---|
95 | void ObserverTest::tearDown() {
|
---|
96 | delete simpleObservable1;
|
---|
97 | delete simpleObservable2;
|
---|
98 | delete callObservable;
|
---|
99 | delete superObservable;
|
---|
100 |
|
---|
101 | delete observer1;
|
---|
102 | delete observer2;
|
---|
103 | delete observer3;
|
---|
104 | delete observer4;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void ObserverTest::doesUpdateTest()
|
---|
108 | {
|
---|
109 | simpleObservable1->signOn(observer1);
|
---|
110 | simpleObservable1->signOn(observer2);
|
---|
111 | simpleObservable1->signOn(observer3);
|
---|
112 | simpleObservable2->signOn(observer2);
|
---|
113 | simpleObservable2->signOn(observer4);
|
---|
114 |
|
---|
115 | simpleObservable1->changeMethod();
|
---|
116 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
117 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
118 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
119 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
|
---|
120 |
|
---|
121 | simpleObservable1->signOff(observer3);
|
---|
122 |
|
---|
123 | simpleObservable1->changeMethod();
|
---|
124 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
125 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
126 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
127 | CPPUNIT_ASSERT_EQUAL( 0, observer4->updates );
|
---|
128 |
|
---|
129 | simpleObservable2->changeMethod();
|
---|
130 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
131 | CPPUNIT_ASSERT_EQUAL( 3, observer2->updates );
|
---|
132 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
133 | CPPUNIT_ASSERT_EQUAL( 1, observer4->updates );
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | void ObserverTest::doesBlockUpdateTest() {
|
---|
138 | callObservable->signOn(observer1);
|
---|
139 |
|
---|
140 | callObservable->changeMethod1();
|
---|
141 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
142 |
|
---|
143 | callObservable->changeMethod2();
|
---|
144 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
145 | }
|
---|
146 |
|
---|
147 | void ObserverTest::doesSubObservableTest() {
|
---|
148 | superObservable->signOn(observer1);
|
---|
149 | superObservable->subObservable->signOn(observer2);
|
---|
150 |
|
---|
151 | superObservable->subObservable->changeMethod();
|
---|
152 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
153 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
154 |
|
---|
155 | superObservable->changeMethod();
|
---|
156 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
157 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | void ObserverTest::CircleDetectionTest() {
|
---|
162 | cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
|
---|
163 | cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
|
---|
164 | cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
|
---|
165 | 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;
|
---|
166 |
|
---|
167 | cout << endl << endl << "The following errors displayed by the observer framework can be ignored" << endl;
|
---|
168 |
|
---|
169 | // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
|
---|
170 | simpleObservable1->signOn(simpleObservable1);
|
---|
171 | simpleObservable1->changeMethod();
|
---|
172 |
|
---|
173 | // more complex test
|
---|
174 | simpleObservable1->signOff(simpleObservable1);
|
---|
175 | simpleObservable1->signOn(simpleObservable2);
|
---|
176 | simpleObservable2->signOn(simpleObservable1);
|
---|
177 | simpleObservable1->changeMethod();
|
---|
178 | simpleObservable1->signOff(simpleObservable2);
|
---|
179 | simpleObservable2->signOff(simpleObservable1);
|
---|
180 | // when we reach this line, although we broke the DAG assumption the circle check works fine
|
---|
181 | CPPUNIT_ASSERT(true);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /********************************************** Main routine **************************************/
|
---|
185 |
|
---|
186 | int main(int argc, char **argv)
|
---|
187 | {
|
---|
188 | // Get the top level suite from the registry
|
---|
189 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
190 |
|
---|
191 | // Adds the test to the list of test to run
|
---|
192 | CppUnit::TextUi::TestRunner runner;
|
---|
193 | runner.addTest( suite );
|
---|
194 |
|
---|
195 | // Change the default outputter to a compiler error format outputter
|
---|
196 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
197 | std::cerr ) );
|
---|
198 | // Run the tests.
|
---|
199 | bool wasSucessful = runner.run();
|
---|
200 |
|
---|
201 | // Return error code 1 if the one of test failed.
|
---|
202 | return wasSucessful ? 0 : 1;
|
---|
203 | };
|
---|