1 /* 2 * Copyright (C) 2012 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.outside; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.collect.Lists; 22 import com.google.common.eventbus.EventBus; 23 import com.google.common.eventbus.Subscribe; 24 import java.util.List; 25 import junit.framework.TestCase; 26 27 /** 28 * Test that EventBus finds the correct subscribers. 29 * 30 * <p>This test must be outside the c.g.c.eventbus package to test correctly. 31 * 32 * @author Louis Wasserman 33 */ 34 public class AnnotatedSubscriberFinderTests { 35 36 private static final Object EVENT = new Object(); 37 38 abstract static class AbstractEventBusTest<H> extends TestCase { createSubscriber()39 abstract H createSubscriber(); 40 41 private H subscriber; 42 getSubscriber()43 H getSubscriber() { 44 return subscriber; 45 } 46 47 @Override setUp()48 protected void setUp() throws Exception { 49 subscriber = createSubscriber(); 50 EventBus bus = new EventBus(); 51 bus.register(subscriber); 52 bus.post(EVENT); 53 } 54 55 @Override tearDown()56 protected void tearDown() throws Exception { 57 subscriber = null; 58 } 59 } 60 61 /* 62 * We break the tests up based on whether they are annotated or abstract in the superclass. 63 */ 64 public static class BaseSubscriberFinderTest 65 extends AbstractEventBusTest<BaseSubscriberFinderTest.Subscriber> { 66 static class Subscriber { 67 final List<Object> nonSubscriberEvents = Lists.newArrayList(); 68 final List<Object> subscriberEvents = Lists.newArrayList(); 69 notASubscriber(Object o)70 public void notASubscriber(Object o) { 71 nonSubscriberEvents.add(o); 72 } 73 74 @Subscribe subscriber(Object o)75 public void subscriber(Object o) { 76 subscriberEvents.add(o); 77 } 78 } 79 testNonSubscriber()80 public void testNonSubscriber() { 81 assertThat(getSubscriber().nonSubscriberEvents).isEmpty(); 82 } 83 testSubscriber()84 public void testSubscriber() { 85 assertThat(getSubscriber().subscriberEvents).contains(EVENT); 86 } 87 88 @Override createSubscriber()89 Subscriber createSubscriber() { 90 return new Subscriber(); 91 } 92 } 93 94 public static class AnnotatedAndAbstractInSuperclassTest 95 extends AbstractEventBusTest<AnnotatedAndAbstractInSuperclassTest.SubClass> { 96 abstract static class SuperClass { 97 @Subscribe overriddenAndAnnotatedInSubclass(Object o)98 public abstract void overriddenAndAnnotatedInSubclass(Object o); 99 100 @Subscribe overriddenInSubclass(Object o)101 public abstract void overriddenInSubclass(Object o); 102 } 103 104 static class SubClass extends SuperClass { 105 final List<Object> overriddenAndAnnotatedInSubclassEvents = Lists.newArrayList(); 106 final List<Object> overriddenInSubclassEvents = Lists.newArrayList(); 107 108 @Subscribe 109 @Override overriddenAndAnnotatedInSubclass(Object o)110 public void overriddenAndAnnotatedInSubclass(Object o) { 111 overriddenAndAnnotatedInSubclassEvents.add(o); 112 } 113 114 @Override overriddenInSubclass(Object o)115 public void overriddenInSubclass(Object o) { 116 overriddenInSubclassEvents.add(o); 117 } 118 } 119 testOverriddenAndAnnotatedInSubclass()120 public void testOverriddenAndAnnotatedInSubclass() { 121 assertThat(getSubscriber().overriddenAndAnnotatedInSubclassEvents).contains(EVENT); 122 } 123 testOverriddenNotAnnotatedInSubclass()124 public void testOverriddenNotAnnotatedInSubclass() { 125 assertThat(getSubscriber().overriddenInSubclassEvents).contains(EVENT); 126 } 127 128 @Override createSubscriber()129 SubClass createSubscriber() { 130 return new SubClass(); 131 } 132 } 133 134 public static class AnnotatedNotAbstractInSuperclassTest 135 extends AbstractEventBusTest<AnnotatedNotAbstractInSuperclassTest.SubClass> { 136 static class SuperClass { 137 final List<Object> notOverriddenInSubclassEvents = Lists.newArrayList(); 138 final List<Object> overriddenNotAnnotatedInSubclassEvents = Lists.newArrayList(); 139 final List<Object> overriddenAndAnnotatedInSubclassEvents = Lists.newArrayList(); 140 final List<Object> differentlyOverriddenNotAnnotatedInSubclassBadEvents = 141 Lists.newArrayList(); 142 final List<Object> differentlyOverriddenAnnotatedInSubclassBadEvents = Lists.newArrayList(); 143 144 @Subscribe notOverriddenInSubclass(Object o)145 public void notOverriddenInSubclass(Object o) { 146 notOverriddenInSubclassEvents.add(o); 147 } 148 149 @Subscribe overriddenNotAnnotatedInSubclass(Object o)150 public void overriddenNotAnnotatedInSubclass(Object o) { 151 overriddenNotAnnotatedInSubclassEvents.add(o); 152 } 153 154 @Subscribe overriddenAndAnnotatedInSubclass(Object o)155 public void overriddenAndAnnotatedInSubclass(Object o) { 156 overriddenAndAnnotatedInSubclassEvents.add(o); 157 } 158 159 @Subscribe differentlyOverriddenNotAnnotatedInSubclass(Object o)160 public void differentlyOverriddenNotAnnotatedInSubclass(Object o) { 161 // the subclass overrides this and does *not* call super.dONAIS(o) 162 differentlyOverriddenNotAnnotatedInSubclassBadEvents.add(o); 163 } 164 165 @Subscribe differentlyOverriddenAnnotatedInSubclass(Object o)166 public void differentlyOverriddenAnnotatedInSubclass(Object o) { 167 // the subclass overrides this and does *not* call super.dOAIS(o) 168 differentlyOverriddenAnnotatedInSubclassBadEvents.add(o); 169 } 170 } 171 172 static class SubClass extends SuperClass { 173 final List<Object> differentlyOverriddenNotAnnotatedInSubclassGoodEvents = 174 Lists.newArrayList(); 175 final List<Object> differentlyOverriddenAnnotatedInSubclassGoodEvents = Lists.newArrayList(); 176 177 @Override overriddenNotAnnotatedInSubclass(Object o)178 public void overriddenNotAnnotatedInSubclass(Object o) { 179 super.overriddenNotAnnotatedInSubclass(o); 180 } 181 182 @Subscribe 183 @Override overriddenAndAnnotatedInSubclass(Object o)184 public void overriddenAndAnnotatedInSubclass(Object o) { 185 super.overriddenAndAnnotatedInSubclass(o); 186 } 187 188 @Override differentlyOverriddenNotAnnotatedInSubclass(Object o)189 public void differentlyOverriddenNotAnnotatedInSubclass(Object o) { 190 differentlyOverriddenNotAnnotatedInSubclassGoodEvents.add(o); 191 } 192 193 @Subscribe 194 @Override differentlyOverriddenAnnotatedInSubclass(Object o)195 public void differentlyOverriddenAnnotatedInSubclass(Object o) { 196 differentlyOverriddenAnnotatedInSubclassGoodEvents.add(o); 197 } 198 } 199 testNotOverriddenInSubclass()200 public void testNotOverriddenInSubclass() { 201 assertThat(getSubscriber().notOverriddenInSubclassEvents).contains(EVENT); 202 } 203 testOverriddenNotAnnotatedInSubclass()204 public void testOverriddenNotAnnotatedInSubclass() { 205 assertThat(getSubscriber().overriddenNotAnnotatedInSubclassEvents).contains(EVENT); 206 } 207 testDifferentlyOverriddenNotAnnotatedInSubclass()208 public void testDifferentlyOverriddenNotAnnotatedInSubclass() { 209 assertThat(getSubscriber().differentlyOverriddenNotAnnotatedInSubclassGoodEvents) 210 .contains(EVENT); 211 assertThat(getSubscriber().differentlyOverriddenNotAnnotatedInSubclassBadEvents).isEmpty(); 212 } 213 testOverriddenAndAnnotatedInSubclass()214 public void testOverriddenAndAnnotatedInSubclass() { 215 assertThat(getSubscriber().overriddenAndAnnotatedInSubclassEvents).contains(EVENT); 216 } 217 testDifferentlyOverriddenAndAnnotatedInSubclass()218 public void testDifferentlyOverriddenAndAnnotatedInSubclass() { 219 assertThat(getSubscriber().differentlyOverriddenAnnotatedInSubclassGoodEvents) 220 .contains(EVENT); 221 assertThat(getSubscriber().differentlyOverriddenAnnotatedInSubclassBadEvents).isEmpty(); 222 } 223 224 @Override createSubscriber()225 SubClass createSubscriber() { 226 return new SubClass(); 227 } 228 } 229 230 public static class AbstractNotAnnotatedInSuperclassTest 231 extends AbstractEventBusTest<AbstractNotAnnotatedInSuperclassTest.SubClass> { 232 abstract static class SuperClass { overriddenInSubclassNowhereAnnotated(Object o)233 public abstract void overriddenInSubclassNowhereAnnotated(Object o); 234 overriddenAndAnnotatedInSubclass(Object o)235 public abstract void overriddenAndAnnotatedInSubclass(Object o); 236 } 237 238 static class SubClass extends SuperClass { 239 final List<Object> overriddenInSubclassNowhereAnnotatedEvents = Lists.newArrayList(); 240 final List<Object> overriddenAndAnnotatedInSubclassEvents = Lists.newArrayList(); 241 242 @Override overriddenInSubclassNowhereAnnotated(Object o)243 public void overriddenInSubclassNowhereAnnotated(Object o) { 244 overriddenInSubclassNowhereAnnotatedEvents.add(o); 245 } 246 247 @Subscribe 248 @Override overriddenAndAnnotatedInSubclass(Object o)249 public void overriddenAndAnnotatedInSubclass(Object o) { 250 overriddenAndAnnotatedInSubclassEvents.add(o); 251 } 252 } 253 testOverriddenAndAnnotatedInSubclass()254 public void testOverriddenAndAnnotatedInSubclass() { 255 assertThat(getSubscriber().overriddenAndAnnotatedInSubclassEvents).contains(EVENT); 256 } 257 testOverriddenInSubclassNowhereAnnotated()258 public void testOverriddenInSubclassNowhereAnnotated() { 259 assertThat(getSubscriber().overriddenInSubclassNowhereAnnotatedEvents).isEmpty(); 260 } 261 262 @Override createSubscriber()263 SubClass createSubscriber() { 264 return new SubClass(); 265 } 266 } 267 268 public static class NeitherAbstractNorAnnotatedInSuperclassTest 269 extends AbstractEventBusTest<NeitherAbstractNorAnnotatedInSuperclassTest.SubClass> { 270 static class SuperClass { 271 final List<Object> neitherOverriddenNorAnnotatedEvents = Lists.newArrayList(); 272 final List<Object> overriddenInSubclassNowhereAnnotatedEvents = Lists.newArrayList(); 273 final List<Object> overriddenAndAnnotatedInSubclassEvents = Lists.newArrayList(); 274 neitherOverriddenNorAnnotated(Object o)275 public void neitherOverriddenNorAnnotated(Object o) { 276 neitherOverriddenNorAnnotatedEvents.add(o); 277 } 278 overriddenInSubclassNowhereAnnotated(Object o)279 public void overriddenInSubclassNowhereAnnotated(Object o) { 280 overriddenInSubclassNowhereAnnotatedEvents.add(o); 281 } 282 overriddenAndAnnotatedInSubclass(Object o)283 public void overriddenAndAnnotatedInSubclass(Object o) { 284 overriddenAndAnnotatedInSubclassEvents.add(o); 285 } 286 } 287 288 static class SubClass extends SuperClass { 289 @Override overriddenInSubclassNowhereAnnotated(Object o)290 public void overriddenInSubclassNowhereAnnotated(Object o) { 291 super.overriddenInSubclassNowhereAnnotated(o); 292 } 293 294 @Subscribe 295 @Override overriddenAndAnnotatedInSubclass(Object o)296 public void overriddenAndAnnotatedInSubclass(Object o) { 297 super.overriddenAndAnnotatedInSubclass(o); 298 } 299 } 300 testNeitherOverriddenNorAnnotated()301 public void testNeitherOverriddenNorAnnotated() { 302 assertThat(getSubscriber().neitherOverriddenNorAnnotatedEvents).isEmpty(); 303 } 304 testOverriddenInSubclassNowhereAnnotated()305 public void testOverriddenInSubclassNowhereAnnotated() { 306 assertThat(getSubscriber().overriddenInSubclassNowhereAnnotatedEvents).isEmpty(); 307 } 308 testOverriddenAndAnnotatedInSubclass()309 public void testOverriddenAndAnnotatedInSubclass() { 310 assertThat(getSubscriber().overriddenAndAnnotatedInSubclassEvents).contains(EVENT); 311 } 312 313 @Override createSubscriber()314 SubClass createSubscriber() { 315 return new SubClass(); 316 } 317 } 318 319 public static class DeepInterfaceTest 320 extends AbstractEventBusTest<DeepInterfaceTest.SubscriberClass> { 321 interface Interface1 { 322 @Subscribe annotatedIn1(Object o)323 void annotatedIn1(Object o); 324 325 @Subscribe annotatedIn1And2(Object o)326 void annotatedIn1And2(Object o); 327 328 @Subscribe annotatedIn1And2AndClass(Object o)329 void annotatedIn1And2AndClass(Object o); 330 declaredIn1AnnotatedIn2(Object o)331 void declaredIn1AnnotatedIn2(Object o); 332 declaredIn1AnnotatedInClass(Object o)333 void declaredIn1AnnotatedInClass(Object o); 334 nowhereAnnotated(Object o)335 void nowhereAnnotated(Object o); 336 } 337 338 interface Interface2 extends Interface1 { 339 @Override 340 @Subscribe declaredIn1AnnotatedIn2(Object o)341 void declaredIn1AnnotatedIn2(Object o); 342 343 @Override 344 @Subscribe annotatedIn1And2(Object o)345 void annotatedIn1And2(Object o); 346 347 @Override 348 @Subscribe annotatedIn1And2AndClass(Object o)349 void annotatedIn1And2AndClass(Object o); 350 declaredIn2AnnotatedInClass(Object o)351 void declaredIn2AnnotatedInClass(Object o); 352 353 @Subscribe annotatedIn2(Object o)354 void annotatedIn2(Object o); 355 } 356 357 static class SubscriberClass implements Interface2 { 358 final List<Object> annotatedIn1Events = Lists.newArrayList(); 359 final List<Object> annotatedIn1And2Events = Lists.newArrayList(); 360 final List<Object> annotatedIn1And2AndClassEvents = Lists.newArrayList(); 361 final List<Object> declaredIn1AnnotatedIn2Events = Lists.newArrayList(); 362 final List<Object> declaredIn1AnnotatedInClassEvents = Lists.newArrayList(); 363 final List<Object> declaredIn2AnnotatedInClassEvents = Lists.newArrayList(); 364 final List<Object> annotatedIn2Events = Lists.newArrayList(); 365 final List<Object> nowhereAnnotatedEvents = Lists.newArrayList(); 366 367 @Override annotatedIn1(Object o)368 public void annotatedIn1(Object o) { 369 annotatedIn1Events.add(o); 370 } 371 372 @Subscribe 373 @Override declaredIn1AnnotatedInClass(Object o)374 public void declaredIn1AnnotatedInClass(Object o) { 375 declaredIn1AnnotatedInClassEvents.add(o); 376 } 377 378 @Override declaredIn1AnnotatedIn2(Object o)379 public void declaredIn1AnnotatedIn2(Object o) { 380 declaredIn1AnnotatedIn2Events.add(o); 381 } 382 383 @Override annotatedIn1And2(Object o)384 public void annotatedIn1And2(Object o) { 385 annotatedIn1And2Events.add(o); 386 } 387 388 @Subscribe 389 @Override annotatedIn1And2AndClass(Object o)390 public void annotatedIn1And2AndClass(Object o) { 391 annotatedIn1And2AndClassEvents.add(o); 392 } 393 394 @Subscribe 395 @Override declaredIn2AnnotatedInClass(Object o)396 public void declaredIn2AnnotatedInClass(Object o) { 397 declaredIn2AnnotatedInClassEvents.add(o); 398 } 399 400 @Override annotatedIn2(Object o)401 public void annotatedIn2(Object o) { 402 annotatedIn2Events.add(o); 403 } 404 405 @Override nowhereAnnotated(Object o)406 public void nowhereAnnotated(Object o) { 407 nowhereAnnotatedEvents.add(o); 408 } 409 } 410 testAnnotatedIn1()411 public void testAnnotatedIn1() { 412 assertThat(getSubscriber().annotatedIn1Events).contains(EVENT); 413 } 414 testAnnotatedIn2()415 public void testAnnotatedIn2() { 416 assertThat(getSubscriber().annotatedIn2Events).contains(EVENT); 417 } 418 testAnnotatedIn1And2()419 public void testAnnotatedIn1And2() { 420 assertThat(getSubscriber().annotatedIn1And2Events).contains(EVENT); 421 } 422 testAnnotatedIn1And2AndClass()423 public void testAnnotatedIn1And2AndClass() { 424 assertThat(getSubscriber().annotatedIn1And2AndClassEvents).contains(EVENT); 425 } 426 testDeclaredIn1AnnotatedIn2()427 public void testDeclaredIn1AnnotatedIn2() { 428 assertThat(getSubscriber().declaredIn1AnnotatedIn2Events).contains(EVENT); 429 } 430 testDeclaredIn1AnnotatedInClass()431 public void testDeclaredIn1AnnotatedInClass() { 432 assertThat(getSubscriber().declaredIn1AnnotatedInClassEvents).contains(EVENT); 433 } 434 testDeclaredIn2AnnotatedInClass()435 public void testDeclaredIn2AnnotatedInClass() { 436 assertThat(getSubscriber().declaredIn2AnnotatedInClassEvents).contains(EVENT); 437 } 438 testNowhereAnnotated()439 public void testNowhereAnnotated() { 440 assertThat(getSubscriber().nowhereAnnotatedEvents).isEmpty(); 441 } 442 443 @Override createSubscriber()444 SubscriberClass createSubscriber() { 445 return new SubscriberClass(); 446 } 447 } 448 } 449