Clover coverage report - Maven Clover report
Coverage timestamp: Sun Mar 18 2007 17:42:32 CET
file stats: LOC: 232   Methods: 21
NCLOC: 171   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CglibProxyFactory.java 100% 96.6% 100% 97.6%
coverage coverage
 1    package com.agical.rmock.extension.cglib;
 2   
 3    import java.io.BufferedReader;
 4    import java.io.IOException;
 5    import java.io.StringReader;
 6    import java.util.HashMap;
 7    import java.util.HashSet;
 8    import java.util.Map;
 9    import java.util.Set;
 10   
 11    import net.sf.cglib.proxy.Callback;
 12    import net.sf.cglib.proxy.Enhancer;
 13   
 14    import com.agical.rmock.core.ProxyFactory;
 15    import com.agical.rmock.core.event.TestCaseListener;
 16    import com.agical.rmock.core.exception.IdMustBeUniqueException;
 17    import com.agical.rmock.core.exception.InterfaceNotAllowedException;
 18    import com.agical.rmock.core.expectation.ExpectationsState;
 19    import com.agical.rmock.core.expectation.InvocationListener;
 20    import com.agical.rmock.core.expectation.reference.ObjectReference;
 21    import com.agical.rmock.core.expectation.reference.PropertyObjectReference;
 22    import com.agical.rmock.core.util.MethodSelector;
 23    import com.agical.rmock.core.util.impl.MethodSelectorImpl;
 24   
 25    /**
 26    * <em>(c) Agical AB 2005</em>
 27    * @author joakim.ohlrogge
 28    *
 29    * This factory uses cglib to create proxys for classes.
 30    */
 31    public class CglibProxyFactory implements ProxyFactory, ObjectReferenceRetriever, TestCaseListener {
 32    private static int count = 0;
 33    private Set existingIds = new HashSet();
 34    com.agical.rmock.core.InvocationHandler invocationHandler = com.agical.rmock.core.InvocationHandler.NULL;
 35    private ExpectationsState expectationsState = ExpectationsState.NULL;
 36    private MethodSelector constructorFinder = new MethodSelectorImpl();
 37    private InvocationListener invocationListener;
 38   
 39    private Map objectReferences = new HashMap();
 40   
 41  45 public CglibProxyFactory() {
 42    }
 43   
 44   
 45  860 private void registerProxy(Class clazz, String id, Object object) {
 46  860 if (existingIds.contains(id)) {
 47  3 throw new IdMustBeUniqueException(id);
 48    }
 49  857 existingIds.add(id);
 50  857 addObjectReference(clazz, id, object);
 51    }
 52   
 53    /**
 54    * @see com.agical.rmock.core.ProxyFactory#create(java.lang.Class)
 55    */
 56  711 public Object createInterfaceProxy(Class clazz, String id) {
 57  711 InterfaceInvocationHandler handler = new InterfaceInvocationHandler(invocationHandler, id, clazz, this, invocationListener, this, expectationsState );
 58   
 59  711 Enhancer e = createEnhancer(handler, id, clazz, RMockNamingPolicy.MOCK);
 60  711 e.setInterfaces(new Class[]{clazz});
 61  711 Object object = e.create();
 62  711 handler.setProxy(object);
 63  711 registerProxy(clazz, id, object);
 64  710 return object;
 65    }
 66   
 67   
 68  2 public Object createObjectMockProxy(Class clazz, Object[] parameters, String id) {
 69  2 return createObjectMockProxy( clazz, null, parameters, id );
 70    }
 71   
 72  17 public Object createObjectMockProxy(Class clazz, Class[] parameterTypes, Object[] parameters, String id ) {
 73  17 if( clazz.isInterface() ) {
 74  2 throw new InterfaceNotAllowedException( clazz.getName() + " is an interface and is not allowed when using mock with constructor parameters.\nMock a class matching the constructor parameters provided or use the mock(..) method taking one or two parameters instead." );
 75    }
 76   
 77  15 if( parameterTypes == null ) {
 78  1 parameterTypes = constructorFinder.getBestAssignableConstructor( clazz, parameters ).getParameterTypes();
 79    }
 80   
 81  15 Object object = null;
 82  15 ObjectMockInvocationHandler handler = new ObjectMockInvocationHandler(invocationHandler, id, clazz, this, expectationsState, invocationListener, this);
 83  15 Enhancer e = createEnhancer(handler, id, clazz, RMockNamingPolicy.MOCK);
 84  15 e.setSuperclass(clazz);
 85   
 86  15 object = (Object) e.create( parameterTypes, parameters );
 87   
 88  15 registerProxy(clazz, id, object);
 89  13 return object;
 90    }
 91   
 92  22 public Object createObjectInterceptionProxy(Class clazz, Object[] parameters, String id) {
 93  22 return createObjectInterceptionProxy( clazz, constructorFinder.getBestAssignableConstructor( clazz, parameters ).getParameterTypes(), parameters, id );
 94    }
 95   
 96  53 public Object createObjectInterceptionProxy(Class clazz, Class[] parameterTypes, Object[] parameters, String id ) {
 97  53 if(clazz.isInterface()) {
 98  1 throw new InterfaceNotAllowedException( clazz.getName() + " is an interface, hence not possible to intercept. Try a intercepting a class instead, or use mock(...) with the interface");
 99    }
 100   
 101  52 Object object = null;
 102  52 ObjectInterceptionInvocationHandler handler = new ObjectInterceptionInvocationHandler(invocationHandler, id, clazz, this, expectationsState, invocationListener, this);
 103  52 Enhancer e = createEnhancer(handler, id, clazz, RMockNamingPolicy.INTERCEPTED);
 104  52 e.setSuperclass(clazz);
 105   
 106  52 object = (Object) e.create( parameterTypes, parameters );
 107   
 108   
 109  52 registerProxy(clazz, id, object);
 110  52 return object;
 111    }
 112   
 113   
 114  778 private Enhancer createEnhancer(Callback handler, final String id, final Class clazz, String type) {
 115  778 Enhancer e = new Enhancer();
 116    //e.setNamingPolicy(new DefaultNamingPolicy());
 117   
 118  778 e.setNamingPolicy(new RMockNamingPolicy(id, type));
 119  778 e.setInterceptDuringConstruction(false);
 120  778 e.setCallback(handler);
 121   
 122  778 return e;
 123    }
 124   
 125  857 private void addObjectReference(Class clazz, String id, Object object) {
 126  857 ObjectReference objectReference = new PropertyObjectReference( id, id, clazz );
 127  857 objectReferences.put( object.getClass().getName(), objectReference );
 128    }
 129   
 130  550 public void clear() {
 131  550 existingIds.clear();
 132  550 objectReferences.clear();
 133    }
 134   
 135  1675 public void setInvocationHandler(com.agical.rmock.core.InvocationHandler invocationHandler) {
 136  1675 this.invocationHandler = invocationHandler;
 137   
 138    }
 139   
 140  1675 public void setExpectationState(ExpectationsState expectationsState) {
 141  1675 this.expectationsState = expectationsState;
 142   
 143    }
 144   
 145  34 public void setInvocationListener(InvocationListener invocationListener) {
 146  34 this.invocationListener = invocationListener;
 147    }
 148   
 149  31 public Object createObjectMonitorProxy(Class clazz, Object[] parameters, String string) {
 150  31 return createObjectMonitorProxy( clazz, null, parameters, string );
 151    }
 152   
 153  83 public Object createObjectMonitorProxy(Class clazz, Class[] parameterTypes, Object[] parameters, String id) {
 154  83 if(clazz.isInterface()) {
 155  1 throw new InterfaceNotAllowedException( clazz.getName() + " is an interface and therefore cannot be monitored." );
 156    }
 157   
 158  82 if( parameterTypes == null ) {
 159  30 parameterTypes = constructorFinder.getBestAssignableConstructor( clazz, parameters ).getParameterTypes();
 160    }
 161   
 162  82 Object object = null;
 163   
 164  82 ObjectMonitorInvocationHandler handler = new ObjectMonitorInvocationHandler(invocationHandler, id, clazz, invocationListener, this, expectationsState );
 165  82 Enhancer e = new Enhancer();
 166  82 e.setSuperclass(clazz);
 167  82 e.setCallback(handler);
 168  82 object = (Object) e.create( parameterTypes, parameters );
 169   
 170   
 171  82 registerProxy(clazz, id, object);
 172  82 return object;
 173    }
 174   
 175  15 public ObjectReference getObjectReferenceFromStackTrace(String stacktrace, int lineToUse) {
 176  15 try {
 177  15 BufferedReader bufferedReader = new BufferedReader( new StringReader( stacktrace ) );
 178  15 for( int i = 0; i < lineToUse; i++ ) {
 179  60 bufferedReader.readLine();
 180    }
 181  15 String theClass = bufferedReader.readLine();
 182  15 theClass = theClass.substring( 4 ); // remove tab-at-space
 183  15 theClass = theClass.substring(0, theClass.lastIndexOf("(") ); // remove line number info
 184  15 theClass = theClass.substring(0, theClass.lastIndexOf(".") ); // remove method
 185  15 ObjectReference objectReference = getObjectReferenceFromClassName(theClass);
 186   
 187  15 return objectReference;
 188    } catch (IOException e) {
 189  0 e.printStackTrace();
 190    }
 191   
 192  0 return ObjectReference.NULL;
 193    }
 194   
 195  30 private ObjectReference getObjectReferenceFromClassName(String className) {
 196  30 ObjectReference objectReference = (ObjectReference) objectReferences.get(className);
 197  30 if( objectReference == null ) {
 198  25 Class cls = Object.class;
 199  25 try {
 200  25 cls = Class.forName(className);
 201    } catch (ClassNotFoundException e) {
 202  0 e.printStackTrace();
 203    }
 204  25 String shortName = className.substring( className.lastIndexOf(".") + 1);
 205  25 objectReference = new PropertyObjectReference( className, shortName, cls );
 206    }
 207  30 return objectReference;
 208    }
 209   
 210  15 public ObjectReference getObjectReferenceFromProxy(Object object) {
 211  15 return getObjectReferenceFromClassName( object.getClass().getName());
 212    }
 213   
 214  547 public void beforeTestCase(Object testCase, String method) {
 215  547 clear();
 216    }
 217   
 218  547 public void afterTestCase() {
 219   
 220    }
 221   
 222  17 public Object createInterfaceInterceptionProxy(Class itf, String id) {
 223  17 Object object = null;
 224  17 InterfaceInterceptionInvocationHandler handler = new InterfaceInterceptionInvocationHandler(invocationHandler, id, itf, this, expectationsState, invocationListener, this);
 225  17 Enhancer e = new Enhancer();
 226   
 227  17 e.setInterceptDuringConstruction(true);
 228  17 e.setInterfaces(new Class[]{itf});
 229  17 e.setCallback(handler);
 230  17 return e.create();
 231    }
 232    }