1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* Copyright (C) 2007 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.eventbus;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.testing.EqualsTester;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import junit.framework.TestCase;
/**
* Tests for {@link Subscriber}.
*
* @author Cliff Biffle
* @author Colin Decker
*/
public class SubscriberTest extends TestCase {
private static final Object FIXTURE_ARGUMENT = new Object();
private EventBus bus;
private boolean methodCalled;
private Object methodArgument;
@Override
protected void setUp() throws Exception {
bus = new EventBus();
methodCalled = false;
methodArgument = null;
}
public void testCreate() {
Subscriber s1 = Subscriber.create(bus, this, getTestSubscriberMethod("recordingMethod"));
assertThat(s1).isInstanceOf(Subscriber.SynchronizedSubscriber.class);
// a thread-safe method should not create a synchronized subscriber
Subscriber s2 = Subscriber.create(bus, this, getTestSubscriberMethod("threadSafeMethod"));
assertThat(s2).isNotInstanceOf(Subscriber.SynchronizedSubscriber.class);
}
public void testInvokeSubscriberMethod_basicMethodCall() throws Throwable {
Method method = getTestSubscriberMethod("recordingMethod");
Subscriber subscriber = Subscriber.create(bus, this, method);
subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT);
assertTrue("Subscriber must call provided method", methodCalled);
assertTrue(
"Subscriber argument must be exactly the provided object.",
methodArgument == FIXTURE_ARGUMENT);
}
public void testInvokeSubscriberMethod_exceptionWrapping() throws Throwable {
Method method = getTestSubscriberMethod("exceptionThrowingMethod");
Subscriber subscriber = Subscriber.create(bus, this, method);
try {
subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT);
fail("Subscribers whose methods throw must throw InvocationTargetException");
} catch (InvocationTargetException expected) {
assertThat(expected).hasCauseThat().isInstanceOf(IntentionalException.class);
}
}
public void testInvokeSubscriberMethod_errorPassthrough() throws Throwable {
Method method = getTestSubscriberMethod("errorThrowingMethod");
Subscriber subscriber = Subscriber.create(bus, this, method);
try {
subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT);
fail("Subscribers whose methods throw Errors must rethrow them");
} catch (JudgmentError expected) {
}
}
public void testEquals() throws Exception {
Method charAt = String.class.getMethod("charAt", int.class);
Method concat = String.class.getMethod("concat", String.class);
new EqualsTester()
.addEqualityGroup(
Subscriber.create(bus, "foo", charAt), Subscriber.create(bus, "foo", charAt))
.addEqualityGroup(Subscriber.create(bus, "bar", charAt))
.addEqualityGroup(Subscriber.create(bus, "foo", concat))
.testEquals();
}
private Method getTestSubscriberMethod(String name) {
try {
return getClass().getDeclaredMethod(name, Object.class);
} catch (NoSuchMethodException e) {
throw new AssertionError();
}
}
/**
* Records the provided object in {@link #methodArgument} and sets {@link #methodCalled}. This
* method is called reflectively by Subscriber during tests, and must remain public.
*
* @param arg argument to record.
*/
@Subscribe
public void recordingMethod(Object arg) {
assertFalse(methodCalled);
methodCalled = true;
methodArgument = arg;
}
@Subscribe
public void exceptionThrowingMethod(Object arg) throws Exception {
throw new IntentionalException();
}
/** Local exception subclass to check variety of exception thrown. */
class IntentionalException extends Exception {
private static final long serialVersionUID = -2500191180248181379L;
}
@Subscribe
public void errorThrowingMethod(Object arg) {
throw new JudgmentError();
}
@Subscribe
@AllowConcurrentEvents
public void threadSafeMethod(Object arg) {}
/** Local Error subclass to check variety of error thrown. */
class JudgmentError extends Error {
private static final long serialVersionUID = 634248373797713373L;
}
}
|