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

http://sigma-h.googlecode.com/ · Java · 97 lines · 72 code · 18 blank · 7 comment · 13 complexity · efcd0b84197bd6509560282a2977a3fe 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.client.dispatch.AsyncMonitor;
  8. import org.sigmah.client.dispatch.Dispatcher;
  9. import org.sigmah.shared.command.BatchCommand;
  10. import org.sigmah.shared.command.Command;
  11. import org.sigmah.shared.command.result.BatchResult;
  12. import org.sigmah.shared.command.result.CommandResult;
  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 DispatcherStub implements Dispatcher {
  21. private Map<Command, CommandResult> results = new HashMap<Command, CommandResult>();
  22. private Map<Class, CommandResult> resultByClass = new HashMap<Class, CommandResult>();
  23. private List<Command> log = new ArrayList<Command>();
  24. public DispatcherStub() {
  25. }
  26. @Override
  27. public <T extends CommandResult> void execute(Command<T> command, AsyncMonitor monitor, AsyncCallback<T> callback) {
  28. if (command instanceof BatchCommand) {
  29. BatchCommand batch = (BatchCommand) command;
  30. List<CommandResult> results = new ArrayList<CommandResult>();
  31. for (Command batchCmd : batch.getCommands()) {
  32. results.add(findResult(batchCmd));
  33. }
  34. callback.onSuccess((T) new BatchResult(results));
  35. } else {
  36. callback.onSuccess((T) findResult(command));
  37. }
  38. }
  39. private CommandResult findResult(Command command) {
  40. CommandResult result = results.get(command);
  41. if (result == null) {
  42. result = resultByClass.get(command.getClass());
  43. if (result == null) {
  44. throw new AssertionError("Unexpected command: " + command.toString());
  45. }
  46. }
  47. log.add(command);
  48. return result;
  49. }
  50. public void setResult(Command command, CommandResult result) {
  51. results.put(command, result);
  52. }
  53. public void setResult(Class<? extends Command> commandClass, CommandResult result) {
  54. resultByClass.put(commandClass, result);
  55. }
  56. public void assertExecuteCount(Class commandClass, int expectedCount) {
  57. int count = 0;
  58. for (Command cmd : log) {
  59. if (commandClass.equals(cmd.getClass())) {
  60. count++;
  61. }
  62. }
  63. if (count != expectedCount) {
  64. throw new AssertionError("Execution count for " + commandClass.getName() + ": expected : " + expectedCount
  65. + " actual count: " + count);
  66. }
  67. }
  68. public <T extends Command> T getLastExecuted(Class commandClass) {
  69. for (int i = log.size() - 1; i >= 0; i--) {
  70. if (log.get(i).getClass().equals(commandClass)) {
  71. return (T) log.get(i);
  72. }
  73. }
  74. throw new AssertionError(commandClass.getName() + " was not excecuted.");
  75. }
  76. public void resetLog() {
  77. log.clear();
  78. }
  79. }