Commits

kubek2k committed 591b47d Merge

Merged in matthauck/springockito/static-map (pull request #2)

Add static map

Comments (0)

Files changed (3)

springockito/src/main/java/org/kubek2k/mockito/spring/MockitoMockBeanDefinitionParser.java

     @Override
     protected void doParse(Element element, BeanDefinitionBuilder bean) {
         bean.addConstructorArgValue(element.getAttribute("class"));
+        bean.addConstructorArgValue(element.getAttribute("id"));
+        bean.addConstructorArgValue(element.getAttribute("useStaticMap"));
     }
 
     @Override

springockito/src/main/java/org/kubek2k/mockito/spring/factory/MockFactoryBean.java

 import org.mockito.Mockito;
 import org.springframework.beans.factory.FactoryBean;
 
+import java.util.Map;
+import java.util.HashMap;
 
 public class MockFactoryBean<T> implements FactoryBean<T> {
 
     private Class<T> mockClass;
-    private T instance;
+    private String beanId;
+    private boolean useStaticMap;
 
-    public MockFactoryBean(Class<T> mockClass) {
+    private T thisInstance;
+
+    private static Map<String, Object> objectMap = new HashMap<String, Object>();
+
+    public MockFactoryBean(Class<T> mockClass, String beanId, boolean useStaticMap) {
         this.mockClass = mockClass;
+        this.beanId = beanId;
+        this.useStaticMap = useStaticMap;
     }
 
     public Class<? extends T> getObjectType() {
     }
 
     public T getObject() throws Exception {
-        if (instance == null) {
-            instance = Mockito.mock(mockClass);
+        if (useStaticMap) {
+            T instance = (T) objectMap.get(beanId);
+
+            if (instance == null) {
+                instance = Mockito.mock(mockClass);
+                objectMap.put(beanId, instance);
+            }
+
+            return instance;
+            
+        } else {
+            if (thisInstance == null) {
+                thisInstance = Mockito.mock(mockClass);
+            }
+            return thisInstance;
         }
-        return instance;
     }
 
 }

springockito/src/main/resources/spring/mockito.xsd

          <xsd:complexContent>
             <xsd:extension base="beans:identifiedType">
                <xsd:attribute name="class" type="xsd:string" use="required"/>
+               <xsd:attribute name="useStaticMap" type="xsd:boolean" default="false" />
             </xsd:extension>
          </xsd:complexContent>
       </xsd:complexType>