/sigmah/src/test/java/org/sigmah/client/mock/DispatcherStub.java
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 6package org.sigmah.client.mock; 7 8import com.google.gwt.user.client.rpc.AsyncCallback; 9import org.sigmah.client.dispatch.AsyncMonitor; 10import org.sigmah.client.dispatch.Dispatcher; 11import org.sigmah.shared.command.BatchCommand; 12import org.sigmah.shared.command.Command; 13import org.sigmah.shared.command.result.BatchResult; 14import org.sigmah.shared.command.result.CommandResult; 15 16import java.util.ArrayList; 17import java.util.HashMap; 18import java.util.List; 19import java.util.Map; 20 21/** 22 * @author Alex Bertram (akbertram@gmail.com) 23 */ 24public class DispatcherStub implements Dispatcher { 25 26 private Map<Command, CommandResult> results = new HashMap<Command, CommandResult>(); 27 private Map<Class, CommandResult> resultByClass = new HashMap<Class, CommandResult>(); 28 29 private List<Command> log = new ArrayList<Command>(); 30 31 public DispatcherStub() { 32 33 } 34 35 @Override 36 public <T extends CommandResult> void execute(Command<T> command, AsyncMonitor monitor, AsyncCallback<T> callback) { 37 38 if (command instanceof BatchCommand) { 39 BatchCommand batch = (BatchCommand) command; 40 List<CommandResult> results = new ArrayList<CommandResult>(); 41 for (Command batchCmd : batch.getCommands()) { 42 results.add(findResult(batchCmd)); 43 } 44 callback.onSuccess((T) new BatchResult(results)); 45 } else { 46 callback.onSuccess((T) findResult(command)); 47 } 48 } 49 50 private CommandResult findResult(Command command) { 51 CommandResult result = results.get(command); 52 if (result == null) { 53 54 result = resultByClass.get(command.getClass()); 55 if (result == null) { 56 throw new AssertionError("Unexpected command: " + command.toString()); 57 } 58 } 59 log.add(command); 60 return result; 61 } 62 63 public void setResult(Command command, CommandResult result) { 64 results.put(command, result); 65 } 66 67 public void setResult(Class<? extends Command> commandClass, CommandResult result) { 68 resultByClass.put(commandClass, result); 69 } 70 71 public void assertExecuteCount(Class commandClass, int expectedCount) { 72 int count = 0; 73 for (Command cmd : log) { 74 if (commandClass.equals(cmd.getClass())) { 75 count++; 76 } 77 } 78 if (count != expectedCount) { 79 throw new AssertionError("Execution count for " + commandClass.getName() + ": expected : " + expectedCount 80 + " actual count: " + count); 81 82 } 83 } 84 85 public <T extends Command> T getLastExecuted(Class commandClass) { 86 for (int i = log.size() - 1; i >= 0; i--) { 87 if (log.get(i).getClass().equals(commandClass)) { 88 return (T) log.get(i); 89 } 90 } 91 throw new AssertionError(commandClass.getName() + " was not excecuted."); 92 } 93 94 public void resetLog() { 95 log.clear(); 96 } 97}