/sigmah/src/test/java/org/sigmah/server/endpoint/gwtrpc/CommandTestCase.java

http://sigma-h.googlecode.com/ · Java · 91 lines · 59 code · 20 blank · 12 comment · 2 complexity · b3c4875abd16c7ab6850ee126e99e237 MD5 · raw file

  1. /*
  2. * All Sigmah code is released under the GNU General Public License v3
  3. * See COPYRIGHT.txt and LICENSE.txt.
  4. */
  5. package org.sigmah.server.endpoint.gwtrpc;
  6. import com.google.gwt.user.client.rpc.AsyncCallback;
  7. import com.google.inject.Inject;
  8. import com.google.inject.Injector;
  9. import org.sigmah.client.dispatch.AsyncMonitor;
  10. import org.sigmah.client.dispatch.Dispatcher;
  11. import org.sigmah.shared.domain.User;
  12. import org.sigmah.server.util.BeanMappingModule;
  13. import org.sigmah.server.util.TemplateModule;
  14. import org.sigmah.shared.command.Command;
  15. import org.sigmah.shared.command.result.CommandResult;
  16. import org.sigmah.shared.exception.CommandException;
  17. import org.sigmah.test.MockHibernateModule;
  18. import org.sigmah.test.Modules;
  19. import javax.persistence.EntityManager;
  20. import java.util.Collections;
  21. import java.util.List;
  22. @Modules({
  23. MockHibernateModule.class,
  24. TemplateModule.class,
  25. BeanMappingModule.class,
  26. GwtRpcModule.class
  27. })
  28. public abstract class CommandTestCase {
  29. @Inject
  30. protected CommandServlet servlet;
  31. @Inject
  32. protected EntityManager em;
  33. @Inject
  34. protected Injector injector;
  35. protected int userId = 1;
  36. protected void setUser(int userId) {
  37. this.userId = userId;
  38. }
  39. protected <T extends CommandResult> T execute(Command<T> command) throws CommandException {
  40. User user = em.find(User.class, userId);
  41. assert user != null;
  42. List<CommandResult> results = servlet.handleCommands(user, Collections.<Command>singletonList(command));
  43. // normally each request and so each handleCommand() gets its own
  44. // EntityManager, but here successive requests in the same test
  45. // will share an EntityManager, which can be bad if there are collections
  46. // still living in the first-level cache
  47. //
  48. // I think these command tests should ultimately become real end-to-end
  49. // tests and so would go through the actual servlet process, but for the moment,
  50. // we'll just add this work aroudn that clears the cache after each command.
  51. em.clear();
  52. CommandResult result = results.get(0);
  53. if (result instanceof CommandException) {
  54. throw (CommandException) result;
  55. }
  56. return (T) result;
  57. }
  58. protected Dispatcher dispatcher = new Dispatcher() {
  59. @Override
  60. public <T extends CommandResult> void execute(Command<T> command,
  61. AsyncMonitor monitor, AsyncCallback<T> callback) {
  62. try {
  63. callback.onSuccess(CommandTestCase.this.execute(command));
  64. } catch(Exception e) {
  65. System.out.println("Exception thrown while handling command " + command.toString() + ": ");
  66. e.printStackTrace();
  67. callback.onFailure(e);
  68. }
  69. }
  70. };
  71. }