Clover coverage report - Maven Clover report
Coverage timestamp: Sun Mar 18 2007 17:42:32 CET
file stats: LOC: 227   Methods: 19
NCLOC: 119   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractStrategyTestCase.java 100% 92.5% 94.7% 93.4%
coverage coverage
 1    package com.agical.rmock.extension.junit;
 2   
 3    import junit.framework.TestCase;
 4   
 5    import com.agical.rmock.core.Assert;
 6    import com.agical.rmock.core.Verifiable;
 7    import com.agical.rmock.core.configuration.ConfiguratorImpl;
 8    import com.agical.rmock.core.event.TestCaseListener;
 9    import com.agical.rmock.core.exception.RMockAssertionFailedException;
 10    import com.agical.rmock.core.exception.RMockExpectationException;
 11    import com.agical.rmock.core.exception.RMockInternalError;
 12    import com.agical.rmock.core.exception.StrategyTerminatingException;
 13    import com.agical.rmock.core.expectation.Engine;
 14    import com.agical.rmock.core.hub.Hub;
 15    import com.agical.rmock.core.match.Expression;
 16    import com.agical.rmock.core.match.constraint.ConstraintFactory;
 17    import com.agical.rmock.core.strategy.StrategyRunner;
 18    import com.agical.rmock.core.strategy.TestStep;
 19    import com.agical.rmock.core.util.PrimitiveToObject;
 20   
 21    /**
 22    * <p>
 23    * A base for a strategy tests. A strategy test is different from a
 24    * traditional JUnit test by allowing more flexibility. Where a traditional
 25    * JUnit testCase simply offers a
 26    * </p>
 27    *
 28    * <ol>
 29    * <li>setup</li>
 30    * <li>run</li>
 31    * <li>tearDown</li>
 32    * </ol>
 33    * <p>
 34    * flow, a strategy test supports any setup that can be implemented from a
 35    * run() method.
 36    * </p>
 37    *
 38    * <p>Apart from providing strategies, this TestCase also provides a Hub for configuration
 39    * (this test configures itself by passing <em>this</em> to the hub within a begin/end scope.
 40    * </p>
 41    *
 42    * <p>Finally, this test also provides an assertThat(actual, expression) method and a Constraint factory
 43    * reachable via the is attribute.</p>
 44    *
 45    * <hr/>
 46    * (c) 2005 Agical AB
 47    * @author joakim.ohlrogge
 48    */
 49    public abstract class AbstractStrategyTestCase extends TestCase {
 50    private TestFinder testFinder;
 51    private TestCaseListener testCaseListener;
 52    private StrategyRunner strategyRunner;
 53    /**
 54    * All expressions for the assertThatMethods are reachable from this factory.
 55    * If you want to add more constraints just extend CostraintFactory and override this
 56    * attribute with an instance of your new type.
 57    */
 58    protected ConstraintFactory is = new ConstraintFactory();
 59    private Assert asserter;
 60    private final Hub hub;
 61    private Verifiable verifiable;
 62   
 63   
 64  1378 public void setStrategyRunner(StrategyRunner strategyRunner) {
 65  1378 this.strategyRunner = strategyRunner;
 66    }
 67   
 68  0 public StrategyRunner getStrategyRunner() {
 69  0 return strategyRunner;
 70    }
 71   
 72   
 73  11 public AbstractStrategyTestCase(String name) {
 74  11 this(name, ConfiguratorImpl.getInstance().getHub());
 75    }
 76   
 77    /**
 78    * Takes the name of the test as a parameter. The name will be passed to the method creating
 79    * the strategy. The name is most likel
 80    * @param name
 81    * @param hub
 82    */
 83  1137 public AbstractStrategyTestCase(String name, Hub hub) {
 84  1137 super(name);
 85  1137 this.hub = hub;
 86    }
 87   
 88    /**
 89    * Implement this method to return a strategy to run for the given test.
 90    * @param test The test to get the strategy for
 91    * @return The strategy for the test.
 92    */
 93    protected abstract TestStep createStrategy(String test);
 94   
 95    /**
 96    * Gets and runs the strategy.
 97    */
 98  557 public void runBare() throws Throwable {
 99   
 100  557 Hub hub = getHub();
 101  557 hub.beginScope();
 102  557 Engine engine = new Engine();
 103  557 hub.connect(engine);
 104  557 hub.connect(this);
 105  557 testCaseListener.beforeTestCase(this, getName());
 106  557 try {
 107  557 TestStep step = createStrategy(getName());
 108  557 if (step == null) {
 109  1 throw new RMockInternalError("The createStrategy(String) returned null!");
 110    }
 111  556 try {
 112  556 strategyRunner.runStrategy(step);
 113    } catch( StrategyTerminatingException exception ) {
 114    // ok, strategy terminated by exception verifier
 115    }
 116  554 verifiable.endVerify();
 117    }
 118    catch( RMockExpectationException exception ) {
 119  0 throw new ExtendedAssertionFailedError(exception);
 120    }
 121    finally {
 122  557 try {
 123  557 testCaseListener.afterTestCase();
 124    }
 125    catch(Exception e) {
 126  0 e.printStackTrace();
 127    }
 128  557 hub.disconnect(engine);
 129  557 hub.endScope();
 130    }
 131    }
 132   
 133  3264 public Hub getHub() {
 134  3264 return hub;
 135    }
 136   
 137    /**
 138    * Assert method that asserts that the object passed in the actual parameter
 139    * passes the constraint passed in the expected parameter.
 140    * @param actual The actual object that is checked against the expected constraint.
 141    * @param expression The constraint that actual is expected to pass
 142    */
 143  564 public void assertThat(Object actual, Expression expression) {
 144  564 try {
 145  564 asserter.assertThat(actual, expression);
 146    }
 147    catch(RMockAssertionFailedException exception) {
 148  1 throw new ExtendedAssertionFailedError(exception);
 149    }
 150   
 151    }
 152   
 153    /**
 154    * @see Assert#assertThat(boolean)
 155    */
 156  162 public void assertThat(boolean result, Expression constraint) {
 157  162 assertThat(PrimitiveToObject.convert(result), constraint);
 158    }
 159   
 160    /**
 161    * @see Assert#assertThat(byte)
 162    */
 163  3 public void assertThat(byte result, Expression constraint) {
 164  3 assertThat(PrimitiveToObject.convert(result), constraint);
 165   
 166    }
 167   
 168    /**
 169    * @see Assert#assertThat(char)
 170    */
 171  3 public void assertThat(char result, Expression constraint) {
 172  3 assertThat(PrimitiveToObject.convert(result), constraint);
 173    }
 174   
 175    /**
 176    * @see Assert#assertThat(short)
 177    */
 178  3 public void assertThat(short result, Expression constraint) {
 179  3 assertThat(PrimitiveToObject.convert(result), constraint);
 180    }
 181   
 182    /**
 183    * @see Assert#assertThat(int)
 184    */
 185  54 public void assertThat(int result, Expression constraint) {
 186  54 assertThat(PrimitiveToObject.convert(result), constraint);
 187    }
 188   
 189    /**
 190    * @see Assert#assertThat(long)
 191    */
 192  3 public void assertThat(long result, Expression constraint) {
 193  3 assertThat(PrimitiveToObject.convert(result), constraint);
 194   
 195    }
 196   
 197    /**
 198    * @see Assert#assertThat(float)
 199    */
 200  3 public void assertThat(float result, Expression constraint) {
 201  3 assertThat(PrimitiveToObject.convert(result), constraint);
 202    }
 203   
 204    /**
 205    * @see Assert#assertThat(double)
 206    */
 207  3 public void assertThat(double result, Expression constraint) {
 208  3 assertThat(PrimitiveToObject.convert(result), constraint);
 209    }
 210   
 211   
 212  1397 public void setAsserter(Assert asserter) {
 213  1397 this.asserter = asserter;
 214    }
 215   
 216  2210 public void setTestCaseListener(TestCaseListener testCaseListener) {
 217  2210 this.testCaseListener = testCaseListener;
 218    }
 219   
 220  2212 public void setVerifiable(Verifiable verifiable) {
 221  2212 this.verifiable = verifiable;
 222    }
 223   
 224  417 protected Verifiable getVerifiable() {
 225  417 return verifiable;
 226    }
 227    }