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