1 /* 2 * Copyright (C) 2011 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 com.google.common.eventbus.EventBus; 20 import com.google.common.eventbus.Subscribe; 21 import java.util.concurrent.atomic.AtomicInteger; 22 import java.util.concurrent.atomic.AtomicReference; 23 import junit.framework.TestCase; 24 25 /** 26 * Test cases for {@code EventBus} that must not be in the same package. 27 * 28 * @author Louis Wasserman 29 */ 30 public class OutsideEventBusTest extends TestCase { 31 32 /* 33 * If you do this test from common.eventbus.EventBusTest, it doesn't actually test the behavior. 34 * That is, even if exactly the same method works from inside the common.eventbus package tests, 35 * it can fail here. 36 */ testAnonymous()37 public void testAnonymous() { 38 final AtomicReference<String> holder = new AtomicReference<>(); 39 final AtomicInteger deliveries = new AtomicInteger(); 40 EventBus bus = new EventBus(); 41 bus.register( 42 new Object() { 43 @Subscribe 44 public void accept(String str) { 45 holder.set(str); 46 deliveries.incrementAndGet(); 47 } 48 }); 49 50 String EVENT = "Hello!"; 51 bus.post(EVENT); 52 53 assertEquals("Only one event should be delivered.", 1, deliveries.get()); 54 assertEquals("Correct string should be delivered.", EVENT, holder.get()); 55 } 56 } 57