PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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