source: src/Patterns/Observer/unittests/stubs/ObserverStub.cpp@ 3324cf

Last change on this file since 3324cf was b760ac5, checked in by Frederik Heber <heber@…>, 14 years ago

Relay can now also relay notifications.

  • NOTE: We do not yet notify the Observables, whose update() we combine, when the Relay is destroyed. They have to signOff before by themselves (or by some other means, e.g. be destroyed before.).
  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[56d62f]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 * ObserverStub.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/extensions/HelperMacros.h>
21
22#include "Assert.hpp"
23
24#include "ObserverStub.hpp"
25
[e2e035e]26#include "Observer/Channels.hpp"
27#include "Observer/Notification.hpp"
28
[56d62f]29/************ UpdateCountObserver **************/
30
31UpdateCountObserver::UpdateCountObserver() :
32 Observer("UpdateCountObserver"),
33 updates(0)
34{};
35
[74e0f7]36UpdateCountObserver::~UpdateCountObserver()
37{}
38
[56d62f]39void UpdateCountObserver::update(Observable *publisher){
40 updates++;
41}
42
43void UpdateCountObserver::subjectKilled(Observable *publisher) {
44}
45
46/*************** SimpleObservable **************/
47
48SimpleObservable::SimpleObservable() :
49 Observable("SimpleObservable")
50{}
51
52void SimpleObservable::changeMethod() {
53 OBSERVE;
54 int i = 0;
55 i++;
56}
57
58/************** CallObservable *****************/
59
60CallObservable::CallObservable() :
61 Observable("CallObservable")
62{}
63
64void CallObservable::changeMethod1() {
65 OBSERVE;
66 int i = 0;
67 i++;
68}
69
70void CallObservable::changeMethod2() {
71 OBSERVE;
72 int i = 0;
73 i++;
74 changeMethod1();
75}
76
77/************* BlockObservable *****************/
78
79BlockObservable::BlockObservable() :
80 Observable("BlockObservable")
81{}
82
83void BlockObservable::changeMethod1(){
84 OBSERVE;
85 // test if we report correctly as blocked
86 CPPUNIT_ASSERT(isBlocked());
87}
88
89void BlockObservable::changeMethod2(){
90 OBSERVE;
91 internalMethod1();
92 internalMethod2();
93}
94
95void BlockObservable::internalMethod1(){
96 // we did not block, but our caller did...
97 // see if this is found
98 CPPUNIT_ASSERT(isBlocked());
99}
100
101void BlockObservable::internalMethod2(){
102 OBSERVE;
103 // Both this method and the caller do block
104 // Does the reporting still work as expected?
105 CPPUNIT_ASSERT(isBlocked());
106}
107
108void BlockObservable::noChangeMethod(){
109 // No Block introduced here
110 // reported correctely?
111 CPPUNIT_ASSERT(!isBlocked());
112}
113
114/*************** SuperObservable ***************/
115
116SuperObservable::SuperObservable():
117 Observable("SuperObservable")
118{
119 subObservable = new SimpleObservable();
120 subObservable->signOn(this);
121}
122SuperObservable::~SuperObservable(){
123 delete subObservable;
124}
125void SuperObservable::changeMethod() {
126 OBSERVE;
127 int i = 0;
128 i++;
129 subObservable->changeMethod();
130}
131
132/************* NotificationObservable **********/
133
134NotificationObservable::NotificationObservable() :
[74e0f7]135 Observable("NotificationObservable")
136{
[e2e035e]137 Channels *OurChannel = new Channels();
[bc2698]138 NotificationChannels.insert( std::make_pair(this, OurChannel) );
139 OurChannel->addChannel(Operation1Notify);
140 OurChannel->addChannel(Operation2Notify);
[74e0f7]141}
[56d62f]142
[74e0f7]143NotificationObservable::~NotificationObservable()
144{
[bc2698]145 NotificationChannels.erase(this);
[56d62f]146}
147
148void NotificationObservable::operation1(){
149 OBSERVE;
[74e0f7]150 NOTIFY(Operation1Notify);
[56d62f]151}
152
153void NotificationObservable::operation2(){
154 OBSERVE;
[74e0f7]155 NOTIFY(Operation2Notify);
[56d62f]156}
157
158/*********** NotificationObserver **************/
159
160NotificationObserver::NotificationObserver(Notification_ptr notification) :
161 Observer("NotificationObserver"),
162 requestedNotification(notification),
163 wasNotified(false)
164{}
165
[74e0f7]166NotificationObserver::~NotificationObserver()
167{}
168
[56d62f]169void NotificationObserver::update(Observable*){}
170void NotificationObserver::subjectKilled(Observable*){}
171void NotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
[40f2e6]172 CPPUNIT_ASSERT(requestedNotification==notification);
[56d62f]173 wasNotified = true;
174}
175
176/**************** ObservableSet ****************/
177
178ObservableSet::ObservableSet(int _num) :
179 Observable("ObservableCollection"),
180 num(_num)
181{
182 for(int i=0; i<num; ++i){
183 SimpleObservable *content = new SimpleObservable();
184 content->signOn(this);
185 theSet.insert(content);
186 }
187}
188
189ObservableSet::~ObservableSet(){
190 set::iterator iter;
191 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
192 delete (*iter);
193 }
194}
195
196ObservableSet::iterator ObservableSet::begin(){
197 return iterator(theSet.begin(),this);
198}
199
200ObservableSet::iterator ObservableSet::end(){
201 return iterator(theSet.end(),this);
202}
203
204/************** ObservableMap ******************/
205
206ObservableMap::ObservableMap(int _num) :
207 Observable("ObservableCollection"),
208 num(_num)
209{
210 for(int i=0; i<num; ++i){
211 SimpleObservable *content = new SimpleObservable();
212 content->signOn(this);
213 theSet.insert(std::make_pair(i,content));
214 }
215}
216
217ObservableMap::~ObservableMap(){
218 set::iterator iter;
219 for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
220 delete iter->second;
221 }
222}
223
224ObservableMap::iterator ObservableMap::begin(){
225 return iterator(theSet.begin(),this);
226}
227
228ObservableMap::iterator ObservableMap::end(){
229 return iterator(theSet.end(),this);
230}
231
[d85532]232/****************** Relay *********************/
233
234RelayTest::RelayTest() :
235 Relay(std::string("RelayTest"))
236{}
237
238RelayTest::~RelayTest()
239{}
240
[b760ac5]241/************ RelayNotification ***************/
242
243RelayNotification::RelayNotification() :
244 Relay(std::string("RelayTest"))
245{
246 Channels *OurChannel = new Channels();
247 NotificationChannels.insert( std::make_pair(this, OurChannel) );
248 OurChannel->addChannel(NotificationObservable::Operation1Notify);
249 OurChannel->addChannel(NotificationObservable::Operation2Notify);
250}
251
252RelayNotification::~RelayNotification()
253{}
254
[d85532]255/************ RelayCountObserver ***************/
256
257RelayCountObserver::RelayCountObserver(const Observable * const _relay) :
258 Observer("RelayCountObserver"),
259 updates(0),
260 relay(_relay)
261{}
262
263RelayCountObserver::~RelayCountObserver()
264{}
265
266void RelayCountObserver::update(Observable *publisher){
267 // check that we are not called by the relay itself
268 CPPUNIT_ASSERT(publisher != relay);
269 updates++;
270}
271
272void RelayCountObserver::subjectKilled(Observable *publisher) {
273}
274
[b760ac5]275/********* RelayNotificationObserver ***********/
276
277RelayNotificationObserver::RelayNotificationObserver(const Observable * const _relay) :
278 Observer("RelayNotificationObserver"),
279 wasNotified(false),
280 relay(_relay)
281{}
282
283RelayNotificationObserver::~RelayNotificationObserver()
284{}
285
286void RelayNotificationObserver::update(Observable*){}
287void RelayNotificationObserver::subjectKilled(Observable*){}
288void RelayNotificationObserver::recieveNotification(Observable *publisher, Notification_ptr notification){
289 // check that we are not called by the relay itself
290 CPPUNIT_ASSERT(publisher != relay);
291 wasNotified = true;
292}
293
Note: See TracBrowser for help on using the repository browser.