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() {
|
---|
88 | simpleObservable = new SimpleObservable();
|
---|
89 | callObservable = new CallObservable();
|
---|
90 | superObservable = new SuperObservable();
|
---|
91 |
|
---|
92 | observer1 = new UpdateCountObserver();
|
---|
93 | observer2 = new UpdateCountObserver();
|
---|
94 | observer3 = new UpdateCountObserver();
|
---|
95 | }
|
---|
96 |
|
---|
97 | void ObserverTest::tearDown() {
|
---|
98 | delete simpleObservable;
|
---|
99 | delete callObservable;
|
---|
100 | delete superObservable;
|
---|
101 |
|
---|
102 | delete observer1;
|
---|
103 | delete observer2;
|
---|
104 | delete observer3;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void ObserverTest::doesUpdateTest()
|
---|
108 | {
|
---|
109 | simpleObservable->signOn(observer1);
|
---|
110 | simpleObservable->signOn(observer2);
|
---|
111 | simpleObservable->signOn(observer3);
|
---|
112 |
|
---|
113 | simpleObservable->changeMethod();
|
---|
114 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
115 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
116 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
117 |
|
---|
118 | simpleObservable->signOff(observer3);
|
---|
119 |
|
---|
120 | simpleObservable->changeMethod();
|
---|
121 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
122 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
123 | CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | void ObserverTest::doesBlockUpdateTest() {
|
---|
128 | callObservable->signOn(observer1);
|
---|
129 |
|
---|
130 | callObservable->changeMethod1();
|
---|
131 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
132 |
|
---|
133 | callObservable->changeMethod2();
|
---|
134 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
135 | }
|
---|
136 |
|
---|
137 | void ObserverTest::doesSubObservableTest() {
|
---|
138 | superObservable->signOn(observer1);
|
---|
139 | superObservable->subObservable->signOn(observer2);
|
---|
140 |
|
---|
141 | superObservable->subObservable->changeMethod();
|
---|
142 | CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
|
---|
143 | CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
|
---|
144 |
|
---|
145 | superObservable->changeMethod();
|
---|
146 | CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
|
---|
147 | CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | void ObserverTest::CircleDetectionTest() {
|
---|
152 | cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
|
---|
153 | cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
|
---|
154 | cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
|
---|
155 | 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;
|
---|
156 |
|
---|
157 | cout << endl << endl << "The following error displayed by the observer framwork can be ignored" << endl;
|
---|
158 |
|
---|
159 | // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
|
---|
160 | simpleObservable->signOn(simpleObservable);
|
---|
161 | simpleObservable->changeMethod();
|
---|
162 | // when we reach this line, although we broke the DAG assumption the circle check works fine
|
---|
163 | CPPUNIT_ASSERT(true);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /********************************************** Main routine **************************************/
|
---|
167 |
|
---|
168 | int main(int argc, char **argv)
|
---|
169 | {
|
---|
170 | // Get the top level suite from the registry
|
---|
171 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
172 |
|
---|
173 | // Adds the test to the list of test to run
|
---|
174 | CppUnit::TextUi::TestRunner runner;
|
---|
175 | runner.addTest( suite );
|
---|
176 |
|
---|
177 | // Change the default outputter to a compiler error format outputter
|
---|
178 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
179 | std::cerr ) );
|
---|
180 | // Run the tests.
|
---|
181 | bool wasSucessful = runner.run();
|
---|
182 |
|
---|
183 | // Return error code 1 if the one of test failed.
|
---|
184 | return wasSucessful ? 0 : 1;
|
---|
185 | };
|
---|