(no commit message)
[utils] / support / general / src / test / java / org / wamblee / observer / ObservableTest.java
1 /*
2  * Copyright 2005-2010 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  * Test of the observer pattern implementation.
28  * 
29  * @author Erik Brakkee
30  */
31 public class ObservableTest extends TestCase {
32     private static final int SUBSCRIBER_COUNT = 100;
33     private static final String UPDATE = "send";
34     private Integer observed;
35     private Observable<Integer, String> observable;
36
37     /*
38      * (non-Javadoc)
39      * 
40      * @see junit.framework.TestCase#setUp()
41      */
42     @Override
43     protected void setUp() throws Exception {
44         super.setUp();
45         observed = Integer.valueOf(1);
46         observable = new Observable<Integer, String>(observed,
47             new DefaultObserverNotifier());
48     }
49
50     /**
51      * Tests subscription and notification of one subscriber.
52      */
53     public void testOneObserver() {
54         final Observer mockObserver = mock(Observer.class);
55         InOrder order = inOrder(mockObserver);
56
57         long subscription = observable.subscribe(mockObserver);
58
59         assertEquals(1, observable.getObserverCount());
60
61         final String message = "hallo";
62         observable.send(message);
63
64         order.verify(mockObserver).send(observed, message);
65         verifyNoMoreInteractions(mockObserver);
66
67         observable.unsubscribe(subscription);
68         assertEquals(0, observable.getObserverCount());
69
70         observable.send(message);
71         verifyNoMoreInteractions(mockObserver);
72     }
73
74     /**
75      * Subscribes many susbcribers and sends notifications to subscribers.
76      * Verifies that unique subscription number are returned. Also verifies that
77      * the correct subscribers are notfied.
78      */
79     public void testManySubscribers() {
80         final int nsubscribers = SUBSCRIBER_COUNT;
81         final Observer[] mocks = new Observer[nsubscribers];
82         final InOrder[] order = new InOrder[nsubscribers];
83
84         List<Long> subscriptions = new ArrayList<Long>();
85
86         for (int i = 0; i < nsubscribers; i++) {
87             mocks[i] = mock(Observer.class);
88             order[i] = inOrder(mocks[i]);
89
90             long subscription = observable.subscribe(mocks[i]);
91             assertTrue(subscriptions.add(subscription));
92         }
93
94         assertEquals(nsubscribers, observable.getObserverCount());
95
96         final String message = "hallo";
97
98         observable.send(message);
99
100         for (int i = 0; i < nsubscribers; i++) {
101             order[i].verify(mocks[i]).send(observed, message);
102         }
103
104         for (int i = nsubscribers / 2; i < nsubscribers; i++) {
105             observable.unsubscribe(subscriptions.get(i));
106         }
107
108         assertEquals(nsubscribers - (nsubscribers - (nsubscribers / 2)),
109             observable.getObserverCount());
110
111         final String message2 = "blabla";
112
113         observable.send(message2);
114
115         for (int i = 0; i < (nsubscribers / 2); i++) {
116             order[i].verify(mocks[i]).send(observed, message2);
117         }
118
119         for (int i = nsubscribers / 2; i < nsubscribers; i++) {
120             verifyNoMoreInteractions(mocks[i]);
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
142         fail();
143     }
144 }