aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/eventbus/EventHandlerTest.java
blob: bdbc33261bac00d18d6ceb057d95827a721793cb (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * 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 com.google.common.testing.EqualsTester;

import junit.framework.TestCase;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Test case for {@link EventHandler}.
 *
 * @author Cliff Biffle
 */
public class EventHandlerTest extends TestCase {

  private static final Object FIXTURE_ARGUMENT = new Object();

  private boolean methodCalled;
  private Object methodArgument;

  @Override protected void setUp() throws Exception {
    super.setUp();

    methodCalled = false;
    methodArgument = null;
  }

  /**
   * Checks that a no-frills, no-issues method call is properly executed.
   *
   * @throws Exception  if the aforementioned proper execution is not to be had.
   */
  public void testBasicMethodCall() throws Exception {
    Method method = getRecordingMethod();

    EventHandler handler = new EventHandler(this, method);

    handler.handleEvent(FIXTURE_ARGUMENT);

    assertTrue("Handler must call provided method.", methodCalled);
    assertTrue("Handler argument must be *exactly* the provided object.",
        methodArgument == FIXTURE_ARGUMENT);
  }

  public void testExceptionWrapping() {
    Method method = getExceptionThrowingMethod();
    EventHandler handler = new EventHandler(this, method);

    try {
      handler.handleEvent(new Object());
      fail("Handlers whose methods throw must throw InvocationTargetException");
    } catch (InvocationTargetException e) {
      assertTrue("Expected exception must be wrapped.",
          e.getCause() instanceof IntentionalException);
    }
  }

  public void testErrorPassthrough() throws InvocationTargetException {
    Method method = getErrorThrowingMethod();
    EventHandler handler = new EventHandler(this, method);

    try {
      handler.handleEvent(new Object());
      fail("Handlers whose methods throw Errors must rethrow them");
    } catch (JudgmentError e) {
      // 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(
            new EventHandler("foo", charAt), new EventHandler("foo", charAt))
        .addEqualityGroup(new EventHandler("bar", charAt))
        .addEqualityGroup(new EventHandler("foo", concat))
        .testEquals();
  }

  /**
   * Gets a reference to {@link #recordingMethod(Object)}.
   *
   * @return a Method wrapping {@link #recordingMethod(Object)}.
   * @throws IllegalStateException if executed in a context where reflection is
   *         unavailable.
   * @throws AssertionError if something odd has happened to
   *         {@link #recordingMethod(Object)}.
   */
  private Method getRecordingMethod() {
    Method method;
    try {
      method = getClass().getMethod("recordingMethod", Object.class);
    } catch (SecurityException e) {
      throw new IllegalStateException("This test needs access to reflection.");
    } catch (NoSuchMethodException e) {
      throw new AssertionError(
          "Someone changed EventHandlerTest#recordingMethod's visibility, " +
          "signature, or removed it entirely.  (Must be public.)");
    }
    return method;
  }

  /**
   * Gets a reference to {@link #exceptionThrowingMethod(Object)}.
   *
   * @return a Method wrapping {@link #exceptionThrowingMethod(Object)}.
   * @throws IllegalStateException if executed in a context where reflection is
   *         unavailable.
   * @throws AssertionError if something odd has happened to
   *         {@link #exceptionThrowingMethod(Object)}.
   */
  private Method getExceptionThrowingMethod() {
    Method method;
    try {
      method = getClass().getMethod("exceptionThrowingMethod", Object.class);
    } catch (SecurityException e) {
      throw new IllegalStateException("This test needs access to reflection.");
    } catch (NoSuchMethodException e) {
      throw new AssertionError(
          "Someone changed EventHandlerTest#exceptionThrowingMethod's " +
          "visibility, signature, or removed it entirely.  (Must be public.)");
    }
    return method;
  }

  /**
   * Gets a reference to {@link #errorThrowingMethod(Object)}.
   *
   * @return a Method wrapping {@link #errorThrowingMethod(Object)}.
   * @throws IllegalStateException if executed in a context where reflection is
   *         unavailable.
   * @throws AssertionError if something odd has happened to
   *         {@link #errorThrowingMethod(Object)}.
   */
  private Method getErrorThrowingMethod() {
    Method method;
    try {
      method = getClass().getMethod("errorThrowingMethod", Object.class);
    } catch (SecurityException e) {
      throw new IllegalStateException("This test needs access to reflection.");
    } catch (NoSuchMethodException e) {
      throw new AssertionError(
          "Someone changed EventHandlerTest#errorThrowingMethod's " +
          "visibility, signature, or removed it entirely.  (Must be public.)");
    }
    return method;
  }

  /**
   * Records the provided object in {@link #methodArgument} and sets
   * {@link #methodCalled}.  This method is called reflectively by EventHandler
   * during tests, and must remain public.
   *
   * @param arg  argument to record.
   */
  public void recordingMethod(Object arg) {
    if (methodCalled == true) {
      throw new IllegalStateException("Method called more than once.");
    }
    methodCalled = true;
    methodArgument = arg;
  }

  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;
  }

  public void errorThrowingMethod(Object arg) {
    throw new JudgmentError();
  }
  /** Local Error subclass to check variety of error thrown. */
  class JudgmentError extends Error {
    private static final long serialVersionUID = 634248373797713373L;
  }
}