PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 6package org.sigmah.client.mock;
 7
 8import com.google.gwt.user.client.rpc.AsyncCallback;
 9import org.sigmah.shared.command.Command;
10import org.sigmah.shared.command.GetSchema;
11import org.sigmah.shared.command.RemoteCommandServiceAsync;
12import org.sigmah.shared.command.result.CommandResult;
13import org.sigmah.shared.dto.SchemaDTO;
14import org.sigmah.shared.exception.CommandException;
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 MockRemoteCommandService implements RemoteCommandServiceAsync {
25
26    public Map<Class, Integer> commandCounts = new HashMap<Class, Integer>();
27
28    public SchemaDTO schema;
29    
30    public MockRemoteCommandService() {
31    	
32    }
33    
34    public MockRemoteCommandService(SchemaDTO schema) {
35    	this.schema = schema;
36    }
37
38
39    public int getCommandCount(Class clazz) {
40        Integer count = commandCounts.get(clazz);
41        if(count == null) {
42            return 0;
43        } else {
44            return count;
45        }
46    }
47
48
49    @Override
50    public void execute(String authToken, List<Command> cmds, AsyncCallback<List<CommandResult>> callback) {
51
52        List<CommandResult> results = new ArrayList<CommandResult>();
53
54        for(Command cmd : cmds ) {
55
56            Integer count = commandCounts.get(cmd.getClass());
57            commandCounts.put(cmd.getClass(), count == null ? 1 : count + 1);
58
59            if(schema!=null && cmd instanceof GetSchema) {
60            	results.add(schema);
61            } else {
62            	results.add(mockExecute(cmd));
63            }
64        }
65        callback.onSuccess(results);
66    }
67
68    protected CommandResult mockExecute(Command cmd) {
69        return new CommandException();
70    }
71}