/sigmah/src/test/java/org/sigmah/shared/command/handler/LocalHandlerTestCase.java

http://sigma-h.googlecode.com/ · Java · 113 lines · 74 code · 24 blank · 15 comment · 4 complexity · 490ced2bb56a429ac1bf13eafa5b5277 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.shared.command.handler;
  6. import java.io.File;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.SQLException;
  10. import java.util.Collections;
  11. import java.util.List;
  12. import javax.persistence.EntityManager;
  13. import javax.persistence.EntityManagerFactory;
  14. import org.junit.Before;
  15. import org.sigmah.client.dispatch.AsyncMonitor;
  16. import org.sigmah.client.dispatch.Dispatcher;
  17. import org.sigmah.client.dispatch.remote.Authentication;
  18. import org.sigmah.client.mock.MockEventBus;
  19. import org.sigmah.client.offline.command.LocalDispatcher;
  20. import org.sigmah.client.offline.sync.Synchronizer;
  21. import org.sigmah.server.endpoint.gwtrpc.CommandServlet;
  22. import org.sigmah.shared.command.Command;
  23. import org.sigmah.shared.command.result.CommandResult;
  24. import org.sigmah.shared.command.result.SyncRegionUpdate;
  25. import org.sigmah.shared.domain.User;
  26. import com.allen_sauer.gwt.log.client.Log;
  27. //import com.bedatadriven.rebar.sync.client.BulkUpdaterAsync;
  28. //import com.bedatadriven.rebar.sync.mock.MockBulkUpdater;
  29. import com.google.gwt.user.client.rpc.AsyncCallback;
  30. import com.google.inject.Inject;
  31. public abstract class LocalHandlerTestCase {
  32. @Inject
  33. private CommandServlet servlet;
  34. @Inject
  35. protected EntityManagerFactory serverEntityManagerFactory;
  36. /**
  37. * this is scoped to Tests as the analog of being
  38. * scoped to a request.
  39. */
  40. @Inject
  41. protected EntityManager serverEm;
  42. protected User user;
  43. protected Dispatcher remoteDispatcher;
  44. protected Authentication localAuth;
  45. protected LocalDispatcher localDispatcher;
  46. protected Connection localConnection;
  47. //private BulkUpdaterAsync updater;
  48. @Before
  49. public void setUp() throws SQLException, ClassNotFoundException {
  50. setUser(1); // default is db owner
  51. remoteDispatcher = new RemoteDispatcherStub();
  52. Class.forName("org.sqlite.JDBC");
  53. File testClientDb = new File("synctest");
  54. if(testClientDb.exists()) {
  55. testClientDb.delete();
  56. }
  57. localConnection = DriverManager.getConnection("jdbc:sqlite:synctest");
  58. // updater = new MockBulkUpdater(localConnection);
  59. Log.setCurrentLogLevel(Log.LOG_LEVEL_DEBUG);
  60. }
  61. protected void setUser(int userId) {
  62. user = new User();
  63. user.setId(userId);
  64. localAuth = new Authentication(user.getId(), "X", user.getEmail());
  65. }
  66. protected void synchronize() {
  67. /* Synchronizer syncr = new Synchronizer(new MockEventBus(), remoteDispatcher, localConnection, updater,
  68. localAuth);
  69. syncr.start();*/
  70. }
  71. protected void newRequest() {
  72. serverEm.clear();
  73. }
  74. private class RemoteDispatcherStub implements Dispatcher {
  75. @Override
  76. public <T extends CommandResult> void execute(Command<T> command, AsyncMonitor monitor, AsyncCallback<T> callback) {
  77. List<CommandResult> results = servlet.handleCommands(user, Collections.<Command>singletonList(command));
  78. CommandResult result = results.get(0);
  79. if(result instanceof SyncRegionUpdate) {
  80. System.out.println(((SyncRegionUpdate) result).getSql());
  81. }
  82. if(result instanceof Exception) {
  83. throw new Error((Throwable) result);
  84. } else {
  85. callback.onSuccess((T) result);
  86. }
  87. }
  88. }
  89. }