/sigmah/src/test/java/org/sigmah/client/mock/MockRemoteCommandService.java

http://sigma-h.googlecode.com/ · Java · 71 lines · 46 code · 18 blank · 7 comment · 8 complexity · 0aecd7c02ed97588bd72936c54860c2d 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.client.mock;
  6. import com.google.gwt.user.client.rpc.AsyncCallback;
  7. import org.sigmah.shared.command.Command;
  8. import org.sigmah.shared.command.GetSchema;
  9. import org.sigmah.shared.command.RemoteCommandServiceAsync;
  10. import org.sigmah.shared.command.result.CommandResult;
  11. import org.sigmah.shared.dto.SchemaDTO;
  12. import org.sigmah.shared.exception.CommandException;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * @author Alex Bertram (akbertram@gmail.com)
  19. */
  20. public class MockRemoteCommandService implements RemoteCommandServiceAsync {
  21. public Map<Class, Integer> commandCounts = new HashMap<Class, Integer>();
  22. public SchemaDTO schema;
  23. public MockRemoteCommandService() {
  24. }
  25. public MockRemoteCommandService(SchemaDTO schema) {
  26. this.schema = schema;
  27. }
  28. public int getCommandCount(Class clazz) {
  29. Integer count = commandCounts.get(clazz);
  30. if(count == null) {
  31. return 0;
  32. } else {
  33. return count;
  34. }
  35. }
  36. @Override
  37. public void execute(String authToken, List<Command> cmds, AsyncCallback<List<CommandResult>> callback) {
  38. List<CommandResult> results = new ArrayList<CommandResult>();
  39. for(Command cmd : cmds ) {
  40. Integer count = commandCounts.get(cmd.getClass());
  41. commandCounts.put(cmd.getClass(), count == null ? 1 : count + 1);
  42. if(schema!=null && cmd instanceof GetSchema) {
  43. results.add(schema);
  44. } else {
  45. results.add(mockExecute(cmd));
  46. }
  47. }
  48. callback.onSuccess(results);
  49. }
  50. protected CommandResult mockExecute(Command cmd) {
  51. return new CommandException();
  52. }
  53. }