Clover coverage report - EasyMock 1.2_Java1.5
Coverage timestamp: So Aug 7 2005 17:48:15 CEST
file stats: LOC: 285   Methods: 30
NCLOC: 241   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RecordState.java 100% 100% 100% 100%
coverage
 1    /*
 2    * Copyright (c) 2001-2005 OFFIS. This program is made available under the terms of
 3    * the MIT License.
 4    */
 5    package org.easymock.internal;
 6   
 7    import java.lang.reflect.Method;
 8    import java.util.HashMap;
 9    import java.util.Map;
 10   
 11    import org.easymock.ArgumentsMatcher;
 12    import org.easymock.MockControl;
 13   
 14    public class RecordState implements IMockControlState {
 15   
 16    private MethodCall lastMethodCall;
 17   
 18    private boolean lastMethodCallUsed = true;
 19   
 20    private IBehavior behavior;
 21   
 22    private static Map<Class, Object> emptyReturnValues = new HashMap<Class, Object>();
 23   
 24    static {
 25  1 emptyReturnValues.put(Void.TYPE, null);
 26  1 emptyReturnValues.put(Boolean.TYPE, Boolean.FALSE);
 27  1 emptyReturnValues.put(Byte.TYPE, new Byte((byte) 0));
 28  1 emptyReturnValues.put(Short.TYPE, new Short((short) 0));
 29  1 emptyReturnValues.put(Character.TYPE, new Character((char) 0));
 30  1 emptyReturnValues.put(Integer.TYPE, new Integer(0));
 31  1 emptyReturnValues.put(Long.TYPE, new Long(0));
 32  1 emptyReturnValues.put(Float.TYPE, new Float(0));
 33  1 emptyReturnValues.put(Double.TYPE, new Double(0));
 34    }
 35   
 36  296 public RecordState(IBehavior behavior) {
 37  296 this.behavior = behavior;
 38    }
 39   
 40  255 public java.lang.Object invoke(Object proxy, Method method, Object[] args) {
 41  255 closeVoidMethod();
 42  253 lastMethodCall = new MethodCall(method, args);
 43  253 lastMethodCallUsed = false;
 44  253 return emptyReturnValueFor(method.getReturnType());
 45    }
 46   
 47  221 public void replay() {
 48  221 closeVoidMethod();
 49    }
 50   
 51  1 public void verify() {
 52  1 throw new RuntimeExceptionWrapper(new IllegalStateException(
 53    "calling verify is not allowed in record state"));
 54    }
 55   
 56  6 public void setDefaultMatcher(ArgumentsMatcher matcher) {
 57  6 behavior.setDefaultMatcher(matcher);
 58    }
 59   
 60  14 public void setMatcher(ArgumentsMatcher matcher) {
 61  14 requireMethodCall("matcher");
 62  13 behavior.setMatcher(lastMethodCall.getMethod(), matcher);
 63    }
 64   
 65  15 public void setVoidCallable(Range count) {
 66  15 requireMethodCall("void callable");
 67  11 requireVoidMethod();
 68  11 behavior.addExpected(lastMethodCall, Result.createReturnResult(null),
 69    count);
 70  10 lastMethodCallUsed = true;
 71    }
 72   
 73  43 public void setThrowable(Throwable throwable, Range count) {
 74  43 requireMethodCall("Throwable");
 75  41 requireValidThrowable(throwable);
 76  38 behavior.addExpected(lastMethodCall, Result
 77    .createThrowResult(throwable), count);
 78  37 lastMethodCallUsed = true;
 79    }
 80   
 81  17 public void setReturnValue(long value, Range count) {
 82  17 requireMethodCall("return value");
 83  15 Class returnType = lastMethodCall.getMethod().getReturnType();
 84  15 Object primitiveReturnValue = createNumberObject(value, returnType);
 85  9 behavior.addExpected(lastMethodCall, Result
 86    .createReturnResult(primitiveReturnValue), count);
 87  9 lastMethodCallUsed = true;
 88    }
 89   
 90  25 public void setReturnValue(boolean value, Range count) {
 91  25 requireMethodCall("return value");
 92  22 requireReturnType(Boolean.TYPE);
 93  19 behavior.addExpected(lastMethodCall, Result
 94  19 .createReturnResult(value ? Boolean.TRUE : Boolean.FALSE),
 95    count);
 96  19 lastMethodCallUsed = true;
 97    }
 98   
 99  48 private void requireReturnType(Class clazz) {
 100  48 if (!lastMethodCall.getMethod().getReturnType().equals(clazz)) {
 101  12 throw new RuntimeExceptionWrapper(new IllegalStateException(
 102    "incompatible return value type"));
 103    }
 104    }
 105   
 106  10 public void setReturnValue(float value, Range count) {
 107  10 requireMethodCall("return value");
 108  8 requireReturnType(Float.TYPE);
 109  5 behavior.addExpected(lastMethodCall, Result
 110    .createReturnResult(new Float(value)), count);
 111  5 lastMethodCallUsed = true;
 112    }
 113   
 114  10 public void setReturnValue(double value, Range count) {
 115  10 requireMethodCall("return value");
 116  8 requireReturnType(Double.TYPE);
 117  5 behavior.addExpected(lastMethodCall, Result
 118    .createReturnResult(new Double(value)), count);
 119  5 lastMethodCallUsed = true;
 120    }
 121   
 122  71 public void setReturnValue(Object value, Range count) {
 123  71 requireMethodCall("return value");
 124  69 requireAssignable(value);
 125  65 behavior.addExpected(lastMethodCall, Result.createReturnResult(value),
 126    count);
 127  65 lastMethodCallUsed = true;
 128    }
 129   
 130  11 public void setDefaultThrowable(Throwable throwable) {
 131  11 requireMethodCall("default Throwable");
 132  10 requireValidThrowable(throwable);
 133  7 behavior.setDefaultResult(lastMethodCall.getMethod(), Result
 134    .createThrowResult(throwable));
 135  7 lastMethodCallUsed = true;
 136    }
 137   
 138  2 public void setDefaultVoidCallable() {
 139  2 requireMethodCall("default void callable");
 140  1 requireVoidMethod();
 141  1 behavior.setDefaultResult(lastMethodCall.getMethod(), Result
 142    .createReturnResult(null));
 143  1 lastMethodCallUsed = true;
 144    }
 145   
 146  8 public void setDefaultReturnValue(long value) {
 147  8 requireMethodCall("default return value");
 148  7 Class returnType = lastMethodCall.getMethod().getReturnType();
 149  7 Object primitiveReturnValue = createNumberObject(value, returnType);
 150  6 behavior.setDefaultResult(lastMethodCall.getMethod(), Result
 151    .createReturnResult(primitiveReturnValue));
 152  6 lastMethodCallUsed = true;
 153    }
 154   
 155  5 public void setDefaultReturnValue(boolean value) {
 156  5 requireMethodCall("default return value");
 157  4 requireReturnType(Boolean.TYPE);
 158  3 behavior.setDefaultResult(lastMethodCall.getMethod(), Result
 159  3 .createReturnResult(value ? Boolean.TRUE : Boolean.FALSE));
 160  3 lastMethodCallUsed = true;
 161    }
 162   
 163  4 public void setDefaultReturnValue(float value) {
 164  4 requireMethodCall("default return value");
 165  3 requireReturnType(Float.TYPE);
 166  2 behavior.setDefaultResult(lastMethodCall.getMethod(), Result
 167    .createReturnResult(new Float(value)));
 168  2 lastMethodCallUsed = true;
 169    }
 170   
 171  4 public void setDefaultReturnValue(double value) {
 172  4 requireMethodCall("default return value");
 173  3 requireReturnType(Double.TYPE);
 174  2 behavior.setDefaultResult(lastMethodCall.getMethod(), Result
 175    .createReturnResult(new Double(value)));
 176  2 lastMethodCallUsed = true;
 177    }
 178   
 179  13 public void setDefaultReturnValue(Object value) {
 180  13 requireMethodCall("default return value");
 181  12 requireAssignable(value);
 182  11 behavior.setDefaultResult(lastMethodCall.getMethod(), Result
 183    .createReturnResult(value));
 184  11 lastMethodCallUsed = true;
 185    }
 186   
 187  22 private Object createNumberObject(long value, Class returnType) {
 188  22 if (returnType.equals(Byte.TYPE)) {
 189  2 return new Byte((byte) value);
 190  20 } else if (returnType.equals(Short.TYPE)) {
 191  2 return new Short((short) value);
 192  18 } else if (returnType.equals(Character.TYPE)) {
 193  2 return new Character((char) value);
 194  16 } else if (returnType.equals(Integer.TYPE)) {
 195  2 return new Integer((int) value);
 196  14 } else if (returnType.equals(Long.TYPE)) {
 197  7 return new Long(value);
 198    }
 199  7 throw new RuntimeExceptionWrapper(new IllegalStateException(
 200    "incompatible return value type"));
 201    }
 202   
 203  476 private void closeVoidMethod() {
 204  476 if (lastMethodCallUsed)
 205  406 return;
 206  70 try {
 207  70 this.requireVoidMethod();
 208    } catch (RuntimeExceptionWrapper e) {
 209  4 throw new RuntimeExceptionWrapper(new IllegalStateException(
 210    "missing behavior definition for the preceeding method call "
 211    + lastMethodCall
 212    .toString(MockControl.EQUALS_MATCHER)));
 213    }
 214  66 behavior.addExpected(lastMethodCall, Result.createReturnResult(null),
 215    MockControl.ONE);
 216  66 lastMethodCallUsed = true;
 217    }
 218   
 219  262 public static Object emptyReturnValueFor(Class type) {
 220  262 return type.isPrimitive() ? emptyReturnValues.get(type) : null;
 221    }
 222   
 223  252 private void requireMethodCall(String failMessage) {
 224  252 if (lastMethodCall == null) {
 225  25 throw new RuntimeExceptionWrapper(new IllegalStateException(
 226    "method call on the mock needed before setting "
 227    + failMessage));
 228    }
 229    }
 230   
 231  81 private void requireAssignable(Object returnValue) {
 232  81 if (lastMethodIsVoidMethod()) {
 233  1 throw new RuntimeExceptionWrapper(new IllegalStateException(
 234    "void method cannot return a value"));
 235    }
 236  80 if (returnValue == null) {
 237  1 return;
 238    }
 239  79 Class<?> returnedType = lastMethodCall.getMethod().getReturnType();
 240  79 if (!returnedType.isAssignableFrom(returnValue.getClass())) {
 241  4 throw new RuntimeExceptionWrapper(new IllegalStateException(
 242    "incompatible return value type"));
 243    }
 244    }
 245   
 246  51 private void requireValidThrowable(Throwable throwable) {
 247  51 if (throwable == null)
 248  2 throw new RuntimeExceptionWrapper(new NullPointerException(
 249    "null cannot be thrown"));
 250  49 if (isValidThrowable(throwable))
 251  45 return;
 252   
 253  4 throw new RuntimeExceptionWrapper(new IllegalArgumentException(
 254    "last method called on mock cannot throw "
 255    + throwable.getClass().getName()));
 256    }
 257   
 258  82 private void requireVoidMethod() {
 259  82 if (!lastMethodIsVoidMethod()) {
 260  4 throw new RuntimeExceptionWrapper(new IllegalStateException(
 261    "last method called on mock is not a void method"));
 262    }
 263    }
 264   
 265  163 private boolean lastMethodIsVoidMethod() {
 266  163 Class returnType = lastMethodCall.getMethod().getReturnType();
 267  163 return returnType.equals(Void.TYPE);
 268    }
 269   
 270  49 private boolean isValidThrowable(Throwable throwable) {
 271  49 if (throwable instanceof RuntimeException) {
 272  34 return true;
 273    }
 274  15 if (throwable instanceof Error) {
 275  2 return true;
 276    }
 277  13 Class<?>[] exceptions = lastMethodCall.getMethod().getExceptionTypes();
 278  13 Class<?> throwableClass = throwable.getClass();
 279  13 for (Class<?> exception : exceptions) {
 280  11 if (exception.isAssignableFrom(throwableClass))
 281  9 return true;
 282    }
 283  4 return false;
 284    }
 285    }