• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Guava 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 com.google.common.eventbus;
18 
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Iterators;
21 import java.util.Iterator;
22 import junit.framework.TestCase;
23 
24 /**
25  * Tests for {@link SubscriberRegistry}.
26  *
27  * @author Colin Decker
28  */
29 public class SubscriberRegistryTest extends TestCase {
30 
31   private final SubscriberRegistry registry = new SubscriberRegistry(new EventBus());
32 
testRegister()33   public void testRegister() {
34     assertEquals(0, registry.getSubscribersForTesting(String.class).size());
35 
36     registry.register(new StringSubscriber());
37     assertEquals(1, registry.getSubscribersForTesting(String.class).size());
38 
39     registry.register(new StringSubscriber());
40     assertEquals(2, registry.getSubscribersForTesting(String.class).size());
41 
42     registry.register(new ObjectSubscriber());
43     assertEquals(2, registry.getSubscribersForTesting(String.class).size());
44     assertEquals(1, registry.getSubscribersForTesting(Object.class).size());
45   }
46 
testUnregister()47   public void testUnregister() {
48     StringSubscriber s1 = new StringSubscriber();
49     StringSubscriber s2 = new StringSubscriber();
50 
51     registry.register(s1);
52     registry.register(s2);
53 
54     registry.unregister(s1);
55     assertEquals(1, registry.getSubscribersForTesting(String.class).size());
56 
57     registry.unregister(s2);
58     assertTrue(registry.getSubscribersForTesting(String.class).isEmpty());
59   }
60 
testUnregister_notRegistered()61   public void testUnregister_notRegistered() {
62     try {
63       registry.unregister(new StringSubscriber());
64       fail();
65     } catch (IllegalArgumentException expected) {
66     }
67 
68     StringSubscriber s1 = new StringSubscriber();
69     registry.register(s1);
70     try {
71       registry.unregister(new StringSubscriber());
72       fail();
73     } catch (IllegalArgumentException expected) {
74       // a StringSubscriber was registered, but not the same one we tried to unregister
75     }
76 
77     registry.unregister(s1);
78 
79     try {
80       registry.unregister(s1);
81       fail();
82     } catch (IllegalArgumentException expected) {
83     }
84   }
85 
testGetSubscribers()86   public void testGetSubscribers() {
87     assertEquals(0, Iterators.size(registry.getSubscribers("")));
88 
89     registry.register(new StringSubscriber());
90     assertEquals(1, Iterators.size(registry.getSubscribers("")));
91 
92     registry.register(new StringSubscriber());
93     assertEquals(2, Iterators.size(registry.getSubscribers("")));
94 
95     registry.register(new ObjectSubscriber());
96     assertEquals(3, Iterators.size(registry.getSubscribers("")));
97     assertEquals(1, Iterators.size(registry.getSubscribers(new Object())));
98     assertEquals(1, Iterators.size(registry.getSubscribers(1)));
99 
100     registry.register(new IntegerSubscriber());
101     assertEquals(3, Iterators.size(registry.getSubscribers("")));
102     assertEquals(1, Iterators.size(registry.getSubscribers(new Object())));
103     assertEquals(2, Iterators.size(registry.getSubscribers(1)));
104   }
105 
testGetSubscribers_returnsImmutableSnapshot()106   public void testGetSubscribers_returnsImmutableSnapshot() {
107     StringSubscriber s1 = new StringSubscriber();
108     StringSubscriber s2 = new StringSubscriber();
109     ObjectSubscriber o1 = new ObjectSubscriber();
110 
111     Iterator<Subscriber> empty = registry.getSubscribers("");
112     assertFalse(empty.hasNext());
113 
114     empty = registry.getSubscribers("");
115 
116     registry.register(s1);
117     assertFalse(empty.hasNext());
118 
119     Iterator<Subscriber> one = registry.getSubscribers("");
120     assertEquals(s1, one.next().target);
121     assertFalse(one.hasNext());
122 
123     one = registry.getSubscribers("");
124 
125     registry.register(s2);
126     registry.register(o1);
127 
128     Iterator<Subscriber> three = registry.getSubscribers("");
129     assertEquals(s1, one.next().target);
130     assertFalse(one.hasNext());
131 
132     assertEquals(s1, three.next().target);
133     assertEquals(s2, three.next().target);
134     assertEquals(o1, three.next().target);
135     assertFalse(three.hasNext());
136 
137     three = registry.getSubscribers("");
138 
139     registry.unregister(s2);
140 
141     assertEquals(s1, three.next().target);
142     assertEquals(s2, three.next().target);
143     assertEquals(o1, three.next().target);
144     assertFalse(three.hasNext());
145 
146     Iterator<Subscriber> two = registry.getSubscribers("");
147     assertEquals(s1, two.next().target);
148     assertEquals(o1, two.next().target);
149     assertFalse(two.hasNext());
150   }
151 
152   public static class StringSubscriber {
153 
154     @Subscribe
handle(String s)155     public void handle(String s) {}
156   }
157 
158   public static class IntegerSubscriber {
159 
160     @Subscribe
handle(Integer i)161     public void handle(Integer i) {}
162   }
163 
164   public static class ObjectSubscriber {
165 
166     @Subscribe
handle(Object o)167     public void handle(Object o) {}
168   }
169 
testFlattenHierarchy()170   public void testFlattenHierarchy() {
171     assertEquals(
172         ImmutableSet.of(
173             Object.class,
174             HierarchyFixtureInterface.class,
175             HierarchyFixtureSubinterface.class,
176             HierarchyFixtureParent.class,
177             HierarchyFixture.class),
178         SubscriberRegistry.flattenHierarchy(HierarchyFixture.class));
179   }
180 
181   private interface HierarchyFixtureInterface {
182     // Exists only for hierarchy mapping; no members.
183   }
184 
185   private interface HierarchyFixtureSubinterface extends HierarchyFixtureInterface {
186     // Exists only for hierarchy mapping; no members.
187   }
188 
189   private static class HierarchyFixtureParent implements HierarchyFixtureSubinterface {
190     // Exists only for hierarchy mapping; no members.
191   }
192 
193   private static class HierarchyFixture extends HierarchyFixtureParent {
194     // Exists only for hierarchy mapping; no members.
195   }
196 }
197