Last active
July 7, 2018 19:07
-
-
Save klimeryk/e090756d9b95dd3bc6aa to your computer and use it in GitHub Desktop.
FakeProvider for EventBinder (http://klimer.eu/2014/08/27/testing-gwteventbinder-with-gwtmockito/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeEventBinderProvider implements FakeProvider<EventBinder<?>> { | |
@Override | |
public EventBinder<?> getFake(Class<?> type) { | |
return (EventBinder<?>) Proxy.newProxyInstance(FakeEventBinderProvider.class.getClassLoader(), new Class<?>[] { type }, new InvocationHandler() { | |
@Override | |
public Object invoke(Object proxy, Method method, final Object[] args) throws Throwable { | |
String methodName = method.getName(); | |
assert methodName.equals("bindEventHandlers"); | |
final List<HandlerRegistration> registrations = new LinkedList<HandlerRegistration>(); | |
EventBus eventBus = (EventBus) args[1]; | |
List<Method> presenterMethods = getAllMethods(args[0].getClass()); | |
for (final Method presenterMethod : presenterMethods) { | |
if (presenterMethod.isAnnotationPresent(EventHandler.class)) { | |
@SuppressWarnings("unchecked") // Should always be ok, since the Generator for EventBinder should do all the safe-checking | |
Class<? extends GenericEvent> eventType = (Class<? extends GenericEvent>) (presenterMethod.getParameterTypes())[0]; | |
registrations.add(eventBus.addHandler(GenericEventType.getTypeOf(eventType), new GenericEventHandler() { | |
@Override | |
public void handleEvent(GenericEvent event) { | |
try { | |
presenterMethod.setAccessible(true); | |
presenterMethod.invoke(args[0], event); | |
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
})); | |
} | |
} | |
return new HandlerRegistration() { | |
@Override | |
public void removeHandler() { | |
for (HandlerRegistration registration : registrations) { | |
registration.removeHandler(); | |
} | |
registrations.clear(); | |
} | |
}; | |
} | |
}); | |
} | |
private List<Method> getAllMethods(Class<?> type) { | |
List<Method> methods = new LinkedList<Method>(); | |
methods.addAll(Arrays.asList(type.getDeclaredMethods())); | |
if (type.getSuperclass() != null) { | |
methods.addAll(getAllMethods(type.getSuperclass())); | |
} | |
return methods; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Before | |
public void setUpEventBindery() { | |
GwtMockito.useProviderForType(EventBinder.class, new FakeEventBinderProvider()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for writing this - it was super useful in helping get integration testing working in a GWT app via Guice.
Since gwteventbinder has been updated to support handling multiple event types in a single handler, I've hacked up support for calling methods based on the event types in their EventHandler annotation. :)