[Contestj-developer] contestj/contestj2/src/main/java/org/contestj/introduction MockImpl.java, NON
Status: Inactive
Brought to you by:
thomasra
|
From: Ståle P. <st...@us...> - 2007-06-20 14:54:18
|
Update of /cvsroot/contestj/contestj/contestj2/src/main/java/org/contestj/introduction In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv14426/contestj2/src/main/java/org/contestj/introduction Added Files: MockImpl.java Mock.java Log Message: Initial upload of Contestj2 even though it supports most of the Contestj features its not ready to be used atm. --- NEW FILE: Mock.java --- package org.contestj.introduction; /** * Every class thats duplicated/mocked can be casted to Mock. * Mock methods are used to setup method return values. * * @author <a href="st...@gm...">Stale W. Pedersen</a> * @version $Revision: 1.1 $ */ public interface Mock { public void setupMock(String methodName, Object returnObject); public void setupMock(String methodName, Object[] params, Object returnObject); public Object getReturnObject(String methodName); public Object getReturnObject(String methodName, Object[] params); public void setupVoidMock(String methodName); public void setupVoidMock(String methodName, Object[] params); public void setupExceptionMock(String methodName, Throwable t); public void setupExceptionMock(String methodName, Object[] params, Throwable t); } --- NEW FILE: MockImpl.java --- package org.contestj.introduction; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.contestj.util.Generator; /** * A MockImpl. * * TODO: need to check if method is static. if it is, add the invocation to MockService. * * @author <a href="st...@gm...">Stale W. Pedersen</a> * @version $Revision: 1.1 $ */ public class MockImpl implements Mock { private HashMap<String, List<Object>> invocationMap = new HashMap<String, List<Object>>(); private HashMap<String, List<Throwable>> exceptionMap = new HashMap<String, List<Throwable>>(); private List<String> voidInvocationList = new ArrayList<String>(); public MockImpl(Object o) { } public Object getReturnObject(String methodName) { System.out.println("getting object with key: "+methodName); System.out.println("size of invocationMap is: "+invocationMap.size()); // if its a void method we just return an empty object if(voidInvocationList.remove(methodName)) { return new Object(); } //getFromInvocationMap //Object o = invocationMap.remove(methodName); Object o = getFromInvocationMap(methodName); if(o != null) { return o; } Throwable t = getException(methodName); if(t != null) { throw new RuntimeException(t); } throw new RuntimeException("Method was not setup with enough return values."); //return invocationMap.remove(methodName); } public Object getReturnObject(String methodName, Object[] params) { return getReturnObject(Generator.generateMethodName(methodName, params)); //return invocationMap.remove(Generator.generateMethodName(methodName, params)); } public void setupMock(String methodName, Object returnObject) { addToInvocationMap(methodName, returnObject); System.out.println("size of invocationMap is: "+invocationMap.size()); } public void setupMock(String methodName, Object[] params, Object returnObject) { addToInvocationMap(Generator.generateMethodName(methodName, params), returnObject); } public void setupVoidMock(String methodName) { voidInvocationList.add(methodName); } public void setupVoidMock(String methodName, Object[] params) { voidInvocationList.add(Generator.generateMethodName(methodName, params)); } public void setupExceptionMock(String methodName, Throwable t) { addException(methodName, t); } public void setupExceptionMock(String methodName, Object[] params, Throwable t) { addException(Generator.generateMethodName(methodName, params), t); } private void addToInvocationMap(String methodName, Object returnObject) { List<Object> invocationList = invocationMap.remove(methodName); if(invocationList != null) { invocationList.add(returnObject); } else { invocationList = new ArrayList<Object>(); invocationList.add(returnObject); } invocationMap.put(methodName, invocationList); } private Object getFromInvocationMap(String methodName) { List<Object> invocationList = invocationMap.remove(methodName); if(invocationList != null && invocationList.size() > 0) { Object o = invocationList.remove(0); if(invocationList.size() > 0) invocationMap.put(methodName, invocationList); return o; } else return null; } private void addException(String methodName, Throwable t) { List<Throwable> exceptionList = exceptionMap.remove(methodName); if(exceptionList != null) { exceptionList.add(t); } else { exceptionList = new ArrayList<Throwable>(); exceptionList.add(t); } exceptionMap.put(methodName, exceptionList); } private Throwable getException(String methodName) { List<Throwable> exceptionList = exceptionMap.remove(methodName); if(exceptionList != null && exceptionList.size() > 0) { Throwable t = exceptionList.remove(0); if(exceptionList.size() > 0) exceptionMap.put(methodName, exceptionList); return t; } else return null; } } |