Clover coverage report - Maven Clover report
Coverage timestamp: Sun Mar 18 2007 17:42:32 CET
file stats: LOC: 471   Methods: 36
NCLOC: 163   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RMockTestCase.java 83.3% 82% 77.8% 80.4%
coverage coverage
 1    package com.agical.rmock.extension.junit;
 2   
 3    import com.agical.rmock.core.Section;
 4    import com.agical.rmock.core.SectionManager;
 5    import com.agical.rmock.core.configuration.ConfiguratorImpl;
 6    import com.agical.rmock.core.documentation.DocumentationControl;
 7    import com.agical.rmock.core.documentation.impl.RMockDocumentationControl;
 8    import com.agical.rmock.core.exception.MonitorCreationException;
 9    import com.agical.rmock.core.exception.manager.ExceptionVerifier;
 10    import com.agical.rmock.core.expectation.modification.ExpectationModifier;
 11    import com.agical.rmock.core.expectation.modification.MultiplicityModifier;
 12    import com.agical.rmock.core.expectation.modification.impl.ExpectationModifierImpl;
 13    import com.agical.rmock.core.expectation.section.SectionFactory;
 14    import com.agical.rmock.core.find.CollectionSelector;
 15    import com.agical.rmock.core.find.Finder;
 16    import com.agical.rmock.core.find.iterator.IteratorFactory;
 17    import com.agical.rmock.core.find.match.MatchActionFactory;
 18    import com.agical.rmock.core.hub.Hub;
 19    import com.agical.rmock.core.match.Expression;
 20    import com.agical.rmock.core.match.multiplicity.MultiplicityFactory;
 21    import com.agical.rmock.core.strategy.TestStep;
 22    import com.agical.rmock.core.util.NamingUtils;
 23   
 24    /**
 25    * <em>(c) Agical AB 2005</em>
 26    * @author joakim.ohlrogge
 27    *
 28   
 29    */
 30    public abstract class RMockTestCase extends AbstractMockStrategyTestCase {
 31   
 32    private NamingUtils namingUtils = new NamingUtils();
 33    private SectionManager sectionManager;
 34    private final Finder finder = new Finder();
 35    protected final SectionFactory s = new SectionFactory();
 36    protected final MultiplicityFactory expect = new MultiplicityFactory();
 37    protected final IteratorFactory all = new IteratorFactory();
 38    protected MatchActionFactory matchAction;
 39    private int counter = 0;
 40    private MultiplicityModifier multiplicityModifier;
 41    protected DocumentationControl doc = new RMockDocumentationControl();
 42    private Expression exceptionExpression;
 43    private boolean exceptionExpressionHasMatched;
 44    private ExceptionVerifier exceptionVerifier;
 45   
 46  1105 public RMockTestCase() {
 47  1105 this( (String)null );
 48    }
 49   
 50  1105 protected RMockTestCase( String name ) {
 51  1105 this( ConfiguratorImpl.getInstance().getHub(), name );
 52    }
 53   
 54  1107 protected RMockTestCase( Hub hub, String name ) {
 55  1107 super( name, hub );
 56    }
 57   
 58  543 protected TestStep createStrategy(String test) {
 59  543 ExpectationModifier expectationModifier = new ExpectationModifierImpl();
 60  543 this.multiplicityModifier = expectationModifier;
 61  543 this.matchAction = new MatchActionFactory(expectationModifier);
 62  543 Hub hub = getHub();
 63   
 64  543 hub.connect(expectationModifier);
 65  543 hub.connect(doc);
 66   
 67  543 return super.createStrategy(test);
 68    }
 69   
 70  1086 public void setMultiplicityModifier(
 71    MultiplicityModifier multiplicityModifier) {
 72  1086 this.multiplicityModifier = multiplicityModifier;
 73    }
 74   
 75    /**
 76    * Creates a mock for the selected class.
 77    * </br>
 78    * If the clazz represents an object the object <b>MUST</b> be non-final and it <b>MUST</b>
 79    * have a default constructor.
 80    * The mocked object is created in the background. The <code>ForwardAction</code> will forward calls
 81    * to that instance. By default the <code>ReturnsAction</code> will be used (just as for mocked interfaces).
 82    *
 83    * @param clazz The interface or object to implement for the mock
 84    * @return A new mock implementing the interface passed in the clazz parameter
 85    */
 86  84 protected Object mock(Class clazz) {
 87  84 return mock( clazz, namingUtils.createNameFromType(clazz));
 88    }
 89   
 90    /**
 91    * Creates a mock for the selected class with the specified id.
 92    * </br>
 93    * The id <b>MUST</b> be unique within the testmethod
 94    * </br>
 95    * If the <code>clazz</code> represents an object the class <b>MUST</b> be non-final and it <b>MUST</b>
 96    * have a default constructor.
 97    * The mocked object is created in the background. The <code>ForwardAction</code> will forward calls
 98    * to that instance. By default the <code>ReturnsAction</code> will be used (just as for mocked interfaces).
 99    *
 100    * @param clazz The interface or object to implement for the mock
 101    * @param id the name of the recorder
 102    * @return A new mock for the object or interface passed in the clazz parameter
 103    */
 104  609 protected Object mock(Class clazz, String id) {
 105  609 if( clazz.isInterface() ) {
 106  604 return getProxyFactory().createInterfaceProxy(clazz, id);
 107    } else {
 108  5 return getProxyFactory().createObjectMockProxy(clazz, new Class[]{}, new Object[]{}, id);
 109    }
 110    }
 111   
 112    /**
 113    * Creates a mock for the selected class with the specified <code>id</code>, using
 114    * the specified <code>args</code>.
 115    * </br>
 116    * The id <b>MUST</b> be unique within the testmethod
 117    * </br>
 118    * The class <b>MUST</b> be non-final and it <b>MUST</b>
 119    * have a <b>only one</b> constructor signature assignable from the provided <code>args</code>.
 120    * The mocked object is created in the background. The <code>ForwardAction</code> will forward calls
 121    * to that instance. By default the <code>ReturnsAction</code> will be used (just as for mocked interfaces).
 122    *
 123    * @param clazz The object to implement for the mock
 124    * @param args The arguments to the constructor
 125    * @param id the name of the recorder
 126    * @return A new mock for the object passed in the clazz parameter
 127    */
 128  1 protected Object mock(Class clazz, Object[] args, String id) {
 129  1 return getProxyFactory().createObjectMockProxy( clazz, args, id );
 130    }
 131   
 132    /**
 133    * Creates a mock for the selected class with the specified <code>id</code>, using
 134    * the specified <code>args</code> using the constructor with the specified <code>signature</code>.
 135    * The mocked object is created in the background. The <code>ForwardAction</code> will forward calls
 136    * to that instance. By default the <code>ReturnsAction</code> will be used (just as for mocked interfaces).
 137    *
 138    * </br>
 139    * The id <b>MUST</b> be unique within the testmethod
 140    * </br>
 141    * The class <b>MUST</b> be non-final and it <b>MUST</b>
 142    * of course have a constructor with the provided <code>signature</code> that can accept the provided <code>args</code>.
 143    *
 144    * @param clazz The object to implement for the mock
 145    * @param signature The signature for the constructor to use
 146    * @param args The arguments to the constructor
 147    * @param id the name of the recorder
 148    * @return A new mock for the object passed in the clazz parameter
 149    */
 150  0 protected Object mock(Class clazz, Class[] signature, Object[] args, String id) {
 151  0 return getProxyFactory().createObjectMockProxy( clazz, signature, args, id );
 152    }
 153   
 154    /**
 155    * <code>intercept</code> creates an instance of the specified class (just as mock for a class), but
 156    * it forwards all calls to that instance. If restrictions should be made on the intercepted object, those can
 157    * be made just as with a mock.
 158    *
 159    * @param clazz The type to implement for the intercepted object
 160    * @return A new intercepted object for the type passed in the clazz parameter
 161    */
 162  0 protected Object intercept(Class clazz) {
 163  0 return intercept(clazz, namingUtils.createNameFromType(clazz));
 164    }
 165   
 166    /**
 167    * <code>intercept</code> creates an instance of the specified class (just as mock for a class), but
 168    * it forwards all calls to that instance. If restrictions should be made on the intercepted object, those can
 169    * be made just as with a mock.
 170    *
 171    * @param clazz The type to implement for the intercepted object
 172    * @param id the name of the intercepted object
 173    * @return A new intercepted object for the type passed in the clazz parameter
 174    */
 175  24 protected Object intercept(Class clazz, String id) {
 176  24 return intercept(clazz, new Class[]{}, new Object[]{}, id );
 177    }
 178   
 179    /**
 180    * <code>intercept</code> creates an instance of the specified class (just as mock for a class), but
 181    * it forwards all calls to that instance. If restrictions should be made on the intercepted object, those can
 182    * be made just as with a mock.
 183    * </br>
 184    * The id <b>MUST</b> be unique within the testmethod
 185    * </br>
 186    * The class <b>MUST</b> be non-final and it <b>MUST</b>
 187    * of course have a constructor with the provided <code>signature</code> that can accept the provided <code>args</code>.
 188    *
 189    * @param clazz The type to implement for the intercepted object
 190    * @param signature The signature for the constructor to use
 191    * @param args The arguments to the constructor
 192    * @param id the name of the intercepted object
 193    * @return A new intercepted object for the type passed in the clazz parameter
 194    */
 195  24 protected Object intercept(Class clazz, Class[] signature, Object[] arguments, String id) {
 196  24 return getProxyFactory().createObjectInterceptionProxy( clazz, signature, arguments, id );
 197    }
 198   
 199    /**
 200    * <code>intercept</code> creates an instance of the specified class (just as mock for a class), but
 201    * it forwards all calls to that instance. If restrictions should be made on the intercepted object, those can
 202    * be made just as with a mock.
 203    * <br/>
 204    * The class <b>MUST</b> be non-final and it <b>MUST</b>
 205    * of course have a constructor with the provided <code>signature</code> that can accept the provided <code>args</code>.
 206    *
 207    * @param clazz The type to implement for the intercepted object
 208    * @param signature The signature for the constructor to use
 209    * @param args The arguments to the constructor
 210    * @return A new intercepted object for the type passed in the clazz parameter
 211    */
 212  0 protected Object intercept(Class clazz, Class[] signature, Object[] arguments) {
 213  0 return intercept(clazz, signature, arguments, namingUtils.createNameFromType(clazz));
 214    }
 215   
 216    /**
 217    * <code>intercept</code> creates an instance of the specified class (just as mock for a class), but
 218    * it forwards all calls to that instance. If restrictions should be made on the intercepted object, those can
 219    * be made just as with a mock.
 220    * <br>
 221    * The id <b>MUST</b> be unique within the testmethod
 222    * </br>
 223    * The class <b>MUST</b> be non-final and it <b>MUST</b>
 224    * have a <b>only one</b> constructor signature assignable from the provided <code>args</code>.
 225    *
 226    * @param clazz The type to implement for the intercepted object
 227    * @param args The arguments to the constructor
 228    * @param id the name of the intercepted object
 229    * @return A new intercepted object for the type passed in the clazz parameter
 230    */
 231  22 protected Object intercept(Class clazz, Object[] arguments, String id) {
 232  22 return getProxyFactory().createObjectInterceptionProxy( clazz, arguments, id );
 233    }
 234   
 235    /**
 236    * <code>intercept</code> creates an instance of the specified class (just as mock for a class), but
 237    * it forwards all calls to that instance. If restrictions should be made on the intercepted object, those can
 238    * be made just as with a mock.
 239    * <br/>
 240    * The class <b>MUST</b> be non-final and it <b>MUST</b>
 241    * have a <b>only one</b> constructor signature assignable from the provided <code>args</code>.
 242    *
 243    * @param clazz The type to implement for the intercepted object
 244    * @param args The arguments to the constructor
 245    * @return A new intercepted object for the type passed in the clazz parameter
 246    */
 247  0 protected Object intercept(Class clazz, Object[] arguments) {
 248  0 return intercept(clazz, arguments, namingUtils.createNameFromType(clazz));
 249    }
 250   
 251    /**
 252    * <code>stub</code> creates an instance of the specified class (just as mock for a class), but
 253    * it forwards all calls to that instance. If restrictions should be made on the stub, those can
 254    * be made just as with a mock.
 255    *
 256    * @param clazz The type to implement for the stub
 257    * @param id the name of the intercepted object
 258    * @return A new intercepted object for the type passed in the clazz parameter
 259    *
 260    * @deprecated use intercept instead
 261    */
 262  0 protected Object stub(Class clazz, String id) {
 263  0 return getProxyFactory().createObjectInterceptionProxy( clazz, new Class[]{}, new Object[]{}, id );
 264    }
 265   
 266    /**
 267    * <code>stub</code> creates an instance of the specified class (just as mock for a class), but
 268    * it forwards all calls to that instance. If restrictions should be made on the stub, those can
 269    * be made just as with a mock.
 270    * </br>
 271    * The id <b>MUST</b> be unique within the testmethod
 272    * </br>
 273    * The class <b>MUST</b> be non-final and it <b>MUST</b>
 274    * of course have a constructor with the provided <code>signature</code> that can accept the provided <code>args</code>.
 275    *
 276    * @param clazz The object to implement for the stub
 277    * @param signature The signature for the constructor to use
 278    * @param args The arguments to the constructor
 279    * @param id the name of the stub
 280    * @return A new stub for the object passed in the clazz parameter
 281    *
 282    * @deprecated use intercept instead
 283    */
 284  0 protected Object stub(Class clazz, Class[] signature, Object[] arguments, String id) {
 285  0 return getProxyFactory().createObjectInterceptionProxy( clazz, signature, arguments, id );
 286    }
 287   
 288    /**
 289    * <code>stub</code> creates an instance of the specified class (just as mock for a class), but
 290    * it forwards all calls to that instance. If restrictions should be made on the stub, those can
 291    * be made just as with a mock.
 292    * <br>
 293    * The id <b>MUST</b> be unique within the testmethod
 294    * </br>
 295    * The class <b>MUST</b> be non-final and it <b>MUST</b>
 296    * have a <b>only one</b> constructor signature assignable from the provided <code>args</code>.
 297    *
 298    * @param clazz The object to implement for the stub
 299    * @param args The arguments to the constructor
 300    * @param id the name of the stub
 301    * @return A new stub for the object passed in the clazz parameter
 302    *
 303    * @deprecated use intercept instead
 304    */
 305  0 protected Object stub(Class clazz, Object[] arguments, String id) {
 306  0 return getProxyFactory().createObjectInterceptionProxy( clazz, arguments, id );
 307    }
 308   
 309    /**
 310    * Fake and intercept is meant to be used on interfaces.
 311    * A fake class that implements the faked interface is dynamically generated
 312    * and returns default values from all methods.
 313    *
 314    * The faked implementation is intercepted by rMock so that expectations can
 315    * optionally be made on the faked interface.
 316    * @param itf The interface to fake and intercept
 317    * @param id The identifier for the resulting proxy
 318    * @return A faked and intercepted interface.
 319    */
 320  7 protected Object fakeAndIntercept(Class itf, String id) {
 321  7 return getProxyFactory().createInterfaceInterceptionProxy(itf, id);
 322    }
 323   
 324    /**
 325    * Fake and intercept is meant to be used on interfaces.
 326    * A fake class that implements the faked interface is dynamically generated
 327    * and returns default values from all methods.
 328    *
 329    * The faked implementation is intercepted by rMock so that expectations can
 330    * optionally be made on the faked interface.
 331    * @param itf The interface to fake and intercept
 332    * @return A faked and intercepted interface.
 333    */
 334  0 protected Object fakeAndIntercept(Class itf) {
 335  0 return getProxyFactory().createInterfaceInterceptionProxy(itf, namingUtils.createNameFromType(itf));
 336    }
 337   
 338    /**
 339    * Change state of the mocks to start verify the recorded calls.
 340    */
 341  417 public void startVerification() {
 342  417 getVerifiable().beginVerify();
 343    }
 344   
 345    /**
 346    * This method is for enabling simpler monitoring setup for e.g. an aspectj
 347    * extension of rMock. For the default version no interceptions are made on
 348    * objects passed to this method.
 349    * @param object
 350    * @param id
 351    * @return the object passed in
 352    */
 353  135 protected Object monitor(Object object, String id) {
 354  135 return object;
 355    }
 356   
 357    /**
 358    * Use this method to create monitored objects to trace in documentation.
 359    * <br>
 360    * <br>
 361    * At the moment this method just creates the object with the given parameters and returns it.
 362    * @param clazz The class to create
 363    * @param id The name of the object for presentation purposes
 364    * @return The created object
 365    */
 366  52 protected Object monitor(Class clazz, String id) {
 367  52 return monitor( clazz, new Class[]{}, new Object[]{}, id );
 368    }
 369   
 370    /**
 371    * Use this method to create monitored objects to trace in documentation.
 372    * <br>
 373    * <br>
 374    * At the moment this method just creates the object with the given parameters and returns it.
 375    * @param clazz The class to create
 376    * @return The created object
 377    */
 378  1 protected Object monitor(Class clazz) {
 379  1 return monitor( clazz, new Class[]{}, new Object[]{}, namingUtils.createNameFromType(clazz) );
 380    }
 381   
 382    /**
 383    * Use this method to create monitored objects to trace in documentation.
 384    * <br>
 385    * <br>
 386    * At the moment this method just creates the object with the given parameters and returns it.
 387    * From the object array provided it will try to find the most appropriate constructor to use.
 388    * @param clazz The class to create
 389    * @param arguments An object array to initialize an instance of class with
 390    * @param id The name of the object for presentation purposes
 391    * @return The created object
 392    */
 393  31 protected Object monitor(Class clazz, Object[] arguments, String id) {
 394  31 Object object = getProxyFactory().createObjectMonitorProxy( clazz, arguments, id );
 395  31 if( object != null ) {
 396  31 return object;
 397    }
 398  0 throw new MonitorCreationException( clazz, null );
 399    }
 400   
 401    /**
 402    * Use this method to create monitored objects to trace in documentation.
 403    * <br>
 404    * <br>
 405    * At the moment this method just creates the object with the given parameters and returns it.
 406    * @param clazz The class to create
 407    * @param parameterTypes The signature to create the object with
 408    * @param arguments The arguments to create the object with
 409    * @param id The name of the object for presentation purposes
 410    * @return The created object
 411    */
 412  55 protected Object monitor(Class clazz, Class[] parameterTypes, Object[] arguments, String id) {
 413  55 Object object = getProxyFactory().createObjectMonitorProxy( clazz, parameterTypes, arguments, id );
 414  55 if( object != null ) {
 415  54 return object;
 416    }
 417  1 throw new MonitorCreationException( clazz, parameterTypes );
 418    }
 419   
 420    /**
 421    * @return
 422    */
 423  430 public MultiplicityModifier modify() {
 424  430 return multiplicityModifier;
 425    }
 426   
 427    /**
 428    * Sets up that an exception is expected to be thrown as a result of this testCase.
 429    * If an exception is thrown that is matched by this expression it will be caught and the test will pass.
 430    * If an exception is thrown that is not matched by this expression an UnexpectedException exception will be thrown
 431    * detailing the expectation and the action exception thrown.
 432    * @param expression The expression to match the exception type
 433    */
 434  19 public void expectThatExceptionThrown(Expression exceptionExpression) {
 435  19 getExceptionVerifier().setUserExceptionExpectation(exceptionExpression);
 436   
 437    }
 438   
 439  76 protected void endSection() {
 440  76 sectionManager.endSection();
 441    }
 442   
 443  65 public void beginSection(Section section) {
 444  65 sectionManager.beginSection( section );
 445    }
 446   
 447  4 protected CollectionSelector forEach(Expression expression) {
 448  4 return finder.forEach(expression);
 449    }
 450   
 451  1090 public void setSectionManager(SectionManager sectionManager) {
 452  1090 this.sectionManager = sectionManager;
 453    }
 454   
 455  19 protected ExceptionVerifier getExceptionVerifier() {
 456  19 return exceptionVerifier;
 457    }
 458   
 459  1084 public void setExceptionVerifier(ExceptionVerifier exceptionVerifier) {
 460  1084 this.exceptionVerifier = exceptionVerifier;
 461    }
 462   
 463  1 public void replaceSection(String id, Section section) {
 464  1 this.sectionManager.overrideSection(id, section);
 465    }
 466   
 467  12 public void appendToSection(String id) {
 468  12 this.sectionManager.extendSection(id);
 469    }
 470   
 471    }