PageRenderTime 1024ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/gwtp-core/gwtp-tester/src/main/java/com/gwtplatform/tester/MockHandlerModule.java

https://code.google.com/p/gwt-platform/
Java | 202 lines | 49 code | 16 blank | 137 comment | 0 complexity | 2228641e4250272fbe3739512958f5ff MD5 | raw file
  1. /**
  2. * Copyright 2011 ArcBees Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.gwtplatform.tester;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.internal.UniqueAnnotations;
  19. import com.gwtplatform.dispatch.client.actionhandler.AbstractClientActionHandler;
  20. import com.gwtplatform.dispatch.client.actionhandler.ClientActionHandler;
  21. import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
  22. import com.gwtplatform.dispatch.shared.Action;
  23. import com.gwtplatform.dispatch.shared.Result;
  24. /**
  25. * Module for use in test cases when creating a guice injector that needs to
  26. * provide mock handlers.
  27. *
  28. * Your injector must also have an class that subclasses {@link com.gwtplatform.dispatch.server.guice.HandlerModule}
  29. * to bind Actions to ActionHandlers and ActionValidators.
  30. * <p/>
  31. * You should subclass this module and use the {@link #configureMockHandlers()}
  32. * method to:
  33. * <ul>
  34. * <li>register mock server-side action handlers with
  35. * {@link #bindMockActionHandler(Class, ActionHandler)}.</li>
  36. * <li>register mock client-side action handlers with
  37. * {@link #bindMockClientActionHandler(Class, AbstractClientActionHandler)}.</li>
  38. * </ul>
  39. *
  40. * <h3>Unit Testing Example</h3>
  41. *
  42. * <pre>
  43. * // create mock handlers
  44. * CreateFooActionHandler mockCreateFooActionHandler =
  45. * mock(CreateFooActionHandler.class);
  46. *
  47. * GeocodeAddressClientActionHandler geocodeAddressClientActionHandler =
  48. * mock(GeocodeAddressClientActionHandler.class);
  49. *
  50. * // create injector
  51. * Injector injector =
  52. * Guice.createInjector(new MyHandlerModule(),
  53. * new MockHandlerModule() {
  54. * {@literal}@Override
  55. * protected void configureMockHandlers() {
  56. * bindMockActionHandler(
  57. * CreateFooActionHandler.class,
  58. * mockCreateFooActionHandler);
  59. * }
  60. * bindMockClientActionHandler(
  61. * GeocodeAddressAction.class,
  62. * geocodeAddressClientActionHandler);
  63. * });
  64. *
  65. * // get dispatcher
  66. * DispatchAsync dispatcher = injector.getInstance(DispatchAsync.class);
  67. *
  68. * // create mock result
  69. * final CreateFooResult result =
  70. * new CreateFooResult(new Key<Foo>(Foo.class, 1));
  71. *
  72. * // configure mockito to return mock result on specific action
  73. * when(
  74. * businessCreateActionHandler.execute(
  75. * eq(new CreateFooAction("Bar")),
  76. * any(ExecutionContext.class))).thenReturn(result);
  77. *
  78. * // configuring mockito to return result for clent action handler
  79. * // is a bit more complex
  80. * final GeocodeAddressResult geocodeAddressResult = new GeocodeAddressResult(...);
  81. * doAnswer(new Answer&lt;Object&gt;() {
  82. * {@literal @SuppressWarnings("unchecked")}
  83. * public Object answer(InvocationOnMock invocation) {
  84. * AsyncCallback&lt;GeocodeAddressResult&gt; callback
  85. * = (AsyncCallback&lt;GeocodeAddressResult&gt;) invocation.getArguments()[1];
  86. * callback.onSuccess(new GeocodeAddressResult(geocodeAddressResult));
  87. * return null;
  88. * }
  89. * }).when(geocodeAddressClientActionHandler)
  90. * .execute(
  91. * eq(new GeocodeAddressAction("2 Google Way, New Zealand",
  92. * "nz")), any(AsyncCallback.class),
  93. * any(ClientDispatchRequest.class), any(ExecuteCommand.class));
  94. *
  95. * </pre>
  96. *
  97. * @author Brendan Doherty
  98. */
  99. public abstract class MockHandlerModule extends AbstractModule {
  100. private static class MockClientActionHandlerMapImpl<A extends Action<R>, R extends Result>
  101. implements MockClientActionHandlerMap {
  102. private final Class<A> actionClass;
  103. private final ClientActionHandler<A, R> clientActionHandler;
  104. public MockClientActionHandlerMapImpl(final Class<A> actionClass,
  105. final ClientActionHandler<A, R> clientActionHandler) {
  106. this.actionClass = actionClass;
  107. this.clientActionHandler = clientActionHandler;
  108. }
  109. @Override
  110. public Class<A> getActionClass() {
  111. return actionClass;
  112. }
  113. @Override
  114. public ClientActionHandler<A, R> getClientActionHandler() {
  115. return clientActionHandler;
  116. }
  117. }
  118. @Override
  119. protected void configure() {
  120. install(new TestDispatchModule());
  121. configureMockHandlers();
  122. }
  123. protected abstract void configureMockHandlers();
  124. /**
  125. * Use bindMockActionHandler instead.
  126. */
  127. @Deprecated
  128. protected <A extends Action<R>, R extends Result, H extends ActionHandler<A, R>> void bindMockHandler(
  129. Class<H> handler, H mockHandler) {
  130. bindMockActionHandler(handler, mockHandler);
  131. }
  132. /**
  133. * Registers a mock server-side action handlers.
  134. * <p/>
  135. * This mock server-side action handler will be executed when the class under
  136. * test calls {@link com.gwtplatform.dispatch.shared.DispatchAsync#execute
  137. * DispatchAsync#execute()} or
  138. * {@link com.gwtplatform.dispatch.shared.DispatchAsync#undo
  139. * DispatchAsync#undo()}.
  140. *
  141. * @param <A> Type of {@link Action} that will be executed by mock handler
  142. * @param <R> Type of {@link Result} that will be returned by mock handler
  143. * @param <H> Type of the mock handler that extends
  144. * {@link ActionHandler}
  145. * @param handlerClass The type of the mock server-side handler
  146. * @param mockHandler Instance of the {@link ActionHandler} to execute
  147. * actions of type {@literal <A>}
  148. *
  149. */
  150. protected <A extends Action<R>, R extends Result, H extends ActionHandler<A, R>> void bindMockActionHandler(
  151. Class<H> handlerClass, H mockHandler) {
  152. bind(handlerClass).toProvider(new MockProvider<H>(mockHandler));
  153. }
  154. /**
  155. * Registers a mock client-side action handlers.
  156. * <p/>
  157. * This mock client-side action handler will be executed when the class under
  158. * test calls {@link com.gwtplatform.dispatch.shared.DispatchAsync#execute
  159. * DispatchAsync#execute()} or
  160. * {@link com.gwtplatform.dispatch.shared.DispatchAsync#undo
  161. * DispatchAsync#undo()}.
  162. * <p/>
  163. *
  164. * If both mock client and mock server action handlers have been registered,
  165. * the server side action handler will only be called if the mock client side
  166. * action handler calls
  167. * {@link com.gwtplatform.dispatch.client.actionhandler.ExecuteCommand#execute
  168. * ExecuteCommand#execute()} or
  169. * {@link com.gwtplatform.dispatch.client.actionhandler.UndoCommand#undo
  170. * UndoCommand#undo()}
  171. *
  172. * @param <A> Type of {@link Action}
  173. * @param <R> Type of {@link Result}
  174. * @param <H> Type of {@link AbstractClientActionHandler}
  175. * @param actionClass Implementation of {@link ActionHandler} to link
  176. * and bind
  177. * @param mockHandler Instance of the {@link ActionHandler} to execute
  178. * actions of type {@literal <A>}
  179. */
  180. protected <A extends Action<R>, R extends Result, H extends AbstractClientActionHandler<A, R>> void bindMockClientActionHandler(
  181. Class<A> actionClass, H mockHandler) {
  182. bind(MockClientActionHandlerMap.class).annotatedWith(
  183. UniqueAnnotations.create()).toInstance(
  184. new MockClientActionHandlerMapImpl<A, R>(actionClass, mockHandler));
  185. }
  186. }