e26571a40d57708975e963b2640fed05373abfd3
[utils] / support / general / 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 static org.mockito.Mockito.*; 
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.mockito.InOrder;
25
26 import junit.framework.TestCase;
27
28 /**
29  * Test of the observer pattern implementation.
30  * 
31  * @author Erik Brakkee
32  */
33 public class ObservableTest extends TestCase {
34
35         private static final int SUBSCRIBER_COUNT = 100;
36
37         private static final String UPDATE = "send";
38
39         private Integer observed;
40         private Observable<Integer, String> observable;
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         }
54
55         /**
56          * Tests subscription and notification of one subscriber.
57          */
58         public void testOneObserver() {
59                 final Observer mockObserver = mock(Observer.class);
60                 InOrder order = inOrder(mockObserver);
61                 
62                 long subscription = observable.subscribe(mockObserver);
63
64                 assertEquals(1, observable.getObserverCount());
65
66                 final String message = "hallo";
67                 observable.send(message);
68                 
69                 order.verify(mockObserver).send(observed, message);
70                 verifyNoMoreInteractions(mockObserver);
71                 
72                 observable.unsubscribe(subscription);
73                 assertEquals(0, observable.getObserverCount());
74
75                 observable.send(message);
76                 verifyNoMoreInteractions(mockObserver);
77         }
78
79         /**
80          * Subscribes many susbcribers and sends notifications to subscribers.
81          * Verifies that unique subscription number are returned. Also verifies that
82          * the correct subscribers are notfied.
83          */
84         public void testManySubscribers() {
85                 final int nsubscribers = SUBSCRIBER_COUNT;
86                 final Observer[] mocks = new Observer[nsubscribers];
87                 final InOrder[] order = new InOrder[nsubscribers];
88
89                 List<Long> subscriptions = new ArrayList<Long>();
90                 for (int i = 0; i < nsubscribers; i++) {
91                         mocks[i] = mock(Observer.class);
92                         order[i] = inOrder(mocks[i]);
93                         long subscription = observable.subscribe(mocks[i]);
94                         assertTrue(subscriptions.add(subscription));
95                 }
96
97                 assertEquals(nsubscribers, observable.getObserverCount());
98
99                 final String message = "hallo";
100
101                 observable.send(message);
102                 for (int i = 0; i < nsubscribers; i++) {
103                         order[i].verify(mocks[i]).send(observed, message);
104                 }
105
106                 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
107                         observable.unsubscribe(subscriptions.get(i));
108                 }
109                 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
110                                 observable.getObserverCount());
111
112                 final String message2 = "blabla";
113
114                 observable.send(message2);
115                 for (int i = 0; i < nsubscribers / 2; i++) {
116                         order[i].verify(mocks[i]).send(observed, message2);
117                 }               
118                 for (int i = nsubscribers/2; i < nsubscribers; i++) {
119                         verifyNoMoreInteractions(mocks[i]);
120                 }               
121                 
122         }
123
124         /**
125          * Subscribes and then unsubscribes with a wrong id. Verifies that
126          * IllegalArgumentException is thrown.
127          * 
128          */
129         public void testUnsubscribeWithWrongSubscription() {
130                 Observer<Integer, String> observer = mock(Observer.class);
131         
132                 long subscription = observable.subscribe(observer);
133
134                 assertEquals(1, observable.getObserverCount());
135
136                 try {
137                         observable.unsubscribe(subscription + 1);
138                 } catch (IllegalArgumentException e) {
139                         return; // ok
140                 }
141                 fail();
142         }
143 }