8ea3e58799e9c96d55a5e92222fda10b1d1d6745
[utils] / support / src / test / java / org / wamblee / observer / ObservableTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.observer;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import junit.framework.TestCase;
23
24 import org.jmock.Expectations;
25 import org.jmock.Mockery;
26
27 /**
28  * Test of the observer pattern implementation.
29  * 
30  * @author Erik Brakkee
31  */
32 public class ObservableTest extends TestCase {
33
34         private static final int SUBSCRIBER_COUNT = 100;
35
36         private static final String UPDATE = "send";
37
38         private Integer _observed;
39         private Observable<Integer, String> _observable;
40         private Mockery _context;
41
42         /*
43          * (non-Javadoc)
44          * 
45          * @see junit.framework.TestCase#setUp()
46          */
47         @Override
48         protected void setUp() throws Exception {
49                 super.setUp();
50                 _observed = new Integer(1);
51                 _observable = new Observable<Integer, String>(_observed,
52                                 new DefaultObserverNotifier());
53                 _context = new Mockery();
54
55         }
56
57         /**
58          * Tests subscription and notification of one subscriber.
59          */
60         public void testOneObserver() {
61                 final Observer observer = _context.mock(Observer.class);
62                 long subscription = _observable.subscribe(observer);
63
64                 assertEquals(1, _observable.getObserverCount());
65
66                 final String message = "hallo";
67                 _context.checking(new Expectations() {
68                         {
69                                 one(observer).send(_observed, message);
70                         }
71                 });
72
73                 _observable.send(message);
74                 _context.assertIsSatisfied();
75                 _observable.unsubscribe(subscription);
76                 assertEquals(0, _observable.getObserverCount());
77
78                 _observable.send(message);
79
80         }
81
82         /**
83          * Subscribes many susbcribers and sends notifications to subscribers.
84          * Verifies that unique subscription number are returned. Also verifies that
85          * the correct subscribers are notfied.
86          */
87         public void testManySubscribers() {
88                 final int nsubscribers = SUBSCRIBER_COUNT;
89                 final Observer[] mocks = new Observer[nsubscribers];
90
91                 List<Long> subscriptions = new ArrayList<Long>();
92                 for (int i = 0; i < nsubscribers; i++) {
93                         mocks[i] = _context.mock(Observer.class, "" + i);
94                         long subscription = _observable.subscribe(mocks[i]);
95                         assertTrue(subscriptions.add(subscription));
96                 }
97
98                 assertEquals(nsubscribers, _observable.getObserverCount());
99
100                 final String message = "hallo";
101                 _context.checking(new Expectations() {
102                         {
103                                 for (int i = 0; i < nsubscribers; i++) {
104                                         one(mocks[i]).send(_observed, message);
105                                 }
106                         }
107                 });
108
109                 _observable.send(message);
110                 _context.assertIsSatisfied();
111
112                 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
113                         _observable.unsubscribe(subscriptions.get(i));
114                 }
115                 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
116                                 _observable.getObserverCount());
117
118                 final String message2 = "blabla";
119                 _context.checking(new Expectations() {
120                         {
121                                 for (int i = 0; i < nsubscribers / 2; i++) {
122                                         one(mocks[i]).send(_observed, message2);
123                                 }
124                         }
125                 });
126                 _observable.send(message2);
127                 _context.assertIsSatisfied();
128         }
129
130         /**
131          * Subscribes and then unsubscribes with a wrong id. Verifies that
132          * IllegalArgumentException is thrown.
133          * 
134          */
135         public void testUnsubscribeWithWrongSubscription() {
136                 Observer<Integer, String> observer = _context.mock(Observer.class);
137                 long subscription = _observable.subscribe(observer);
138
139                 assertEquals(1, _observable.getObserverCount());
140
141                 try {
142                         _observable.unsubscribe(subscription + 1);
143                 } catch (IllegalArgumentException e) {
144                         return; // ok
145                 }
146                 fail();
147         }
148 }